#include <opencv2/opencv.hpp> using namespace std; int main(){ const char* filename = "C:\\images\\lena.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); // 在圖邊補 0 值 copyMakeBorder(I, padded, 0, m - I.rows, 0, n - I.cols, BORDER_CONSTANT, Scalar::all(0)); Mat planes[] = { Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F) }; Mat complexI; // 用0加到另一個擴增面 merge(planes, 2, complexI); // 將結果大小與原圖穩合 dft(complexI, complexI); // 計算擴增值病患成對數的刻度 // => log(1 + sqrt(Re(DFT(I))^2 + Im(DFT(I))^2)) // planes[0] = Re(DFT(I), planes[1] = Im(DFT(I)) split(complexI, planes); // planes[0] = magnitude magnitude(planes[0], planes[1], planes[0]); Mat magI = planes[0]; // 換成對數的刻度 magI += Scalar::all(1); log(magI, magI); // 如果有奇數的行或列就修剪光譜 (spectrum) magI = magI(Rect(0, 0, magI.cols & -2, magI.rows & -2)); // 安排傅立葉圖的四象區塊(quadrants) // 如此原點在圖的中間 int cx = magI.cols / 2; int cy = magI.rows / 2; // 左上 - 每區都產生圖塊 Mat q0(magI, Rect(0, 0, cx, cy)); // 右上 Mat q1(magI, Rect(cx, 0, cx, cy)); // 左下 Mat q2(magI, Rect(0, cy, cx, cy)); // 右下 Mat q3(magI, Rect(cx, cy, cx, cy)); Mat tmp; // 四象區塊左上與右下對調 q0.copyTo(tmp); q3.copyTo(q0); tmp.copyTo(q3); // 四象區塊右上與左下對調 q1.copyTo(tmp); q2.copyTo(q1); tmp.copyTo(q2); // 以浮點值(0與1之間)轉換圖像到可見的圖 into a normalize(magI, magI, 0, 1, CV_MINMAX); // Show the result imshow("Input Image", I); imshow("spectrum magnitude", magI); waitKey(0); |
文章標籤
全站熱搜
留言列表