简体   繁体   中英

How Do I Detect If a Pixel in a Bitmap Is Black

I'm making a program and I need to know how to see if a pixel in a bitmap image is very dark or black. Big thanks if you can help.

//need help
if (_Bitmap.GetPixel(c, d, Color.FromArgb(255) == 255)) mouse_event((int)Test.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
//need help

In order to get color of a pixel you should use GetPixel method. You can use GetBrightness method of Color class to get a brightness and check "how dark" is the color. Brightness equals to 0 for pure black, and maximum value is 1.

So you can compare a brightness with some threshold for example 0.02.

if (_Bitmap.GetPixel(c,d).GetBrightness() < 0.02) { ... } 

Your question is either very simple or deceptively complex.

Whether a pixel is black or not depends on the colour model the pixel is expressed in. In RGB a black pixel is expressed by the integer 0. Other colour models have other representations for black. In each case it is very straight forward to figure out whether a pixel is pure black. However you also ask whether a pixel is very, very dark, and that may be tricky.

If you have a clear threshold for darkness (say the intensity/luminescence is less than 10%) then the answer is also straight forward. This is easier in colour models with a separate intensity component but in RGB you might make due with the following approximation redComponent < 25 && greenComponent < 25 && blueComponent < 25

However if you ask whether a pixel is perceived as dark , then we thread into more complex territory. To determine whether a pixel is perceived as dark you'll need to take into consideration the values of adjacent pixels and figure out whether a darkening gradient exists. Some pixel shades will look bright if they are near the local intensity maximum, but will look dark if they are near the local intensity minimum (see checker shadow illusion for a well known example). Likewise a very dark vary narrow line adjacent a bright object may look as the edge of object and not as a dark element in the image.

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