Cambios del domingo: cambio en la funcion crossing number, optimizacion de spursRemov...
[trabajo-pid:trabajo-pid.git] / PID_trabajo / auxiliar.cpp
1 //#include "auxiliar.h"
2 #include "stdafx.h"
3 //#include "Form1.h"
4
5 using namespace  System::Drawing;
6 using namespace System;
7
8 unsigned char** imagenEG; //Imagen escala de grises
9 unsigned char** imagenBN; //Imagen blanco y negro
10
11 minucia buscaMinucia(list<minucia>* lIn, int x, int y){
12         list<minucia>::iterator it = lIn->begin();
13          while(it != lIn->end()){
14                  if((((minucia)*it).x == x) && (((minucia)*it).y == y))
15                          return (minucia)*it;
16                  it++;
17          }
18
19         minucia m;
20         m.x = -1;
21         m.y = -1;
22         m.cn = -1;
23         return m;
24 }
25
26
27
28
29 unsigned char HLStoEG(float H, float L, float S){
30         float R = 0.0, G = 0.0, B = 0.0;
31         float M2, M1;
32         float h;
33         unsigned char resultado;
34         if (L <= 0.5)
35                 M2 = L *(1 + S);
36         else
37                 M2 = L + S - (L * S);
38
39         M1 = (2 * L) - M2 ;
40
41         if (S = 0){
42                 R = L;
43                 G = L;
44                 B = L;
45         } else {
46                 h = H + 120.0; 
47
48                 if (h > 360.0) 
49                         h =  h - 360.0; 
50
51                 if (h < 60.0)
52                         R = (( M1 + ( M2 - M1 ) * h) / 60.0);
53                 else if (h < 180.0)
54                         R = M2; 
55                 else if (h < 240.0)
56                         R = ((M1 + ( M2 - M1 ) * ( 240 - h )) / 60.0);
57                 else 
58                         R = M1;
59
60                 h = H; 
61                 if (h < 60.0)
62                         G = (( M1 + ( M2 - M1 ) * h) / 60.0);
63                 else if (h < 180.0) 
64                         G = M2;
65                 else if (h < 240.0)
66                         G = ((M1 + ( M2 - M1 ) * ( 240 - h )) / 60.0);
67                 else 
68                         G = M1;
69
70                 h = H - 120.0;
71                 if (h < 0.0)
72                         h = h + 360.0;
73                 if (h < 60.0)
74                         B =(( M1 + ( M2 - M1 ) * h )/ 60.0);
75                 else if (h < 180.0) 
76                         B = M2;
77                 else if (h < 240.0)
78                         B =((M1 + ( M2 - M1 ) * ( 240 - h )) / 60.0);
79                 else 
80                         B = M1;
81         }
82         resultado = (unsigned char) (255*((R * 0.3) + (G * 0.59) + (B * 0.11)));
83         return resultado;
84 }
85
86 unsigned char** inicializarImagen(int width, int height) {
87         int i;
88         unsigned char **imagen = (unsigned char**) malloc(width * sizeof(unsigned char *));
89         for(i = 0; i < width; i++){
90                 imagen[i] = (unsigned char*) malloc(height * sizeof(unsigned char));
91         }
92         return imagen;
93 }
94
95 unsigned char** generarBN(Bitmap ^bp, int umbral){
96         int width, height;
97         int i, j;
98         width = bp->Width;
99         height = bp->Height;
100
101         imagenBN = (unsigned char**) malloc(width * sizeof(unsigned char *));
102         for(i = 0; i < width; i++){
103                 imagenBN[i] = (unsigned char*) malloc(height * sizeof(unsigned char));
104         }
105
106         for(i = 0; i < width; i++){
107                 for(j = 0; j < height; j++){
108                         Color px = bp->GetPixel(i,j);
109                         unsigned char aux = HLStoEG(px.GetHue(), px.GetBrightness(), px.GetSaturation());
110
111                         if(aux < umbral)
112                                 imagenBN[i][j] = 1;
113                         else
114                                 imagenBN[i][j] = 0;
115                 }
116         }
117
118         return imagenBN;
119 }
120
121 void hacerEsqueleto(unsigned char **imagen, int y, int x) {
122         Rosenfeld8(imagen, x, y);
123         //FastThin(imagen, x, y);
124 }
125
126 /* Algoritmo 1 para sacar el esqueleto */
127 void FastThin(unsigned char **list0, int Dx, int Dy) {
128         int  i,j,k,flag;
129         int  f[11],ap1,bp1;
130         char *buf,**list;
131
132         buf =(char*)  malloc(Dx*Dy*sizeof(char));
133         list=(char**) malloc(Dy*sizeof(char*));
134
135         for(k=0;k<Dy;k++) {
136                 list[k]=buf+k*Dx;
137                 memcpy(list[k],list0[k],Dx);
138         }
139
140         do {
141                 flag=0;
142                 for (j=1; j<Dy-1; j++) {
143                         for (i=1; i<Dx-1; i++) {
144                                 if  (list[j][i] != 1)  continue;
145                                 f[1]=list[j][i];
146                                 f[2]=list[j][i-1];
147                                 f[3]=list[j+1][i-1];
148                                 f[4]=list[j+1][i];
149                                 f[5]=list[j+1][i+1];
150                                 f[6]=list[j][i+1];
151                                 f[7]=list[j-1][i+1];
152                                 f[8]=list[j-1][i];
153                                 f[9]=list[j-1][i-1];
154                                 f[10]=f[2];
155
156                                 for (k=2; k<=10; k++)
157                                         f[k] = (f[k]>0) ? 1 : 0;
158                                 if (f[4]&&f[6])
159                                         if (f[2]||f[8])  continue;
160                                 bp1=0;
161                                 for (k=2; k<=9; k++)
162                                         bp1+=f[k];
163                                 if ((bp1<2)||(bp1>6))  continue;
164                                 ap1=0;
165                                 for (k=2; k<=9; k++)
166                                         if ((f[k]==0) && (f[k+1]==1))
167                                                 ap1++;
168                                 if (ap1!=1)  continue;
169                                 list[j][i]=3;
170                         }
171                 }
172                 for (j=1; j<Dy-1; j++) {
173                         for (i=1; i<Dx-1; i++) {
174                                 if (list[j][i] == 3) {
175                                         list[j][i] = 0;
176                                         //list0[j][i]= COLOR;
177                                         list0[j][i]= 0;
178                                         flag=1;
179                                 }
180                         }
181                 }
182                 if (flag==0)  break;
183                 flag=0;
184                 for (j=1; j<Dy-1; j++) {
185                         for (i=1; i<Dx-1; i++) {
186                                 if  (list[j][i] != 1)  continue;
187                                 f[1]=list[j][i];
188                                 f[2]=list[j][i-1];
189                                 f[3]=list[j+1][i-1];
190                                 f[4]=list[j+1][i];
191                                 f[5]=list[j+1][i+1];
192                                 f[6]=list[j][i+1];
193                                 f[7]=list[j-1][i+1];
194                                 f[8]=list[j-1][i];
195                                 f[9]=list[j-1][i-1];
196                                 f[10]=f[2];
197
198                                 for (k=2; k<=10; k++)
199                                         f[k] = (f[k]>0) ? 1 : 0;
200                                 if (f[2]&&f[8])
201                                         if (f[4]||f[6])  continue;
202                                 bp1=0;
203                                 for (k=2; k<=9; k++)
204                                         bp1+=f[k];
205                                 if ((bp1<2)||(bp1>6))  continue;
206                                 ap1=0;
207                                 for (k=2; k<=9; k++)
208                                         if ((f[k]==0) && (f[k+1]==1))
209                                                 ap1++;
210                                 if (ap1!=1)  continue;
211                                 list[j][i] = 3;
212                         }
213                 }
214                 for (j=1; j<Dy-1; j++) {
215                         for (i=1; i<Dx-1; i++) {
216                                 if (list[j][i] == 3) {
217                                         list[j][i] = 0;
218                                         //list0[j][i]=COLOR;
219                                         list0[j][i]=0;
220                                         flag=1;
221                                 }
222                         }
223                 }
224         }
225         while(flag==1);
226
227         free(list);                   
228         free(buf);
229 }
230
231 /* Algoritmo 2 para sacar el esqueleto (versiĆ³n 8-adyacencia)
232 */
233 void Rosenfeld8(unsigned char** list0, int Dx, int Dy) {
234         int  i, j, k, ii, jj, kk, flag;
235         int  N[10];
236         int  jbd[5] = {0, -1, 1, 0, 0};
237         int  ibd[5] = {0, 0, 0, 1, -1};
238         char NRND, N48, N26, N24, N46, N68, N82;
239         char N123, N345, N567, N781;
240         char *buf, **list;
241
242         buf = (char*) malloc(Dx * Dy * sizeof(char));
243         list = (char**) malloc(Dy * sizeof(char*));
244         for(k = 0; k < Dy; k++) {
245                 list[k] = buf + k* Dx;
246                 memcpy(list[k], list0[k], Dx);
247         }
248
249         do {
250                 flag = 0;
251                 for (kk = 1; kk <= 4; kk++) {
252                         jj = jbd[kk];
253                         for (j = 1; j < Dy - 1; j++) {
254                                 for (i = 1; i < Dx - 1; i++) {
255                                         ii = ibd[kk];
256                                         if (!list[j][i])
257                                                 continue;
258                                         if (list[j+jj][i+ii])
259                                                 continue;
260                                         N[1] = list[j+1][i-1];
261                                         N[2] = list[j][i-1];
262                                         N[3] = list[j-1][i-1];
263                                         N[4] = list[j-1][i];
264                                         N[5] = list[j-1][i+1];
265                                         N[6] = list[j][i+1];
266                                         N[7] = list[j+1][i+1];
267                                         N[8] = list[j+1][i];
268
269                                         NRND = 0;
270                                         for (k = 1; k <= 8; k++)
271                                                 NRND += N[k];
272                                         if (NRND <= 1)
273                                                 continue;
274                                         N48 = N[4] + N[8];
275                                         N567 = N[5] + N[6] + N[7];
276                                         if (N[2] == 1 && N48 == 0 && N567 > 0)
277                                                 continue;
278                                         N123 = N[1] + N[2] + N[3];
279                                         if (N[6] == 1 && N48 == 0 && N123 > 0)
280                                                 continue;
281                                         N26 = N[2] + N[6];
282                                         N345 = N[3] + N[4] + N[5];
283                                         if (N[8] == 1 && N26 == 0 && N345 > 0)
284                                                 continue;
285                                         N781 = N[7] + N[8] + N[1];
286                                         if (N[4] == 1 && N26 == 0 && N781 > 0)
287                                                 continue;
288                                         N46 = N[4] + N[6];
289                                         if (N[5] == 1 && N46 == 0)  continue;
290                                         N68 = N[6] + N[8];
291                                         if (N[7] == 1 && N68 == 0)  continue;
292                                         N82 = N[8] + N[2];
293                                         if (N[1] == 1 && N82 == 0)  continue;
294                                         N24 = N[2] + N[4];
295                                         if (N[3] == 1 && N24 == 0)  continue;
296                                         list0[j][i] = 0;
297                                         flag = 1;
298                                 }
299                         }
300                         for (i = 0; i < Dy; i++) {
301                                 memcpy(list[i], list0[i], Dx);
302                                 for (j = 0; j < Dx; j++)
303                                         if (list[i][j] != 1)
304                                                 list[i][j] = 0;
305                         }
306                 }
307         } while(flag == 1);
308
309         free(list);                   
310         free(buf);
311 }
312
313 /*Calcular el crossing number de cada pixel
314 */
315 unsigned char** crossingNumbers(unsigned char **imagen, int width, int height, list<minucia> *l1) {
316         unsigned char** resultado;
317         int sumatorio = 0;
318         int i,j;
319         minucia m;
320         resultado = (unsigned char**) malloc(width*sizeof(unsigned char*));
321         for(i = 0 ; i < width; i++){
322                 resultado[i] = (unsigned char*) malloc(height*sizeof(unsigned char));
323         }
324
325
326         for (i = 1; i < width - 1; i++){
327                 for (j=1; j < height - 1; j++){
328                         if (imagen[i][j] != 1) continue;
329                         sumatorio = 0;
330                         sumatorio += abs(imagen[i][j+1] - imagen[i-1][j+1]);
331                         sumatorio += abs(imagen[i-1][j+1] - imagen[i-1][j]);
332                         sumatorio += abs(imagen[i-1][j] - imagen[i-1][j-1]);
333                         sumatorio += abs(imagen[i-1][j-1] - imagen[i][j-1]);
334                         sumatorio += abs(imagen[i][j-1] - imagen[i+1][j-1]);
335                         sumatorio += abs(imagen[i+1][j-1] - imagen[i+1][j]);
336                         sumatorio += abs(imagen[i+1][j] - imagen[i+1][j+1]);
337                         sumatorio += abs(imagen[i+1][j+1] - imagen[i][j+1]);
338                         sumatorio /= 2;
339                         resultado[i][j] = sumatorio;
340                         if (sumatorio != 2) {
341                                 m.x = i;
342                                 m.y = j;
343                                 m.cn = sumatorio;
344                                 l1->push_back(m);
345                         }
346                 }
347         }
348         return resultado;
349 }
350
351
352