Detecting pixel color between ARGB range

Well, I work a lot on my car license plate detection algorithm and need a little help with something simple.

Basically I try to do the following: the code itself explains, I just cannot 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 answers to everyone, I did not think much about it, and saw a problem with it after creating this thread. I think that I will go with a comparison of brightness, since my image was gray and has high contrast.

+3
source share
4 answers

Do you think you're working in a different color space? With HSV / HSB you can just do something like

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

min-max / 0-255. , , .

Edit:

System.Drawing.Color, 0.0 1.0. , ~ :

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

. , . ( wiki article .)

/ . , -.

:

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, , .

+2

Comparison operators are not defined for System.Drawing.Color, so you need 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;
}
+1
source

Here is my solution to compare colors:

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 suggest that you can modify to fit your specific needs! It mainly compares color by hue, then by saturation, and finally by brightness! (I use this to sort the colors.)

0
source

Source: https://habr.com/ru/post/1786765/


All Articles