You are on page 1of 9

28/9/21 19:18 Count pixels of different colors in C# - C# HelperC# Helper

C# Helper
Tips, tricks, and example programs for
C# programmers.

Count pixels of different colors in C#


Posted on October 19, 2016 by Rod Stephens

The following CountPixels method counts pixels in an image that match a target color.

// Return the number of matching pixels.

private int CountPixels(Bitmap bm, Color target_color)

// Loop through the pixels.

int matches = 0;

for (int y = 0; y < bm.Height; y++)

for (int x = 0; x < bm.Width; x++)

csharphelper.com/blog/2016/10/count-pixels-of-different-colors-in-c/ 1/9
28/9/21 19:18 Count pixels of different colors in C# - C# HelperC# Helper

if (bm.GetPixel(x, y) == target_color) matches++;

return matches;

This code is reasonably straightforward. It loops through the pixels calling the GetPixels method to get each pixel’s
color. It then compares the pixel’s returned value to the target color.

The only strangeness here is that the Color class’s Equals method, which is used by == to determine equality, treats
named colors differently from those obtained in other ways, including colors obtained with the GetPixel method.
That means this method won’t work if you pass it a named color such as Color.Black. If you used such a color, the
method would find no matching pixels.

The following code shows how this program uses the CountPixels method to count pixels that are black or white in
the image.

// Count the black and white pixels.

private void btnCount_Click(object sender, EventArgs e)

Bitmap bm = new Bitmap(picImage.Image);

int black_pixels =

CountPixels(bm, Color.FromArgb(255, 0, 0, 0));

int white_pixels =

CountPixels(bm, Color.FromArgb(255, 255, 255, 255));

lblBlack.Text = black_pixels + " black pixels";

lblWhite.Text = white_pixels + " white pixels";

csharphelper.com/blog/2016/10/count-pixels-of-different-colors-in-c/ 2/9
28/9/21 19:18 Count pixels of different colors in C# - C# HelperC# Helper

lblTotal.Text = white_pixels + black_pixels + " total pixels";

The code is also reasonably straightforward. The only thing to note is that it uses the colors returned by
Color.FromArgb(255, 0, 0, 0) and Color.FromArgb(255, 255, 255, 255) instead of the named colors Color.Black and
Color.White.

     

This entry was posted in algorithms, graphics, image processing and tagged algorithms, C#, C# programming, count black pixels, count pixels, example, example program,
GetPixel, graphics, image processing, Windows Forms programming. Bookmark the permalink.

17 Responses to Count pixels of different colors in C#

Rod Stephens says:


May 23, 2012 at 3:29 pm

Lots of other posts show how to do this. The steps are:

csharphelper.com/blog/2016/10/count-pixels-of-different-colors-in-c/ 3/9
28/9/21 19:18 Count pixels of different colors in C# - C# HelperC# Helper

– Add a OpenFileDialog to the form.

– Add something to let the user tell the program to open a file. For example, add a button or a File > Open menu item.

– When the user wants to open a file, call the OpenFileDialog’s ShowDialog method. If ShowDialog returns OK, then open
the file given by the dialog’s FileName property.

For example, see today’s post Pixellate parts of an image in C#. It lets you open (save save) an image file.

Rod Stephens says:


April 6, 2014 at 1:37 pm

Sorry but if you’re asking a question I don’t understand what it is.

Rod Stephens says:


April 7, 2014 at 2:09 pm

Do you mean you want to calculate the area of the non-transparent pixels? You can use this same technique. Just look for
pixels where the alpha component is 0. Those are the transparent ones. In other words:

if (GetPixel(x, y).A == 0) num_transparent++;

This won’t work with pixels that are translucent (i.e. only partly transparent). Although you could do something like the
following, which looks for pixels that are mostly transparent.

if (GetPixel(x, y).A < 128) num_transparent++;

csharphelper.com/blog/2016/10/count-pixels-of-different-colors-in-c/ 4/9
28/9/21 19:18 Count pixels of different colors in C# - C# HelperC# Helper

Abdul Amin Khan says:


October 14, 2018 at 1:52 am

Welldone

hieu nguyen says:


April 10, 2019 at 9:00 am

How to count similar pixel blacks

For example : Count these color : RGB(6,4,7) RGB(3,9,11) RGB(4,6,2) …v.v

Because these color is very similar with RGB(0,0,0) . How to count for all the pixel like that.

RodStephens says:
April 11, 2019 at 8:43 am

You need to decide how to tell if a pixel is close enough. For example, you could calculate the squares of the
differences between the red, green, and blue color components and count the pixel if the result is within a certain
distance.

int dr = target_r - pixel.R;

int dg = target_g - pixel.G;

int db = target_b - pixel.B;

if (dr * dr + dg * dg + db * db < difff_squared) matches++;

Carlos Rum says:

csharphelper.com/blog/2016/10/count-pixels-of-different-colors-in-c/ 5/9
28/9/21 19:18 Count pixels of different colors in C# - C# HelperC# Helper

February 3, 2021 at 12:49 pm

Hello RodStephens, i’ve a question. How you have gotten this definition?

RodStephens says:
February 4, 2021 at 7:39 am

What definition?

This example was for a very specific problem. It’s not very generalizable.

Carlos Rum says:


February 4, 2021 at 3:46 pm

if (dr * dr + dg * dg + db * db < diff_squared) matches++;

Ok, is a Euclidian method ?.

Thanks

RodStephens says:
February 5, 2021 at 8:19 am

Yes. It’s the Euclidean distance formula in three dimensions. The dimensions are basically
the red, green, and blue values.

The code compares the square of the distance to diff_squared so it doesn’t need to take the
square root and that saves a little time. A program like this one performs a LOT of
calculations for a large image, so the savings is worthwhile.
csharphelper.com/blog/2016/10/count-pixels-of-different-colors-in-c/ 6/9
28/9/21 19:18 Count pixels of different colors in C# - C# HelperC# Helper

Sorry I didn’t make this clearer in the original post.

Carlos Rum says:


February 9, 2021 at 9:59 am

@RodStephens .. thanks a lot about your time and help

Regina says:
February 21, 2021 at 7:50 am

Can you use scanline to do this? How?

RodStephens says:
February 22, 2021 at 1:39 pm

I’m not sure what you mean. This program loops through the pixels in the image by scanline, more or less.

This example is really only for small images. If you have a larger image, it will be slow. To make it faster, see this
post:

Use the Bitmap32 class to manipulate image pixels very quickly in C#

Regina says:
July 14, 2021 at 4:29 am

And where should i put the Bitmap32 class in this code? Sorry im new to this

RodStephens says:

csharphelper.com/blog/2016/10/count-pixels-of-different-colors-in-c/ 7/9
28/9/21 19:18 Count pixels of different colors in C# - C# HelperC# Helper

July 14, 2021 at 8:12 am

Download the example program to see all of the details.

If you’re asking where the class’s code goes, the Bitmap32 class is a separate class. You can put it in a
separate file or inside the main program but outside of any other class.

If you’re asking where you create an instance of the class, you normally do that where you want to
manipulate the image’s pixels.

Regina says:
July 20, 2021 at 6:22 am

Thank you so much for your help it really save me!! Last question, can you tell me the argb
value for Red? I’ve tried with (120,250,0,0) but it didn’t return anything

RodStephens says:
July 20, 2021 at 7:05 am

Opaque red would be (255, 255, 0, 0). Note that the color must match exactly for this
program to count a pixel. For example, if a pixel in the image has color (255, 254, 0, 0), you
would not notice that it wasn’t perfectly red but the program would.

C# Helper
Proudly powered by WordPress.

csharphelper.com/blog/2016/10/count-pixels-of-different-colors-in-c/ 8/9
28/9/21 19:18 Count pixels of different colors in C# - C# HelperC# Helper

csharphelper.com/blog/2016/10/count-pixels-of-different-colors-in-c/ 9/9

You might also like