-
Notifications
You must be signed in to change notification settings - Fork 12
/
valid_curve.c
458 lines (399 loc) · 15.2 KB
/
valid_curve.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*----------------------------------------------------------------------------
ELSD - Ellipse and Line Segment Detector
Copyright (c) 2012 viorica patraucean ([email protected])
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
----------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <limits.h>
#include <float.h>
#include <ctype.h>
#include "elsd.h"
#include "valid_curve.h"
#include "process_curve.h"
#include "process_line.h"
/*---------------------------------------------------------------------------*/
/** Compute max element in an array and return the max value and its position.
*/
double max_array(double *a, int sz, int *poz)
{
/* check parameters */
if (a == NULL) error("max: invalid pointer");
int i;
double max = (double)LONG_MIN;
for (i=0;i<sz;i++)
if (max<a[i]) {max = a[i]; *poz = i;}
return max;
}
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
/** Quicksort the elements of an array and return the sorted array
and the original positions of the elements.
*/
void quickSort(double *arr, int elements, int *pos) {
#define MAX_LEVELS 300
int beg[MAX_LEVELS], end[MAX_LEVELS], i, L, R, swap ;
double piv;
for (i=0;i<elements;i++) pos[i] = i;
int ptmp;
beg[0]=0; end[0]=elements;
i = 0;
while (i>=0)
{
L=beg[i]; R=end[i]-1;
if (L<R)
{
piv=arr[L];
ptmp = pos[L];
while (L<R)
{
while (arr[R]>=piv && L<R) R--;
if (L<R)
{
arr[L]=arr[R]; pos[L] = pos[R]; L++;
}
while (arr[L]<=piv && L<R) L++;
if (L<R)
{
arr[R]=arr[L]; pos[R] = pos[L]; R--;
}
}
arr[L]=piv; pos[L]=ptmp; beg[i+1]=L+1; end[i+1]=end[i]; end[i++]=L;
if (end[i]-beg[i]>end[i-1]-beg[i-1])
{
swap=beg[i]; beg[i]=beg[i-1]; beg[i-1]=swap;
swap=end[i]; end[i]=end[i-1]; end[i-1]=swap;
}
}
else
{
i--;
}
}
}
/*---------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/** Compute the delimiting angles of a circular/elliptical arc. Return
the delimiting angles and the index positions of the extreme points.
*/
void extreme_sorted_angles(int sz, double *ang_start,double *ang_end, int *idx)
{
/* check parameters */
if (sz<=0) error("extreme_angles : invalid size of angles list");
int i;
double alphamax = 0.0;
int poz;
double difftmp;
if (sz>gSizeBufferInt)
{
gBufferInt = (int*)realloc(gBufferInt, sz*sizeof(int));
if (!gBufferInt) error("extreme_sorted_angles: not enough memory");
gSizeBufferInt = sz;
}
quickSort(gBufferDouble,sz,gBufferInt);
for (i=0;i<sz-1;i++)
{
difftmp = gBufferDouble[i+1]-gBufferDouble[i];
if (difftmp>alphamax)
{
alphamax = difftmp;
poz = i;
}
}
difftmp = gBufferDouble[0] + M_2__PI - gBufferDouble[sz-1];
if (difftmp>alphamax)
{
alphamax = difftmp;
poz = sz - 1;
}
*ang_end = gBufferDouble[poz];
idx[1] = gBufferInt[poz];
if (poz != (sz-1))
{
*ang_start = gBufferDouble[poz+1];
idx[0] = gBufferInt[poz+1];
}
else
{
*ang_start = gBufferDouble[0];
idx[0] = gBufferInt[0];
}
}
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
/** Test if angle 'ang' is in interval ['ang1', 'ang2'];
'ang', 'ang1', 'ang2' are in [0 , 2pi].
*/
int isInAng(double ang, double ang1, double ang2)
{
int ok = 0;
if (ang2>ang1)
{
if (ang >= ang1 && ang <= ang2) ok = 1;
}
else
if (ang >= ang1 || ang <= ang2) ok = 1;
return ok;
}
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
/** Clean region.
*/
void clean_reg(struct point* reg, int reg_size, image_char used)
{
int i = 0;
for (i=0;i<reg_size;i++)
used->data[reg[i].y*used->xsize+reg[i].x] = NOTUSED;
}
/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/** Compute a circle's NFA value : discrete NFA.
*/
double valid_circle(struct point *reg, int reg_size, image_char used, double prec,
double p, image_double angles, image_double grad, image_double gradx,
image_double grady, double *param, double logNTC, int dir, int *pext,
struct point3 *regc, int *regc_size, int min_size, double mlog10eps)
{
int alg = 0;
double d_min = angles->xsize;
double d_max = -d_min;
double d,theta,theta0;
int i,xx,yy;
double ang_start,ang_end;
int idx[2];
/* check parameters */
if( angles == NULL ) error("valid_circle: invalid 'angles'.");
/* clean region for validation */
clean_reg(reg,reg_size,used);
/* refine: search for connected aligned points on the given circle starting from the same seed point */
used->data[reg[0].y*used->xsize+reg[0].x] = USED;
*regc_size = 1;
for(i=0; i<*regc_size; i++)
for(xx=reg[i].x-1; xx<=reg[i].x+1; xx++)
for(yy=reg[i].y-1; yy<=reg[i].y+1; yy++)
if (xx>=0 && yy>=0 && xx<(int)used->xsize && yy<(int)used->ysize &&
used->data[xx+yy*used->xsize] != USED)
{
theta0 = atan2((double)yy-param[1],(double)xx-param[0]);
if (dir==0)
{
if (theta0>0) theta = -(M_PI-theta0);
else theta = M_PI + theta0;
}
else theta = theta0;
if (isaligned( xx, yy, angles, theta, prec))
{
used->data[xx+yy*used->xsize] = USED;
reg[*regc_size].x = xx;
reg[*regc_size].y = yy;
++(*regc_size);
}
}
if (*regc_size>min_size)
{
/* reestimate circle on the connected aligned points to have a better precision */
double vgg[9];
fit_equations(reg,*regc_size,gradx,grady,vgg);
fitcircle(*regc_size,vgg,param);
/* compute parameters of the circular ring: width and delimiting angles */
for(i=0;i<*regc_size;i++)
{
/* compute width and store angles in global temp gArray1 */
d = sqrt((reg[i].x-param[0])*(reg[i].x-param[0])+(reg[i].y-param[1])*(reg[i].y-param[1]))-param[2];
if (d<d_min) d_min = d;
if (d>d_max) d_max = d;
gBufferDouble[i] = atan2((double)reg[i].y-param[1],(double)reg[i].x-param[0]);
if (gBufferDouble[i]<0) gBufferDouble[i] += M_2__PI;
used->data[reg[i].y*grad->xsize+reg[i].x] = NOTUSED;
}
/* compute delimiting angles */
if (*regc_size>2) extreme_sorted_angles(*regc_size,&ang_start,&ang_end,idx);
/* extract extreme contour points */
pext[0] = reg[idx[0]].x; pext[1] = reg[idx[0]].y;
pext[2] = reg[idx[1]].x; pext[3] = reg[idx[1]].y;
/* scan the circular ring and count the number of points and the number of aligned points */
*regc_size = 1;
used->data[reg[0].y*used->xsize+reg[0].x] = USEDCIRC;
regc[0].x = reg[0].x;
regc[0].y = reg[0].y;
regc[0].z = USEDCIRC;
for(i=0; i<*regc_size; i++)
for(xx=regc[i].x-1; xx<=regc[i].x+1; xx++)
for(yy=regc[i].y-1; yy<=regc[i].y+1; yy++)
if (xx>=0 && yy>=0 && xx<(int)used->xsize && yy<(int)used->ysize &&
used->data[xx+yy*used->xsize] != USED && used->data[xx+yy*used->xsize] != USEDCIRC)
{
d = sqrt((xx-param[0])*(xx-param[0])+(yy-param[1])*(yy-param[1]))-param[2];
theta0 = atan2((double)yy-param[1],(double)xx-param[0]);
if (theta0<0) theta = theta0 + M_2__PI;
else theta = theta0;
if(d>=d_min && d<=d_max && isInAng(theta,ang_start,ang_end))
{
if (dir==0)
{
if (theta0>0) theta = -(M_PI-theta0);
else theta = M_PI + theta0;
}
else theta = theta0;
if (isaligned( xx, yy, angles, theta, prec))
{
++alg;
regc[*regc_size].z = USEDCIRC;
}
else
{
regc[*regc_size].z = USEDCIRCNA;
}
used->data[xx+yy*used->xsize] = USEDCIRC;
regc[*regc_size].x = xx;
regc[*regc_size].y = yy;
++(*regc_size);
}
}
return nfa(*regc_size,alg,p,logNTC); /* compute NFA value */
}
else
return mlog10eps;
}
/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/** Compute ellipse foci, given ellipse params.
*/
void ellipse_foci(double *param, double *foci)
{
double f = sqrt(param[2]*param[2]-param[3]*param[3]);
foci[0] = param[0]+f*cos(param[4]);
foci[1] = param[1]+f*sin(param[4]);
foci[2] = param[0]-f*cos(param[4]);
foci[3] = param[1]-f*sin(param[4]);
}
/*----------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
/** Compute an ellipse's NFA value : discrete NFA.
*/
double valid_ellipse(struct point *reg, int reg_size, image_char used, double prec,
double p, image_double angles, image_double grad, image_double gradx,
image_double grady, double *param, double logNTE, int dir, int *pext,
struct point3 *rege, int *rege_size, int min_size, double mlog10eps)
{
int alg = 0;
double d_min = angles->xsize;
double d_max = -d_min;
double d,theta,theta0;
int i,xx,yy;
double ang_start,ang_end;
int idx[2];
double foci[4]; /* xf1, yf1, xf2, yf2 */
/* check parameters */
if( angles == NULL ) error("valid_ellipse: invalid 'angles'.");
ellipse_foci(param, foci);
/* refine: search for connected aligned points on the given ellipse starting from the same seed point */
*rege_size = 1;
used->data[reg[0].y*used->xsize+reg[0].x] = USED;
for(i=0; i<*rege_size; i++)
for(xx=reg[i].x-1; xx<=reg[i].x+1; xx++)
for(yy=reg[i].y-1; yy<=reg[i].y+1; yy++)
if (xx>=0 && yy>=0 && xx<(int)used->xsize && yy<(int)used->ysize &&
used->data[xx+yy*used->xsize] != USED)
{
theta = angle((double)xx, (double)yy, foci);
if (dir==0)
{
if (theta>0) theta = -(M_PI-theta);
else theta = M_PI + theta;
}
if (isaligned( xx, yy, angles, theta, prec))
{
used->data[xx+yy*used->xsize] = USED;
reg[*rege_size].x = xx;
reg[*rege_size].y = yy;
++(*rege_size);
}
}
if (*rege_size>min_size)
{
/* reestimate ellipse on the connected aligned points to have a better precision */
double vgg[9];
fit_equations(reg,*rege_size,gradx,grady,vgg);
fitellipse(*rege_size,vgg,param);
int ell_ok = check_ellipse(param);
if (ell_ok)
{
ellipse_foci(param, foci);
/* compute parameters of the elliptical ring: width and delimiting angles */
for(i=0;i<*rege_size;i++)
{
/* compute width and store angles in global temp gArray1 */
d = d_rosin(param,(double)reg[i].x, (double)reg[i].y);
if (d<d_min) d_min = d;
if (d>d_max) d_max = d;
gBufferDouble[i] = atan2((double)reg[i].y-param[1],(double)reg[i].x-param[0]);
if (gBufferDouble[i]<0) gBufferDouble[i] += M_2__PI;
used->data[reg[i].y*grad->xsize+reg[i].x] = NOTUSED;
}
/* compute delimiting angles */
if (*rege_size>2) extreme_sorted_angles(*rege_size, &ang_start, &ang_end, idx);
/* extract extreme contour points */
pext[4] = reg[idx[0]].x; pext[5] = reg[idx[0]].y;
pext[6] = reg[idx[1]].x; pext[7] = reg[idx[1]].y;
/* scan the elliptical ring and count the number of points and the number of aligned points */
*rege_size = 1;
used->data[reg[0].y*used->xsize+reg[0].x] = USEDELL;
rege[0].x = reg[0].x;
rege[0].y = reg[0].y;
rege[0].z = USEDELL;
for(i=0; i<*rege_size; i++)
for(xx=rege[i].x-1; xx<=rege[i].x+1; xx++)
for(yy=rege[i].y-1; yy<=rege[i].y+1; yy++)
if (xx>=0 && yy>=0 && xx<(int)used->xsize && yy<(int)used->ysize &&
used->data[xx+yy*used->xsize] != USED && used->data[xx+yy*used->xsize] != USEDELL)
{
d = d_rosin(param,(double)xx,(double)yy);
theta0 = atan2((double)yy-param[1],(double)xx-param[0]);
if (theta0<0) theta = theta0 + M_2__PI;
else theta = theta0;
if(d>=d_min && d<=d_max && isInAng(theta,ang_start,ang_end))
{
theta = angle((double)xx, (double)yy, foci);
if (dir==0)
{
if (theta>0) theta = -(M_PI-theta);
else theta = M_PI + theta;
}
if (isaligned( xx, yy, angles, theta, prec))
{
++alg;
rege[*rege_size].z = USEDELL;
}
else
{
rege[*rege_size].z = USEDELLNA;
}
used->data[xx+yy*used->xsize] = USEDELL;
rege[*rege_size].x = xx;
rege[*rege_size].y = yy;
++(*rege_size);
}
}
return nfa(*rege_size,alg,p,logNTE); /* compute NFA value */
}
else
{
*rege_size = 0;
return mlog10eps;
}
}
else
return mlog10eps;
}
/*----------------------------------------------------------------------------*/