I have a fucntion that create histogram of each Bitmap. How can i create another 3 histograms for R.G.B of each Bitmap?

Posted by Daniel Lip on Stack Overflow See other posts from Stack Overflow or by Daniel Lip
Published on 2012-12-11T15:24:45Z Indexed on 2012/12/11 17:03 UTC
Read the original article Hit count: 216

Filed under:

This is the histogram function im using today and if im not worng it's creating an histogram by Gray color.

What i want is another fucntion that will return me 3 histograms of each Bitmap:

The first histogram will be of the Red color of the bitmap the second for the Green color and the last one for the Blue color.

public static long[] GetHistogram(Bitmap b)
        {
            long[] myHistogram = new long[256]; 
            BitmapData bmData = null;

            try
            {
                //Lock it fixed with 32bpp
                bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
                int scanline = bmData.Stride;
                System.IntPtr Scan0 = bmData.Scan0;
                unsafe
                {
                    byte* p = (byte*)(void*)Scan0;
                    int nWidth = b.Width;
                    int nHeight = b.Height;
                    for (int y = 0; y < nHeight; y++)
                    {
                        for (int x = 0; x < nWidth; x++)
                        {
                            long Temp = 0;
                            Temp += p[0]; //  p[0] - blue, p[1] - green , p[2]-red
                            Temp += p[1];
                            Temp += p[2];
                            Temp = (int)Temp / 3;
                            myHistogram[Temp]++;
                            //we do not need to use any offset, we always can increment by pixelsize when
                            //locking in 32bppArgb - mode
                            p += 4;
                        }
                    }
                }
                b.UnlockBits(bmData);
            }
            catch
            {
                try
                {
                    b.UnlockBits(bmData);
                }
                catch
                {
                }
            }
            return myHistogram;
       }

How may i do it ?

© Stack Overflow or respective owner

Related posts about c#