【问题标题】:How can I speed up this histogram class?我怎样才能加快这个直方图类?
【发布时间】:2010-11-04 03:36:15
【问题描述】:

这应该是计算 8 位灰度图像的直方图。使用 1024x770 的测试位图,CreateTime 的结束时间约为 890 毫秒。我怎样才能让这个(方式,方式)更快?

编辑:我应该提到这实际上并没有计算直方图,它只是从位图中获取值。所以我真的应该问,从 8 位灰度图像中检索所有像素值的最快方法是什么?

public class Histogram {

    private static int[,] values;

    public Histogram(Bitmap b) {
        var sw = Stopwatch.StartNew();
        values = new int[b.Width, b.Height];

        for (int w = 0; w < b.Width; ++w) {
            for (int h = 0; h < b.Height; ++h) {
                values[w, h] = b.GetPixel(w, h).R;
            }
        }

        sw.Stop();
        CreateTime = (sw.ElapsedTicks /
            (double)Stopwatch.Frequency) * 1000;
    }

    public double CreateTime { get; set; }
}

【问题讨论】:

  • 查看 McWafflestix 发布的链接。我曾经在 C++ 中执行此操作,但是当我尝试将我的 lib 迁移到 C# 时,我在那个站点上学习了 C# 工具来快速访问像素。

标签: c# performance image-processing histogram


【解决方案1】:

基本的直方图算法是这样的:

int[] hist = new hist[256];
//at this point dont forget to initialize your vector with 0s.

for(int i = 0; i < height; ++i)
{
   for(int j = 0 ; j < widthl ++j)
   {
        hist[ image[i,j] ]++;
   }
}

该算法将您拥有的值为 0 的像素数、值为 1 的像素数相加,依此类推。 基本思想是使用像素值作为直方图位置的索引。

我有一个使用非托管代码为 C# 编写的算法的版本(速度很快)我不知道是否比你的更快,但请随时接受并测试,代码如下:

    public void Histogram(double[] histogram, Rectangle roi)
    {
        BitmapData data = Util.SetImageToProcess(image, roi);

        if (image.PixelFormat != PixelFormat.Format8bppIndexed)
            return;

        if (histogram.Length < Util.GrayLevels)
            return;

        histogram.Initialize();
        int width = data.Width;
        int height = data.Height;
        int offset = data.Stride - width;

        unsafe
        {
            byte* ptr = (byte*)data.Scan0;

            for (int y = 0; y < height; ++y)
            {
                for (int x = 0; x < width; ++x, ++ptr)
                    histogram[ptr[0]]++;

                ptr += offset;
            }
        }
        image.UnlockBits(data);         
    }

    static public BitmapData SetImageToProcess(Bitmap image, Rectangle roi)
    {
        if (image != null)
            return image.LockBits(
                roi,
                ImageLockMode.ReadWrite,
                image.PixelFormat);

        return null;
    }

希望能帮到你。

【讨论】:

  • 直方图是正确的,正确的是,读取像素是要优化的事情。
【解决方案2】:

您需要使用 Bitmap.LockBits 方法来访问像素数据。 This 是该过程的一个很好的参考。本质上,您将需要使用unsafe 代码来迭代位图数据。

【讨论】:

    【解决方案3】:

    这是我基于此线程提出的函数的副本/可粘贴版本。

    不安全代码要求位图为 Format24bppRgb,如果不是,它将位图转换为该格式并在克隆版本上操作。

    请注意,如果您使用索引像素格式(例如 Format4bppIndexed)传入位图,则会引发对 image.Clone() 的调用。

    在我的开发机器上从 9100x2048 的图像中获取直方图需要大约 200 毫秒。

        private long[] GetHistogram(Bitmap image)
        {
            var histogram = new long[256];
    
            bool imageWasCloned = false;
    
            if (image.PixelFormat != PixelFormat.Format24bppRgb)
            {
                //the unsafe code expects Format24bppRgb, so convert the image...
                image = image.Clone(new Rectangle(0, 0, image.Width, image.Height), PixelFormat.Format24bppRgb);
                imageWasCloned = true;
            }
    
            BitmapData bmd = null;
            try
            {
                bmd = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly,
                                     PixelFormat.Format24bppRgb);
    
                const int pixelSize = 3; //pixels are 3 bytes each w/ Format24bppRgb
    
                //For info on locking the bitmap bits and finding the 
                //pixels using unsafe code, see http://www.bobpowell.net/lockingbits.htm
                int height = bmd.Height;
                int width = bmd.Width;
                int rowPadding = bmd.Stride - (width * pixelSize);
                unsafe
                {
                    byte* pixelPtr = (byte*)bmd.Scan0;//starts on the first row
                    for (int y = 0; y < height; ++y)
                    {
                        for (int x = 0; x < width; ++x)
                        {
                            histogram[(pixelPtr[0] + pixelPtr[1] + pixelPtr[2]) / 3]++;
                            pixelPtr += pixelSize;//advance to next pixel in the row
                        }
                        pixelPtr += rowPadding;//advance ptr to the next pixel row by skipping the padding @ the end of each row.
                    }
                }
            }
            finally
            {
                if (bmd != null)
                    image.UnlockBits(bmd);
                if (imageWasCloned)
                    image.Dispose();
            }
    
            return histogram;
        }
    

    【讨论】:

      猜你喜欢
      • 2014-08-13
      • 2020-01-03
      • 2010-11-03
      • 2021-11-21
      • 2021-08-01
      • 1970-01-01
      • 2011-09-27
      • 2021-07-31
      相关资源
      最近更新 更多