我正在将文档扫描为JPG图像。 扫描仪必须将所有页面扫描为彩色或黑白两种颜色中的其中一种。 由于我的许多页面是彩色的,所以我必须将所有页面都扫描成彩色。 扫描完成后,我想用 .Net 检查这些图像并尝试检测哪些图像是黑白的,以便我可以将这些图像转换为灰度并节省存储空间。
有人知道如何使用 .Net 检测灰度图像吗?
请告诉我。
我正在将文档扫描为JPG图像。 扫描仪必须将所有页面扫描为彩色或黑白两种颜色中的其中一种。 由于我的许多页面是彩色的,所以我必须将所有页面都扫描成彩色。 扫描完成后,我想用 .Net 检查这些图像并尝试检测哪些图像是黑白的,以便我可以将这些图像转换为灰度并节省存储空间。
有人知道如何使用 .Net 检测灰度图像吗?
请告诉我。
如果你找不到相关的库,可以尝试获取一张图片的大量像素(或全部像素),然后查看它们的r、g和b值是否相差不大(你可以根据经验或设置来确定一个阈值)。如果相似,则该图像是灰度图。
我肯定会将测试阈值设得比0大一些...例如我不会测试r=g,而是(abs(r-g) < e),其中e是你的阈值。这样可以减少误判情况...因为我猜你否则将获得相当多的假彩色结果,除非原始图像和扫描技术恰好是纯灰度的。
/// <summary>
/// This function accepts a bitmap and then performs a delta
/// comparison on all the pixels to find the highest delta
/// color in the image. This calculation only works for images
/// which have a field of similar color and some grayscale or
/// near-grayscale outlines. The result ought to be that the
/// calculated color is a sample of the "field". From this we
/// can infer which color in the image actualy represents a
/// contiguous field in which we're interested.
/// See the documentation of GetRgbDelta for more information.
/// </summary>
/// <param name="bmp">A bitmap for sampling</param>
/// <returns>The highest delta color</returns>
public static Color CalculateColorKey(Bitmap bmp)
{
Color keyColor = Color.Empty;
int highestRgbDelta = 0;
for (int x = 0; x < bmp.Width; x++)
{
for (int y = 0; y < bmp.Height; y++)
{
if (GetRgbDelta(bmp.GetPixel(x, y)) <= highestRgbDelta) continue;
highestRgbDelta = GetRgbDelta(bmp.GetPixel(x, y));
keyColor = bmp.GetPixel(x, y);
}
}
return keyColor;
}
/// <summary>
/// Utility method that encapsulates the RGB Delta calculation:
/// delta = abs(R-G) + abs(G-B) + abs(B-R)
/// So, between the color RGB(50,100,50) and RGB(128,128,128)
/// The first would be the higher delta with a value of 100 as compared
/// to the secong color which, being grayscale, would have a delta of 0
/// </summary>
/// <param name="color">The color for which to calculate the delta</param>
/// <returns>An integer in the range 0 to 510 indicating the difference
/// in the RGB values that comprise the color</returns>
private static int GetRgbDelta(Color color)
{
return
Math.Abs(color.R - color.G) +
Math.Abs(color.G - color.B) +
Math.Abs(color.B - color.R);
}
bool grayScale;
Bitmap bmp = new Bitmap(strPath + "\\temp.png");
grayScale = TestGrayScale(bmp, 8);
if (grayScale)
MessageBox.Show("Grayscale image");
/// <summary>Test a image is in grayscale</summary>
/// <param name="bmp">The bmp to test</param>
/// <param name="threshold">The threshold for maximun color difference</param>
/// <returns>True if is grayscale. False if is color image</returns>
public bool TestGrayScale(Bitmap bmp, int threshold)
{
Color pixelColor = Color.Empty;
int rgbDelta;
for (int x = 0; x < bmp.Width; x++)
{
for (int y = 0; y < bmp.Height; y++)
{
pixelColor = bmp.GetPixel(x, y);
rgbDelta = Math.Abs(pixelColor.R - pixelColor.G) + Math.Abs(pixelColor.G - pixelColor.B) + Math.Abs(pixelColor.B - pixelColor.R);
if (rgbDelta > threshold) return false;
}
}
return true;
}
你有更快的吗?
由于JPEG支持元数据,因此您应该首先检查扫描软件是否在保存的图像上放置了一些特殊数据,并且您是否可以依赖该信息。
我在Python板块发布的答案可能会有所帮助。你在网上找到的灰度图像通常不具有相同的R、G、B值,这需要一些方差计算和某种采样过程,以便您不必检查一百万个像素。Paul给出的解决方案基于最大差异,因此来自扫描仪的单个红色像素伪影可能会将灰度图像转换为非灰度图像。我发布的解决方案在13000张图像上达到了99.1%的精度和92.5%的召回率。
我认为这种方法应该需要最少的代码,它已经在JPEG上进行了测试。下面的bImage是一个字节数组。
MemoryStream ms = new MemoryStream(bImage);
System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
if (returnImage.Palette.Flags == 2)
{
System.Diagnostics.Debug.WriteLine("Image is greyscale");
}