• Open Source Computer Vision Library

离散傅立叶变换(DFT)

Wikipedia,自由的百科全书

Lena灰度图像的结果:

Image:Example-lena-dft.png

代码

  1. #include <cxcore.h>
  2. #include <cv.h>
  3. #include <highgui.h>
  4.  
  5. // Rearrange the quadrants of Fourier image so that the origin is at
  6. // the image center
  7. // src & dst arrays of equal size & type
  8. void cvShiftDFT(CvArr * src_arr, CvArr * dst_arr )
  9. {
  10. CvMat * tmp;
  11. CvMat q1stub, q2stub;
  12. CvMat q3stub, q4stub;
  13. CvMat d1stub, d2stub;
  14. CvMat d3stub, d4stub;
  15. CvMat * q1, * q2, * q3, * q4;
  16. CvMat * d1, * d2, * d3, * d4;
  17.  
  18. CvSize size = cvGetSize(src_arr);
  19. CvSize dst_size = cvGetSize(dst_arr);
  20. int cx, cy;
  21.  
  22. if(dst_size.width != size.width ||
  23. dst_size.height != size.height){
  24. cvError( CV_StsUnmatchedSizes, "cvShiftDFT", "Source and Destination arrays must have equal sizes", __FILE__, __LINE__ );
  25. }
  26.  
  27. if(src_arr==dst_arr){
  28. tmp = cvCreateMat(size.height/2, size.width/2, cvGetElemType(src_arr));
  29. }
  30.  
  31. cx = size.width/2;
  32. cy = size.height/2; // image center
  33.  
  34. q1 = cvGetSubRect( src_arr, &q1stub, cvRect(0,0,cx, cy) );
  35. q2 = cvGetSubRect( src_arr, &q2stub, cvRect(cx,0,cx,cy) );
  36. q3 = cvGetSubRect( src_arr, &q3stub, cvRect(cx,cy,cx,cy) );
  37. q4 = cvGetSubRect( src_arr, &q4stub, cvRect(0,cy,cx,cy) );
  38. d1 = cvGetSubRect( src_arr, &d1stub, cvRect(0,0,cx,cy) );
  39. d2 = cvGetSubRect( src_arr, &d2stub, cvRect(cx,0,cx,cy) );
  40. d3 = cvGetSubRect( src_arr, &d3stub, cvRect(cx,cy,cx,cy) );
  41. d4 = cvGetSubRect( src_arr, &d4stub, cvRect(0,cy,cx,cy) );
  42.  
  43. if(src_arr!=dst_arr){
  44. if( !CV_ARE_TYPES_EQ( q1, d1 )){
  45. cvError( CV_StsUnmatchedFormats, "cvShiftDFT", "Source and Destination arrays must have the same format", __FILE__, __LINE__ );
  46. }
  47. cvCopy(q3, d1, 0);
  48. cvCopy(q4, d2, 0);
  49. cvCopy(q1, d3, 0);
  50. cvCopy(q2, d4, 0);
  51. }
  52. else{
  53. cvCopy(q3, tmp, 0);
  54. cvCopy(q1, q3, 0);
  55. cvCopy(tmp, q1, 0);
  56. cvCopy(q4, tmp, 0);
  57. cvCopy(q2, q4, 0);
  58. cvCopy(tmp, q2, 0);
  59. }
  60. }
  61.  
  62. int main(int argc, char ** argv)
  63. {
  64. const char* filename = argc >=2 ? argv[1] : "lena.jpg";
  65. IplImage * im;
  66.  
  67. IplImage * realInput;
  68. IplImage * imaginaryInput;
  69. IplImage * complexInput;
  70. int dft_M, dft_N;
  71. CvMat* dft_A, tmp;
  72. IplImage * image_Re;
  73. IplImage * image_Im;
  74. double m, M;
  75.  
  76. im = cvLoadImage( filename, CV_LOAD_IMAGE_GRAYSCALE );
  77. if( !im )
  78. return -1;
  79.  
  80. realInput = cvCreateImage( cvGetSize(im), IPL_DEPTH_64F, 1);
  81. imaginaryInput = cvCreateImage( cvGetSize(im), IPL_DEPTH_64F, 1);
  82. complexInput = cvCreateImage( cvGetSize(im), IPL_DEPTH_64F, 2);
  83.  
  84. cvScale(im, realInput, 1.0, 0.0);
  85. cvZero(imaginaryInput);
  86. cvMerge(realInput, imaginaryInput, NULL, NULL, complexInput);
  87.  
  88. dft_M = cvGetOptimalDFTSize( im->height - 1 );
  89. dft_N = cvGetOptimalDFTSize( im->width - 1 );
  90.  
  91. dft_A = cvCreateMat( dft_M, dft_N, CV_64FC2 );
  92. image_Re = cvCreateImage( cvSize(dft_N, dft_M), IPL_DEPTH_64F, 1);
  93. image_Im = cvCreateImage( cvSize(dft_N, dft_M), IPL_DEPTH_64F, 1);
  94.  
  95. // copy A to dft_A and pad dft_A with zeros
  96. cvGetSubRect( dft_A, &tmp, cvRect(0,0, im->width, im->height));
  97. cvCopy( complexInput, &tmp, NULL );
  98. if( dft_A->cols > im->width )
  99. {
  100. cvGetSubRect( dft_A, &tmp, cvRect(im->width,0, dft_A->cols - im->width, im->height));
  101. cvZero( &tmp );
  102. }
  103.  
  104. // no need to pad bottom part of dft_A with zeros because of
  105. // use nonzero_rows parameter in cvDFT() call below
  106.  
  107. cvDFT( dft_A, dft_A, CV_DXT_FORWARD, complexInput->height );
  108.  
  109. cvNamedWindow("win", 0);
  110. cvNamedWindow("magnitude", 0);
  111. cvShowImage("win", im);
  112.  
  113. // Split Fourier in real and imaginary parts
  114. cvSplit( dft_A, image_Re, image_Im, 0, 0 );
  115.  
  116. // Compute the magnitude of the spectrum Mag = sqrt(Re^2 + Im^2)
  117. cvPow( image_Re, image_Re, 2.0);
  118. cvPow( image_Im, image_Im, 2.0);
  119. cvAdd( image_Re, image_Im, image_Re, NULL);
  120. cvPow( image_Re, image_Re, 0.5 );
  121.  
  122. // Compute log(1 + Mag)
  123. cvAddS( image_Re, cvScalarAll(1.0), image_Re, NULL ); // 1 + Mag
  124. cvLog( image_Re, image_Re ); // log(1 + Mag)
  125.  
  126.  
  127. // Rearrange the quadrants of Fourier image so that the origin is at
  128. // the image center
  129. cvShiftDFT( image_Re, image_Re );
  130.  
  131. cvMinMaxLoc(image_Re, &m, &M, NULL, NULL, NULL);
  132. cvScale(image_Re, image_Re, 1.0/(M-m), 1.0*(-m)/(M-m));
  133. cvShowImage("magnitude", image_Re);
  134.  
  135. cvWaitKey(-1);
  136. return 0;
  137. }
  138.  
Views
Personal tools