使用.Net检测灰度图像

时间:2009-12-09 22:32:56

标签: .net image-processing grayscale

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

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

请告诉我。

6 个答案:

答案 0 :(得分:14)

如果你找不到这个库,你可以尝试抓取图像的大量(或全部)像素,看看它们的r,g和b值是否在某个阈值内(你是可以根据经验设定,或者作为一个设定)。如果是,则图像为灰度。

我肯定会使测试的阈值大于0,但是......所以我不会测试r = g,例如,但是(abs(rg)< e)其中e是你的阈值。这样你可以保持你的假色正面...我怀疑你会得到一个不错的数字,除非你的原始图像和扫描技术给精确灰度。

答案 1 :(得分:6)

测试颜色的简单算法:在嵌套的for循环(宽度和高度)中逐个像素地遍历图像,并测试像素的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);
}

答案 2 :(得分:1)

更快的版本。测试的阈值为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;
}

你有更快的吗?

答案 3 :(得分:0)

由于JPEG支持元数据,因此您应首先检查您的扫描仪软件是否在保存的图像上放置了一些特殊数据,以及是否可以依赖该信息。

答案 4 :(得分:0)

answer I posted in the python section可能会有所帮助。您找到的图片例如在网上,人们会认为灰度通常没有相同的R,G,B值。您需要对方差和某种采样过程进行一些计算,这样您就不必检查一百万像素。 Paul给出的解决方案基于最大差异,因此来自扫描仪的单个红色像素伪像可以将灰度图像转换为非灰度图像。我发布的解决方案在13,000张图像上获得了99.1%的精度和92.5%的回忆率。

答案 5 :(得分:0)

我认为这种方法应该需要最少的代码,它已经在jpegs上进行了测试。 bImage下面是一个字节数组。

mongoTemplate.save(fbUserProfile);