Goal
- What is a Fourier transform and why use it?(什么是傅里叶变换)
- How to do it in OpenCV?(傅里叶变换在OpenCV中是怎么用的)
- 学习了这几个函数的使用:copyMakeBorder() , merge() ,split(), dft() , getOptimalDFTSize() , log() and normalize() .
Source code
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
//help 函数,没有什么内容
static void help(void)
{
cout << endl
<< "This program demonstrated the use of the discrete Fourier transform (DFT). " << endl
<< "The dft of an image is taken and it's power spectrum is displayed." << endl
<< "Usage:" << endl
<< "./discrete_fourier_transform [image_name -- default ../data/lena.jpg]" << endl;
}
int main(int argc, char ** argv)
{
help();
const char* filename = argc >=2 ? argv[1] : "./lena.jpg";
Mat I = imread(filename, IMREAD_GRAYSCALE);
if( I.empty()){
cout << "Error opening image" << endl;
return -1;
}
//! [expand]
//为了傅立叶变换具有较高的速度,获得最佳的尺寸。
Mat padded; //expand input image to optimal sizeint 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));
//输入,输出,上,下,左,右,边界类型,颜色。
//! [expand]//! [complex_and_real]
//由于傅立叶变换具有两个参数
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
//合并为多通道数据,指向矩阵的指针,矩阵数量,输出。是为了一个通道保存实数,另外一个保存虚数
//! [complex_and_real]//! [dft]
dft(complexI, complexI); // this way the result may fit in the source matrix
//输入,输出
//! [dft]// compute the magnitude and switch to logarithmic scale
// => log(1 + sqrt(Re(DFT(I))^2 + Im(DFT(I))^2))
//! [magnitude] 通道分离,为计算幅值
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];//! [magnitude]
//! [log]
magI += Scalar::all(1); // switch to logarithmic scale对数坐标,方便显示。
log(magI, magI);
//! [log]
//! [crop_rearrange]由于对原图像进行了扩展,需要舍弃一部分,同时令行列必须为偶数
// 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 that 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);
//! [crop_rearrange]
//! [normalize] //正则化
normalize(magI, magI, 0, 1, NORM_MINMAX); // Transform the matrix with float values into a
// viewable image form (float between values 0 and 1).
//! [normalize]
imshow("Input Image" , I ); // Show the result
imshow("spectrum magnitude", magI);
waitKey();
return 0;
}