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
|
/** \file
* \brief Morphology Operations for Binary Images
*
* See Copyright Notice in im_lib.h
* $Id: im_morphology_bin.cpp,v 1.1 2008/10/17 06:16:33 scuri Exp $
*/
#include <im.h>
#include <im_util.h>
#include <im_counter.h>
#include "im_process_loc.h"
#include "im_process_pon.h"
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <string.h>
#include <math.h>
static int DoBinMorphConvolve(imbyte *map, imbyte* new_map, int width, int height, const imImage* kernel, int counter, int hit_value, int miss_value)
{
int *kernel_line;
int offset, new_offset, i, j, x, y;
int kh, kw, kh2, kw2, hit;
kh = kernel->height;
kw = kernel->width;
kh2 = kernel->height/2;
kw2 = kernel->width/2;
int* kernel_data = (int*)kernel->data[0];
for(j = 0; j < height; j++)
{
new_offset = j * width;
for(i = 0; i < width; i++)
{
hit = 1;
for(y = -kh2; y <= kh2 && hit; y++)
{
kernel_line = kernel_data + (y+kh2)*kernel->width;
if ((j + y < 0) || // pass the bottom border
(j + y >= height)) // pass the top border
offset = -1;
else
offset = (j + y) * width;
for(x = -kw2; x <= kw2; x++)
{
if ((offset == -1) ||
(i + x < 0) || // pass the left border
(i + x >= width)) // pass the right border
{
if(kernel_line[x+kw2] != -1 && kernel_line[x+kw2] != 0) // 0 extension beyond borders
hit = 0;
}
else
{
if(kernel_line[x+kw2] != -1 && kernel_line[x+kw2] != map[offset + (i + x)])
hit = 0;
}
}
}
new_map[new_offset + i] = (imbyte)(hit? hit_value: miss_value);
}
if (!imCounterInc(counter))
return 0;
}
return 1;
}
int imProcessBinMorphConvolve(const imImage* src_image, imImage* dst_image, const imImage *kernel, int hit_white, int iter)
{
int j, ret = 0, hit_value, miss_value;
void *tmp = NULL;
int counter;
if (hit_white)
{
hit_value = 1;
miss_value = 0;
}
else
{
hit_value = 0;
miss_value = 1;
}
counter = imCounterBegin("Binary Morphological Convolution");
const char* msg = (const char*)imImageGetAttribute(kernel, "Description", NULL, NULL);
if (!msg) msg = "Processing...";
imCounterTotal(counter, src_image->height*iter, msg);
if (iter > 1)
tmp = malloc(src_image->size);
for (j = 0; j < iter; j++)
{
if (j == 0)
ret = DoBinMorphConvolve((imbyte*)src_image->data[0], (imbyte*)dst_image->data[0], src_image->width, src_image->height, kernel, counter, hit_value, miss_value);
else
{
memcpy(tmp, dst_image->data[0], src_image->size);
ret = DoBinMorphConvolve((imbyte*)tmp, (imbyte*)dst_image->data[0], src_image->width, src_image->height, kernel, counter, hit_value, miss_value);
}
if (!ret)
break;
}
if (tmp) free(tmp);
imCounterEnd(counter);
return ret;
}
int imProcessBinMorphErode(const imImage* src_image, imImage* dst_image, int kernel_size, int iter)
{
imImage* kernel = imImageCreate(kernel_size, kernel_size, IM_GRAY, IM_INT);
imImageSetAttribute(kernel, "Description", IM_BYTE, -1, (void*)"Erode");
int* kernel_data = (int*)kernel->data[0];
for(int i = 0; i < kernel->count; i++)
kernel_data[i] = 1;
int ret = imProcessBinMorphConvolve(src_image, dst_image, kernel, 1, iter);
imImageDestroy(kernel);
return ret;
}
int imProcessBinMorphDilate(const imImage* src_image, imImage* dst_image, int kernel_size, int iter)
{
imImage* kernel = imImageCreate(kernel_size, kernel_size, IM_GRAY, IM_INT);
imImageSetAttribute(kernel, "Description", IM_BYTE, -1, (void*)"Dilate");
// Kernel is all zeros
int ret = imProcessBinMorphConvolve(src_image, dst_image, kernel, 0, iter);
imImageDestroy(kernel);
return ret;
}
int imProcessBinMorphOpen(const imImage* src_image, imImage* dst_image, int kernel_size, int iter)
{
imImage*temp = imImageClone(src_image);
if (!temp)
return 0;
if (!imProcessBinMorphErode(src_image, temp, kernel_size, iter)) {imImageDestroy(temp); return 0;}
if (!imProcessBinMorphDilate(temp, dst_image, kernel_size, iter)) {imImageDestroy(temp); return 0;}
imImageDestroy(temp);
return 1;
}
int imProcessBinMorphClose(const imImage* src_image, imImage* dst_image, int kernel_size, int iter)
{
imImage*temp = imImageClone(src_image);
if (!temp)
return 0;
if (!imProcessBinMorphDilate(src_image, temp, kernel_size, iter)) {imImageDestroy(temp); return 0;}
if (!imProcessBinMorphErode(temp, dst_image, kernel_size, iter)) {imImageDestroy(temp); return 0;}
imImageDestroy(temp);
return 1;
}
int imProcessBinMorphOutline(const imImage* src_image, imImage* dst_image, int kernel_size, int iter)
{
if (!imProcessBinMorphErode(src_image, dst_image, kernel_size, iter)) return 0;
imProcessArithmeticOp(src_image, dst_image, dst_image, IM_BIN_DIFF);
return 1;
}
/* Direction masks: */
/* N S W E */
static int masks[] = { 0200, 0002, 0040, 0010 };
/* True if pixel neighbor map indicates the pixel is 8-simple and */
/* not an end point and thus can be deleted. The neighborhood */
/* map is defined as an integer of bits abcdefghi with a non-zero */
/* bit representing a non-zero pixel. The bit assignment for the */
/* neighborhood is: */
/* */
/* a b c */
/* d e f */
/* g h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 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
};
static void DoThinImage(imbyte *map, int xsize, int ysize)
{
int x, y; /* Pixel location */
int i; /* Pass index */
int pc = 0; /* Pass count */
int count = 1; /* Deleted pixel count */
int p, q; /* Neighborhood maps of adjacent cells */
imbyte *qb; /* Neighborhood maps of previous scanline */
int m; /* Deletion direction mask */
qb = (imbyte *) malloc(xsize);
qb[xsize-1] = 0; /* Used for lower-right pixel */
while ( count )
{
/* Scan src_image while deletions */
pc++;
count = 0;
for ( i = 0 ; i < 4 ; i++ )
{
m = masks[i];
/* Build initial previous scan buffer. */
p = map[0] != 0;
for (x = 0 ; x < xsize-1 ; x++)
{
p = ((p<<1)&0006) | (map[x+1] != 0);
qb[x] = (imbyte)p;
}
/* Scan src_image for pixel deletion candidates. */
for ( y = 0 ; y < ysize-1 ; y++ )
{
q = qb[0];
p = ((q<<3)&0110) | (map[(y+1)*xsize] != 0);
for ( x = 0 ; x < xsize-1 ; x++ )
{
q = qb[x];
p = ((p<<1)&0666) | ((q<<3)&0110) | (map[(y+1)*xsize + x+1] != 0);
qb[x] = (imbyte)p;
if (((p&m) == 0) && isdelete[p] )
{
count++;
map[y*xsize + x] = 0;
}
}
/* Process right edge pixel. */
p = (p<<1)&0666;
if ( (p&m) == 0 && isdelete[p] )
{
count++;
map[y*xsize + xsize-1] = 0;
}
}
/* Process bottom scan line. */
for ( x = 0 ; x < xsize ; x++ )
{
q = qb[x];
p = ((p<<1)&0666) | ((q<<3)&0110);
if ( (p&m) == 0 && isdelete[p] )
{
count++;
map[(ysize-1)*xsize + x] = 0;
}
}
}
}
free (qb);
}
void imProcessBinMorphThin(const imImage* src_image, imImage* dst_image)
{
imImageCopyData(src_image, dst_image);
DoThinImage((imbyte*)dst_image->data[0], dst_image->width, dst_image->height);
}
|