本文主要是介绍【opencv练习12 - DFT离散傅立叶变换】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
/******************************************************测试程序 - 【 DFT 离散傅立叶变换】时间:2016年8月23日
*******************************************************/
int main6(void)
{Mat I = imread("YY01.jpg", CV_LOAD_IMAGE_GRAYSCALE);if( I.empty())return -1;//1、扩展图像尺寸Mat padded; //expand input image to optimal sizeint m = getOptimalDFTSize( I.rows );int n = getOptimalDFTSize( I.cols ); // on the border add zero valuescopyMakeBorder(I, padded, 0, m - I.rows, 0, n - I.cols, BORDER_CONSTANT, Scalar::all(0));//2、分别为实数,复数申请空间// planes[0]-->Re(DFT(I), planes[1]--> Im(DFT(I))Mat planes[] = {Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F)};Mat complexI;merge(planes, 2, complexI); // Add to the expanded another plane with zeros//【3、傅立叶变换】dft(complexI, complexI); // this way the result may fit in the source matrix//【4、根据实虚计算幅度】// compute the magnitude and switch to logarithmic scale// => log(1 + sqrt(Re(DFT(I))^2 + Im(DFT(I))^2))split(complexI, planes); // planes[0] = Re(DFT(I), planes[1] = Im(DFT(I))magnitude(planes[0], planes[1], planes[0]);// planes[0] = magnitudeMat magI = planes[0];//【5、切换对数模式】magI += Scalar::all(1); // switch to logarithmic scalelog(magI, magI);//【6、重排】// crop the spectrum, if it has an odd number of rows or columnsmagI = magI(Rect(0, 0, magI.cols & -2, magI.rows & -2));// rearrange the quadrants of Fourier image so that the origin is at the image centerint cx = magI.cols/2;int cy = magI.rows/2;Mat q0(magI, Rect(0, 0, cx, cy)); // Top-Left - Create a ROI per quadrant【设置ROI】Mat q1(magI, Rect(cx, 0, cx, cy)); // Top-RightMat q2(magI, Rect(0, cy, cx, cy)); // Bottom-LeftMat q3(magI, Rect(cx, cy, cx, cy)); // Bottom-RightMat tmp; //【交换变换区域】swap quadrants (Top-Left with Bottom-Right)q0.copyTo(tmp);q3.copyTo(q0);tmp.copyTo(q3);q1.copyTo(tmp); // swap quadrant (Top-Right with Bottom-Left)q2.copyTo(q1);tmp.copyTo(q2);//【7、归一化】normalize(magI, magI, 0, 1, CV_MINMAX); // Transform the matrix with float values into a// viewable image form (float between values 0 and 1).imshow("Input Image" , I ); // Show the resultimshow("spectrum magnitude", magI);waitKey();return 0;
}
这篇关于【opencv练习12 - DFT离散傅立叶变换】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!