#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include
using namespace cv;
int main( )
{
Mat srcImage = imread("lena.jpg", 0);
if(!srcImage.data ) { printf("读取图片错误,请确定目录下是否有imread函数指定图片存在~! \n"); return false; }
imshow("原始图像" , srcImage);
int m = getOptimalDFTSize( srcImage.rows );
int n = getOptimalDFTSize( srcImage.cols );
Mat padded;
copyMakeBorder(srcImage, padded, 0, m - srcImage.rows, 0, n - srcImage.cols, BORDER_CONSTANT, Scalar::all(0));
Mat planes[] = {Mat_(padded), Mat::zeros(padded.size(), CV_32F)};
Mat complexI;
merge(planes, 2, complexI);
dft(complexI, complexI);
split(complexI, planes);
magnitude(planes[0], planes[1], planes[0]);
Mat magnitudeImage = planes[0];
magnitudeImage += Scalar::all(1);
log(magnitudeImage, magnitudeImage);//求自然对数
magnitudeImage = magnitudeImage(Rect(0, 0, magnitudeImage.cols & -2, magnitudeImage.rows & -2));
int cx = magnitudeImage.cols/2;
int cy = magnitudeImage.rows/2;
Mat q0(magnitudeImage, Rect(0, 0, cx, cy)); // ROI区域的左上
Mat q1(magnitudeImage, Rect(cx, 0, cx, cy)); // ROI区域的右上
Mat q2(magnitudeImage, Rect(0, cy, cx, cy)); // ROI区域的左下
Mat q3(magnitudeImage, Rect(cx, cy, cx, cy)); // ROI区域的右下
Mat tmp;
q0.copyTo(tmp);
q3.copyTo(q0);
tmp.copyTo(q3);
q1.copyTo(tmp);
q2.copyTo(q1);
tmp.copyTo(q2);
normalize(magnitudeImage, magnitudeImage, 0, 1, NORM_MINMAX);
imshow("频谱幅值", magnitudeImage);
waitKey();
return 0;
}
I = rgb2gray(imread('d:\lena.jpg'));
fcoef=fft2(double(I)); %FFT变换
tmp1 =log(1+abs(fcoef));
spectrum = fftshift(fcoef); %调整中心
tmp2 = log(1+abs(spectrum));
ifcoef = ifft2(fcoef); %逆变换
figure %显示处理结果
subplot(2,2,1), imshow(I), title('source image');
subplot(2,2,2), imshow(tmp1,[]), title('FFT image');
subplot(2,2,3), imshow(tmp2,[]), title('shift FFT image');
subplot(2,2,4), imshow(ifcoef,[]), title('IFFT image');
https://blog.csdn.net/qq_20823641/article/details/52335018