2 from opencv.cv import *
3 from opencv.highgui import *
6 # Rearrange the quadrants of Fourier image so that the origin is at
8 # src & dst arrays of equal size & type
9 def cvShiftDFT(src_arr, dst_arr ):
11 size = cvGetSize(src_arr)
12 dst_size = cvGetSize(dst_arr)
14 if(dst_size.width != size.width or
15 dst_size.height != size.height) :
16 cvError( CV_StsUnmatchedSizes, "cvShiftDFT", "Source and Destination arrays must have equal sizes", __FILE__, __LINE__ )
18 if(src_arr is dst_arr):
19 tmp = cvCreateMat(size.height/2, size.width/2, cvGetElemType(src_arr))
22 cy = size.height/2 # image center
24 q1 = cvGetSubRect( src_arr, cvRect(0,0,cx, cy) )
25 q2 = cvGetSubRect( src_arr, cvRect(cx,0,cx,cy) )
26 q3 = cvGetSubRect( src_arr, cvRect(cx,cy,cx,cy) )
27 q4 = cvGetSubRect( src_arr, cvRect(0,cy,cx,cy) )
28 d1 = cvGetSubRect( src_arr, cvRect(0,0,cx,cy) )
29 d2 = cvGetSubRect( src_arr, cvRect(cx,0,cx,cy) )
30 d3 = cvGetSubRect( src_arr, cvRect(cx,cy,cx,cy) )
31 d4 = cvGetSubRect( src_arr, cvRect(0,cy,cx,cy) )
33 if(src_arr is not dst_arr):
34 if( not CV_ARE_TYPES_EQ( q1, d1 )):
35 cvError( CV_StsUnmatchedFormats, "cvShiftDFT", "Source and Destination arrays must have the same format", __FILE__, __LINE__ )
50 if __name__ == "__main__":
52 im = cvLoadImage( sys.argv[1], CV_LOAD_IMAGE_GRAYSCALE)
54 realInput = cvCreateImage( cvGetSize(im), IPL_DEPTH_64F, 1)
55 imaginaryInput = cvCreateImage( cvGetSize(im), IPL_DEPTH_64F, 1)
56 complexInput = cvCreateImage( cvGetSize(im), IPL_DEPTH_64F, 2)
58 cvScale(im, realInput, 1.0, 0.0)
59 cvZero(imaginaryInput)
60 cvMerge(realInput, imaginaryInput, None, None, complexInput)
62 dft_M = cvGetOptimalDFTSize( im.height - 1 )
63 dft_N = cvGetOptimalDFTSize( im.width - 1 )
65 dft_A = cvCreateMat( dft_M, dft_N, CV_64FC2 )
66 image_Re = cvCreateImage( cvSize(dft_N, dft_M), IPL_DEPTH_64F, 1)
67 image_Im = cvCreateImage( cvSize(dft_N, dft_M), IPL_DEPTH_64F, 1)
69 # copy A to dft_A and pad dft_A with zeros
70 tmp = cvGetSubRect( dft_A, cvRect(0,0, im.width, im.height))
71 cvCopy( complexInput, tmp, None )
72 if(dft_A.width > im.width):
73 tmp = cvGetSubRect( dft_A, cvRect(im.width,0, dft_N - im.width, im.height))
76 # no need to pad bottom part of dft_A with zeros because of
77 # use nonzero_rows parameter in cvDFT() call below
79 cvDFT( dft_A, dft_A, CV_DXT_FORWARD, complexInput.height )
81 cvNamedWindow("win", 0)
82 cvNamedWindow("magnitude", 0)
83 cvShowImage("win", im)
85 # Split Fourier in real and imaginary parts
86 cvSplit( dft_A, image_Re, image_Im, None, None )
88 # Compute the magnitude of the spectrum Mag = sqrt(Re^2 + Im^2)
89 cvPow( image_Re, image_Re, 2.0)
90 cvPow( image_Im, image_Im, 2.0)
91 cvAdd( image_Re, image_Im, image_Re, None)
92 cvPow( image_Re, image_Re, 0.5 )
94 # Compute log(1 + Mag)
95 cvAddS( image_Re, cvScalarAll(1.0), image_Re, None ) # 1 + Mag
96 cvLog( image_Re, image_Re ) # log(1 + Mag)
99 # Rearrange the quadrants of Fourier image so that the origin is at
101 cvShiftDFT( image_Re, image_Re )
103 min, max, pt1, pt2 = cvMinMaxLoc(image_Re)
104 cvScale(image_Re, image_Re, 1.0/(max-min), 1.0*(-min)/(max-min))
105 cvShowImage("magnitude", image_Re)