Update to 2.0.0 tree from current Fremantle build
[opencv] / samples / c / dft.c
1 #define CV_NO_BACKWARD_COMPATIBILITY
2
3 #include <cxcore.h>
4 #include <cv.h>
5 #include <highgui.h>
6
7 // Rearrange the quadrants of Fourier image so that the origin is at
8 // the image center
9 // src & dst arrays of equal size & type
10 void cvShiftDFT(CvArr * src_arr, CvArr * dst_arr )
11 {
12     CvMat * tmp=0;
13     CvMat q1stub, q2stub;
14     CvMat q3stub, q4stub;
15     CvMat d1stub, d2stub;
16     CvMat d3stub, d4stub;
17     CvMat * q1, * q2, * q3, * q4;
18     CvMat * d1, * d2, * d3, * d4;
19
20     CvSize size = cvGetSize(src_arr);
21     CvSize dst_size = cvGetSize(dst_arr);
22     int cx, cy;
23
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__ );
27     }
28
29     if(src_arr==dst_arr){
30         tmp = cvCreateMat(size.height/2, size.width/2, cvGetElemType(src_arr));
31     }
32
33     cx = size.width/2;
34     cy = size.height/2; // image center
35
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) );
44
45     if(src_arr!=dst_arr){
46         if( !CV_ARE_TYPES_EQ( q1, d1 )){
47             cvError( CV_StsUnmatchedFormats, "cvShiftDFT", "Source and Destination arrays must have the same format", __FILE__, __LINE__ );
48         }
49         cvCopy(q3, d1, 0);
50         cvCopy(q4, d2, 0);
51         cvCopy(q1, d3, 0);
52         cvCopy(q2, d4, 0);
53     }
54     else{
55         cvCopy(q3, tmp, 0);
56         cvCopy(q1, q3, 0);
57         cvCopy(tmp, q1, 0);
58         cvCopy(q4, tmp, 0);
59         cvCopy(q2, q4, 0);
60         cvCopy(tmp, q2, 0);
61     }
62 }
63
64 int main(int argc, char ** argv)
65 {
66     const char* filename = argc >=2 ? argv[1] : "lena.jpg";
67     IplImage * im;
68
69     IplImage * realInput;
70     IplImage * imaginaryInput;
71     IplImage * complexInput;
72     int dft_M, dft_N;
73     CvMat* dft_A, tmp;
74     IplImage * image_Re;
75     IplImage * image_Im;
76     double m, M;
77
78     im = cvLoadImage( filename, CV_LOAD_IMAGE_GRAYSCALE );
79     if( !im )
80         return -1;
81
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);
85
86     cvScale(im, realInput, 1.0, 0.0);
87     cvZero(imaginaryInput);
88     cvMerge(realInput, imaginaryInput, NULL, NULL, complexInput);
89
90     dft_M = cvGetOptimalDFTSize( im->height - 1 );
91     dft_N = cvGetOptimalDFTSize( im->width - 1 );
92
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);
96
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 )
101     {
102         cvGetSubRect( dft_A, &tmp, cvRect(im->width,0, dft_A->cols - im->width, im->height));
103         cvZero( &tmp );
104     }
105
106     // no need to pad bottom part of dft_A with zeros because of
107     // use nonzero_rows parameter in cvDFT() call below
108
109     cvDFT( dft_A, dft_A, CV_DXT_FORWARD, complexInput->height );
110
111     cvNamedWindow("win", 0);
112     cvNamedWindow("magnitude", 0);
113     cvShowImage("win", im);
114
115     // Split Fourier in real and imaginary parts
116     cvSplit( dft_A, image_Re, image_Im, 0, 0 );
117
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 );
123
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)
127
128
129     // Rearrange the quadrants of Fourier image so that the origin is at
130     // the image center
131     cvShiftDFT( image_Re, image_Re );
132
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);
136
137     cvWaitKey(-1);
138     return 0;
139 }