-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMorphology.cpp
319 lines (260 loc) · 9.14 KB
/
Morphology.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include "Morphology.h"
/*----------------------------------------------MORPHOLOGICAL OPERATIONS----------------------------------------------------------------------*/
int erosion_elem = 0;
int erosion_size = 0;
int dilation_elem = 0;
int dilation_size = 0;
int morph_size = 0;
int morph_elem = 0;
int const max_elem = 2;
int const max_kernel_size = 21;
/*-------------------------------------------------------EROTION WITH 4 NEIGHBOURHOOD-------------------------------------------------------------*/
/**
* @function erosion
* @param input an image(binary) in Mat format
* @brief it erode an image with a square mask of 3x3
* @return Return eroded image of Mat data-type
*/
Mat IITkgp_functions::erosion(Mat image)
{
int row = image.rows;
int col = image.cols;
int i,j;
Mat tempimage;
image.copyTo(tempimage);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
tempimage.data[i*col+j] = 255;
}
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(image.data[i*col+j]==0)
{
if(i-1<0||i+1>=row||j-1<0||j+1>=col)
tempimage.data[i*col+j]=255;
else if(image.data[(i-1)*col+j]==0&&image.data[(i+1)*col+j]==0&&image.data[i*col+(j-1)]==0&&image.data[i*col+(j+1)]==0)
tempimage.data[i*col+j]=0;
else
tempimage.data[i*col+j]=255;
}
else
tempimage.data[i*col+j]=255;
}
}
return (tempimage);
}
/*-------------------------------------------------------------------------------------------------------------------------*/
/*-------------------------------------------------------------- BOUNDARY EXTRACTION--------------------------------------------------*/
/**
* @function boundaryextraction
* @param input an image(binary) in Mat format
* @brief it find the boundary of the input image
* @return Return boundary of input image(binary in nature)
*/
Mat IITkgp_functions::boundaryextraction(Mat image)
{
int i,j;
Mat erodedimage;
Mat extractedimage;
image.copyTo(erodedimage);
image.copyTo(extractedimage);
int row,col;
row = image.rows;
col = image.cols;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
erodedimage.data[i*col+j] = 255;
extractedimage.data[i*col+j] = 255;
}
}
erodedimage=erosion(image);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(image.data[i*col+j]==erodedimage.data[i*col+j])
extractedimage.data[i*col+j]=255;
else
extractedimage.data[i*col+j]=0;
}
}
return(extractedimage);
}
/** @function Erosion
* @param input
* element type
* 0: kernel = Rectangle
* 1: kernel = CROSS
* 2: kernel = ELLIPSE
* @param input erosion Size(n) : Create a kernel or window of 2n+1
* @param input an image in Mat format(image).
* @brief it find Eroded Image of the input image with given kernel type and size
* @return Return Eroded image of input image
*/
Mat IITkgp_functions::Erosion( int erosion_elem, int erosion_size, Mat image)
{
int erosion_type;
if( erosion_elem == 0 ){ erosion_type = MORPH_RECT; }
else if( erosion_elem == 1 ){ erosion_type = MORPH_CROSS; }
else if( erosion_elem == 2) { erosion_type = MORPH_ELLIPSE; }
Mat element = getStructuringElement( erosion_type,
Size( 2*erosion_size + 1, 2*erosion_size+1 ),
Point( erosion_size, erosion_size ) );
Mat ErodedImage;
/// Apply the erosion operation
erode( image, ErodedImage, element );
return(ErodedImage);
}
/** @function Dilation
* @param input
* element type
* 0: kernel = Rectangle
* 1: kernel = CROSS
* 2: kernel = ELLIPSE
* @param input Dilation Size(n) : Create a kernel or window of 2n+1
* @param input an image in Mat format(image).
* @brief it find Dilated Image of the input image with given kernel type and size
* @return Return Dilateded image of input image
*/
Mat IITkgp_functions::Dilation( int dilation_elem, int dilation_size, Mat image )
{
Mat DilatedImage;
int dilation_type;
if( dilation_elem == 0 ){ dilation_type = MORPH_RECT; }
else if( dilation_elem == 1 ){ dilation_type = MORPH_CROSS; }
else if( dilation_elem == 2) { dilation_type = MORPH_ELLIPSE; }
Mat element = getStructuringElement( dilation_type,
Size( 2*dilation_size + 1, 2*dilation_size+1 ),
Point( dilation_size, dilation_size ) );
/// Apply the dilation operation
dilate( image, DilatedImage, element );
return(DilatedImage);
}
/** @function Open
* @param input
* element type
* 0: kernel = Rectangle
* 1: kernel = CROSS
* 2: kernel = ELLIPSE
* @param input element Size(n) : Create a kernel or window of 2n+1
* @param input an image in Mat format(image).
* @brief it find Open Image of the input image with given kernel type and size
* @return Return Open image of input image
*/
Mat IITkgp_functions::Open(int open_elem, int open_size, Mat image)
{
Mat OpenImage;
int open_type;
if( open_elem == 0 ){ open_type = MORPH_RECT; }
else if( open_elem == 1 ){ open_type = MORPH_CROSS; }
else if( open_elem == 2) { open_type = MORPH_ELLIPSE; }
Mat element = getStructuringElement( open_type,
Size( 2*open_size + 1, 2*open_size+1 ),
Point( open_size, open_size ) );
Mat ErodedImage;
erode(image, ErodedImage, element);
dilate(ErodedImage, OpenImage, element);
//ErodedImage = Erosion(open_elem,open_size, image);
//OpenImage = Dilation(open_elem,open_size, ErodedImage);
return(OpenImage);
}
/** @function Close
* @param input
* element type
* 0: kernel = Rectangle
* 1: kernel = CROSS
* 2: kernel = ELLIPSE
* @param input element Size(n) : Create a kernel or window of 2n+1
* @param input an image in Mat format(image).
* @brief it find Close Image of the input image with given kernel type and size
* @return Return Close image of input image(binary in nature)
*/
Mat IITkgp_functions::Close(int close_elem, int close_size, Mat image)
{
Mat CloseImage;
int close_type;
if( close_elem == 0 ){ close_type = MORPH_RECT; }
else if( close_elem == 1 ){ close_type = MORPH_CROSS; }
else if( close_elem == 2) { close_type = MORPH_ELLIPSE; }
Mat element = getStructuringElement( close_type,
Size( 2*close_size + 1, 2*close_size+1 ),
Point( close_size, close_size ) );
Mat DilatedImage;
dilate( image, DilatedImage, element );
erode(DilatedImage, CloseImage, element);
// DilatedImage = Dilation(close_elem, close_size, image);
// CloseImage = Erosion(close_elem, close_size, DilatedImage);
return(CloseImage);
}
/*-------------------------------------------------------------------------------------------------------------------------*/
int Morphology_type;
Mat TempMorphImage,TempTargetImage;
void MorphOperation(int, void*)
{
int morph_type;
if( morph_elem == 0 ){ morph_type = MORPH_RECT; }
else if( morph_elem == 1 ){ morph_type = MORPH_CROSS; }
else if( morph_elem == 2) { morph_type = MORPH_ELLIPSE; }
Mat element = getStructuringElement( morph_type,
Size( 2*morph_size + 1, 2*morph_size+1 ),
Point( morph_size, morph_size ) );
if(Morphology_type == 0) // erosion
{
erode( TempTargetImage, TempMorphImage, element );
}
else if(Morphology_type == 1) // dilation
{
dilate(TempTargetImage, TempMorphImage, element);
}
else if(Morphology_type == 2) //open
{
Mat ErodedImage;
erode( TempTargetImage, ErodedImage, element );
dilate(ErodedImage, TempMorphImage, element);
ErodedImage.release();
}
else if(Morphology_type == 3) //close
{
Mat DilatedImage;
dilate( TempTargetImage, DilatedImage, element );
erode(DilatedImage, TempMorphImage, element);
DilatedImage.release();
}
imshow("Morphological Operartion",TempMorphImage);
}
/** @function Morphology_Gui
* @param input
* Mat image
* int type
* 0: Erosion
* 1: Dilation
* 2: Open
* 3: Close
*
* @return Return Morphologycally processed image
*/
Mat IITkgp_functions::Morphology_Gui(Mat image, int type)
{
Mat ProcessedImage;
Morphology_type = type;
image.copyTo(TempTargetImage);
namedWindow("Morphological Operartion",CV_WINDOW_KEEPRATIO);
createTrackbar( "Element:\n 0: Rect \n 1: Cross \n 2: Ellipse", "Morphological Operartion",
&morph_elem, max_elem,
MorphOperation );
createTrackbar( "Kernel size:\n 2n +1", "Morphological Operartion",
&morph_size, max_kernel_size,
MorphOperation );
MorphOperation(0,0);
waitKey(0);
TempTargetImage.release();
image.release();
TempMorphImage.copyTo(ProcessedImage);
TempMorphImage.release();
return(ProcessedImage);
}