首页 > 解决方案 > 如何在 C# 中对图像执行 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;

标签: c#image-processingdct

解决方案


我建议您使用“ emguCV ”(“OpenCV”)进行此类操作。该库在性能方面设计精良,并公开了许多有用的计算机视觉功能。

可以在Git repo中找到一个示例DCT

      /// <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的DCT-Function声明为:

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

推荐阅读