[英]Photo to grayscale in C#.NET and Windows Forms
How do I make this into a grayscale? 如何将其变成灰度?
After much googling it seems the best I can do is change the tint of the picture (in the code bellow, it's a green tint). 经过大量的谷歌搜索之后,似乎我能做的最好的就是改变图片的色彩(在下面的代码中,这是绿色的色彩)。 How can I do it? 我该怎么做?
byte[] redGreenBlueVal = new byte[numBytes];
for (int i = 0; i < redGreenBlueVal .Length; i += 4)
{
redGreenBlueVal [i + 0] = (byte)(.114 * redGreenBlueVal [i + 0]); --> blue
redGreenBlueVal [i + 1] = (byte)(.587 * redGreenBlueVal [i + 1]); --> green
redGreenBlueVal [i + 2] = (byte)(.299 * redGreenBlueVal [i + 2]); --> red
}
In effect you're adjusting HSB so this should get you a better grayscale image: 30% RED 59% GREEN 11% BLUE 实际上,您正在调整HSB,这样可以为您提供更好的灰度图像:30%红色59%绿色11%蓝色
byte[] redGreenBlueVal = new byte[numBytes];
for (int i = 0; i < redGreenBlueVal .Length; i += 4)
{
gray = (byte)(.11 * redGreenBlueVal [i + 0]);
gray += (byte)(.59 * redGreenBlueVal [i + 1]);
gray += (byte)(.3 * redGreenBlueVal [i + 2]);
redGreenBlueVal [i + 0] = gray;
redGreenBlueVal [i + 1] = gray;
redGreenBlueVal [i + 2] = gray;
}
You could try setting each pixels colour channels to the average value for that pixel. 您可以尝试将每个像素的颜色通道设置为该像素的平均值。
ie 即
for( int i = 0; i < redGreenBlueVal.Length; += 4 )
{
int average = (redGreenBlueVal[i + 0] + redGreenBlueVal[i + 1] + redGreenBlueVal[i + 2])/3;
redGreenBlueVal[i + 0] = average;
redGreenBlueVal[i + 1] = average;
redGreenBlueVal[i + 2] = average;
}
To get a grayscale image, the basic idea is to have all the 3 channels hold the same value. 为了获得灰度图像,基本思想是使所有3个通道保持相同的值。 There are many ways to do this, the simplest ones are: 有很多方法可以做到这一点,最简单的方法是:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.