图像的EMGU / OpenCV FFT未产生预期结果

时间:2013-05-21 11:34:09

标签: c# image-processing emgucv

我正在尝试使用EMGU可视化图像的FFT。这是我正在处理的图像:

enter image description here

这是预期的结果:

enter image description here

这是我得到的: enter image description here

这是我的代码:

Image<Gray, float> image = new Image<Gray, float>(@"C:\Users\me\Desktop\sample3.jpg");
IntPtr complexImage = CvInvoke.cvCreateImage(image.Size, Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_32F, 2);

CvInvoke.cvSetZero(complexImage);
CvInvoke.cvSetImageCOI(complexImage, 1);
CvInvoke.cvCopy(image, complexImage, IntPtr.Zero);
CvInvoke.cvSetImageCOI(complexImage, 0);

Matrix<float> dft = new Matrix<float>(image.Rows, image.Cols, 2);
CvInvoke.cvDFT(complexImage, dft, Emgu.CV.CvEnum.CV_DXT.CV_DXT_FORWARD, 0);

Matrix<float> outReal = new Matrix<float>(image.Size);
Matrix<float> outIm = new Matrix<float>(image.Size);
CvInvoke.cvSplit(dft, outReal, outIm, IntPtr.Zero, IntPtr.Zero);

Image<Gray, float> fftImage = new Image<Gray, float>(outReal.Size);
CvInvoke.cvCopy(outReal, fftImage, IntPtr.Zero);

pictureBox1.Image = image.ToBitmap();
pictureBox2.Image = fftImage.Log().ToBitmap();

我在这里犯了什么错误?

更新:根据Roger Rowland的建议,这是我的更新代码。结果看起来更好,但我不是100%肯定它是正确的。结果如下:
enter image description here

Image<Gray, float> image = new Image<Gray, float>(@"C:\Users\yytov\Desktop\sample3.jpg");
        IntPtr complexImage = CvInvoke.cvCreateImage(image.Size, Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_32F, 2);

        CvInvoke.cvSetZero(complexImage);  // Initialize all elements to Zero
        CvInvoke.cvSetImageCOI(complexImage, 1);
        CvInvoke.cvCopy(image, complexImage, IntPtr.Zero);
        CvInvoke.cvSetImageCOI(complexImage, 0);

        Matrix<float> dft = new Matrix<float>(image.Rows, image.Cols, 2);
        CvInvoke.cvDFT(complexImage, dft, Emgu.CV.CvEnum.CV_DXT.CV_DXT_FORWARD, 0);

        //The Real part of the Fourier Transform
        Matrix<float> outReal = new Matrix<float>(image.Size);
        //The imaginary part of the Fourier Transform
        Matrix<float> outIm = new Matrix<float>(image.Size);
        CvInvoke.cvSplit(dft, outReal, outIm, IntPtr.Zero, IntPtr.Zero);

        CvInvoke.cvPow(outReal, outReal, 2.0);
        CvInvoke.cvPow(outIm, outIm, 2.0);

        CvInvoke.cvAdd(outReal, outIm, outReal, IntPtr.Zero);
        CvInvoke.cvPow(outReal, outReal, 0.5);

        CvInvoke.cvAddS(outReal, new MCvScalar(1.0), outReal, IntPtr.Zero); // 1 + Mag
        CvInvoke.cvLog(outReal, outReal); // log(1 + Mag)

        // Swap quadrants
        int cx = outReal.Cols / 2;
        int cy = outReal.Rows / 2;

        Matrix<float> q0 = outReal.GetSubRect(new Rectangle(0, 0, cx, cy));
        Matrix<float> q1 = outReal.GetSubRect(new Rectangle(cx, 0, cx, cy));
        Matrix<float> q2 = outReal.GetSubRect(new Rectangle(0, cy, cx, cy));
        Matrix<float> q3 = outReal.GetSubRect(new Rectangle(cx, cy, cx, cy));
        Matrix<float> tmp = new Matrix<float>(q0.Size);

        q0.CopyTo(tmp);
        q3.CopyTo(q0);
        tmp.CopyTo(q3);
        q1.CopyTo(tmp);                    
        q2.CopyTo(q1);
        tmp.CopyTo(q2);

        CvInvoke.cvNormalize(outReal, outReal, 0.0, 255.0, Emgu.CV.CvEnum.NORM_TYPE.CV_MINMAX, IntPtr.Zero);

        Image<Gray, float> fftImage = new Image<Gray, float>(outReal.Size);
        CvInvoke.cvCopy(outReal, fftImage, IntPtr.Zero);

        pictureBox1.Image = image.ToBitmap();
        pictureBox2.Image = fftImage.ToBitmap();  

1 个答案:

答案 0 :(得分:3)

我无法评论所得图像的幅度/强度,但我可以给你一个关于图像中点的空间分布的提示。

OpenCV不会重新排列象限,将原点[0,0]置于图像中心。您必须手动重新排列象限。

请查看以下页面的第6步: http://docs.opencv.org/doc/tutorials/core/discrete_fourier_transform/discrete_fourier_transform.html

这是OpenCV的官方文档,所以它是用C ++编写的,但原则仍然存在。