1 
2 ///////////////////////////////////////////////////////////
3 //                                                       //
4 //                         SAGA                          //
5 //                                                       //
6 //      System for Automated Geoscientific Analyses      //
7 //                                                       //
8 //                     Tool Library                      //
9 //                        OpenCV                         //
10 //                                                       //
11 //-------------------------------------------------------//
12 //                                                       //
13 //                   opencv_fourier.cpp                  //
14 //                                                       //
15 //                 Copyright (C) 2009 by                 //
16 //                      Olaf Conrad                      //
17 //                                                       //
18 //-------------------------------------------------------//
19 //                                                       //
20 // This file is part of 'SAGA - System for Automated     //
21 // Geoscientific Analyses'. SAGA is free software; you   //
22 // can redistribute it and/or modify it under the terms  //
23 // of the GNU General Public License as published by the //
24 // Free Software Foundation, either version 2 of the     //
25 // License, or (at your option) any later version.       //
26 //                                                       //
27 // SAGA is distributed in the hope that it will be       //
28 // useful, but WITHOUT ANY WARRANTY; without even the    //
29 // implied warranty of MERCHANTABILITY or FITNESS FOR A  //
30 // PARTICULAR PURPOSE. See the GNU General Public        //
31 // License for more details.                             //
32 //                                                       //
33 // You should have received a copy of the GNU General    //
34 // Public License along with this program; if not, see   //
35 // <http://www.gnu.org/licenses/>.                       //
36 //                                                       //
37 //-------------------------------------------------------//
38 //                                                       //
39 //    e-mail:     oconrad@saga-gis.org                   //
40 //                                                       //
41 //    contact:    Olaf Conrad                            //
42 //                Institute of Geography                 //
43 //                University of Hamburg                  //
44 //                Germany                                //
45 //                                                       //
46 ///////////////////////////////////////////////////////////
47 
48 //---------------------------------------------------------
49 #include "opencv_fourier.h"
50 
51 
52 ///////////////////////////////////////////////////////////
53 //														 //
54 //														 //
55 //														 //
56 ///////////////////////////////////////////////////////////
57 
58 //---------------------------------------------------------
59 bool	DFT		(IplImage *pInput, IplImage **ppReal, IplImage **ppImag);
60 
61 
62 ///////////////////////////////////////////////////////////
63 //														 //
64 //														 //
65 //														 //
66 ///////////////////////////////////////////////////////////
67 
68 //---------------------------------------------------------
COpenCV_FFT(void)69 COpenCV_FFT::COpenCV_FFT(void)
70 {
71 	Set_Name		(_TL("Fourier Transformation (OpenCV)"));
72 
73 	Set_Author		("O.Conrad (c) 2009");
74 
75 	Set_Description	(_TW(
76 		"Fourier Transformation."
77 	));
78 
79 	Add_Reference("https://opencv.org/", SG_T("OpenCV - Open Source Computer Vision"));
80 
81 	//-----------------------------------------------------
82 	Parameters.Add_Grid(
83 		"", "INPUT"	, _TL("Input"),
84 		_TL(""),
85 		PARAMETER_INPUT
86 	);
87 
88 	Parameters.Add_Grid(
89 		"", "REAL"	, _TL("Fourier Transformation (Real)"),
90 		_TL(""),
91 		PARAMETER_OUTPUT
92 	);
93 
94 	Parameters.Add_Grid(
95 		"", "IMAG"	, _TL("Fourier Transformation (Imaginary)"),
96 		_TL(""),
97 		PARAMETER_OUTPUT
98 	);
99 }
100 
101 
102 ///////////////////////////////////////////////////////////
103 //														 //
104 ///////////////////////////////////////////////////////////
105 
106 //---------------------------------------------------------
On_Execute(void)107 bool COpenCV_FFT::On_Execute(void)
108 {
109 	CSG_Grid	*pInput	= Parameters("INPUT")->asGrid();
110 	CSG_Grid	*pReal	= Parameters("REAL" )->asGrid();
111 	CSG_Grid	*pImag	= Parameters("IMAG" )->asGrid();
112 
113 	//-----------------------------------------------------
114 	IplImage	*cv_pInput	= Get_CVImage(pInput, SG_DATATYPE_Float);
115 	IplImage	*cv_pReal	= NULL;// Get_CVImage(Get_NX(), Get_NY(), SG_DATATYPE_Float);
116 	IplImage	*cv_pImag	= NULL;// Get_CVImage(Get_NX(), Get_NY(), SG_DATATYPE_Float);
117 
118 	//-----------------------------------------------------
119 	DFT(cv_pInput, &cv_pReal, &cv_pImag);
120 
121 	//-----------------------------------------------------
122 	Copy_CVImage_To_Grid(pReal, cv_pReal, false);
123 	Copy_CVImage_To_Grid(pImag, cv_pImag, false);
124 
125     cvReleaseImage(&cv_pInput);
126     cvReleaseImage(&cv_pReal);
127     cvReleaseImage(&cv_pImag);
128 
129 	pReal->Fmt_Name("%s [DFT, %s]", pInput->Get_Name(), _TL("Real"     ));
130 	pImag->Fmt_Name("%s [DFT, %s]", pInput->Get_Name(), _TL("Imaginary"));
131 
132 	return( true );
133 }
134 
135 
136 ///////////////////////////////////////////////////////////
137 //														 //
138 //														 //
139 //														 //
140 ///////////////////////////////////////////////////////////
141 
142 //---------------------------------------------------------
143 // Rearrange the quadrants of Fourier image so that the origin is at
144 // the image center
145 // src & dst arrays of equal size & type
cvShiftDFT(CvArr * src_arr,CvArr * dst_arr)146 bool cvShiftDFT(CvArr * src_arr, CvArr * dst_arr )
147 {
148 	CvMat * tmp;
149 	CvMat q1stub, q2stub;
150 	CvMat q3stub, q4stub;
151 	CvMat d1stub, d2stub;
152 	CvMat d3stub, d4stub;
153 	CvMat * q1, * q2, * q3, * q4;
154 	CvMat * d1, * d2, * d3, * d4;
155 
156 	CvSize size = cvGetSize(src_arr);
157 	CvSize dst_size = cvGetSize(dst_arr);
158 	int cx, cy;
159 
160 	if( dst_size.width != size.width || dst_size.height != size.height )
161 	{
162 		// cvError( CV_StsUnmatchedSizes, "cvShiftDFT", "Source and Destination arrays must have equal sizes", __FILE__, __LINE__ );
163 		return( false );
164 	}
165 
166 	if(src_arr==dst_arr){
167 		tmp = cvCreateMat(size.height/2, size.width/2, cvGetElemType(src_arr));
168 	}
169 
170 	cx = size.width/2;
171 	cy = size.height/2; // image center
172 
173 	q1 = cvGetSubRect( src_arr, &q1stub, cvRect(0,0,cx, cy) );
174 	q2 = cvGetSubRect( src_arr, &q2stub, cvRect(cx,0,cx,cy) );
175 	q3 = cvGetSubRect( src_arr, &q3stub, cvRect(cx,cy,cx,cy) );
176 	q4 = cvGetSubRect( src_arr, &q4stub, cvRect(0,cy,cx,cy) );
177 	d1 = cvGetSubRect( src_arr, &d1stub, cvRect(0,0,cx,cy) );
178 	d2 = cvGetSubRect( src_arr, &d2stub, cvRect(cx,0,cx,cy) );
179 	d3 = cvGetSubRect( src_arr, &d3stub, cvRect(cx,cy,cx,cy) );
180 	d4 = cvGetSubRect( src_arr, &d4stub, cvRect(0,cy,cx,cy) );
181 
182 	if(src_arr!=dst_arr){
183 		if( !CV_ARE_TYPES_EQ( q1, d1 )){
184 		//	cvError( CV_StsUnmatchedFormats, "cvShiftDFT", "Source and Destination arrays must have the same format", __FILE__, __LINE__ );
185 			return( false );
186 		}
187 		cvCopy(q3, d1, 0);
188 		cvCopy(q4, d2, 0);
189 		cvCopy(q1, d3, 0);
190 		cvCopy(q2, d4, 0);
191 	}
192 	else{
193 		cvCopy(q3, tmp, 0);
194 		cvCopy(q1, q3, 0);
195 		cvCopy(tmp, q1, 0);
196 		cvCopy(q4, tmp, 0);
197 		cvCopy(q2, q4, 0);
198 		cvCopy(tmp, q2, 0);
199 	}
200 
201 	return( true );
202 }
203 
204 //---------------------------------------------------------
DFT(IplImage * im,IplImage ** ppReal,IplImage ** ppImag)205 bool DFT(IplImage *im, IplImage **ppReal, IplImage **ppImag)
206 {
207 	IplImage * realInput;
208 	IplImage * imaginaryInput;
209 	IplImage * complexInput;
210 	int dft_M, dft_N;
211 	CvMat* dft_A, tmp;
212 	IplImage * image_Re;
213 	IplImage * image_Im;
214 
215 	if( !im )
216 		return false;
217 
218 	realInput		= cvCreateImage( cvGetSize(im), IPL_DEPTH_64F, 1);
219 	imaginaryInput	= cvCreateImage( cvGetSize(im), IPL_DEPTH_64F, 1);
220 	complexInput	= cvCreateImage( cvGetSize(im), IPL_DEPTH_64F, 2);
221 
222 	cvScale(im, realInput, 1.0, 0.0);
223 	cvZero(imaginaryInput);
224 	cvMerge(realInput, imaginaryInput, NULL, NULL, complexInput);
225 
226 	dft_M = cvGetOptimalDFTSize( im->height - 1 );
227 	dft_N = cvGetOptimalDFTSize( im->width - 1 );
228 
229 	dft_A = cvCreateMat( dft_M, dft_N, CV_64FC2 );
230 	*ppReal	= image_Re = cvCreateImage( cvSize(dft_N, dft_M), IPL_DEPTH_64F, 1);
231 	*ppImag	= image_Im = cvCreateImage( cvSize(dft_N, dft_M), IPL_DEPTH_64F, 1);
232 
233 	// copy A to dft_A and pad dft_A with zeros
234 	cvGetSubRect( dft_A, &tmp, cvRect(0,0, im->width, im->height));
235 	cvCopy( complexInput, &tmp, NULL );
236 	if( dft_A->cols > im->width )
237 	{
238 		cvGetSubRect( dft_A, &tmp, cvRect(im->width,0, dft_A->cols - im->width, im->height));
239 		cvZero( &tmp );
240 	}
241 
242 	// no need to pad bottom part of dft_A with zeros because of
243 	// use nonzero_rows parameter in cvDFT() call below
244 
245 	cvDFT( dft_A, dft_A, CV_DXT_FORWARD, complexInput->height );
246 
247 	// Split Fourier in real and imaginary parts
248 	cvSplit( dft_A, image_Re, image_Im, 0, 0 );
249 
250 	// Compute the magnitude of the spectrum Mag = sqrt(Re^2 + Im^2)
251 //	cvPow( image_Re, image_Re, 2.0);
252 //	cvPow( image_Im, image_Im, 2.0);
253 //	cvAdd( image_Re, image_Im, image_Re, NULL);
254 //	cvPow( image_Re, image_Re, 0.5 );
255 
256 	// Compute log(1 + Mag)
257 //	cvAddS( image_Re, cvScalarAll(1.0), image_Re, NULL ); // 1 + Mag
258 //	cvLog( image_Re, image_Re ); // log(1 + Mag)
259 
260 	// Rearrange the quadrants of Fourier image so that the origin is at
261 	// the image center
262 //	cvShiftDFT( image_Re, image_Re );
263 
264 //	cvMinMaxLoc(image_Re, &m, &M, NULL, NULL, NULL);
265 //	cvScale(image_Re, image_Re, 1.0/(M-m), 1.0*(-m)/(M-m));
266 
267 	return true;
268 }
269 
270 
271 ///////////////////////////////////////////////////////////
272 //														 //
273 //														 //
274 //														 //
275 ///////////////////////////////////////////////////////////
276 
277 //---------------------------------------------------------
278