简体   繁体   中英

Reading black pixels from an image

I have a 125*25 black and white .png picture. I want to read black pixels. For a led display. How can I do? In c#.

for (int i = 0; i < image.Height ; i++)
{
    for (int j = 0; j < image.Width  ; j++)
    {
        Color c = image.GetPixel(j, i);      //
        x = c.R ;

I can read red pixels but i want only black pixels.

There are quite a number of ways you can achieve this:

  • The way you yourself used, checking if the individual RGB values equal those of black:

     if (cR == 0 && cG == 0 && cB == 0) 
  • Checking if the brightness is zero:

     if (c.GetBrightness() == 0) 
  • Or checking if a color equals another color:

     if (c.Equals(Color.Black)) 

And probably some more.

However if you use images, the colors are not guaranteed to be exactly black, especially if you use a photo or compressed image. You can work around this using a threshold, which you will have to (empirically) determine. A way of implementing such a threshold:

if (c.R < 30 && c.G < 30 && c.B < 30)

or like

if (c.GetBrightness() < 0.2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM