Update the changelog
[opencv] / samples / python / dft.py
1 #!/usr/bin/python
2 from opencv.cv import *
3 from opencv.highgui import *
4 import sys
5
6 # Rearrange the quadrants of Fourier image so that the origin is at
7 # the image center
8 # src & dst arrays of equal size & type
9 def cvShiftDFT(src_arr, dst_arr ):
10
11     size = cvGetSize(src_arr);
12     dst_size = cvGetSize(dst_arr);
13
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__ );    
17
18     if(src_arr is dst_arr):
19         tmp = cvCreateMat(size.height/2, size.width/2, cvGetElemType(src_arr));
20     
21     cx = size.width/2;
22     cy = size.height/2; # image center
23
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) );
32
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__ );    
36         
37         cvCopy(q3, d1);
38         cvCopy(q4, d2);
39         cvCopy(q1, d3);
40         cvCopy(q2, d4);
41     
42     else:
43         cvCopy(q3, tmp);
44         cvCopy(q1, q3);
45         cvCopy(tmp, q1);
46         cvCopy(q4, tmp);
47         cvCopy(q2, q4);
48         cvCopy(tmp, q2);
49
50 if __name__ == "__main__":
51     
52     im = cvLoadImage( sys.argv[1], CV_LOAD_IMAGE_GRAYSCALE);
53
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);
57
58     cvScale(im, realInput, 1.0, 0.0);
59     cvZero(imaginaryInput);
60     cvMerge(realInput, imaginaryInput, None, None, complexInput);
61
62     dft_M = cvGetOptimalDFTSize( im.height - 1 );
63     dft_N = cvGetOptimalDFTSize( im.width - 1 );
64
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);
68
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));
74         cvZero( tmp );
75
76     # no need to pad bottom part of dft_A with zeros because of
77     # use nonzero_rows parameter in cvDFT() call below
78
79     cvDFT( dft_A, dft_A, CV_DXT_FORWARD, complexInput.height );
80
81     cvNamedWindow("win", 0);
82     cvNamedWindow("magnitude", 0);
83     cvShowImage("win", im);
84
85     # Split Fourier in real and imaginary parts
86     cvSplit( dft_A, image_Re, image_Im, None, None );
87
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 );
93
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)
97
98
99     # Rearrange the quadrants of Fourier image so that the origin is at
100     # the image center
101     cvShiftDFT( image_Re, image_Re );
102
103     min, max = cvMinMaxLoc(image_Re);
104     cvScale(image_Re, image_Re, 1.0/(max-min), 1.0*(-min)/(max-min));
105     cvShowImage("magnitude", image_Re);
106
107     cvWaitKey(-1);