/santa/OpenCV/samples/c/dft.c

https://github.com/siegleal/iSanta · C · 137 lines · 101 code · 25 blank · 11 comment · 9 complexity · 5823adeadc6bbe0dc51d818a2539e64f MD5 · raw file

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