1 #define CV_NO_BACKWARD_COMPATIBILITY
7 // Rearrange the quadrants of Fourier image so that the origin is at
9 // src & dst arrays of equal size & type
10 void cvShiftDFT(CvArr * src_arr, CvArr * dst_arr )
17 CvMat * q1, * q2, * q3, * q4;
18 CvMat * d1, * d2, * d3, * d4;
20 CvSize size = cvGetSize(src_arr);
21 CvSize dst_size = cvGetSize(dst_arr);
24 if(dst_size.width != size.width ||
25 dst_size.height != size.height){
26 cvError( CV_StsUnmatchedSizes, "cvShiftDFT", "Source and Destination arrays must have equal sizes", __FILE__, __LINE__ );
30 tmp = cvCreateMat(size.height/2, size.width/2, cvGetElemType(src_arr));
34 cy = size.height/2; // image center
36 q1 = cvGetSubRect( src_arr, &q1stub, cvRect(0,0,cx, cy) );
37 q2 = cvGetSubRect( src_arr, &q2stub, cvRect(cx,0,cx,cy) );
38 q3 = cvGetSubRect( src_arr, &q3stub, cvRect(cx,cy,cx,cy) );
39 q4 = cvGetSubRect( src_arr, &q4stub, cvRect(0,cy,cx,cy) );
40 d1 = cvGetSubRect( src_arr, &d1stub, cvRect(0,0,cx,cy) );
41 d2 = cvGetSubRect( src_arr, &d2stub, cvRect(cx,0,cx,cy) );
42 d3 = cvGetSubRect( src_arr, &d3stub, cvRect(cx,cy,cx,cy) );
43 d4 = cvGetSubRect( src_arr, &d4stub, cvRect(0,cy,cx,cy) );
46 if( !CV_ARE_TYPES_EQ( q1, d1 )){
47 cvError( CV_StsUnmatchedFormats, "cvShiftDFT", "Source and Destination arrays must have the same format", __FILE__, __LINE__ );
64 int main(int argc, char ** argv)
66 const char* filename = argc >=2 ? argv[1] : "lena.jpg";
70 IplImage * imaginaryInput;
71 IplImage * complexInput;
78 im = cvLoadImage( filename, CV_LOAD_IMAGE_GRAYSCALE );
82 realInput = cvCreateImage( cvGetSize(im), IPL_DEPTH_64F, 1);
83 imaginaryInput = cvCreateImage( cvGetSize(im), IPL_DEPTH_64F, 1);
84 complexInput = cvCreateImage( cvGetSize(im), IPL_DEPTH_64F, 2);
86 cvScale(im, realInput, 1.0, 0.0);
87 cvZero(imaginaryInput);
88 cvMerge(realInput, imaginaryInput, NULL, NULL, complexInput);
90 dft_M = cvGetOptimalDFTSize( im->height - 1 );
91 dft_N = cvGetOptimalDFTSize( im->width - 1 );
93 dft_A = cvCreateMat( dft_M, dft_N, CV_64FC2 );
94 image_Re = cvCreateImage( cvSize(dft_N, dft_M), IPL_DEPTH_64F, 1);
95 image_Im = cvCreateImage( cvSize(dft_N, dft_M), IPL_DEPTH_64F, 1);
97 // copy A to dft_A and pad dft_A with zeros
98 cvGetSubRect( dft_A, &tmp, cvRect(0,0, im->width, im->height));
99 cvCopy( complexInput, &tmp, NULL );
100 if( dft_A->cols > im->width )
102 cvGetSubRect( dft_A, &tmp, cvRect(im->width,0, dft_A->cols - im->width, im->height));
106 // no need to pad bottom part of dft_A with zeros because of
107 // use nonzero_rows parameter in cvDFT() call below
109 cvDFT( dft_A, dft_A, CV_DXT_FORWARD, complexInput->height );
111 cvNamedWindow("win", 0);
112 cvNamedWindow("magnitude", 0);
113 cvShowImage("win", im);
115 // Split Fourier in real and imaginary parts
116 cvSplit( dft_A, image_Re, image_Im, 0, 0 );
118 // Compute the magnitude of the spectrum Mag = sqrt(Re^2 + Im^2)
119 cvPow( image_Re, image_Re, 2.0);
120 cvPow( image_Im, image_Im, 2.0);
121 cvAdd( image_Re, image_Im, image_Re, NULL);
122 cvPow( image_Re, image_Re, 0.5 );
124 // Compute log(1 + Mag)
125 cvAddS( image_Re, cvScalarAll(1.0), image_Re, NULL ); // 1 + Mag
126 cvLog( image_Re, image_Re ); // log(1 + Mag)
129 // Rearrange the quadrants of Fourier image so that the origin is at
131 cvShiftDFT( image_Re, image_Re );
133 cvMinMaxLoc(image_Re, &m, &M, NULL, NULL, NULL);
134 cvScale(image_Re, image_Re, 1.0/(M-m), 1.0*(-m)/(M-m));
135 cvShowImage("magnitude", image_Re);