1 
2 ///////////////////////////////////////////////////////////
3 //                                                       //
4 //                         SAGA                          //
5 //                                                       //
6 //      System for Automated Geoscientific Analyses      //
7 //                                                       //
8 //                     Tool Library                      //
9 //                        OpenCV                         //
10 //                                                       //
11 //-------------------------------------------------------//
12 //                                                       //
13 //                 opencv_morphology.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_morphology.h"
50 
51 #include <opencv2/imgproc.hpp>
52 #include <opencv2/imgproc/imgproc_c.h>
53 
54 
55 ///////////////////////////////////////////////////////////
56 //														 //
57 //														 //
58 //														 //
59 ///////////////////////////////////////////////////////////
60 
61 //---------------------------------------------------------
COpenCV_Morphology(void)62 COpenCV_Morphology::COpenCV_Morphology(void)
63 {
64 	Set_Name		(_TL("Morphological Filter (OpenCV)"));
65 
66 	Set_Author		("O.Conrad (c) 2009");
67 
68 	Set_Description	(_TW(
69 		"Morphological Filter."
70 	));
71 
72 	Add_Reference("https://opencv.org/", SG_T("OpenCV - Open Source Computer Vision"));
73 
74 	//-----------------------------------------------------
75 	Parameters.Add_Grid(
76 		"", "INPUT"		, _TL("Input"),
77 		_TL(""),
78 		PARAMETER_INPUT
79 	);
80 
81 	Parameters.Add_Grid(
82 		"", "OUTPUT"	, _TL("Output"),
83 		_TL(""),
84 		PARAMETER_OUTPUT, SG_DATATYPE_Byte
85 	);
86 
87 	Parameters.Add_Choice(
88 		"", "TYPE"		, _TL("Operation"),
89 		_TL(""),
90 		CSG_String::Format("%s|%s|%s|%s|%s|%s|%s",
91 			_TL("dilation"),
92 			_TL("erosion"),
93 			_TL("opening"),
94 			_TL("closing"),
95  			_TL("morpological gradient"),
96 			_TL("top hat"),
97 			_TL("black hat")
98 		)
99 	);
100 
101 	Parameters.Add_Choice(
102 		"", "SHAPE"		, _TL("Element Shape"),
103 		_TL(""),
104 		CSG_String::Format("%s|%s|%s",
105 			_TL("ellipse"),
106 			_TL("rectangle"),
107 			_TL("cross")
108 		)
109 	);
110 
111 	Parameters.Add_Int(
112 		"", "RADIUS"		, _TL("Radius (cells)"),
113 		_TL(""),
114 		1, 0, true
115 	);
116 
117 	Parameters.Add_Int(
118 		"", "ITERATIONS"	, _TL("Iterations"),
119 		_TL(""),
120 		1, 1, true
121 	);
122 }
123 
124 
125 ///////////////////////////////////////////////////////////
126 //														 //
127 ///////////////////////////////////////////////////////////
128 
129 //---------------------------------------------------------
On_Execute(void)130 bool COpenCV_Morphology::On_Execute(void)
131 {
132 	int			Type, Shape, Radius, Iterations;
133 	CSG_Grid	*pInput, *pOutput;
134 
135 	pInput		= Parameters("INPUT"     )->asGrid();
136 	pOutput		= Parameters("OUTPUT"    )->asGrid();
137 	Type		= Parameters("TYPE"      )->asInt();
138 	Shape		= Parameters("SHAPE"     )->asInt();
139 	Radius		= Parameters("RADIUS"    )->asInt();
140 	Iterations	= Parameters("ITERATIONS")->asInt();
141 
142 	//-----------------------------------------------------
143 	switch( Shape )
144 	{
145 	default:
146 	case 0:	Shape	= CV_SHAPE_ELLIPSE;	break;
147 	case 1:	Shape	= CV_SHAPE_RECT   ;	break;
148 	case 2:	Shape	= CV_SHAPE_CROSS  ;	break;
149 	}
150 
151 	//-----------------------------------------------------
152 	IplImage	*cv_pInput	= Get_CVImage(pInput);
153 	IplImage	*cv_pOutput	= Get_CVImage(Get_NX(), Get_NY(), pInput->Get_Type());
154 	IplImage	*cv_pTmp	= NULL;
155 
156 	//-----------------------------------------------------
157 	IplConvKernel	*cv_pElement	= cvCreateStructuringElementEx(Radius * 2 + 1, Radius * 2 + 1, Radius, Radius, Shape, 0);
158 
159 	switch( Type )
160 	{
161 	case 0:	// dilation
162 		cvDilate		(cv_pInput, cv_pOutput, cv_pElement, Iterations);
163 		break;
164 
165 	case 1:	// erosion
166 		cvErode			(cv_pInput, cv_pOutput, cv_pElement, Iterations);
167 		break;
168 
169 	case 2:	// opening
170 		cvMorphologyEx	(cv_pInput, cv_pOutput, cv_pTmp,
171 			cv_pElement, CV_MOP_OPEN    , Iterations
172 		);
173 		break;
174 
175 	case 3:	// closing
176 		cvMorphologyEx	(cv_pInput, cv_pOutput, cv_pTmp,
177 			cv_pElement, CV_MOP_CLOSE   , Iterations
178 		);
179 		break;
180 
181 	case 4:	// morpological gradient
182 		cvMorphologyEx	(cv_pInput, cv_pOutput, cv_pTmp	= Get_CVImage(Get_NX(), Get_NY(), pInput->Get_Type()),
183 			cv_pElement, CV_MOP_GRADIENT, Iterations
184 		);
185 		break;
186 
187 	case 5:	// top hat
188 		cvMorphologyEx	(cv_pInput, cv_pOutput, cv_pTmp	= Get_CVImage(Get_NX(), Get_NY(), pInput->Get_Type()),
189 			cv_pElement, CV_MOP_TOPHAT  , Iterations
190 		);
191 		break;
192 
193 	case 6:	// black hat
194 		cvMorphologyEx	(cv_pInput, cv_pOutput, cv_pTmp	= Get_CVImage(Get_NX(), Get_NY(), pInput->Get_Type()),
195 			cv_pElement, CV_MOP_BLACKHAT, Iterations
196 		);
197 		break;
198 	}
199 
200 	cvReleaseStructuringElement(&cv_pElement);
201 
202 	//-----------------------------------------------------
203 	Copy_CVImage_To_Grid(pOutput, cv_pOutput);
204 
205     cvReleaseImage(&cv_pInput);
206     cvReleaseImage(&cv_pOutput);
207 
208 	if( cv_pTmp )
209 	{
210 		cvReleaseImage(&cv_pTmp);
211 	}
212 
213 	pOutput->Fmt_Name("%s [%s]", pInput->Get_Name(), Get_Name().c_str());
214 
215 	return( true );
216 }
217 
218 
219 ///////////////////////////////////////////////////////////
220 //														 //
221 //														 //
222 //														 //
223 ///////////////////////////////////////////////////////////
224 
225 //---------------------------------------------------------
226