我正在将文档扫描到JPG图像。扫描仪必须将所有页面扫描为颜色或所有页面为黑白页面。由于我的许多页面都是颜色,因此我必须将所有页面扫描为颜色。扫描完成后,我想用.NET检查图像,并尝试检测哪些图像是黑白的,以便我可以将这些图像转换为灰度并保存存储。

有人知道如何使用.NET检测灰度图像吗?

请告诉我。

有帮助吗?

解决方案

一种用于测试颜色的简单算法:通过像素将图像像素在嵌套的循环(宽度和高度)中漫步,并测试以查看像素的RGB值是否相等。如果不是,则图像具有颜色信息。如果您在所有像素都不遇到此条件的情况下一直在所有像素中进行操作,那么您将拥有一个灰度图像。

使用更复杂的算法进行修订:

在这篇文章的第一轮中,我提出了一种简单的算法,该算法假定像素为灰度,如果每个像素的RGB为值相等。因此,0,0,0或128,128,128或230,230,230的RGB都将测试为灰色,而123,90,78则不会。简单的。

这是一个代码段,测试了灰色的差异。这两种方法是一个更复杂的过程的一小部分,但应该提供足够的原始代码来帮助解决原始问题。

/// <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);
}

其他提示

如果找不到此库,则可以尝试抓取图像的大量像素(或全部),看看它们的R,G和B值是否在一定的阈值之内(您可能会经验地设置该阈值,或者是彼此的设置。如果是,图像是灰度。

不过,我肯定会使测试的阈值大于0 ...所以我不会测试r = g,但是(abs(rg)<e)e是您的阈值。这样,您可以保持虚假颜色的阳性...我怀疑,除非您的原始图像和扫描技术给予,否则您会得到一个不错的数字 恰恰 灰度。

更快的Versión。用8个阈值进行测试

利用:

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值。您需要对差异和某种抽样过程的计算进行一些计算,因此您不必检查一百万像素。保罗给出的解决方案是基于最大差异,因此来自扫描仪的单个红色像素伪像可以将灰度图像变成非式图像。我发布的解决方案的精度为99.1%,在13,000张图像上召回了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");
 }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top