/
binmorph.c
366 lines (338 loc) · 9.99 KB
/
binmorph.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
/*
* binmorph.c - functions for morphological operations on binary image
*
* Copyright 2015 Edward V. Emelianoff <eddy@sao.ru>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <err.h>
#include <sys/time.h>
#include "binmorph.h"
#include "types.h"
#include "usefull_macros.h"
#include "linfilter.h"
// global arrays for erosion/dilation masks
static uint8_t *ER = NULL, *DIL = NULL;
static bool __Init_done = false; // == true if arrays are inited
/*
* =================== AUXILIARY FUNCTIONS ===================>
*/
/**
* This function inits masks arrays for erosion and dilation
* You may call it yourself or it will be called when one of
* `erosion` or `dilation` functions will be ran first time
*/
static void morph_init(){
if(__Init_done) return;
int i;
ER = MALLOC(uint8_t, 256);
DIL = MALLOC(uint8_t, 256);
for(i = 0; i < 256; i++){
ER[i] = i & ((i << 1) | 1) & ((i >> 1) | (0x80)); // don't forget that << and >> set borders to zero
DIL[i] = i | (i << 1) | (i >> 1);
}
__Init_done = true;
}
/*
* <=================== AUXILIARY FUNCTIONS ===================
*/
/*
* =================== CONVERT IMAGE TYPES ===================>
*/
/**
* Convert boolean image into pseudo-packed (1 char == 8 pixels)
* @param im (i) - image to convert
* @param W, H - size of image im (must be larger than 1)
* @param W_0 (o) - (stride) new width of image
* @return allocated memory area with "packed" image
*/
uint8_t *u16tochar(uint16_t *im, int W, int H, int *W_0){
if(W < 2 || H < 2) ERRX("image size too small");
int y, W0 = (W + 7) / 8;
uint8_t *ret = MALLOC(uint8_t, W0 * H);
OMP_FOR()
for(y = 0; y < H; y++){
int x, i, X;
uint16_t *ptr = &im[y*W];
uint8_t *rptr = &ret[y*W0];
for(x = 0, X = 0; x < W0; x++, rptr++){
for(i = 7; i > -1 && X < W; i--, X++, ptr++){
*rptr |= *ptr << i;
}
}
}
if(W_0) *W_0 = W0;
return ret;
}
/**
* Convert "packed" image into boolean
* @param image (i) - input image
* @param W, H, W_0 - size of image and width of "packed" image
* @return allocated memory area with "unpacked" image
*
bool *char2bool(uint8_t *image, int W, int H, int W_0){
int y;
bool *ret = MALLOC(bool, W * H);
OMP_FOR()
for(y = 0; y < H; y++){
int x, X, i;
bool *optr = &ret[y*W];
uint8_t *iptr = &image[y*W_0];
for(x = 0, X = 0; x < W_0; x++, iptr++)
for(i = 7; i > -1 && X < W; i--, X++, optr++){
*optr = (*iptr >> i) & 1;
}
}
return ret;
}*/
/**
* Convert "packed" image into size_t array for conncomp procedure
* @param image (i) - input image
* @param W, H, W_0 - size of image and width of "packed" image
* @return allocated memory area with copy of an image
*/
uint16_t *chartou16(uint8_t *image, int W, int H, int W_0){
int y;
uint16_t *ret = MALLOC(uint16_t, W * H);
OMP_FOR()
for(y = 0; y < H; y++){
int x, X, i;
uint16_t *optr = &ret[y*W];
uint8_t *iptr = &image[y*W_0];
for(x = 0, X = 0; x < W_0; x++, iptr++)
for(i = 7; i > -1 && X < W; i--, X++, optr++){
*optr = (*iptr >> i) & 1;
}
}
return ret;
}
/*
* <=================== CONVERT IMAGE TYPES ===================
*/
/*
* =================== MORPHOLOGICAL OPERATIONS ===================>
*/
/**
* Remove all non-4-connected pixels
* @param image (i) - input image
* @param W, H - size of image
* @return allocated memory area with converted input image
*/
uint8_t *FC_filter(uint8_t *image, int W, int H){
if(W < 1 || H < 2) errx(1, "4-connect: image size too small");
uint8_t *ret = MALLOC(uint8_t, W*H);
int y = 0, w = W-1, h = H-1;
// top of image, y = 0
#define IM_UP
#include "fc_filter.h"
#undef IM_UP
// mid of image, y = 1..h-1
#include "fc_filter.h"
// image bottom, y = h
y = h;
#define IM_DOWN
#include "fc_filter.h"
#undef IM_DOWN
return ret;
}
/**
* Make morphological operation of dilation
* @param image (i) - input image
* @param W, H - size of image
* @return allocated memory area with dilation of input image
*/
uint8_t *dilation(uint8_t *image, int W, int H){
if(W < 2 || H < 2) errx(1, "Dilation: image size too small");
if(!__Init_done) morph_init();
uint8_t *ret = MALLOC(uint8_t, W*H);
int y = 0, w = W-1, h = H-1;
// top of image, y = 0
#define IM_UP
#include "dilation.h"
#undef IM_UP
// mid of image, y = 1..h-1
#include "dilation.h"
// image bottom, y = h
y = h;
#define IM_DOWN
#include "dilation.h"
#undef IM_DOWN
return ret;
}
/**
* Make morphological operation of erosion
* @param image (i) - input image
* @param W, H - size of image
* @return allocated memory area with erosion of input image
*/
uint8_t *erosion(uint8_t *image, int W, int H){
if(W < 2 || H < 2) errx(1, "Erosion: image size too small");
if(!__Init_done) morph_init();
uint8_t *ret = MALLOC(uint8_t, W*H);
int y, w = W-1, h = H-1;
OMP_FOR()
for(y = 1; y < h; y++){ // reset first & last rows of image
uint8_t *iptr = &image[W*y];
uint8_t *optr = &ret[W*y];
uint8_t p = ER[*iptr] & 0x7f & iptr[-W] & iptr[W];
int x;
if(!(iptr[1] & 0x80)) p &= 0xfe;
*optr++ = p;
iptr++;
for(x = 1; x < w; x++, iptr++, optr++){
p = ER[*iptr] & iptr[-W] & iptr[W];
if(!(iptr[-1] & 1)) p &= 0x7f;
if(!(iptr[1] & 0x80)) p &= 0xfe;
*optr = p;
}
p = ER[*iptr] & 0xfe & iptr[-W] & iptr[W];
if(!(iptr[-1] & 1)) p &= 0x7f;
*optr++ = p;
iptr++;
}
return ret;
}
/*
* <=================== MORPHOLOGICAL OPERATIONS ===================
*/
/*
* =================== LOGICAL OPERATIONS ===================>
*/
/**
* Logical AND of two images
* @param im1, im2 (i) - two images
* @param W, H - their size (of course, equal for both images)
* @return allocated memory area with image = (im1 AND im2)
*/
uint8_t *imand(uint8_t *im1, uint8_t *im2, int W, int H){
uint8_t *ret = MALLOC(uint8_t, W*H);
int y;
OMP_FOR()
for(y = 0; y < H; y++){
int x, S = y*W;
uint8_t *rptr = &ret[S], *p1 = &im1[S], *p2 = &im2[S];
for(x = 0; x < W; x++)
*rptr++ = *p1++ & *p2++;
}
return ret;
}
/**
* Substitute image 2 from image 1: reset to zero all bits of image 1 which set to 1 on image 2
* @param im1, im2 (i) - two images
* @param W, H - their size (of course, equal for both images)
* @return allocated memory area with image = (im1 AND (!im2))
*/
uint8_t *substim(uint8_t *im1, uint8_t *im2, int W, int H){
uint8_t *ret = MALLOC(uint8_t, W*H);
int y;
OMP_FOR()
for(y = 0; y < H; y++){
int x, S = y*W;
uint8_t *rptr = &ret[S], *p1 = &im1[S], *p2 = &im2[S];
for(x = 0; x < W; x++)
*rptr++ = *p1++ & (~*p2++);
}
return ret;
}
/*
* <=================== LOGICAL OPERATIONS ===================
*/
/*
* =================== CONNECTED COMPONENTS LABELING ===================>
*/
/**
* label 4-connected components on image
* (slow algorythm, but easy to parallel)
*
* @param I (i) - image ("packed")
* @param W,H,W_0 - size of the image (W - width in pixels, W_0 - width in octets)
* @param Nobj (o) - number of objects found
* @return an array of labeled components
*/
uint16_t *_cclabel4(uint8_t *Img, int W, int H, int W_0, size_t *Nobj){
uint8_t *I = FC_filter(Img, W_0, H);
uint16_t *labels = chartou16(I, W, H, W_0);
FREE(I);
#include "cclabling.h"
return labels;
}
// label 8-connected components, look cclabel4
uint16_t *_cclabel8(uint16_t *labels, int W, int H, size_t *Nobj){
#define LABEL_8
#include "cclabling.h"
#undef LABEL_8
return labels;
}
/**
* Make connection-component labeling
* output image vould have uint16_t data
* @param img (i) - input image
* @param threshold (i) - threshold level in value of dynamic range (0,1)
* @param Nobj (o) - amount of object found (or NULL if not needed)
*/
IMAGE *cclabel4(IMAGE *img, double threshold, size_t *Nobj){
/*if(N != 4 || N != 8){
/// Могу работать лишь для четырех- и восьмисвязных областей
ERRX(_("Can work only for 4- or 8-connected components"));
}*/
double thrval;
uint16_t *binary = binarize(img, threshold, &thrval);
if(!binary) return NULL;
int W_0;
uint8_t *Ima = u16tochar(binary, img->width, img->height, &W_0);
FREE(binary);
size_t N;
uint16_t *dat = _cclabel4(Ima, img->width, img->height, W_0, &N);
if(Nobj) *Nobj = N;
FREE(Ima);
IMAGE *ret = buildFITSfromdat(img->height, img->width, SHORT_IMG, (uint8_t*)dat);
FREE(dat);
char buf[80];
snprintf(buf, 80, "COMMENT found %zd 4-connected components, threshold value %g",
N, (double)thrval);
list_add_record(&ret->keylist, buf);
snprintf(buf, 80, "COMMENT (%g%% fromdata range%s)", fabs(threshold)*100.,
(threshold < 0.) ? ", inverted" : "");
list_add_record(&ret->keylist, buf);
return ret;
}
IMAGE *cclabel8(IMAGE *img, double threshold, size_t *Nobj){
double thrval;
uint16_t *binary = binarize(img, threshold, &thrval);
if(!binary) return NULL;
size_t N;
_cclabel8(binary, img->width, img->height, &N);
if(Nobj) *Nobj = N;
IMAGE *ret = buildFITSfromdat(img->height, img->width, SHORT_IMG, (uint8_t*)binary);
FREE(binary);
char buf[80];
snprintf(buf, 80, "COMMENT found %zd 4-connected components, threshold value %g",
N, (double)thrval);
list_add_record(&ret->keylist, buf);
snprintf(buf, 80, "COMMENT (%g%% fromdata range%s)", fabs(threshold)*100.,
(threshold < 0.) ? ", inverted" : "");
list_add_record(&ret->keylist, buf);
return ret;
}
/*
* <=================== CONNECTED COMPONENTS LABELING ===================
*/
/*
* <=================== template ===================>
*/