C# 减少C中像素的灰度位表示#
如果标题中的问题描述不够,我很抱歉。但是,基本上我的问题如下。 我采取位图,并使其灰度。如果我不减少位的数量,并且仍然使用8位,那么它工作得很好。然而,硬件的重点是显示当我减少保存信息的位数时图像是如何变化的。在下面的示例中,我将二进制字符串缩减为4位,然后再次重建图像。问题是图像变黑了。我认为这是因为图像大多是灰度值(在80年代的范围内),当我减少二进制字符串时,只剩下黑色图像。在我看来,我需要检查低灰度值和高灰度值,然后使浅灰色变为白色,深灰色变为黑色。最后,用1位表示法,我应该只有黑白图像。你知道我该怎么做吗 谢谢C# 减少C中像素的灰度位表示#,c#,image,image-processing,bitmap,C#,Image,Image Processing,Bitmap,如果标题中的问题描述不够,我很抱歉。但是,基本上我的问题如下。 我采取位图,并使其灰度。如果我不减少位的数量,并且仍然使用8位,那么它工作得很好。然而,硬件的重点是显示当我减少保存信息的位数时图像是如何变化的。在下面的示例中,我将二进制字符串缩减为4位,然后再次重建图像。问题是图像变黑了。我认为这是因为图像大多是灰度值(在80年代的范围内),当我减少二进制字符串时,只剩下黑色图像。在我看来,我需要检查低灰度值和高灰度值,然后使浅灰色变为白色,深灰色变为黑色。最后,用1位表示法,我应该只有黑白图像
Bitmap bmpIn = (Bitmap)Bitmap.FromFile("c:\\test.jpg");
var grayscaleBmp = MakeGrayscale(bmpIn);
public Bitmap MakeGrayscale(Bitmap original)
{
//make an empty bitmap the same size as original
Bitmap newBitmap = new Bitmap(original.Width, original.Height);
for (int i = 0; i < original.Width; i++)
{
for (int j = 0; j < original.Height; j++)
{
//get the pixel from the original image
Color originalColor = original.GetPixel(i, j);
//create the grayscale version of the pixel
int grayScale = (int)((originalColor.R * .3) + (originalColor.G * .59)
+ (originalColor.B * .11));
//now turn it into binary and reduce the number of bits that hold information
byte test = (byte) grayScale;
string binary = Convert.ToString(test, 2).PadLeft(8, '0');
string cuted = binary.Remove(4);
var converted = Convert.ToInt32(cuted, 2);
//create the color object
Color newColor = Color.FromArgb(converted, converted, converted);
//set the new image's pixel to the grayscale version
newBitmap.SetPixel(i, j, newColor);
}
}
return newBitmap;
}
Bitmap bmpIn=(Bitmap)Bitmap.FromFile(“c:\\test.jpg”);
var grayscaleBmp=MakeGrayscale(bmpIn);
公共位图生成灰度(位图原始)
{
//使空位图与原始位图大小相同
位图newBitmap=新位图(original.Width、original.Height);
对于(int i=0;i
正如姆贝基什所说,它使用起来更简单、更快
手动执行此操作的一种方法是获取图像中灰度像素的中值,并将其用于黑白之间的阈值。如果仔细阅读文章,可能重复的“否”不会重复。我的要求完全不同。那篇文章只讨论了黑白,而不是我所面临的问题。解释一下区别,我可能会被说服。我最终想谈黑白,但我该如何做中间步骤?按照链接解决方案中的建议,使用。它将比遍历每个像素并调用SetPixel快得多,这几乎是不推荐的。