本文主要是介绍.NET Core 图像处理(图像二值化,图像灰度化,图像灰度反转),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
ImageSharp 是支持.NET Core跨平台图形处理库,本文将利用ImageSharp库实现图像二值化,图像灰度化,图像灰度反转。
- 图像二值化
public static Image<Rgba32> Binaryzation(Image<Rgba32> image)
{int avg = 0;for (int i = 0; i < image.Width; i++){for (int j = 0; j < image.Height; j++){var color = image.GetPixelReference(i, j);avg += color.B;}}avg = (int)avg / (image.Width * image.Height);for (int i = 0; i < image.Width; i++){for (int j = 0; j < image.Height; j++){//获取该像素点的RGB的颜色var color = image.GetPixelReference(i, j);int value = 255 - color.B;//计算颜色,大于平均值为黑,小于平均值为黑Color newColor = value > avg ? Color.FromArgb(0, 0, 0) : Color.FromArgb(255,255,255);//修改该像素点的RGB的颜色image.GetPixelReference(i, j).Rgba =(uint)newColor.ToArgb();}}return image;
}
- 图像灰度化
public static Image<Rgba32> GrayProcessing(Image<Rgba32> image)
{for (int i = 0; i < image.Width; i++){for (int j = 0; j < image.Height; j++){//获取该像素点的RGB的颜色var color = image.GetPixelReference(i, j);//计算灰度值int gray = (int)(color.R * 0.3 + color.G * 0.59 + color.B * 0.11);Color newColor = Color.FromArgb(gray, gray, gray);//修改该像素点的RGB的颜色image.GetPixelReference(i, j).Rgba = (uint)newColor.ToArgb();}}return image;
}
- 图像灰度反转
public static Image<Rgba32> GrayReversal(Image<Rgba32> image){for (int i = 0; i < image.Width; i++){for (int j = 0; j < image.Height; j++){//获取该像素点的RGB的颜色var color = image.GetPixelReference(i, j);//取相反色Color newColor = Color.FromArgb(255-color.R,255-color.G,255-color.B);//修改该像素点的RGB的颜色image.GetPixelReference(i, j).Rgba = (uint)newColor.ToArgb();}}return image;}
这篇关于.NET Core 图像处理(图像二值化,图像灰度化,图像灰度反转)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!