如何在C#中对图像执行DCT

时间:2018-06-04 14:33:45

标签: c# image-processing dct

public Rgb Jasnosc()
    {
        Byte rob =(byte)(0.299*this.r + 0.587*this.g + 0.114*this.b);
        Rgb ret;
        ret.r = rob;
        ret.g = rob;
        ret.b = rob;
        return ret;
    }

所以我有这种结构,我正在将加载的图像更改为灰度。 我的问题是我想使用类似的方法在图像上执行DCT,但我完全不知道该怎么做。这是我用来执行它的代码:

public static Bitmap Jasnosc(Bitmap bitmapaWe)
    {
        int wysokosc = bitmapaWe.Height;
        int szerokosc = bitmapaWe.Width;

        Bitmap bitmapaWy = new Bitmap(szerokosc, wysokosc, PixelFormat.Format24bppRgb);

        BitmapData bmWeData = bitmapaWe.LockBits(new Rectangle(0, 0, wysokosc, wysokosc), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
        BitmapData bmWyData = bitmapaWy.LockBits(new Rectangle(0, 0, wysokosc, wysokosc), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

        int strideWe = bmWeData.Stride;
        int strideWy = bmWeData.Stride;

        IntPtr scanWe = bmWeData.Scan0;
        IntPtr scanWy = bmWyData.Scan0;

        unsafe
        {
            for (int y = 0; y < wysokosc; y++)
            {
                byte* pWe = (byte*)(void*)scanWe + y * strideWe;
                byte* pWy = (byte*)(void*)scanWy + y * strideWy;

                for (int x = 0; x < szerokosc; x++)
                {

                    ((Rgb*)pWy)[x] = ((Rgb*)pWe)[x].Jasnosc();
                    ((Rgb*)pWy)[y] = ((Rgb*)pWe)[y].Jasnosc();

                }
            }

        }
        bitmapaWy.UnlockBits(bmWyData);
        bitmapaWe.UnlockBits(bmWeData);

        return bitmapaWy;

1 个答案:

答案 0 :(得分:0)

我建议您使用&#34; emguCV&#34; (&#34; OpenCV&#34;)用于此类操作。 该库设计精良,性能良好。暴露了许多有用的计算机视觉功能。

example中可以找到DCT的{​​{3}}。

      /// <summary>
      /// performs forward or inverse transform of 2D floating-point image
      /// </summary>
      /// <param name="type">Transformation flags</param>
      /// <returns>The result of DCT</returns>
      [ExposableMethod(Exposable = true, Category = "Discrete Transforms")]
      public Image<TColor, Single> DCT(CvEnum.CV_DCT_TYPE type)
      {
         Image<TColor, Single> res = new Image<TColor, float>(Width, Height);
         CvInvoke.cvDCT(Ptr, res.Ptr, type);
         return res;
      }

EMGUCV的Git repo声明为:

public static void Dct(
    IInputArray src,
    IOutputArray dst,
    DctType flags
)