应用C#和SkiaSharp对图像进行对比度调整

huangapple go评论87阅读模式
英文:

Apply contrast to images using C# and SkiaSharp

问题

我正在尝试使用C#和SkiaSharp应用一些图像增强效果,但文档不够清晰。我找不到如何使用SkiaSharp应用对比度的方法。我也使用Emgu.CV,但是我找不到如何使用Emgu进行矩阵卷积,所以我正在尝试只使用一个库。

我正在尝试使用C#和SkiaSharp来完成以下操作(C++示例代码):

// 创建一个SkColorMatrix。
SkColorMatrix matrix;

// 修改对比度。对比度参数是一个浮点数,其中:
// 0.0 表示图像将完全变为灰色。
// 1.0 表示将保留图像的原始对比度。
// 大于1.0的值将增加对比度。
float contrast = 1.0f;
matrix.SetContrast(contrast);

// 使用该矩阵创建一个颜色滤镜。
sk_sp<SkColorFilter> colorFilter = SkColorFilters.Matrix(matrix);
英文:

I'm trying to apply some image enhancements using C# and SkiaSharp, but the documentation isn't clear. I couldn't find how to apply contrast using SkiaSharp.
I'm using Emgu.CV to, but I couldn't find Matrix convolution using Emgu so I'm trying to use only one library.

I'm trying to figure out how to do this (C++) using c# and SkiaSharp

/ Create an SkColorMatrix.

SkColorMatrix matrix;

 

// Modify the contrast. The contrast parameter is a float where:

// 0.0 means the image will be entirely gray.

// 1.0 means the image&#39;s original contrast will be preserved.

// Values higher than 1.0 will increase contrast.

float contrast = 1.0f;

matrix.setContrast(contrast);

 

// Create a color filter with the matrix.

sk_sp&lt;SkColorFilter&gt; colorFilter = SkColorFilters::Matrix(matrix);

答案1

得分: 0

这不是关于矩阵的问题,但你可以应用颜色滤镜,设置你的SKPaintColorFilter属性,并使用它来绘制图像:

/// <summary>
/// 调整图像的对比度。amount是调整级别。负值减少对比度,正值增加对比度,0表示不改变。
/// </summary>
/// <param name="amount"></param>
/// <returns></returns>
public static SKColorFilter Contrast(float amount)
{
    float translatedContrast = amount + 1;
    float averageLuminance = 0.5f * (1 - amount);

    return SKColorFilter.CreateColorMatrix(new float[]
    {
        translatedContrast, 0, 0, 0, averageLuminance,
        0, translatedContrast, 0, 0, averageLuminance,
        0, 0, translatedContrast, 0, averageLuminance,
        0, 0, 0, 1, 0
    });
}
英文:

It's not about matrix but you can apply a color filter, set ColorFilter prop of your SKPaint and draw image with it:

/// &lt;summary&gt;
/// Adjusts the contrast of an image. amount is the adjustment level. Negative values decrease contrast, positive values increase contrast, and 0 means no change.
/// &lt;/summary&gt;
/// &lt;param name=&quot;amount&quot;&gt;&lt;/param&gt;
/// &lt;returns&gt;&lt;/returns&gt;
public static SKColorFilter Contrast(float amount)
{
	float translatedContrast = amount + 1;
	float averageLuminance = 0.5f * (1 - amount);

	return SKColorFilter.CreateColorMatrix(new float[]
	{
		translatedContrast, 0, 0, 0, averageLuminance,
		0, translatedContrast, 0, 0, averageLuminance,
		0, 0, translatedContrast, 0, averageLuminance,
		0, 0, 0, 1, 0
	});
}

huangapple
  • 本文由 发表于 2023年8月4日 04:54:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76831547.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定