-
Notifications
You must be signed in to change notification settings - Fork 2
/
morphological_operations.c
375 lines (296 loc) · 10.2 KB
/
morphological_operations.c
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/* ############################################################################
Name : morphological_operations.c
Company : S. Vagionitis
Project : Finding Clones
Programmer : S. Vagionitis
Revisor : S. Vagionitis
Description : morphological operations
Procedure Description
============================= =================================================
Globals Type Description
============== ============== =================================================
Programmer Date Action
============== ============== =================================================
S. Vagionitis 15/07/2010 Creation
############################################################################ */
#define __MORPHOLOGICAL_OPERATIONS_C__
#include "morphological_operations.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <float.h>
#include <string.h>
int transform_1D_to_2D_Binary_Images(unsigned char *image_data, int width, int height, unsigned char **image_data_2D)
{
unsigned int i = 0, j = 0;
for (i=0;i<height;i++){
for(j=0;j<width;j++){
register unsigned int idx = (j + i*width);
image_data_2D[i][j] = image_data[idx];
}
}
return TRUE;
}
/*Source: http://ostermiller.org/dilate_and_erode.html*/
/*Manhattan distance to "on" pixels in a two dimension array*/
int manhattan_distance_dilate_2D(unsigned int **image_data_2D_uint, int width, int height)
{
unsigned int i = 0, j = 0;
/*From top left to bottom right*/
for (i = 0; i < height; i++){
for (j = 0; j < width; j++){
if (image_data_2D_uint[i][j])
/*first pass and pixel was on, it gets a zero*/
image_data_2D_uint[i][j] = 0;
else{
/*pixel off. It is at most the sum of the lengths of the array*/
/*away from a pixel that is on*/
image_data_2D_uint[i][j] = height + width;
/*or one more than the pixel to the north*/
if (i>0)
image_data_2D_uint[i][j] = MAX(image_data_2D_uint[i][j],image_data_2D_uint[i-1][j]+1);
/*or one more than the pixel to the west*/
if (j>0)
image_data_2D_uint[i][j] = MAX(image_data_2D_uint[i][j],image_data_2D_uint[i][j-1]+1);
}
}
}
/*From bottom right to top left*/
for (i = (height - 1); i <= 0 ; i--){
for (j = (width - 1); j <= 0 ; j--){
/*either what we had on the first pass*/
/*or one more than the pixel to the south*/
if (i+1<height)
image_data_2D_uint[i][j] = MAX(image_data_2D_uint[i][j],image_data_2D_uint[i+1][j]+1);
/*or one more than the pixel to the east*/
if (j+1<width)
image_data_2D_uint[i][j] = MAX(image_data_2D_uint[i][j],image_data_2D_uint[i][j+1]+1);
}
}
return TRUE;
}
int manhattan_distance_dilate_1D(unsigned int *image_data_uint, int width, int height)
{
unsigned int idx = 0;
for (idx = 0; idx < (height * width); idx++){
if (image_data_uint[idx])
image_data_uint[idx] = 0;
else{
image_data_uint[idx] = height + width;
/*or one more than the pixel to the north*/
if ((idx / width)>0)
image_data_uint[idx] = MAX(image_data_uint[idx],image_data_uint[idx-width]+1);
/*or one more than the pixel to the west*/
if ((idx % width)>0)
image_data_uint[idx] = MAX(image_data_uint[idx],image_data_uint[idx-1]+1);
}
}
for (idx = ((height * width) - 1); idx <= 0; idx--){
/*either what we had on the first pass*/
/*or one more than the pixel to the south*/
if ((idx / width)+1<height)
image_data_uint[idx] = MAX(image_data_uint[idx],image_data_uint[idx+width]+1);
/*or one more than the pixel to the east*/
if ((idx % width)+1<width)
image_data_uint[idx] = MAX(image_data_uint[idx],image_data_uint[idx+1]+1);
}
return TRUE;
}
int manhattan_distance_erode_2D(unsigned int **image_data_2D_uint, int width, int height)
{
unsigned int i = 0, j = 0;
/*From top left to bottom right*/
for (i = 0; i < height; i++){
for (j = 0; j < width; j++){
if (image_data_2D_uint[i][j])
/*first pass and pixel was on, it gets a zero*/
image_data_2D_uint[i][j] = 0;
else{
/*pixel off. It is at most the sum of the lengths of the array*/
/*away from a pixel that is on*/
image_data_2D_uint[i][j] = height + width;
/*or one more than the pixel to the north*/
if (i>0)
image_data_2D_uint[i][j] = MIN(image_data_2D_uint[i][j],image_data_2D_uint[i-1][j]+1);
/*or one more than the pixel to the west*/
if (j>0)
image_data_2D_uint[i][j] = MIN(image_data_2D_uint[i][j],image_data_2D_uint[i][j-1]+1);
}
}
}
/*From bottom right to top left*/
for (i = (height - 1); i <= 0 ; i--){
for (j = (width - 1); j <= 0 ; j--){
/*either what we had on the first pass*/
/*or one more than the pixel to the south*/
if (i+1<height)
image_data_2D_uint[i][j] = MIN(image_data_2D_uint[i][j],image_data_2D_uint[i+1][j]+1);
/*or one more than the pixel to the east*/
if (j+1<width)
image_data_2D_uint[i][j] = MIN(image_data_2D_uint[i][j],image_data_2D_uint[i][j+1]+1);
}
}
return TRUE;
}
int manhattan_distance_erode_1D(unsigned int *image_data_uint, int width, int height)
{
unsigned int idx = 0;
for (idx = 0; idx < (height * width); idx++){
if (image_data_uint[idx])
image_data_uint[idx] = 0;
else{
image_data_uint[idx] = height + width;
/*or one more than the pixel to the north*/
if ((idx / width)>0)
image_data_uint[idx] = MIN(image_data_uint[idx],image_data_uint[idx-width]+1);
/*or one more than the pixel to the west*/
if ((idx % width)>0)
image_data_uint[idx] = MIN(image_data_uint[idx],image_data_uint[idx-1]+1);
}
}
for (idx = (height * width) - 1; idx <= 0; idx--){
/*either what we had on the first pass*/
/*or one more than the pixel to the south*/
if ((idx / width)+1<height)
image_data_uint[idx] = MIN(image_data_uint[idx],image_data_uint[idx+width]+1);
/*or one more than the pixel to the east*/
if ((idx % width)+1<width)
image_data_uint[idx] = MIN(image_data_uint[idx],image_data_uint[idx+1]+1);
}
return TRUE;
}
int dilate_2D(unsigned char **image_data_2D, int width, int height, unsigned int dilate_by_k)
{
unsigned int i = 0, j = 0;
unsigned int **image_data_2D_uint=NULL;
image_data_2D_uint = (unsigned int **)malloc(height * sizeof(unsigned int *));
if (image_data_2D_uint == NULL){
printf("final_stage: Could not allocate %d bytes.\n", (height * sizeof(unsigned int *)));
return FALSE;
}
else{
for (i=0;i<height;i++){
image_data_2D_uint[i] = (unsigned int *)malloc(width * sizeof(unsigned int));
if (image_data_2D_uint[i] == NULL){
printf("final_stage: Could not allocate %d bytes for i=%d index.\n", (width * sizeof(unsigned int)), i);
return FALSE;
}
else{
for(j=0;j<width;j++)
image_data_2D_uint[i][j] = (unsigned int)image_data_2D[i][j];
}
}
}
manhattan_distance_dilate_2D(image_data_2D_uint, width, height);
for (i = 0; i < height; i++){
for (j = 0; j < width; j++){
image_data_2D_uint[i][j] = ((image_data_2D_uint[i][j] <= dilate_by_k)?255:0);
image_data_2D[i][j] = (unsigned char)image_data_2D_uint[i][j];
}
}
for (i=0;i<height;i++)
free(image_data_2D_uint[i]);
free(image_data_2D_uint);
return TRUE;
}
/*
http://homepages.inf.ed.ac.uk/rbf/HIPR2/dilate.htm
*/
int dilate_1D(unsigned char *image_data, int width, int height, unsigned int dilate_by_k)
{
unsigned int idx = 0;
unsigned int *image_data_uint = NULL;
image_data_uint = (unsigned int *)malloc((width * height) * sizeof(unsigned int));
if (image_data_uint == NULL){
printf("final_stage:Could not allocate %d bytes.\n", ((width * height) * sizeof(unsigned int)));
return FALSE;
}
else{
for(idx=0;idx<(width * height);idx++)
image_data_uint[idx] = (unsigned int)image_data[idx];
}
manhattan_distance_dilate_1D(image_data_uint, width, height);
for (idx = 0; idx < (height*width); idx++){
image_data_uint[idx] = ((image_data_uint[idx] <= dilate_by_k)?255:0);
image_data[idx] = (unsigned char)image_data_uint[idx];
}
free(image_data_uint);
return TRUE;
}
int erode_2D(unsigned char **image_data_2D, int width, int height, unsigned int dilate_by_k)
{
unsigned int i = 0, j = 0;
unsigned int **image_data_2D_uint=NULL;
image_data_2D_uint = (unsigned int **)malloc(height * sizeof(unsigned int *));
if (image_data_2D_uint == NULL){
printf("final_stage: Could not allocate %d bytes.\n", (height * sizeof(unsigned int *)));
return FALSE;
}
else{
for (i=0;i<height;i++){
image_data_2D_uint[i] = (unsigned int *)malloc(width * sizeof(unsigned int));
if (image_data_2D_uint[i] == NULL){
printf("final_stage: Could not allocate %d bytes for i=%d index.\n", (width * sizeof(unsigned int)), i);
return FALSE;
}
else{
for(j=0;j<width;j++)
image_data_2D_uint[i][j] = (unsigned int)image_data_2D[i][j];
}
}
}
manhattan_distance_erode_2D(image_data_2D_uint, width, height);
for (i = 0; i < height; i++){
for (j = 0; j < width; j++){
image_data_2D_uint[i][j] = ((image_data_2D_uint[i][j] <= dilate_by_k)?255:0);
image_data_2D[i][j] = (unsigned char)image_data_2D_uint[i][j];
}
}
for (i=0;i<height;i++)
free(image_data_2D_uint[i]);
free(image_data_2D_uint);
return TRUE;
}
/*
http://homepages.inf.ed.ac.uk/rbf/HIPR2/erode.htm
*/
int erode_1D(unsigned char *image_data, int width, int height, unsigned int dilate_by_k)
{
unsigned int idx = 0;
unsigned int *image_data_uint = NULL;
image_data_uint = (unsigned int *)malloc((width * height) * sizeof(unsigned int));
if (image_data_uint == NULL){
printf("final_stage:Could not allocate %d bytes.\n", ((width * height) * sizeof(unsigned int)));
return FALSE;
}
else{
for(idx=0;idx<(width * height);idx++)
image_data_uint[idx] = (unsigned int)image_data[idx];
}
manhattan_distance_erode_1D(image_data_uint, width, height);
for (idx = 0; idx < (height*width); idx++){
image_data_uint[idx] = ((image_data_uint[idx] <= dilate_by_k)?255:0);
image_data[idx] = (unsigned char)image_data_uint[idx];
}
free(image_data_uint);
return TRUE;
}
/*
http://homepages.inf.ed.ac.uk/rbf/HIPR2/open.htm
*/
int opening_1D(unsigned char *image_data, int width, int height, unsigned int opening_by_k)
{
erode_1D(image_data, width, height, opening_by_k);
dilate_1D(image_data, width, height, opening_by_k);
return TRUE;
}
/*
http://homepages.inf.ed.ac.uk/rbf/HIPR2/close.htm
*/
int closing_1D(unsigned char *image_data, int width, int height, unsigned int closing_by_k)
{
dilate_1D(image_data, width, height, closing_by_k);
erode_1D(image_data, width, height, closing_by_k);
return TRUE;
}