-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlabelling.cpp
283 lines (252 loc) · 7.32 KB
/
labelling.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
#define _CRT_SECURE_NO_WARNINGS
#include <string.h>
#include <stdio.h>
#include <malloc.h>
#include "vc.h"
#include "labelling.h"
#include <string.h>
// Etiquetagem de blobs
// src : Imagem binária de entrada
// dst : Imagem grayscale (irá conter as etiquetas)
// nlabels : Endereço de memória de uma variável, onde será armazenado o número de etiquetas encontradas.
// OVC* : Retorna um array de estruturas de blobs (objectos), com respectivas etiquetas. É necessário libertar posteriormente esta memória.
OVC* vc_binary_blob_labelling(IVC *src, IVC *dst, int *nlabels)
{
unsigned char *datasrc = (unsigned char *)src->data;
unsigned char *datadst = (unsigned char *)dst->data;
int width = src->width;
int height = src->height;
int bytesperline = src->bytesperline;
int channels = src->channels;
int x, y, a, b;
long int i, size;
long int posX, posA, posB, posC, posD;
int labeltable[256] = { 0 };
int labelarea[256] = { 0 };
int label = 1; // Etiqueta inicial.
int num, tmplabel;
OVC *blobs; // Apontador para array de blobs (objectos) que será retornado desta função.
// Verificação de erros
if ((src->width <= 0) || (src->height <= 0) || (src->data == NULL)) return 0;
if ((src->width != dst->width) || (src->height != dst->height) || (src->channels != dst->channels)) return NULL;
if (channels != 1) return NULL;
// Copia dados da imagem binária para imagem grayscale
memcpy(datadst, datasrc, bytesperline * height);
// Todos os pixéis de plano de fundo devem obrigatóriamente ter valor 0
// Todos os pixéis de primeiro plano devem obrigatóriamente ter valor 255
// Serão atribuídas etiquetas no intervalo [1,254]
// Este algoritmo está assim limitado a 255 labels
for (i = 0, size = bytesperline * height; i<size; i++)
{
if (datadst[i] != 0) datadst[i] = 255;
}
// Limpa os rebordos da imagem binária
for (y = 0; y<height; y++)
{
datadst[y * bytesperline + 0 * channels] = 0;
datadst[y * bytesperline + (width - 1) * channels] = 0;
}
for (x = 0; x<width; x++)
{
datadst[0 * bytesperline + x * channels] = 0;
datadst[(height - 1) * bytesperline + x * channels] = 0;
}
// Efectua a etiquetagem
for (y = 1; y<height - 1; y++)
{
for (x = 1; x<width - 1; x++)
{
// Kernel:
// A B C
// D X
posA = (y - 1) * bytesperline + (x - 1) * channels; // A
posB = (y - 1) * bytesperline + x * channels; // B
posC = (y - 1) * bytesperline + (x + 1) * channels; // C
posD = y * bytesperline + (x - 1) * channels; // D
posX = y * bytesperline + x * channels; // X
// Se o pixel foi marcado
if (datadst[posX] != 0)
{
if ((datadst[posA] == 0) && (datadst[posB] == 0) && (datadst[posC] == 0) && (datadst[posD] == 0))
{
datadst[posX] = label;
labeltable[label] = label;
label++;
}
else
{
num = 255;
// Se A está marcado
if (datadst[posA] != 0) num = labeltable[datadst[posA]];
// Se B está marcado, e é menor que a etiqueta "num"
if ((datadst[posB] != 0) && (labeltable[datadst[posB]] < num)) num = labeltable[datadst[posB]];
// Se C está marcado, e é menor que a etiqueta "num"
if ((datadst[posC] != 0) && (labeltable[datadst[posC]] < num)) num = labeltable[datadst[posC]];
// Se D está marcado, e é menor que a etiqueta "num"
if ((datadst[posD] != 0) && (labeltable[datadst[posD]] < num)) num = labeltable[datadst[posD]];
// Atribui a etiqueta ao pixel
datadst[posX] = num;
labeltable[num] = num;
// Actualiza a tabela de etiquetas
if (datadst[posA] != 0)
{
if (labeltable[datadst[posA]] != num)
{
for (tmplabel = labeltable[datadst[posA]], a = 1; a<label; a++)
{
if (labeltable[a] == tmplabel)
{
labeltable[a] = num;
}
}
}
}
if (datadst[posB] != 0)
{
if (labeltable[datadst[posB]] != num)
{
for (tmplabel = labeltable[datadst[posB]], a = 1; a<label; a++)
{
if (labeltable[a] == tmplabel)
{
labeltable[a] = num;
}
}
}
}
if (datadst[posC] != 0)
{
if (labeltable[datadst[posC]] != num)
{
for (tmplabel = labeltable[datadst[posC]], a = 1; a<label; a++)
{
if (labeltable[a] == tmplabel)
{
labeltable[a] = num;
}
}
}
}
if (datadst[posD] != 0)
{
if (labeltable[datadst[posD]] != num)
{
for (tmplabel = labeltable[datadst[posC]], a = 1; a<label; a++)
{
if (labeltable[a] == tmplabel)
{
labeltable[a] = num;
}
}
}
}
}
}
}
}
// Volta a etiquetar a imagem
for (y = 1; y<height - 1; y++)
{
for (x = 1; x<width - 1; x++)
{
posX = y * bytesperline + x * channels; // X
if (datadst[posX] != 0)
{
datadst[posX] = labeltable[datadst[posX]];
}
}
}
//printf("\nMax Label = %d\n", label);
// Contagem do número de blobs
// Passo 1: Eliminar, da tabela, etiquetas repetidas
for (a = 1; a<label - 1; a++)
{
for (b = a + 1; b<label; b++)
{
if (labeltable[a] == labeltable[b]) labeltable[b] = 0;
}
}
// Passo 2: Conta etiquetas e organiza a tabela de etiquetas, para que não hajam valores vazios (zero) entre etiquetas
*nlabels = 0;
for (a = 1; a<label; a++)
{
if (labeltable[a] != 0)
{
labeltable[*nlabels] = labeltable[a]; // Organiza tabela de etiquetas
(*nlabels)++; // Conta etiquetas
}
}
// Se não há blobs
if (*nlabels == 0) return NULL;
// Cria lista de blobs (objectos) e preenche a etiqueta
blobs = (OVC *)calloc((*nlabels), sizeof(OVC));
if (blobs != NULL)
{
for (a = 0; a<(*nlabels); a++) blobs[a].label = labeltable[a];
}
else return NULL;
return blobs;
}
int vc_binary_blob_info(IVC *src, OVC *blobs, int nblobs)
{
unsigned char *data = (unsigned char *)src->data;
int width = src->width;
int height = src->height;
int bytesperline = src->bytesperline;
int channels = src->channels;
int x, y, i;
long int pos;
int xmin, ymin, xmax, ymax;
long int sumx, sumy;
// Verificação de erros
if ((src->width <= 0) || (src->height <= 0) || (src->data == NULL)) return 0;
if (channels != 1) return 0;
// Conta área de cada blob
for (i = 0; i<nblobs; i++)
{
xmin = width - 1;
ymin = height - 1;
xmax = 0;
ymax = 0;
sumx = 0;
sumy = 0;
blobs[i].area = 0;
for (y = 1; y<height - 1; y++)
{
for (x = 1; x<width - 1; x++)
{
pos = y * bytesperline + x * channels;
if (data[pos] == blobs[i].label)
{
// Área
blobs[i].area++;
// Centro de Gravidade
sumx += x;
sumy += y;
// Bounding Box
if (xmin > x) xmin = x;
if (ymin > y) ymin = y;
if (xmax < x) xmax = x;
if (ymax < y) ymax = y;
// Perímetro
// Se pelo menos um dos quatro vizinhos não pertence ao mesmo label, então é um pixel de contorno
if ((data[pos - 1] != blobs[i].label) || (data[pos + 1] != blobs[i].label) || (data[pos - bytesperline] != blobs[i].label) || (data[pos + bytesperline] != blobs[i].label))
{
blobs[i].perimeter++;
}
}
}
}
// Bounding Box
blobs[i].x = xmin;
blobs[i].y = ymin;
blobs[i].width = (xmax - xmin) + 1;
blobs[i].height = (ymax - ymin) + 1;
// Centro de Gravidade
//blobs[i].xc = (xmax - xmin) / 2;
//blobs[i].yc = (ymax - ymin) / 2;
blobs[i].xc = sumx / MAX(blobs[i].area, 1);
blobs[i].yc = sumy / MAX(blobs[i].area, 1);
}
return 1;
}