【发布时间】:2015-01-21 17:43:52
【问题描述】:
如果标题中的问题描述性不够,我很抱歉。但是,基本上我的问题如下。 我正在使用 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;
}
【问题讨论】:
-
如果您仔细阅读帖子,No 不是重复的。我在问完全不同的事情。那篇文章只谈论黑白,而不是我遇到的问题。
-
解释差异,我可能会被说服。
-
我想最终登陆黑白,但我将如何做中间步骤?
-
按照链接解决方案中的建议,使用ImageAttributes.SetThreshold 执行此操作。这将比遍历每个像素并调用 SetPixel 快得多,这几乎不推荐。
标签: c# image image-processing bitmap