The frequency domain transformation in OPENCV

Source: Internet
Author: User

The frequency domain enhanced Fourier transform in OpenCV has matured, and in his official tutorials document there is a complete example of getting a spectrum map, as follows:

#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
int main (int argc, char * * argv)
{
Const char* filename = argc >=2? ARGV[1]: "Lena.jpg";

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); On the border add zero values
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 is the result may fit in the source matrix

Compute the magnitude and switch to logarithmic scale
= = Log (1 + sqrt (Re (DFT (i)) ^2 + Im (DFT (i)) ^2))
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);

Crop the spectrum, if it has an odd number of rows or columns
MagI = MagI (Rect (0, 0, Magi.cols &-2, Magi.rows & 2));

Rearrange the quadrants of Fourier image so, the origin is at the image center
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; Swap quadrants (top-left with Bottom-right)
Q0.copyto (TMP);
Q3.copyto (q0);
Tmp.copyto (Q3);

Q1.copyto (TMP); Swap quadrant (top-right with Bottom-left)
Q2.copyto (Q1);
Tmp.copyto (Q2);

Normalize (MagI, MagI, 0, 1, cv_minmax); Transform The matrix with float values into a
Viewable image form (float between values 0 and 1).

Imshow ("Input Image", I); Show the result
Imshow ("Spectrum magnitude", magI);
Waitkey ();

return 0;
}

Well written, but not complete enough. The complete frequency domain processing process should be home--------------------------------------- In his example, only the first two steps have been completed. For some reason, I am not easy to post the full process code, so the following will only post the core code of the next three steps.

1. Filtering

Frequency domain filters are commonly used in low-pass filters (ideal low-pass, Butworth low-pass, Gaussian low-pass), high-pass filter (ideal high-pass, Butworth high-pass, Gaussian high-pass), selective filtering (band-pass filter, bandpass filter, limited-wave filter), etc., the principle of each filter can consult the relevant books, strongly Digital Image Processing ". Here, for example, the Gaussian high-pass filter is the following code:

void Createghpf (Mat *dst, float D0)
{
Center position
int CX = dst->cols/2;
int cy = dst->rows/2;
for (int i=0; i<dst->cols; i++)
{
for (int j=0; j<dst->rows; j + +)
{
Float X = i-cx + 1;
Float Y = j-cy + 1;
float D = sqrt (x*x + y*y);
float param = exp (-pow (d,2)/(2*pow (d0,2)));
float Parami = 1-param;
Dst->at<float> (Point (i,j)) = Parami;
}
}
Return
}

The filter is stored in DST, because the frequency of the Fourier transform is the plural concept, the mat is saved with two channels, so the filter is also to be saved as a double-channel representation of the plural.

Shift (filter); Centralized processing, which is in the tutorials code above, can be presented as a function alone
Mat f_planes[] = {Mat::zeros (complexi.size (), cv_32f), Mat::zeros (Complexi.size (), cv_32f)};
F_planes[0] = filter;
F_PLANES[1] = filter;
Mat F_filter;
Merge (F_planes, 2, f_filter);

Mulspectrums (complex, F_filter, complex, dft_rows); Only Dft_rows Accepted

The last sentence is the two matrix corresponding to the position multiplication operation (different from the matrix multiplication), in-situ conversion after the spectrum map is stored in the filtered spectrogram.

2. Fourier inverse transformation

The above is filtered spectrum map,

The frequency domain transformation in OPENCV

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.