确定 ARGB 范围之间的像素颜色

发布于 2024-10-12 22:50:43 字数 434 浏览 4 评论 0原文

好吧,我正在努力研究我的车牌检测算法,并且需要一些简单的帮助。

基本上我正在尝试执行以下操作,代码是不言自明的,我只是找不到我想要实现的示例。

提前感谢

if (img.GetPixel(bottomRightc.X, y) <= Color.FromArgb(255, 255, 255, 255) 
    && 
    img.GetPixel(bottomRightc.X, y) >= Color.FromArgb(255, 166,166,166))
           {
               return false;
           }

编辑:

感谢大家的回复,我没有太多考虑比较,并在创建此线程后看到了问题。我想我会进行亮度比较,因为我的图像已经灰度化并且具有高对比度。

Well I'm working hard on my vehicle license plate detection algorithm, and need a little help with something simple.

Basically I'm trying to do the following, the code is self explanatory, I just can't find an example of what I'm trying to implement.

Thanks in advance

if (img.GetPixel(bottomRightc.X, y) <= Color.FromArgb(255, 255, 255, 255) 
    && 
    img.GetPixel(bottomRightc.X, y) >= Color.FromArgb(255, 166,166,166))
           {
               return false;
           }

EDIT:

Thanks for the replies everyone, I didn't put much thought into the comparison, and saw the problem with it after creating this thread. I think I'll go with brightness comparison since my image has been grayscaled and has a high contrast.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

满栀 2024-10-19 22:50:43

您是否考虑过在其他色彩空间中工作?使用 HSV/HSB,您可以简单地执行诸如

if (pixelColor.V <= 255 && pixelColor.V >= 166)
{
    return false;
}

假设值/亮度 0-255 的最小值-最大值之类的操作。假设您正在尝试完成亮度比较,这对我来说并不完全清楚。

编辑:

System.Drawing.Color中有相关方法,亮度在0.0和1.0之间。所以上面的内容会变成这样:

    if (pixelColor.GetBrightness() <= 1.0f && pixelColor.GetBrightness() >= 166.0f/255.0f)

Have you considered working in another color space? With HSV/HSB you could simply do something like

if (pixelColor.V <= 255 && pixelColor.V >= 166)
{
    return false;
}

Assuming min-max of Value/Brightness 0-255. And assuming you are trying to accomplish brightness comparison, which is not entirely clear to me.

Edit:

There are methods for this in System.Drawing.Color, and brightness is between 0.0 and 1.0. So the above would become ~something like this:

    if (pixelColor.GetBrightness() <= 1.0f && pixelColor.GetBrightness() >= 166.0f/255.0f)
提笔书几行 2024-10-19 22:50:43

为了正确比较,您需要为每种颜色导出一个值。一个很好的候选者是光度,它很好 此处介绍。 (有关该主题的 wiki 文章 使用一组略有不同的系数进行计算。 )

测试亮度可以让您比较两种颜色的相对明度/暗度。这对于您的车牌检测算法来说非常方便,因为车牌是黑白的。

文章中计算颜色的光度或亮度的示例:

private static int Brightness(Color c)
{
   return (int)Math.Sqrt(
      c.R * c.R * .241 + 
      c.G * c.G * .691 + 
      c.B * c.B * .068);
}

尝试比较各个 R、G 和 B 值很可能会给您带来麻烦。

For proper comparison you will need to derive to a single value for each color. A good candidate is luminosity which is nicely covered here. (The wiki article on the topic uses a slightly different set of coefficients for calculations.)

Testing luminosity will allow you to compare the relative lightness/darkness of two colors. This could be very handy for your license plate detection algorithm since the plate is black and white.

Article's example of calculating a color's luminosity, or brightness:

private static int Brightness(Color c)
{
   return (int)Math.Sqrt(
      c.R * c.R * .241 + 
      c.G * c.G * .691 + 
      c.B * c.B * .068);
}

Trying to compare on the individual R, G and B values will most likely get you into trouble otherwise.

哽咽笑 2024-10-19 22:50:43

没有为 System.Drawing.Color 定义比较运算符,因此您必须实现自己的比较方法。我建议使用扩展方法,例如:

static class ColorExtensions
{
    public static bool Between(this Color c, Color a, Color b)
    {
        /* insert comparison logic here */
    }

    public static bool LessOrEqual(this Color a, Color b)
    {
        /* insert comparison logic here */
    }

    public static bool MoreOrEqual(this Color a, Color b)
    {
        /* insert comparison logic here */
    }
}

这样您就可以使用

var color = img.GetPixel(bottomRightc.X, y);
if(color.LessOrEqual(Color.FromArgb(255, 255, 255, 255) &&
   color.MoreOrEqual(Color.FromArgb(255, 166, 166, 166)))
{
    return false;
}

if(img.GetPixel(bottomRightc.X, y).Between(
   Color.FromArgb(255, 166, 166, 166),
   Color.FromArgb(255, 255, 255, 255)))
{
    return false;
}

Comparison operators are not defined for System.Drawing.Color, so you have to implement your own comparison methods. I suggest using an extension method, for example:

static class ColorExtensions
{
    public static bool Between(this Color c, Color a, Color b)
    {
        /* insert comparison logic here */
    }

    public static bool LessOrEqual(this Color a, Color b)
    {
        /* insert comparison logic here */
    }

    public static bool MoreOrEqual(this Color a, Color b)
    {
        /* insert comparison logic here */
    }
}

so you can use

var color = img.GetPixel(bottomRightc.X, y);
if(color.LessOrEqual(Color.FromArgb(255, 255, 255, 255) &&
   color.MoreOrEqual(Color.FromArgb(255, 166, 166, 166)))
{
    return false;
}

or

if(img.GetPixel(bottomRightc.X, y).Between(
   Color.FromArgb(255, 166, 166, 166),
   Color.FromArgb(255, 255, 255, 255)))
{
    return false;
}
情栀口红 2024-10-19 22:50:43

这是我比较颜色的解决方案:

public int Compare(Color x, Color y)
{
    if (x.ToArgb() == y.ToArgb())
        return 0;
    float hx, hy, sx, sy, bx, by;

    // get saturation values
    sx = x.GetSaturation();
    sy = y.GetSaturation();
    // get hue values
    hx = x.GetHue();
    hy = y.GetHue();
    // get brightness values
    bx = x.GetBrightness();
    by = y.GetBrightness();

    // determine order
    // 1 : hue
    if (hx < hy)
        return -1;
    else if (hx > hy)
        return 1;
    else
    {
        // 2 : saturation
        if (sx < sy)
            return -1;
        else if (sx > sy)
            return 1;
        else
        {
            // 3 : brightness
            if (bx < by)
                return -1;
            else if (bx > by)
                return 1;
            else
                return 0;
        }
    }
}

我想您可以进行修改以满足您的特定需求!基本上它通过色调比较颜色,然后通过饱和度,最后通过亮度! (我用它来排序颜色。)

Here's my solution to compare colours:

public int Compare(Color x, Color y)
{
    if (x.ToArgb() == y.ToArgb())
        return 0;
    float hx, hy, sx, sy, bx, by;

    // get saturation values
    sx = x.GetSaturation();
    sy = y.GetSaturation();
    // get hue values
    hx = x.GetHue();
    hy = y.GetHue();
    // get brightness values
    bx = x.GetBrightness();
    by = y.GetBrightness();

    // determine order
    // 1 : hue
    if (hx < hy)
        return -1;
    else if (hx > hy)
        return 1;
    else
    {
        // 2 : saturation
        if (sx < sy)
            return -1;
        else if (sx > sy)
            return 1;
        else
        {
            // 3 : brightness
            if (bx < by)
                return -1;
            else if (bx > by)
                return 1;
            else
                return 0;
        }
    }
}

I suppose that you can modify to fit your specific needs! Basically it compares the colour by hue, then by saturation and at last by brightness! (I use this for sorting colours.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文