#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <sstream>
using namespace std;
using namespace cv;
int main(int argc, char* argv[]) {
const char* filename = "tiger.jpg";
Mat I = imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
if (I.empty()) {
return -1;
}
// 将图像延扩到最佳尺寸-----------------------------------------------------------------------
Mat padded;
int m = getOptimalDFTSize(I.rows);
int n = getOptimalDFTSize(I.cols);
copyMakeBorder(I, padded, 0, m - I.rows, 0, I.cols, // on the border add zero values
BORDER_CONSTANT, Scalar::all(0));
Mat planes[] = {Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F)}; // Add to the expanded another plane with zeros
Mat complexI;
merge(planes, 2, complexI);
// 进行离散傅立叶变换---------------------------------------------------------------------------
dft(complexI, complexI);
// 将复数转换为幅度-----------------------------------------------------------------------------
split(complexI, planes); // planes[0] = Re(DFT(I), planes[1] = Im(DFT(I))
magnitude(planes[0], planes[1], planes[0]); // *=> log(1 + sqrt(Re(DFT(I))^2 + Im(DFT(I))^2))
Mat magI = planes[0];
// 对数尺度缩放----------------------------------------------------------------------------------
magI += Scalar::all(1);
log(magI, magI);
// 剪切和重分布幅度图象限------------------------------------------------------------------------
magI = magI(Rect(0, 0, magI.cols & -2, magI.rows & -2));
int cx = magI.cols/2;
int cy = magI.rows/2;
Mat q0(magI, Rect(0, 0, cx, cy)); // Top-Left -为每一个象限创建ROI
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
// swap quadrants (Top-Left with Bottom-Right)
Mat temp;
q0.copyTo(temp);
q3.copyTo(q0);
temp.copyTo(q3);
// swap quadrant (Top-Right with Bottom-Left)
q1.copyTo(temp);
q2.copyTo(q1);
temp.copyTo(q2);
// 为了显示,归一化,float[0, 1]--------------------------------------------------------------------
normalize(magI, magI, 0, 1, CV_MINMAX);
imshow("Input Image", I);
imshow("spectrum magnitude", magI);
waitKey(0);
return 0;
}
dft
最新推荐文章于 2024-05-19 20:43:48 发布