5 // Rearrange the quadrants of Fourier image so that the origin is at
7 // src & dst arrays of equal size & type
8 void cvShiftDFT(CvArr * src_arr, CvArr * dst_arr )
15 CvMat * q1, * q2, * q3, * q4;
16 CvMat * d1, * d2, * d3, * d4;
18 CvSize size = cvGetSize(src_arr);
19 CvSize dst_size = cvGetSize(dst_arr);
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__ );
28 tmp = cvCreateMat(size.height/2, size.width/2, cvGetElemType(src_arr));
32 cy = size.height/2; // image center
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) );
44 if( !CV_ARE_TYPES_EQ( q1, d1 )){
45 cvError( CV_StsUnmatchedFormats, "cvShiftDFT", "Source and Destination arrays must have the same format", __FILE__, __LINE__ );
62 int main(int argc, char ** argv)
64 const char* filename = argc >=2 ? argv[1] : "lena.jpg";
68 IplImage * imaginaryInput;
69 IplImage * complexInput;
76 im = cvLoadImage( filename, CV_LOAD_IMAGE_GRAYSCALE );
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);
84 cvScale(im, realInput, 1.0, 0.0);
85 cvZero(imaginaryInput);
86 cvMerge(realInput, imaginaryInput, NULL, NULL, complexInput);
88 dft_M = cvGetOptimalDFTSize( im->height - 1 );
89 dft_N = cvGetOptimalDFTSize( im->width - 1 );
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);
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 )
100 cvGetSubRect( dft_A, &tmp, cvRect(im->width,0, dft_A->cols - im->width, im->height));
104 // no need to pad bottom part of dft_A with zeros because of
105 // use nonzero_rows parameter in cvDFT() call below
107 cvDFT( dft_A, dft_A, CV_DXT_FORWARD, complexInput->height );
109 cvNamedWindow("win", 0);
110 cvNamedWindow("magnitude", 0);
111 cvShowImage("win", im);
113 // Split Fourier in real and imaginary parts
114 cvSplit( dft_A, image_Re, image_Im, 0, 0 );
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 );
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)
127 // Rearrange the quadrants of Fourier image so that the origin is at
129 cvShiftDFT( image_Re, image_Re );
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);