EMGU/OpenCV FFT of image not yielding expected res

2019-07-10 02:29发布

问题:

I'm trying to visualize the FFT of an image with EMGU. Here's the image I'm processing:

Here's the expected result:

Here's what I get:

Here's my code:

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();

What mistake am I making here?

Update: as per Roger Rowland's suggestion here's my updated code. The result looks better but I'm not 100% sure it's correct. Here's the result:

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:

I cannot comment on the magnitude/intensity of the resulting image, but I can give you a tip about the spatial distribution of points in your image.

OpenCV doesn't rearrange the quadrants to put origin [0,0] into center of image. You have to rearrange the quadrants manually.

Look at step 6 at the following page: http://docs.opencv.org/doc/tutorials/core/discrete_fourier_transform/discrete_fourier_transform.html

It's official doc for OpenCV, so it's in C++, but principle holds.