1
2 ///////////////////////////////////////////////////////////
3 // //
4 // SAGA //
5 // //
6 // System for Automated Geoscientific Analyses //
7 // //
8 // Tool Library //
9 // OpenCV //
10 // //
11 //-------------------------------------------------------//
12 // //
13 // opencv.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.h"
50
51 #include <opencv2/core/core_c.h>
52
53
54 ///////////////////////////////////////////////////////////
55 // //
56 // //
57 // //
58 ///////////////////////////////////////////////////////////
59
60 //---------------------------------------------------------
Get_CVMatrix_Type(TSG_Data_Type Type)61 int Get_CVMatrix_Type (TSG_Data_Type Type)
62 {
63 switch( Type )
64 {
65 case SG_DATATYPE_Bit :
66 case SG_DATATYPE_Byte : return( CV_8U ); // Unsigned 8-bit integer
67
68 case SG_DATATYPE_Char : return( CV_8S ); // Signed 8-bit integer
69
70 case SG_DATATYPE_Word : return( CV_16U ); // Unsigned 16-bit integer
71
72 case SG_DATATYPE_Short : return( CV_16S ); // Signed 16-bit integer
73
74 case SG_DATATYPE_Color :
75 case SG_DATATYPE_DWord :
76 case SG_DATATYPE_ULong :
77 case SG_DATATYPE_Long :
78 case SG_DATATYPE_Int : return( CV_32S ); // Signed 32-bit integer
79
80 default:
81 case SG_DATATYPE_Float : return( CV_32F ); // Single-precision floating point
82
83 case SG_DATATYPE_Double: return( CV_64F ); // Double-precision floating point
84 }
85 }
86
87
88 ///////////////////////////////////////////////////////////
89 // //
90 ///////////////////////////////////////////////////////////
91
92 //---------------------------------------------------------
Copy_Grid_To_CVMatrix(CSG_Grid * pGrid,cv::Mat & Matrix,bool bCheckSize)93 bool Copy_Grid_To_CVMatrix(CSG_Grid *pGrid, cv::Mat &Matrix, bool bCheckSize)
94 {
95 if( pGrid && (!bCheckSize || (pGrid->Get_NX() == Matrix.cols && pGrid->Get_NY() == Matrix.rows)) )
96 {
97 int nx = pGrid->Get_NX() < Matrix.cols ? pGrid->Get_NX() : Matrix.cols;
98 int ny = pGrid->Get_NY() < Matrix.rows ? pGrid->Get_NY() : Matrix.rows;
99
100 #pragma omp parallel for
101 for(int y=0; y<ny; y++)
102 {
103 uchar *Row = Matrix.row(y).ptr();
104
105 for(int x=0; x<nx; x++)
106 {
107 switch( Matrix.type() )
108 {
109 case CV_8U : ( Row)[x] = pGrid->asByte (x, y); break;
110 case CV_8S : ((char *)Row)[x] = pGrid->asChar (x, y); break;
111 case CV_16U: ((WORD *)Row)[x] = pGrid->asShort (x, y); break;
112 case CV_16S: ((short *)Row)[x] = pGrid->asShort (x, y); break;
113 case CV_32S: ((int *)Row)[x] = pGrid->asInt (x, y); break;
114 case CV_32F: ((float *)Row)[x] = pGrid->asFloat (x, y); break;
115 case CV_64F: ((double *)Row)[x] = pGrid->asDouble(x, y); break;
116 }
117 }
118 }
119
120 return( true );
121 }
122
123 return( false );
124 }
125
126 //---------------------------------------------------------
Copy_CVMatrix_To_Grid(CSG_Grid * pGrid,cv::Mat & Matrix,bool bCheckSize)127 bool Copy_CVMatrix_To_Grid(CSG_Grid *pGrid, cv::Mat &Matrix, bool bCheckSize)
128 {
129 if( pGrid && (!bCheckSize || (pGrid->Get_NX() == Matrix.cols && pGrid->Get_NY() == Matrix.rows)) )
130 {
131 int nx = pGrid->Get_NX() < Matrix.cols ? pGrid->Get_NX() : Matrix.cols;
132 int ny = pGrid->Get_NY() < Matrix.rows ? pGrid->Get_NY() : Matrix.rows;
133
134 #pragma omp parallel for
135 for(int y=0; y<ny; y++)
136 {
137 uchar *Row = Matrix.row(y).ptr();
138
139 for(int x=0; x<nx; x++)
140 {
141 switch( Matrix.type() )
142 {
143 case CV_8U : pGrid->Set_Value(x, y, ( Row)[x]); break;
144 case CV_8S : pGrid->Set_Value(x, y, ((char *)Row)[x]); break;
145 case CV_16U: pGrid->Set_Value(x, y, ((WORD *)Row)[x]); break;
146 case CV_16S: pGrid->Set_Value(x, y, ((short *)Row)[x]); break;
147 case CV_32S: pGrid->Set_Value(x, y, ((int *)Row)[x]); break;
148 case CV_32F: pGrid->Set_Value(x, y, ((float *)Row)[x]); break;
149 case CV_64F: pGrid->Set_Value(x, y, ((double *)Row)[x]); break;
150 }
151 }
152 }
153
154 return( true );
155 }
156
157 return( false );
158 }
159
160
161 ///////////////////////////////////////////////////////////
162 // //
163 ///////////////////////////////////////////////////////////
164
165 //---------------------------------------------------------
Get_CVMatrix(cv::Mat & Matrix,int nx,int ny,TSG_Data_Type Type)166 bool Get_CVMatrix(cv::Mat &Matrix, int nx, int ny, TSG_Data_Type Type)
167 {
168 if( nx > 0 && ny > 0 )
169 {
170 CvSize Size;
171
172 Size.width = nx;
173 Size.height = ny;
174
175 Matrix.create(Size, Get_CVMatrix_Type(Type));
176
177 return( true );
178 }
179
180 return( false );
181 }
182
183 //---------------------------------------------------------
Get_CVMatrix(cv::Mat & Matrix,CSG_Grid * pGrid,TSG_Data_Type Type)184 bool Get_CVMatrix(cv::Mat &Matrix, CSG_Grid *pGrid, TSG_Data_Type Type)
185 {
186 if( pGrid && pGrid->is_Valid() && Get_CVMatrix(Matrix, pGrid->Get_NX(), pGrid->Get_NY(), Type == SG_DATATYPE_Undefined ? pGrid->Get_Type() : Type) )
187 {
188 Copy_Grid_To_CVMatrix(pGrid, Matrix);
189
190 return( true );
191 }
192
193 return( false );
194 }
195
196
197 ///////////////////////////////////////////////////////////
198 // //
199 // //
200 // //
201 ///////////////////////////////////////////////////////////
202
203 //---------------------------------------------------------
Get_CVImage_Type(TSG_Data_Type Type)204 int Get_CVImage_Type (TSG_Data_Type Type)
205 {
206 switch( Type )
207 {
208 case SG_DATATYPE_Bit :
209 case SG_DATATYPE_Byte : return( IPL_DEPTH_8U ); // Unsigned 8-bit integer
210
211 case SG_DATATYPE_Char : return( IPL_DEPTH_8S ); // Signed 8-bit integer
212
213 case SG_DATATYPE_Word : return( IPL_DEPTH_16U ); // Unsigned 16-bit integer
214
215 case SG_DATATYPE_Short : return( IPL_DEPTH_16S ); // Signed 16-bit integer
216
217 case SG_DATATYPE_Color :
218 case SG_DATATYPE_DWord :
219 case SG_DATATYPE_ULong :
220 case SG_DATATYPE_Long :
221 case SG_DATATYPE_Int : return( IPL_DEPTH_32S ); // Signed 32-bit integer
222
223 default:
224 case SG_DATATYPE_Float : return( IPL_DEPTH_32F ); // Single-precision floating point
225
226 case SG_DATATYPE_Double: return( IPL_DEPTH_64F ); // Double-precision floating point
227 }
228 }
229
230
231 ///////////////////////////////////////////////////////////
232 // //
233 ///////////////////////////////////////////////////////////
234
235 //---------------------------------------------------------
Copy_Grid_To_CVImage(CSG_Grid * pGrid,IplImage * pImage,bool bCheckSize)236 bool Copy_Grid_To_CVImage(CSG_Grid *pGrid, IplImage *pImage, bool bCheckSize)
237 {
238 if( pImage && pGrid && (!bCheckSize || (pGrid->Get_NX() == pImage->width && pGrid->Get_NY() == pImage->height)) )
239 {
240 int nx = pGrid->Get_NX() < pImage->width ? pGrid->Get_NX() : pImage->width;
241 int ny = pGrid->Get_NY() < pImage->height ? pGrid->Get_NY() : pImage->height;
242
243 #pragma omp parallel for
244 for(int y=0; y<ny; y++)
245 {
246 CvMat Row;
247
248 cvGetRow(pImage, &Row, y);
249
250 for(int x=0; x<nx; x++)
251 {
252 switch( (unsigned int)(pImage->depth) )
253 {
254 case IPL_DEPTH_8U: Row.data.ptr[x] = pGrid->asByte (x, y); break;
255 case IPL_DEPTH_8S: Row.data.ptr[x] = pGrid->asChar (x, y); break;
256 case IPL_DEPTH_16U: Row.data.s [x] = pGrid->asShort (x, y); break;
257 case IPL_DEPTH_16S: Row.data.s [x] = pGrid->asShort (x, y); break;
258 case IPL_DEPTH_32S: Row.data.i [x] = pGrid->asInt (x, y); break;
259 case IPL_DEPTH_32F: Row.data.fl [x] = pGrid->asFloat (x, y); break;
260 case IPL_DEPTH_64F: Row.data.db [x] = pGrid->asDouble(x, y); break;
261 }
262 }
263 }
264
265 return( true );
266 }
267
268 return( false );
269 }
270
271 //---------------------------------------------------------
Copy_CVImage_To_Grid(CSG_Grid * pGrid,IplImage * pImage,bool bCheckSize)272 bool Copy_CVImage_To_Grid(CSG_Grid *pGrid, IplImage *pImage, bool bCheckSize)
273 {
274 if( pImage && pGrid && (!bCheckSize || (pGrid->Get_NX() == pImage->width && pGrid->Get_NY() == pImage->height)) )
275 {
276 int nx = pGrid->Get_NX() < pImage->width ? pGrid->Get_NX() : pImage->width;
277 int ny = pGrid->Get_NY() < pImage->height ? pGrid->Get_NY() : pImage->height;
278
279 #pragma omp parallel for
280 for(int y=0; y<ny; y++)
281 {
282 CvMat Row;
283
284 cvGetRow(pImage, &Row, y);
285
286 for(int x=0; x<nx; x++)
287 {
288 switch( (unsigned int)(pImage->depth) )
289 {
290 case IPL_DEPTH_8U : pGrid->Set_Value(x, y, Row.data.ptr[x]); break;
291 case IPL_DEPTH_8S : pGrid->Set_Value(x, y, ((char *)Row.data.ptr)[x]); break;
292 case IPL_DEPTH_16U: pGrid->Set_Value(x, y, ((WORD *)Row.data.s )[x]); break;
293 case IPL_DEPTH_16S: pGrid->Set_Value(x, y, Row.data.s [x]); break;
294 case IPL_DEPTH_32S: pGrid->Set_Value(x, y, Row.data.i [x]); break;
295 case IPL_DEPTH_32F: pGrid->Set_Value(x, y, Row.data.fl [x]); break;
296 case IPL_DEPTH_64F: pGrid->Set_Value(x, y, Row.data.db [x]); break;
297 }
298 }
299 }
300
301 return( true );
302 }
303
304 return( false );
305 }
306
307
308 ///////////////////////////////////////////////////////////
309 // //
310 ///////////////////////////////////////////////////////////
311
312 //---------------------------------------------------------
Get_CVImage(int nx,int ny,TSG_Data_Type Type)313 IplImage * Get_CVImage(int nx, int ny, TSG_Data_Type Type)
314 {
315 if( nx > 0 && ny > 0 )
316 {
317 CvSize Size;
318
319 Size.width = nx;
320 Size.height = ny;
321
322 return( cvCreateImage(Size, Get_CVImage_Type(Type), 1) );
323 }
324
325 return( NULL );
326 }
327
328 //---------------------------------------------------------
Get_CVImage(CSG_Grid * pGrid,TSG_Data_Type Type)329 IplImage * Get_CVImage(CSG_Grid *pGrid, TSG_Data_Type Type)
330 {
331 IplImage *pImage = NULL;
332
333 if( pGrid && pGrid->is_Valid() && (pImage = Get_CVImage(pGrid->Get_NX(), pGrid->Get_NY(), Type == SG_DATATYPE_Undefined ? pGrid->Get_Type() : Type)) != NULL )
334 {
335 Copy_Grid_To_CVImage(pGrid, pImage);
336 }
337
338 return( pImage );
339 }
340
341
342 ///////////////////////////////////////////////////////////
343 // //
344 // //
345 // //
346 ///////////////////////////////////////////////////////////
347
348 //---------------------------------------------------------
349