-
Notifications
You must be signed in to change notification settings - Fork 2
/
morphological_features.c
382 lines (309 loc) · 11.3 KB
/
morphological_features.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
375
376
377
378
379
380
381
382
/* ############################################################################
Name : morphological_features.c
Company : S. Vagionitis
Project : Finding Clones
Programmer : S. Vagionitis
Revisor : S. Vagionitis
Description : morphological features
Procedure Description
============================= =================================================
Globals Type Description
============== ============== =================================================
Programmer Date Action
============== ============== =================================================
S. Vagionitis 08/07/2010 Creation
############################################################################ */
#define __MORPHOLOGICAL_FEATURES_C__
#include "morphological_features.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <float.h>
#include <string.h>
/* ############################################################################
Name : morphological_feature_circularity_index
Description : Calculate the circularity index which is a morphological
feature. Values range from 1, for a perfect circle, to 0
for a line. It is useful to give an impression of elongation
as well as roughness of the object's shape. Other significant
characteristics of this feature are that it is invariant to
scale, translation and rotation.
Arguments Type Description
============== ================ ===============================================
area(IN) int * Area covered by the objects.
perim(IN) double * Perimeter of the objects.
n_object(IN) int Number of objects from mask.
circ(OUT) double * Circularity index of objects.
Return Values Description
=============================== ===============================================
TRUE If everything is fine.
Globals Type Description
============== ================ ===============================================
Locals Type Description
============== ================ ===============================================
i unsigned in General purpose index.
############################################################################ */
int morphological_feature_circularity_index(int *area, double *perim, int n_object, double *circ)
{
unsigned int i = 0;
#if ALLOW_PRINTF == TRUE
printf("Circularity index:\n");
#endif
/* calcuate cirularity index */
for (i = 0; i < n_object; i++){
circ[i] = 4.0*PI * area[i] / (perim[i]*perim[i]);
#if ALLOW_PRINTF == TRUE
printf("%d %.3f\n", i, circ[i]);
#endif
}
return TRUE;
}
/*moment of order (p+q)*/
int morphological_feature_object_moment(double p, double q, int **obj_id, int width, int height, int n_object, unsigned char *image, double *moment)
{
unsigned int i = 0, j = 0;
double *sum = NULL;
sum = (double *)malloc(n_object * sizeof(double));
if (sum == NULL){
printf("morphological_feature_object_moment:Cannot allocate %d bytes for memory.\n", (n_object * sizeof(double)));
exit(-1);
}
memset(sum, 0, sizeof(n_object * sizeof(double)));
for (i = 0; i < height; i++) {
double a = pow(i, p);
for (j = 0; j < width; j++) {
double b = pow(j, q);
if (!obj_id[i][j])
continue;
register unsigned int idx = (j + i*width)*3;
unsigned char val = GREYSCALE(image[idx], image[idx+1], image[idx+2]);
moment[(obj_id[i][j] - 1)] += a * b * (double)val;
sum[(obj_id[i][j] - 1)] += (double)val;
}
}
#if ALLOW_PRINTF == TRUE
printf("Moment(%.0f, %.0f) :\n", p, q);
for (i = 0; i < n_object; i++){
printf("%d %.3f\n", i, moment[i]);
}
#endif
free(sum);
return TRUE;
}
int morphological_feature_calculate_centroids(int **obj_id, int width, int height, int n_object, unsigned char *image, double **centroids)
{
unsigned int i = 0;
double *moment00 = NULL;
moment00 = (double *)malloc(n_object * sizeof(double));
if (moment00 == NULL){
printf("Cannot allocate %d bytes for memory.\n", (n_object * sizeof(double)));
exit(-1);
}
memset(moment00, 0, (n_object * sizeof(double)));
double *moment10 = NULL;
moment10 = (double *)malloc(n_object * sizeof(double));
if (moment10 == NULL){
printf("Cannot allocate %d bytes for memory.\n", (n_object * sizeof(double)));
exit(-1);
}
memset(moment10, 0, (n_object * sizeof(double)));
double *moment01 = NULL;
moment01 = (double *)malloc(n_object * sizeof(double));
if (moment01 == NULL){
printf("Cannot allocate %d bytes for memory.\n", (n_object * sizeof(double)));
exit(-1);
}
memset(moment01, 0, (n_object * sizeof(double)));
morphological_feature_object_moment(0.0, 0.0, obj_id, width, height, n_object, image, moment00);
morphological_feature_object_moment(1.0, 0.0, obj_id, width, height, n_object, image, moment10);
morphological_feature_object_moment(0.0, 1.0, obj_id, width, height, n_object, image, moment01);
#if ALLOW_PRINTF == TRUE
printf("Centroids :\n");
#endif
for (i = 0; i < n_object; i++){
centroids[i][0] = moment10[i] / moment00[i];
centroids[i][1] = moment01[i] / moment00[i];
#if ALLOW_PRINTF == TRUE
printf("%d %.3f %.3f\n", i, centroids[i][0], centroids[i][1]);
#endif
}
free(moment00);
free(moment10);
free(moment01);
return TRUE;
}
int morphological_feature_central_moments(double p, double q, int **obj_id, int width, int height, int n_object, unsigned char *image, double *central_moment)
{
unsigned int i = 0, j = 0;
double **centroid = NULL;
centroid = (double **)malloc(n_object*sizeof(double *));
if (centroid == NULL){
printf("Could not allocate %d bytes.\n", n_object*sizeof(double *));
exit(0);
}
else{
for (i = 0; i < n_object; i++){
centroid[i] = (double *)malloc(2*sizeof(double));
if (centroid[i] == NULL){
printf("Could not allocate %d bytes for i=%d index.\n", 2*sizeof(double), i);
exit(0);
}
else{
centroid[i][0] = 0.0;
centroid[i][1] = 0.0;
}
}
}
morphological_feature_calculate_centroids(obj_id, width, height, n_object, image, centroid);
for (i = 0; i < height; i++) {
for (j = 0; j < width; j++) {
if (!obj_id[i][j])
continue;
double a = pow((i - centroid[(obj_id[i][j] - 1)][0]), p);
double b = pow((j - centroid[(obj_id[i][j] - 1)][1]), q);
register unsigned int idx = (j + i*width)*3;
unsigned char val = GREYSCALE(image[idx], image[idx+1], image[idx+2]);
central_moment[(obj_id[i][j] - 1)] += a * b * (double)val;
}
}
#if ALLOW_PRINTF == TRUE
printf("Central moment(%.0f, %.0f) :\n", p, q);
for (i = 0; i < n_object; i++){
printf("%d %.3f\n", i, central_moment[i]);
}
#endif
for (i = 0; i < n_object; i++)
free(centroid[i]);
free(centroid);
return TRUE;
}
int morphological_feature_object_orientation(int **obj_id, int width, int height, int n_object, unsigned char *image, double *object_orientation)
{
unsigned int i = 0;
double *mi00 = NULL;
mi00 = (double *)malloc(n_object * sizeof(double));
if (mi00 == NULL){
printf("Cannot allocate %d bytes for memory.\n", (n_object * sizeof(double)));
exit(-1);
}
memset(mi00, 0, (n_object * sizeof(double)));
double *mi11 = NULL;
mi11 = (double *)malloc(n_object * sizeof(double));
if (mi11 == NULL){
printf("Cannot allocate %d bytes for memory.\n", (n_object * sizeof(double)));
exit(-1);
}
memset(mi11, 0, (n_object * sizeof(double)));
double *mi20 = NULL;
mi20 = (double *)malloc(n_object * sizeof(double));
if (mi20 == NULL){
printf("Cannot allocate %d bytes for memory.\n", (n_object * sizeof(double)));
exit(-1);
}
memset(mi20, 0, (n_object * sizeof(double)));
double *mi02 = NULL;
mi02 = (double *)malloc(n_object * sizeof(double));
if (mi02 == NULL){
printf("Cannot allocate %d bytes for memory.\n", (n_object * sizeof(double)));
exit(-1);
}
memset(mi02, 0, (n_object * sizeof(double)));
morphological_feature_central_moments(0.0, 0.0, obj_id, width, height, n_object, image, mi00);
morphological_feature_central_moments(1.0, 1.0, obj_id, width, height, n_object, image, mi11);
morphological_feature_central_moments(2.0, 0.0, obj_id, width, height, n_object, image, mi20);
morphological_feature_central_moments(0.0, 2.0, obj_id, width, height, n_object, image, mi02);
#if ALLOW_PRINTF == TRUE
printf("Object orientation:\n");
#endif
for (i = 0; i < n_object; i++){
object_orientation[i] = 0.5*atan((2.0*(mi11[i] / mi00[i])) / ((mi20[i] / mi00[i]) - (mi02[i] / mi00[i])));
#if ALLOW_PRINTF == TRUE
printf("%d %.3f %.3f\n", i, object_orientation[i], RADIANS_TO_DEGREES(object_orientation[i]));
#endif
}
free(mi00);
free(mi11);
free(mi20);
free(mi02);
return TRUE;
}
int morphological_feature_object_eccentricity(int **obj_id, int width, int height, int n_object, unsigned char *image, double *object_eccentricity)
{
unsigned int i = 0;
double *mi00 = NULL;
mi00 = (double *)malloc(n_object * sizeof(double));
if (mi00 == NULL){
printf("Cannot allocate %d bytes for memory.\n", (n_object * sizeof(double)));
exit(-1);
}
memset(mi00, 0, (n_object * sizeof(double)));
double *mi20 = NULL;
mi20 = (double *)malloc(n_object * sizeof(double));
if (mi20 == NULL){
printf("Cannot allocate %d bytes for memory.\n", (n_object * sizeof(double)));
exit(-1);
}
memset(mi20, 0, (n_object * sizeof(double)));
double *mi02 = NULL;
mi02 = (double *)malloc(n_object * sizeof(double));
if (mi02 == NULL){
printf("Cannot allocate %d bytes for memory.\n", (n_object * sizeof(double)));
exit(-1);
}
memset(mi02, 0, (n_object * sizeof(double)));
morphological_feature_central_moments(0.0, 0.0, obj_id, width, height, n_object, image, mi00);
morphological_feature_central_moments(2.0, 0.0, obj_id, width, height, n_object, image, mi20);
morphological_feature_central_moments(0.0, 2.0, obj_id, width, height, n_object, image, mi02);
#if ALLOW_PRINTF == TRUE
printf("Object eccentricity:\n");
#endif
for (i = 0; i < n_object; i++){
double mei02 = (mi02[i] / mi00[i]);
double mei20 = (mi20[i] / mi00[i]);
object_eccentricity[i] = fabs((mei02 - mei20) / (mei02 + mei20));
#if ALLOW_PRINTF == TRUE
printf("%d %.3f\n", i, object_eccentricity[i]);
#endif
}
free(mi00);
free(mi20);
free(mi02);
return TRUE;
}
int morphological_feature_central_invariant_moments(double p, double q, int **obj_id, int width, int height, int n_object, unsigned char *image, double *central_invariant_moment)
{
unsigned int i = 0;
if (p+q < 2.0){
printf("morphological_feature_central_invariant_moments: p+q < 2!!! p+q should be >=2\n");
return FALSE;
}
double *mi00 = NULL;
mi00 = (double *)malloc(n_object * sizeof(double));
if (mi00 == NULL){
printf("Cannot allocate %d bytes for memory.\n", (n_object * sizeof(double)));
exit(-1);
}
memset(mi00, 0, (n_object * sizeof(double)));
double *mipq = NULL;
mipq = (double *)malloc(n_object * sizeof(double));
if (mipq == NULL){
printf("Cannot allocate %d bytes for memory.\n", (n_object * sizeof(double)));
exit(-1);
}
memset(mipq, 0, (n_object * sizeof(double)));
morphological_feature_central_moments(0.0, 0.0, obj_id, width, height, n_object, image, mi00);
morphological_feature_central_moments(p, q, obj_id, width, height, n_object, image, mipq);
#if ALLOW_PRINTF == TRUE
printf("Central invariant moment(%.0f, %.0f) :\n", p, q);
#endif
for (i = 0; i < n_object; i++){
central_invariant_moment[i] = mipq[i] / pow(mi00[i], (1.0 + ((p+q)/2.0)));
#if ALLOW_PRINTF == TRUE
printf("%d %f\n", i, central_invariant_moment[i]);
#endif
}
free(mi00);
free(mipq);
return TRUE;
}