英文:
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'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<SkColorFilter> colorFilter = SkColorFilters::Matrix(matrix);
答案1
得分: 0
这不是关于矩阵的问题,但你可以应用颜色滤镜,设置你的SKPaint
的ColorFilter
属性,并使用它来绘制图像:
/// <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:
/// <summary>
/// Adjusts the contrast of an image. amount is the adjustment level. Negative values decrease contrast, positive values increase contrast, and 0 means no change.
/// </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
});
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论