【问题标题】:I have a function that creates histogram of each Bitmap. How can I create another 3 histograms for R.G.B of each Bitmap?我有一个创建每个位图的直方图的函数。如何为每个位图的 R.G.B 创建另外 3 个直方图?
【发布时间】:2013-11-21 23:05:37
【问题描述】:

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

我想要的是另一个函数,它将返回每个位图的 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;
       }

我该怎么做?

【问题讨论】:

    标签: c#


    【解决方案1】:

    在你指定的部分

    Temp += p[0]
    ...
    

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

    histB[p[0]]++;
    histG[p[1]]++;
    histR[p[2]]++;
    

    【讨论】:

      【解决方案2】:

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

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 2013-05-02
        • 2023-03-29
        • 2014-12-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-31
        相关资源
        最近更新 更多