KlajdPaja
KlajdPaja

Reputation: 959

Count black pixels using red value

I'm using the following code on an image who has only black/white values so that if a color is black it should be counted but somehow the following if statement doesn't work. Is it written correctly or Im just using a good logic here

for (int y = 0; y < image.Height; y++)
{  
    Color pixel = image.GetPixel(x, y);
    if(pixel.R > 0)
    {
        //some code here
    }
}

Upvotes: 0

Views: 142

Answers (1)

AlexD
AlexD

Reputation: 32576

Assuming no transparency, try

if(pixel == Color.Black)
    ....

(pixel.R>0 just checks color's Red component. It is 0 for Black.)


For barcodes, it might be better to use some thresholds to differentiate colors, e.g.:

int threshold = (255 + 255 + 255) / 2;
if (pixel.R + pixel.G + pixel.B < threshold)
    .... 

Upvotes: 4

Related Questions