如果标题中的问题描述性不够,我很抱歉。但是,基本上我的问题是以下几点。 我正在使用Bitmap并使其成为灰度。如果我不减少位数,我仍然使用8位,它很好用。但是,我所拥有的点是显示当我减少保存信息的位数时图像如何变化。在下面的例子中,我将二进制字符串减少到4位,然后再次重建图像。问题是图像变黑。我认为是因为图像主要是灰度值(在80' s范围内),当我缩小二进制字符串时,我只剩下黑色图像。在我看来,我试图检查较低和较高的灰度值,然后使更浅灰色变为白色,深灰色变为黑色。最后用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;
}
答案 0 :(得分:0)
正如mbeckish所说,使用ImageAttributes.SetThreshold会更容易,也更快。
手动执行此操作的一种方法是获取图像中灰度像素的中值,并将其用于黑白之间的阈值。