检测灰度图像与.net [英] Detecting grayscale images with .Net

查看:94
本文介绍了检测灰度图像与.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的扫描文件,JPG图片。扫描仪必须扫描所有的网页的颜色或所有页面为黑色和白色。由于我的许多网页都是彩色的,我必须扫描所有页面的颜色。扫描完成后,我想检查与.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.

有谁知道如何检测一个灰度图像与.net?

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

请让我知道。

推荐答案

一个简单的算法来测试颜色:由像素嵌套走在图像像素循环(宽度和高度),并测试,看看像素的RGB值是相等的。如果它们不那么图像具有颜色信息。如果你把它所有的方式通过所有的像素没有遇到这种情况,那么你有一个灰度图像。

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.

修订与更复杂的算法:

在这篇文章的第一个转我提出了一个简单的算法,假设像素灰度,如果每个像素的RGB的值相等。 0,0,0或128,128,128或230230230所以位RGB将所有的测试为灰色,而123,90,78不会。简单的。

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.

下面是code代码段,用于测试由灰色方差。这两种方法是一个小款的过程比较复杂,但应该提供足够的原料code,以帮助原来的问题。

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

这篇关于检测灰度图像与.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆