0

这是我今天使用的直方图函数,如果我没记错的话,它是用灰色创建一个直方图。

我想要的是另一个函数,它将返回每个位图的 3 个直方图:

第一个直方图是位图的红色,第二个是绿色,最后一个是蓝色。

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;
       }

我该怎么做?

4

2 回答 2

1

在您指定的部分

Temp += p[0]
...

将三个值放入单独的直方图中:

histB[p[0]]++;
histG[p[1]]++;
histR[p[2]]++;
于 2012-12-11T15:29:30.733 回答
0

您可以使用锯齿状数组(数组数组),其中 p[0],p[1],p[2] 中的值可以放入锯齿状数组中。然后使用锯齿状数组的索引值。

希望这可以帮助

于 2012-12-11T17:02:07.173 回答