0

I want to write a code in c#. I want to read the pixel of an Image.
I want to find out the Percentage of these colors...Any help would be appreciated
Here is my code:

myimage = new Bitmap(image Path);
pictureBox1.Image = myimage;
ht = myimage.Height;
wid = myimage.Width;
sum=ht*wid;

for (int i = 0; i < ht; ++i)
{
    for (int j = 0; j < wid; ++j)
    {
        pixel = myimage.GetPixel(j,i);

        //pixelColorStringValue = pixel.R.ToString("D3") + " " + pixel.G.ToString("D3") + " " + pixel.B.ToString("D3");
        Arrr = pixel.R;
        Aggg = pixel.G;
        Abbb = pixel.B;
        pixelColorStringValue = Arrr + " " + Aggg + " " + Abbb.ToString();

        switch (pixelColorStringValue)
        {
            case "255 255 255":
            {
                white = white + 1;
                break;
            }
            case "000 000 000":
            {
                // black pixel
                black = black + 1;
                break;
            }
        }
    }
}

wprs=(white / sum) * 100;    `
bprs=(black / sum) * 100;
blackres.Text = wprs.ToString();
whiteres.Text = bprs.ToString();
DIF
  • 2,470
  • 6
  • 35
  • 49
  • 1
    You mention what you want to achieve, but what is your question? – C.Evenhuis Mar 23 '15 at 10:04
  • What about `myimage.GetPixel(j, i) == Color.Black` and `Color.White` respectivelly? It's still going to be slow, since you're using `GetPixel`, but at least it's not absurd :) – Luaan Mar 23 '15 at 10:37
  • Actually..i am new in Image pixel reading..My question is how can i acheive my goal.. – Shahbazsaeed38 Mar 23 '15 at 14:50

1 Answers1

2

For small images your code is not exactly wrong.

However a few improvements are called for, especially the comparison is very ineffective.

If you only want to count black and white pixels you can use this:

for (int i = 0; i < ht; ++i)
{
    for (int j = 0; j < wid; ++j)
    {
        Color pixel = myimage.GetPixel(j,i);
        if (pixel == Color.FromArgb(0, 0, 0)) black++;
        else if (pixel == Color.FromArgb(255, 255, 255)) white++;
    }
}

Console.WriteLine(black + " black  and " + white + " white pixels of of " + sum);

If you want to count all colors in the image you can use this code to create a histogram:

Dictionary<Color, long> colorCount = new Dictionary<Color, long>();

for (int i = 0; i < ht; ++i)
{
    for (int j = 0; j < wid; ++j)
    {
        Color pixel = myimage.GetPixel(j, i);
        if (colorCount.ContainsKey(pixel)) colorCount[pixel]++;
        else colorCount.Add(pixel, 1);
    }
}

foreach (Color c in colorCount.Keys)
    Console.WriteLine( c.ToString() + " occurs " + colorCount[c] + " times.");

Note that for images with many colors it will create many lines of output ;-)

Also note that for larger images or if you need to run this kind of analysis often you should not use GetPixel but Lockbits instead, which will be a lot (>10x) faster. See here for an example. The colors in the Lockbits example are in the bytes of the data array.

TaW
  • 53,122
  • 8
  • 69
  • 111