Update to 2.0.0 tree from current Fremantle build
[opencv] / samples / octave / dft.m
1 #! /usr/bin/env octave
2 cv;
3 highgui;
4
5 ## Rearrange the quadrants of Fourier image so that the origin is at
6 ## the image center
7 ## src & dst arrays of equal size & type
8 function cvShiftDFT(src_arr, dst_arr )
9
10   size = cvGetSize(src_arr);
11   dst_size = cvGetSize(dst_arr);
12
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", \
17             __FILE__, __LINE__ );    
18   endif
19
20   if(swig_this(src_arr) == swig_this(dst_arr))
21     tmp = cvCreateMat(size.height/2, size.width/2, cvGetElemType(src_arr));
22   endif
23   
24   cx = size.width/2;
25   cy = size.height/2; # image center
26
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) );
35
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", \
40               __FILE__, __LINE__ );    
41     endif
42     
43     cvCopy(q3, d1);
44     cvCopy(q4, d2);
45     cvCopy(q1, d3);
46     cvCopy(q2, d4);
47     
48   else
49     cvCopy(q3, tmp);
50     cvCopy(q1, q3);
51     cvCopy(tmp, q1);
52     cvCopy(q4, tmp);
53     cvCopy(q2, q4);
54     cvCopy(tmp, q2);
55   endif
56 endfunction
57
58
59 im = cvLoadImage( argv(){1}, CV_LOAD_IMAGE_GRAYSCALE);
60
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);
64
65 cvScale(im, realInput, 1.0, 0.0);
66 cvZero(imaginaryInput);
67 cvMerge(realInput, imaginaryInput, [], [], complexInput);
68
69 dft_M = cvGetOptimalDFTSize( im.height - 1 );
70 dft_N = cvGetOptimalDFTSize( im.width - 1 );
71
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);
75
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));
81   cvZero( tmp );
82 endif
83
84 ## no need to pad bottom part of dft_A with zeros because of
85 ## use nonzero_rows parameter in cvDFT() call below
86
87 cvDFT( dft_A, dft_A, CV_DXT_FORWARD, complexInput.height );
88
89 cvNamedWindow("win", 0);
90 cvNamedWindow("magnitude", 0);
91 cvShowImage("win", im);
92
93 ## Split Fourier in real and imaginary parts
94 cvSplit( dft_A, image_Re, image_Im, [], [] );
95
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 );
101
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)
105
106
107 ## Rearrange the quadrants of Fourier image so that the origin is at
108 ## the image center
109 cvShiftDFT( image_Re, image_Re );
110
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);
114
115 cvWaitKey(-1);