Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting grayscale images with .Net

I am scanning documents to JPG images. The scanner must scan all pages as color or all pages as black and white. Since many of my pages are color, I must scan all pages as color. After the scanning is complete, I would like to examine the images with .Net and try to detect what images are black and white so that I can convert those images to grayscale and save on storage.

Does anybody know how to detect a grayscale image with .Net?

Please let me know.

like image 918
Dave Avatar asked Dec 09 '09 22:12

Dave


3 Answers

If you can't find a library for this, you could try grabbing a large number (or all) of the pixels for an image and see if their r, g, and b values are within a certain threshold (which you might set empirically, or have as a setting) of one another. If they are, the image is grayscale.

I would definitely make the threshold for a test a bit larger than 0, though...so I wouldn't test r=g, for example, but (abs(r-g) < e) where e is your threshold. That way you can keep your false color positives down...as I suspect you'll otherwise get a decent number, unless your original image and scanning techniques give precisely grayscale.

like image 144
Beska Avatar answered Oct 07 '22 15:10

Beska


A simple algorithm to test for color: Walk the image pixel by pixel in a nested for loop (width and height) and test to see if the pixel's RGB values are equal. If they are not then the image has color info. If you make it all the way through all the pixels without encountering this condition, then you have a gray scale image.

Revision with a more complex algorithm:

In the first rev of this post i proposed a simple algorithm that assumes that pixels are gray scale if each pixel's RGB are values are equal. So RGBs of 0,0,0 or 128,128,128 or 230,230,230 would all test as gray while 123,90,78 would not. Simple.

Here's a snippet of code that tests for a variance from gray. The two methods are a small subsection of a more complex process but ought to provide enough raw code to help with the original question.

/// <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);
}
like image 40
Paul Sasik Avatar answered Oct 07 '22 15:10

Paul Sasik


A faster versión. Test with a threshold of 8. Work well for my

Use:

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

Do you have a faster one?

like image 44
Jose Manuel Piñeiro Avatar answered Oct 07 '22 16:10

Jose Manuel Piñeiro