#include <iostream>
#include <opencv2/opencv.hpp>
#include <sstream>
using namespace std;
using namespace cv;
int main()
{
string filename = "C:/Users/Administrator/Desktop/标准测试图片/dota/big432000.jpg";
Mat img = imread(filename, IMREAD_GRAYSCALE);
if (img.empty())
{
return -1;
}
int cols = getOptimalDFTSize(img.cols);
int rows = getOptimalDFTSize(img.rows);
//复制制作边界
Mat padded;
copyMakeBorder(img, padded, 0, rows - img.rows, 0, cols - img.cols, BORDER_CONSTANT, Scalar::all(0));
Mat planes[] = { Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F) };
//进行傅里叶变换
Mat ComplexImg;
merge(planes,2, ComplexImg);
dft(ComplexImg, ComplexImg);
//log(x+1) 进行变换
split(ComplexImg, planes);
magnitude(planes[0], planes[1], planes[0]);
Mat mag = planes[0];
mag += Scalar::all(1);
log(mag, mag);
mag = mag(Rect(0, 0, mag.cols&-2, mag.rows&-2));
int cx = mag.cols / 2;
int cy = mag.rows / 2;
Mat temp;
Mat q1 = mag(Rect(0, 0, cx, cy));
Mat q2 = mag(Rect(cx, 0, cx, cy));
Mat q3 = mag(Rect(0, cy, cx, cy));
Mat q4 = mag(Rect(cx, cy, cx, cy));
q1.copyTo(temp);
q4.copyTo(q1);
temp.copyTo(q4);
q2.copyTo(temp);
q3.copyTo(q2);
temp.copyTo(q3);
//归一化结果
normalize(mag,mag, 0, 1, NORM_MINMAX);
//现在可以看看变换之后的结果
imshow("DFT", mag);
waitKey(0);
return 0;
}
DFT
最新推荐文章于 2024-05-29 17:27:53 发布