1 #include "opencv2/core/core.hpp" 2 #include "opencv2/imgproc/imgproc.hpp" 3 #include "opencv2/highgui/highgui.hpp" 4 #include <iostream> 5 int main(int argc, char ** argv) 6 { 7 const char* filename = argc >=2 ? argv[1] : "lena.jpg"; 8 9 Mat I = imread(filename, CV_LOAD_IMAGE_GRAYSCALE); 10 if( I.empty()) 11 return -1; 12 13 Mat padded; //expand input image to optimal size 14 int m = getOptimalDFTSize( I.rows ); 15 int n = getOptimalDFTSize( I.cols ); // on the border add zero values 16 copyMakeBorder(I, padded, 0, m - I.rows, 0, n - I.cols, BORDER_CONSTANT, Scalar::all(0)); 17 18 Mat planes[] = {Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F)}; 19 Mat complexI; 20 merge(planes, 2, complexI); // Add to the expanded another plane with zeros 21 22 dft(complexI, complexI); // this way the result may fit in the source matrix 23 24 // compute the magnitude and switch to logarithmic scale 25 // => log(1 + sqrt(Re(DFT(I))^2 + Im(DFT(I))^2)) 26 split(complexI, planes); // planes[0] = Re(DFT(I), planes[1] = Im(DFT(I)) 27 magnitude(planes[0], planes[1], planes[0]);// planes[0] = magnitude 28 Mat magI = planes[0]; 29 30 magI += Scalar::all(1); // switch to logarithmic scale 31 log(magI, magI); 32 33 // crop the spectrum, if it has an odd number of rows or columns 34 magI = magI(Rect(0, 0, magI.cols & -2, magI.rows & -2)); 35 36 // rearrange the quadrants of Fourier image so that the origin is at the image center 37 int cx = magI.cols/2; 38 int cy = magI.rows/2; 39 40 Mat q0(magI, Rect(0, 0, cx, cy)); // Top-Left - Create a ROI per quadrant 41 Mat q1(magI, Rect(cx, 0, cx, cy)); // Top-Right 42 Mat q2(magI, Rect(0, cy, cx, cy)); // Bottom-Left 43 Mat q3(magI, Rect(cx, cy, cx, cy)); // Bottom-Right 44 45 Mat tmp; // swap quadrants (Top-Left with Bottom-Right) 46 q0.copyTo(tmp); 47 q3.copyTo(q0); 48 tmp.copyTo(q3); 49 50 q1.copyTo(tmp); // swap quadrant (Top-Right with Bottom-Left) 51 q2.copyTo(q1); 52 tmp.copyTo(q2); 53 54 normalize(magI, magI, 0, 1, CV_MINMAX); // Transform the matrix with float values into a 55 // viewable image form (float between values 0 and 1). 56 57 imshow("Input Image" , I ); // Show the result 58 imshow("spectrum magnitude", magI); 59 waitKey(); 60 61 return 0; 62 }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)