OpenCV Fourier transform and its inverse transformation example and its understanding

Source: Internet
Author: User
Tags scalar

Fourier transform is the transformation of image from space domain to frequency domain.

Space Domain

In general, the spatial domain image is f (x,y) = Gray level (0-255), the image point is a two-dimensional matrix, each coordinate corresponds to a color value.

frequency Domain

Introduce a few concepts first

Frequency: For the image can refer to the image color value of the gradient, that is, the change in the speed of gray level

Amplitude: Can be simply understood as the right of frequency, that is, the proportion of the frequency

Energy = amplitude (may not be accurate)

Transform result to f (u,v)

The f represents the amplitude value, u represents the frequency of the x direction, and V represents the frequency of the y direction.


Generally, the transformation results will be processed in order to show the form of the picture, the result is that the amplitude value is used as a gray level, bright place to express energy high

The main part of the image is concentrated in the low-frequency part, and the boundary and noise are mainly concentrated in the high-frequency part

Since the original point of the image transformation is not easy to display at the edge, the origin is moved to the center part.

The result is that one of the highlights in the middle is diverging toward the periphery, and the lower the energy (the darker) the farther away from the central position.

Next, let's show the code

#include <opencv2/opencv.hpp> #include <iostream> using namespace std;
using namespace CV; int main (int a,char **p) {Mat input=imread (P[1],cv_load_image_grayscale);//Read the picture in grayscale image//If you don't know how to get in to p[1]. Can be changed to//mat input=imread ("Image.jpg", Cv_load_image_grayscale);  imshow ("input", input);//display artwork int W=getop
	Timaldftsize (Input.cols);
	int H=getoptimaldftsize (input.rows),//Get the best size, fast Fourier transform requires a size of 2 of n-th square Mat padded; Copymakeborder (Input,padded,0,h-input.rows,0,w-input.cols,border_constant,scalar::all (0))//fill image saved to padded Mat
	Plane[]={mat_<float> (padded), Mat::zeros (Padded.size (), cv_32f)};//create channel Mat Complexim;
	Merge (Plane,2,complexim);//merged channel DFT (COMPLEXIM,COMPLEXIM)//Fourier transform, the result is stored in its own split (Complexim,plane);//Separate channel Magnitude (plane[0],plane[1],plane[0])//Get amplitude image, 0 channel is real channel, 1 is imaginary number, because the two-dimensional Fourier transform result is plural int cx=padded.cols/2;int cy=
	padded.rows/2;//the operation is to move the image, left upper and lower right exchange position, upper right and left lower exchange position Mat temp;
	Mat part1 (Plane[0],rect (0,0,cx,cy));
	Mat Part2 (Plane[0],rect (cx,0,cx,cy)); Mat PArt3 (Plane[0],rect (0,cy,cx,cy));
	

	Mat part4 (Plane[0],rect (cx,cy,cx,cy));
	Part1.copyto (temp);
	Part4.copyto (part1);
	
	Temp.copyto (PART4);
	Part2.copyto (temp);
	Part3.copyto (part2);
Temp.copyto (PART3); Mat _complexim (Complexim,rect (
	PADDED.COLS/4,PADDED.ROWS/4,PADDED.COLS/2,PADDED.ROWS/2)); Opymakeborder (_complexim,_complexim,padded.rows/4,padded.rows/4,padded.cols/4,padded.cols/4,border_constant,
	Scalar::all (0.75));
	Mat _complexim; Complexim.copyto (_complexim)//change the results of a copy of the reverse transformation, that is, restore the original image Mat Idft[]={mat::zeros (Plane[0].size (), cv_32f), Mat::zeros (Plane[0].size (), cv_32f)};//Create two channels, the type is float, size is the dimension of the Fill idft (_complexim,_complexim);//Fourier inverse transform split (_complexim,idft )//The result appears to be the plural magnitude (idft[0],idft[1],idft[0]);//separate channel, mainly obtain 0 channel normalize (IDFT[0],IDFT[0],1,0,CV_MINMAX), and/or normalized treatment, Float type display range is 0-1, greater than 1 is white, less than 0 is black imshow ("Idft", idft[0]); Plane[0]+=scalar::all (1);//////////////////////////Ft.

	Normalize (Plane[0],plane[0],1,0,cv_minmax);
	Imshow ("DFT", plane[0]);
	Waitkey (100086110);
return 0;
	 }
Run results


Next, let's introduce the results of the operation

To the left to the right, the first image is the original, the second is the amplitude of the graph, the third is the inverse of the transformation of the result

Amplitude graph, the result is the same as expected

The result of the inverse transformation shows that there is a circle of black edges, because the result of the fill


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.