1

尺寸一直是 1920x1080,但我希望它是例如 10x10,所以函数中的过程会更快。我不介意位图的大小或其质量,我只是希望它更快。

在这个函数中,我创建了每个位图的直方图。问题是 FOR 循环需要很长时间。

public static long[] GetHistogram(Bitmap b)
        {
            long[] myHistogram = new long[256]; // histogram סופר כמה יש מכל גוון

            BitmapData bmData = null;

            //b = new Bitmap(100, 100);
            //b.SetPixel(0,0,Color.FromArgb(10,30,40,50)); //run and make it come to here


            try
            {
                ResizeBitmap(b, 10, 10);
                //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; // to save to a file the histogram of all the bitmaps/frames each line contain 0 to 256 values of a frame.
        }

所以我调用这个函数: ResizeBitmap(b, 10, 10); 并尝试调整每个位图的大小,但在这一行之后我看到位图大小仍然是 1920x1080

这是 ResizeBitmap 函数:

public static Bitmap ResizeBitmap(Bitmap b, int nWidth, int nHeight)
        {
            Bitmap result = new Bitmap(nWidth, nHeight);
            using (Graphics g = Graphics.FromImage((Image)result))
                g.DrawImage(b, 0, 0, nWidth, nHeight);
            return result;
        }

也许还有另一种方法可以使 GetHistogram 函数更快地工作?

4

1 回答 1

0

该方法返回新的位图,因此您应该使用返回值:

Bitmap newBitmap = ResizeBitmap(b, 10, 10);
b.Dispose();
b = newBitmap;

旁注:您忽略了图像的步幅,这意味着指针可以超出图像数据。如果位图倒置存储,则认为步幅为负。

于 2012-10-30T16:31:49.530 回答