PageRenderTime 78ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/opencv/src/dft.c

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