【问题标题】:High-speed performance of image filtering in C#C#中图像过滤的高速性能
【发布时间】:2010-12-07 11:57:51
【问题描述】:

我有位图。我想将中值滤波器应用于我的位图。但是我不能使用 GetPixel() 和 SetPixel() 因为速度对我来说非常重要。我需要非常快速的方法来做到这一点。也许可以通过Graphics.DrawImage(Image, Point[], Rectangle, GraphicsUnit, ImageAttributes) 来完成。

在中值滤波之后我想应用二值化滤波(为每个像素计算亮度:B=0.299*R+0.5876*G+0.114B,如果亮度小于阈值(阈值是我在 [0... 255]) 然后我的像素在结果图像中的值为 1,否则 - 0) 二值化滤波器的速度对我来说也很重要

【问题讨论】:

    标签: c# .net image-processing filtering


    【解决方案1】:

    使用CopyPixels 将数据复制到数组中,然后对数组进行操作。这是我取平均颜色的代码 sn-p:

    int stride = (bmp.PixelWidth * bmp.Format.BitsPerPixel + 7) / 8;
    byte[] pixels = new byte[bmp.PixelHeight * stride];
    bmp.CopyPixels(pixels, stride, 0);
    double[] averageComponents = new double[bmp.Format.BitsPerPixel / 8];
    
    for (int pixel = 0; pixel < pixels.Length; pixel += averageComponents.Length)
    {
        for (int component = 0; component < averageComponents.Length; component++)
        {
            averageComponents[component] += pixels[pixel + component];
        }
    }
    

    您使用的过滤器应该运行得足够快,无需任何进一步的优化(只是不要做一些算法上慢的事情)。

    【讨论】:

      【解决方案2】:

      刚刚找到这个链接:A fast way to grayscale an image in .NET (C#)

      /// <summary>
      /// Grayscales a given image.
      /// </summary>
      /// <param name="image">
      /// The image that is transformed to a grayscale image.
      /// </param>
      public static void GrayScaleImage(Bitmap image)
      {
          if (image == null)
              throw new ArgumentNullException("image");
      
          // lock the bitmap.
          var data = image.LockBits(
                        new Rectangle(0, 0, image.Width, image.Height), 
                        ImageLockMode.ReadWrite, image.PixelFormat);
          try
          {
              unsafe
              {
                  // get a pointer to the data.
                  byte* ptr = (byte*)data.Scan0;
      
                  // loop over all the data.
                  for (int i = 0; i < data.Height; i++)
                  {
                      for (int j = 0; j < data.Width; j++)
                      {
                          // calculate the gray value.
                          byte y = (byte)(
                              (0.299 * ptr[2]) + 
                              (0.587 * ptr[1]) + 
                              (0.114 * ptr[0]));
      
                          // set the gray value.
                          ptr[0] = ptr[1] = ptr[2] = y;
      
                          // increment the pointer.
                          ptr += 3;
                      }
      
                      // move on to the next line.
                      ptr += data.Stride - data.Width * 3;
                  }
              }
          }
          finally
          {
              // unlock the bits when done or when 
              // an exception has been thrown.
              image.UnlockBits(data);
          }
      }
      

      编辑:查看更多信息:

      1. Using the LockBits method to access image data
      2. GrayScale and ColorMatrix

      【讨论】:

        【解决方案3】:

        如果复制对您来说太慢,请使用LockBits 和不安全块直接修改生成的 BitmapData 结构。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-10-03
          • 1970-01-01
          • 1970-01-01
          • 2017-12-07
          • 1970-01-01
          • 2013-07-03
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多