Ask Your Question
0

Fourier Spectrum

asked 2017-03-09 11:52:06 -0600

updated 2017-03-09 11:55:01 -0600

berak gravatar image

Hello, I'm new to OpenCV, I've done the Fourier Transform of an image and got it's Spectrum.

I would like to remove frequency components (from the Spectrum) that are greater than a circle that's diameter is 100, I don't think my code is the right thing for what I want, thank you in advance for helping me Here's my code :

#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include "stdafx.h"
#include <opencv2\opencv.hpp>
using namespace cv;
using namespace std;
int main(int argc, char ** argv)
{
    const char* filename = argc >= 2 ? argv[1] : "lena.bmp";

    Mat I = imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
        if (I.empty())
        return -1;

    Mat padded;                            //expand input image to optimal size
    int m = getOptimalDFTSize(I.rows);
    int n = getOptimalDFTSize(I.cols); 
    copyMakeBorder(I, padded, 0, m - I.rows, 0, n - I.cols, BORDER_CONSTANT, Scalar::all(0));

    Mat planes[] = { Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F) };
    Mat complexI;
    merge(planes, 2, complexI); // Add to the expanded another plane with zeros

    dft(complexI, complexI); // this way the result may fit in the source matrix


    split(complexI, planes); // planes[0] = Re(DFT(I), planes[1] = Im(DFT(I))

    magnitude(planes[0], planes[1], planes[0]);// planes[0] = magnitude

    Mat magI = planes[0];

    magI += Scalar::all(1);                    // switch to logarithmic scale
    log(magI, magI);

    // Recadrer le spectre, si il y a un nombre impair de lignes ou de colonnes
    magI = magI(Rect(0, 0, magI.cols & -2, magI.rows & -2));

int cx = magI.cols / 2;
    int cy = magI.rows / 2;

    Mat q0(magI, Rect(0, 0, cx, cy));   // Top-Left - Create a ROI per quadrant
    Mat q1(magI, Rect(cx, 0, cx, cy));  // Top-Right
    Mat q2(magI, Rect(0, cy, cx, cy));  // Bottom-Left
    Mat q3(magI, Rect(cx, cy, cx, cy)); // Bottom-Right

    Mat tmp; 
    q0.copyTo(tmp);
    q3.copyTo(q0);
    tmp.copyTo(q3);

    q1.copyTo(tmp); 
    q2.copyTo(q1);
    tmp.copyTo(q2);

    normalize(magI, magI, 0, 1, CV_MINMAX); 
    imshow("Input Image", I);   imshow("spectrum magnitude", magI); 
/*________________________________________________________________________________________________*/



    Mat src, dst;

    Mat kernel;
    Point anchor;
    double delta;
    int ddepth;
    int kernel_size;
    char* window_name = "filter2D Demo";

    int c;

    /// Create window
    namedWindow(window_name, CV_WINDOW_AUTOSIZE);

    /// Initialize arguments for the filter
    anchor = Point(-1, -1);
    delta = 0;
    ddepth = -1;

    /// Loop - Will filter the image with different kernel sizes each 0.5 seconds
    int ind = 0;
    while (true)
    {
        c = waitKey(500);
        /// Press 'ESC' to exit the program
        if ((char)c == 27)
        {
            break;
        }

        /// Update kernel size for a normalized box filter


        kernel_size = 10 + 10 * (ind % 10);
        ind++;
        if (kernel_size == 100) { break; }
        kernel = Mat::ones(kernel_size, kernel_size, CV_32F) / (float)(kernel_size*kernel_size);

        /// Apply filter
        filter2D(magI, dst, ddepth, kernel, anchor, delta, BORDER_DEFAULT);
        imshow(window_name, dst);
        ind++;

        /*_______________________________________________________________________________________*/

            //calculating the idft
        Mat inverseTransform;
        dft(complexI, inverseTransform, DFT_INVERSE | DFT_REAL_OUTPUT);
        normalize(inverseTransform, inverseTransform, 0, 1, CV_MINMAX);
        imshow("Reconstructed", inverseTransform);


        waitKey();

        return 0;
    }
}
edit retag flag offensive close merge delete

Comments

What is your problem exactly?

LBerger gravatar imageLBerger ( 2017-03-09 13:38:57 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-03-09 18:29:39 -0600

Tetragramm gravatar image

Filter2d is definitely the wrong function.

Simply create an image the size of your spectrum of the same data type (CV_32F) and use the circle function to draw a binary circle.

Mat mask = Mat(rows, cols, CV_32F);
mask.setTo(0);
circle(mask, Point(rows/2, cols/2), 50, 1.0, -1);  //50 is the radius, 1.0 is the color, -1 means filled.

Then multiply your amplitude by the mask. Remember to undo the fft quadrant shift before doing the inverse DFT.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-03-09 11:52:06 -0600

Seen: 591 times

Last updated: Mar 09 '17