5 ## Rearrange the quadrants of Fourier image so that the origin is at
7 ## src & dst arrays of equal size & type
8 function cvShiftDFT(src_arr, dst_arr )
10 size = cvGetSize(src_arr);
11 dst_size = cvGetSize(dst_arr);
13 if(dst_size.width != size.width || \
14 dst_size.height != size.height)
15 cvError( CV_StsUnmatchedSizes, "cvShiftDFT", \
16 "Source and Destination arrays must have equal sizes", \
20 if(swig_this(src_arr) == swig_this(dst_arr))
21 tmp = cvCreateMat(size.height/2, size.width/2, cvGetElemType(src_arr));
25 cy = size.height/2; # image center
27 q1 = cvGetSubRect( src_arr, cvRect(0,0,cx, cy) );
28 q2 = cvGetSubRect( src_arr, cvRect(cx,0,cx,cy) );
29 q3 = cvGetSubRect( src_arr, cvRect(cx,cy,cx,cy) );
30 q4 = cvGetSubRect( src_arr, cvRect(0,cy,cx,cy) );
31 d1 = cvGetSubRect( src_arr, cvRect(0,0,cx,cy) );
32 d2 = cvGetSubRect( src_arr, cvRect(cx,0,cx,cy) );
33 d3 = cvGetSubRect( src_arr, cvRect(cx,cy,cx,cy) );
34 d4 = cvGetSubRect( src_arr, cvRect(0,cy,cx,cy) );
36 if(swig_this(src_arr) != swig_this(dst_arr))
37 if( !CV_ARE_TYPES_EQ( q1, d1 ))
38 cvError( CV_StsUnmatchedFormats, \
39 "cvShiftDFT", "Source and Destination arrays must have the same format", \
59 im = cvLoadImage( argv(){1}, CV_LOAD_IMAGE_GRAYSCALE);
61 realInput = cvCreateImage( cvGetSize(im), IPL_DEPTH_64F, 1);
62 imaginaryInput = cvCreateImage( cvGetSize(im), IPL_DEPTH_64F, 1);
63 complexInput = cvCreateImage( cvGetSize(im), IPL_DEPTH_64F, 2);
65 cvScale(im, realInput, 1.0, 0.0);
66 cvZero(imaginaryInput);
67 cvMerge(realInput, imaginaryInput, [], [], complexInput);
69 dft_M = cvGetOptimalDFTSize( im.height - 1 );
70 dft_N = cvGetOptimalDFTSize( im.width - 1 );
72 dft_A = cvCreateMat( dft_M, dft_N, CV_64FC2 );
73 image_Re = cvCreateImage( cvSize(dft_N, dft_M), IPL_DEPTH_64F, 1);
74 image_Im = cvCreateImage( cvSize(dft_N, dft_M), IPL_DEPTH_64F, 1);
76 ## copy A to dft_A and pad dft_A with zeros
77 tmp = cvGetSubRect( dft_A, cvRect(0,0, im.width, im.height));
78 cvCopy( complexInput, tmp, [] );
79 if(dft_A.width > im.width)
80 tmp = cvGetSubRect( dft_A, cvRect(im.width,0, dft_N - im.width, im.height));
84 ## no need to pad bottom part of dft_A with zeros because of
85 ## use nonzero_rows parameter in cvDFT() call below
87 cvDFT( dft_A, dft_A, CV_DXT_FORWARD, complexInput.height );
89 cvNamedWindow("win", 0);
90 cvNamedWindow("magnitude", 0);
91 cvShowImage("win", im);
93 ## Split Fourier in real and imaginary parts
94 cvSplit( dft_A, image_Re, image_Im, [], [] );
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, []);
100 cvPow( image_Re, image_Re, 0.5 );
102 ## Compute log(1 + Mag)
103 cvAddS( image_Re, cvScalarAll(1.0), image_Re, [] ); # 1 + Mag
104 cvLog( image_Re, image_Re ); # log(1 + Mag)
107 ## Rearrange the quadrants of Fourier image so that the origin is at
109 cvShiftDFT( image_Re, image_Re );
111 [min, max] = cvMinMaxLoc(image_Re);
112 cvScale(image_Re, image_Re, 1.0/(max-min), 1.0*(-min)/(max-min));
113 cvShowImage("magnitude", image_Re);