Ask Your Question

Revision history [back]

Fourier Spectrum

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;
}

}

click to hide/show revision 2
No.2 Revision

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

berak gravatar image

Fourier Spectrum

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>

#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 char* filename = argc >= 2 ? argv[1] : "lena.bmp";

"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;

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

/________________________________________________________________________________________________/

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;
 }
}

}