-
Notifications
You must be signed in to change notification settings - Fork 144
/
generateEllipseCandidates.cpp
5059 lines (4590 loc) · 168 KB
/
generateEllipseCandidates.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
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "mex.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <limits.h>
#include <float.h>
#include <iostream>
#include "lapack.h" //matlab
//#include "include/lapacke_config.h" //lapack手动,未成功
//#include "include/lapacke.h"
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/nonfree/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <opencv2\opencv.hpp>
using namespace cv;
#ifndef FALSE
#define FALSE 0
#endif /* !FALSE */
#ifndef TRUE
#define TRUE 1
#endif /* !TRUE */
/** Label for pixels with undefined gradient. */
#define NOTDEF -1024.0
/** PI */
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif /* !M_PI */
#define M_1_2_PI 1.57079632679489661923
#define M_1_4_PI 0.785398163
#define M_3_4_PI 2.35619449
#define M_1_8_PI 0.392699081
#define M_3_8_PI 1.178097245
#define M_5_8_PI 1.963495408
#define M_7_8_PI 2.748893572
#define M_4_9_PI 1.396263401595464 //80°
#define M_1_9_PI 0.34906585 //20°
#define M_1_10_PI 0.314159265358979323846 //18°
#define M_1_12_PI 0.261799387 //15°
#define M_1_15_PI 0.20943951 //12°
#define M_1_18_PI 0.174532925 //10°
/** 3/2 pi */
#define M_3_2_PI 4.71238898038
/** 2 pi */
#define M_2__PI 6.28318530718
/** Doubles relative error factor
*/
#define RELATIVE_ERROR_FACTOR 100.0
struct point2i //(or pixel).
{
int x,y;
};
struct point2d
{
double x,y;
};
struct point1d1i
{
double data;
int cnt;
};
struct point3d
{
double x,y;
double r;
};
struct point3i
{
int x,y;
int z;
};
struct point2d1i
{
double x,y;
int z;
};
struct point5d
{
double x,y;
double a,b;
double phi;
};
/*----------------------------------------------------------------------------*/
/** Rectangle structure: line segment with width.
*/
struct rect
{
double x1,y1,x2,y2; /* first and second point of the line segment */
double width; /* rectangle width */
double x,y; /* center of the rectangle */
double theta; /* angle */
double dx,dy; /* (dx,dy) is vector oriented as the line segment,dx = cos(theta), dy = sin(theta) */
int polarity; /* if the arc direction is the same as the edge direction, polarity = 1, else if opposite ,polarity = -1.*/
double prec; /* tolerance angle */
double p; /* probability of a point with angle within 'prec' */
};
typedef struct
{
double vx[4]; /* rectangle's corner X coordinates in circular order */
double vy[4]; /* rectangle's corner Y coordinates in circular order */
double ys,ye; /* start and end Y values of current 'column' */
int x,y; /* coordinates of currently explored pixel */
} rect_iter;
typedef struct image_double_s
{
double * data;
int xsize,ysize;
} * image_double;
//==================================================================================================
//=============================miscellaneous functions==============================================
inline double min(double v1,double v2)
{
return (v1<v2?v1:v2);
}
inline double max(double v1,double v2)
{
return (v1>v2?v1:v2);
}
/** Compare doubles by relative error.
The resulting rounding error after floating point computations
depend on the specific operations done. The same number computed by
different algorithms could present different rounding errors. For a
useful comparison, an estimation of the relative rounding error
should be considered and compared to a factor times EPS. The factor
should be related to the cumulated rounding error in the chain of
computation. Here, as a simplification, a fixed factor is used.
*/
int double_equal(double a, double b)
{
double abs_diff,aa,bb,abs_max;
/* trivial case */
if( a == b ) return TRUE;
abs_diff = fabs(a-b);
aa = fabs(a);
bb = fabs(b);
abs_max = aa > bb ? aa : bb;
/* DBL_MIN is the smallest normalized number, thus, the smallest
number whose relative error is bounded by DBL_EPSILON. For
smaller numbers, the same quantization steps as for DBL_MIN
are used. Then, for smaller numbers, a meaningful "relative"
error should be computed by dividing the difference by DBL_MIN. */
if( abs_max < DBL_MIN ) abs_max = DBL_MIN;
/* equal if relative error <= factor x eps */
return (abs_diff / abs_max) <= (RELATIVE_ERROR_FACTOR * DBL_EPSILON); //RELATIVE_ERROR_FACTOR=100.0,
}
/*----------------------------------------------------------------------------*/
/** Absolute value angle difference.
*/
//得到2个弧度制角度的夹角的绝对值
double angle_diff(double a, double b)
{
a -= b;
while( a <= -M_PI ) a += M_2__PI;
while( a > M_PI ) a -= M_2__PI;
if( a < 0.0 ) a = -a;
return a;
}
/*----------------------------------------------------------------------------*/
/** Signed angle difference.
*/
double angle_diff_signed(double a, double b)
{
a -= b;
while( a <= -M_PI ) a += M_2__PI;
while( a > M_PI ) a -= M_2__PI;
return a;
}
/*----------------------------------------------------------------------------*/
/** Fatal error, print a message to standard-error output and exit.
*/
void error(char * msg)
{
fprintf(stderr,"circleDetection Error: %s\n",msg);
exit(EXIT_FAILURE);
}
/*----------------------------------------------------------------------------*/
/** Computes Euclidean distance between point (x1,y1) and point (x2,y2).
*/
double dist(double x1, double y1, double x2, double y2)
{
return sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) );
}
//向量内积
double dotProduct(point2d vec1, point2d vec2)
{
return (vec1.x*vec2.x+vec1.y*vec2.y);
}
/*----------------------------------------------------------------------------*/
/** Copy one rectangle structure to another.
*/
void rect_copy(struct rect * in, struct rect * out)//in is the src, out is the dst
{
/* check parameters */
if( in == NULL || out == NULL ) error("rect_copy: invalid 'in' or 'out'.");
/* copy values */
out->x1 = in->x1;
out->y1 = in->y1;
out->x2 = in->x2;
out->y2 = in->y2;
out->width = in->width;
out->x = in->x;
out->y = in->y;
out->theta = in->theta;
out->dx = in->dx;
out->dy = in->dy;
out->polarity = in->polarity;
out->prec = in->prec;
out->p = in->p;
}
/*----------------------------------------------------------------------------*/
/** Interpolate y value corresponding to 'x' value given, in
the line 'x1,y1' to 'x2,y2'; if 'x1=x2' return the smaller
of 'y1' and 'y2'.
The following restrictions are required:
- x1 <= x2
- x1 <= x
- x <= x2
*/
double inter_low(double x, double x1, double y1, double x2, double y2)
{
/* check parameters */
if( x1 > x2 || x < x1 || x > x2 )
error("inter_low: unsuitable input, 'x1>x2' or 'x<x1' or 'x>x2'.");
/* interpolation */
if( double_equal(x1,x2) && y1<y2 ) return y1;
if( double_equal(x1,x2) && y1>y2 ) return y2;
return y1 + (x-x1) * (y2-y1) / (x2-x1);
}
/*----------------------------------------------------------------------------*/
/** Interpolate y value corresponding to 'x' value given, in
the line 'x1,y1' to 'x2,y2'; if 'x1=x2' return the larger
of 'y1' and 'y2'.
The following restrictions are required:
- x1 <= x2
- x1 <= x
- x <= x2
*/
double inter_hi(double x, double x1, double y1, double x2, double y2)
{
/* check parameters */
if( x1 > x2 || x < x1 || x > x2 )
error("inter_hi: unsuitable input, 'x1>x2' or 'x<x1' or 'x>x2'.");
/* interpolation */
if( double_equal(x1,x2) && y1<y2 ) return y2;
if( double_equal(x1,x2) && y1>y2 ) return y1;
return y1 + (x-x1) * (y2-y1) / (x2-x1);
}
/*----------------------------------------------------------------------------*/
/** Free memory used by a rectangle iterator.
*/
void ri_del(rect_iter * iter)
{
if( iter == NULL ) error("ri_del: NULL iterator.");
free( (void *) iter );
}
/*----------------------------------------------------------------------------*/
/** Check if the iterator finished the full iteration.
See details in \ref rect_iter
*/
int ri_end(rect_iter * i)
{
/* check input */
if( i == NULL ) error("ri_end: NULL iterator.");
/* if the current x value is larger than the largest
x value in the rectangle (vx[2]), we know the full
exploration of the rectangle is finished. */
return (double)(i->x) > i->vx[2];
}
/*----------------------------------------------------------------------------*/
/** Increment a rectangle iterator.
See details in \ref rect_iter
*/
void ri_inc(rect_iter * i)
{
/* check input */
if( i == NULL ) error("ri_inc: NULL iterator.");
/* if not at end of exploration,
increase y value for next pixel in the 'column' */
if( !ri_end(i) ) i->y++;
/* if the end of the current 'column' is reached,
and it is not the end of exploration,
advance to the next 'column' */
while( (double) (i->y) > i->ye && !ri_end(i) )
{
/* increase x, next 'column' */
i->x++;
/* if end of exploration, return */
if( ri_end(i) ) return;
/* update lower y limit (start) for the new 'column'.
We need to interpolate the y value that corresponds to the
lower side of the rectangle. The first thing is to decide if
the corresponding side is
vx[0],vy[0] to vx[3],vy[3] or
vx[3],vy[3] to vx[2],vy[2]
Then, the side is interpolated for the x value of the
'column'. But, if the side is vertical (as it could happen if
the rectangle is vertical and we are dealing with the first
or last 'columns') then we pick the lower value of the side
by using 'inter_low'.
*/
if( (double) i->x < i->vx[3] )
i->ys = inter_low((double)i->x,i->vx[0],i->vy[0],i->vx[3],i->vy[3]);
else
i->ys = inter_low((double)i->x,i->vx[3],i->vy[3],i->vx[2],i->vy[2]);
/* update upper y limit (end) for the new 'column'.
We need to interpolate the y value that corresponds to the
upper side of the rectangle. The first thing is to decide if
the corresponding side is
vx[0],vy[0] to vx[1],vy[1] or
vx[1],vy[1] to vx[2],vy[2]
Then, the side is interpolated for the x value of the
'column'. But, if the side is vertical (as it could happen if
the rectangle is vertical and we are dealing with the first
or last 'columns') then we pick the lower value of the side
by using 'inter_low'.
*/
if( (double)i->x < i->vx[1] )
i->ye = inter_hi((double)i->x,i->vx[0],i->vy[0],i->vx[1],i->vy[1]);
else
i->ye = inter_hi((double)i->x,i->vx[1],i->vy[1],i->vx[2],i->vy[2]);
/* new y */
i->y = (int) ceil(i->ys);
}
}
/*----------------------------------------------------------------------------*/
/** Create and initialize a rectangle iterator.
See details in \ref rect_iter
*/
rect_iter * ri_ini(struct rect * r)
{
double vx[4],vy[4];
int n,offset;
rect_iter * i;
/* check parameters */
if( r == NULL ) error("ri_ini: invalid rectangle.");
/* get memory */
i = (rect_iter *) malloc(sizeof(rect_iter));
if( i == NULL ) error("ri_ini: Not enough memory.");
/* build list of rectangle corners ordered
in a circular way around the rectangle */
//从线段的起点(x1,y1)处的一端开始按照逆时针重构出矩形的四个定点
vx[0] = r->x1 - r->dy * r->width / 2.0;
vy[0] = r->y1 + r->dx * r->width / 2.0;
vx[1] = r->x2 - r->dy * r->width / 2.0;
vy[1] = r->y2 + r->dx * r->width / 2.0;
vx[2] = r->x2 + r->dy * r->width / 2.0;
vy[2] = r->y2 - r->dx * r->width / 2.0;
vx[3] = r->x1 + r->dy * r->width / 2.0;
vy[3] = r->y1 - r->dx * r->width / 2.0;
/* compute rotation of index of corners needed so that the first
point has the smaller x.
if one side is vertical, thus two corners have the same smaller x
value, the one with the largest y value is selected as the first.
*/
if( r->x1 < r->x2 && r->y1 <= r->y2 ) offset = 0;
else if( r->x1 >= r->x2 && r->y1 < r->y2 ) offset = 1;
else if( r->x1 > r->x2 && r->y1 >= r->y2 ) offset = 2;
else offset = 3;
/* apply rotation of index. */
for(n=0; n<4; n++)
{
i->vx[n] = vx[(offset+n)%4];
i->vy[n] = vy[(offset+n)%4];
}
/* Set an initial condition.
The values are set to values that will cause 'ri_inc' (that will
be called immediately) to initialize correctly the first 'column'
and compute the limits 'ys' and 'ye'.
'y' is set to the integer value of vy[0], the starting corner.
'ys' and 'ye' are set to very small values, so 'ri_inc' will
notice that it needs to start a new 'column'.
The smallest integer coordinate inside of the rectangle is
'ceil(vx[0])'. The current 'x' value is set to that value minus
one, so 'ri_inc' (that will increase x by one) will advance to
the first 'column'.
*/
i->x = (int) ceil(i->vx[0]) - 1;
i->y = (int) ceil(i->vy[0]);
i->ys = i->ye = -DBL_MAX;
/* advance to the first pixel */
ri_inc(i);
return i;
}
/*----------------------------------------------------------------------------*/
/** Free memory used in image_double 'i'.
*/
void free_image_double(image_double i)
{
if( i == NULL || i->data == NULL )
error("free_image_double: invalid input image.");
free( (void *) i->data );
free( (void *) i );
}
/*----------------------------------------------------------------------------*/
/** Create a new image_double of size 'xsize' times 'ysize'.
*/
image_double new_image_double(int xsize, int ysize)
{
image_double image;
/* check parameters */
if( xsize == 0 || ysize == 0 ) error("new_image_double: invalid image size.");
/* get memory */
image = (image_double) malloc( sizeof(struct image_double_s) );
if( image == NULL ) error("not enough memory.");
image->data = (double *) calloc( (size_t) (xsize*ysize), sizeof(double) );
if( image->data == NULL ) error("not enough memory.");
/* set image size */
image->xsize = xsize;
image->ysize = ysize;
return image;
}
/*----------------------------------------------------------------------------*/
/** Create a new image_double of size 'xsize' times 'ysize'
with the data pointed by 'data'.
*/
image_double new_image_double_ptr( int xsize,
int ysize, double * data )
{
image_double image;
/* check parameters */
if( xsize == 0 || ysize == 0 )
error("new_image_double_ptr: invalid image size.");
if( data == NULL ) error("new_image_double_ptr: NULL data pointer.");
/* get memory */
image = (image_double) malloc( sizeof(struct image_double_s) );
if( image == NULL ) error("not enough memory.");
/* set image */
image->xsize = xsize;
image->ysize = ysize;
image->data = data;
return image;
}
//=================================================================================================================
//===========================================LSD functions=========================================================
/** ln(10) */
#ifndef M_LN10
#define M_LN10 2.30258509299404568402 //ln10
#endif /* !M_LN10 */
/** Label for pixels not used in yet. */
#define NOTUSED 0
/** Label for pixels already used in detection. */
#define USED 1
//对于构成圆弧的像素标记极性,如果梯度的方向和弧的方向指向一致,则为SAME_POLE,否则为OPP_POLE,该标记初始是为0
#define NOTDEF_POL 0
#define SAME_POL 1
#define OPP_POL -1
/*----------------------------------------------------------------------------*/
/** Chained list of coordinates.
*/
struct coorlist
{
int x,y;
struct coorlist * next;
};
typedef struct ntuple_list_s
{
int size;
int max_size;
int dim;
double * values;
} * ntuple_list;
/*----------------------------------------------------------------------------*/
/** Free memory used in n-tuple 'in'.
*/
static void free_ntuple_list(ntuple_list in)
{
if( in == NULL || in->values == NULL )
error("free_ntuple_list: invalid n-tuple input.");
free( (void *) in->values );
free( (void *) in );
}
/*----------------------------------------------------------------------------*/
/** Create an n-tuple list and allocate memory for one element.
@param dim the dimension (n) of the n-tuple.
*/
static ntuple_list new_ntuple_list(int dim)
{
ntuple_list n_tuple;
/* check parameters */
if( dim == 0 ) error("new_ntuple_list: 'dim' must be positive.");
/* get memory for list structure */
n_tuple = (ntuple_list) malloc( sizeof(struct ntuple_list_s) );
if( n_tuple == NULL ) error("not enough memory.");
/* initialize list */
n_tuple->size = 0;
n_tuple->max_size = 1;
n_tuple->dim = dim;
/* get memory for tuples */
n_tuple->values = (double *) malloc( dim*n_tuple->max_size * sizeof(double) );
if( n_tuple->values == NULL ) error("not enough memory.");
return n_tuple;
}
/*----------------------------------------------------------------------------*/
/** Enlarge the allocated memory of an n-tuple list.
*/
static void enlarge_ntuple_list(ntuple_list n_tuple)
{
/* check parameters */
if( n_tuple == NULL || n_tuple->values == NULL || n_tuple->max_size == 0 )
error("enlarge_ntuple_list: invalid n-tuple.");
/* duplicate number of tuples */
n_tuple->max_size *= 2;
/* realloc memory */
n_tuple->values = (double *) realloc( (void *) n_tuple->values,
n_tuple->dim * n_tuple->max_size * sizeof(double) );
if( n_tuple->values == NULL ) error("not enough memory.");
}
/*----------------------------------------------------------------------------*/
/** Add a 7-tuple to an n-tuple list.
*/
static void add_7tuple( ntuple_list out, double v1, double v2, double v3,
double v4, double v5, double v6, double v7 )
{
/* check parameters */
if( out == NULL ) error("add_7tuple: invalid n-tuple input.");
if( out->dim != 7 ) error("add_7tuple: the n-tuple must be a 7-tuple.");
/* if needed, alloc more tuples to 'out' */
if( out->size == out->max_size ) enlarge_ntuple_list(out);
if( out->values == NULL ) error("add_7tuple: invalid n-tuple input.");
/* add new 7-tuple */
out->values[ out->size * out->dim + 0 ] = v1;
out->values[ out->size * out->dim + 1 ] = v2;
out->values[ out->size * out->dim + 2 ] = v3;
out->values[ out->size * out->dim + 3 ] = v4;
out->values[ out->size * out->dim + 4 ] = v5;
out->values[ out->size * out->dim + 5 ] = v6;
out->values[ out->size * out->dim + 6 ] = v7;
/* update number of tuples counter */
out->size++;
}
/*----------------------------------------------------------------------------*/
/** Add a 8-tuple to an n-tuple list.
*/
static void add_8tuple( ntuple_list out, double v1, double v2, double v3,
double v4, double v5, double v6, double v7, int v8)
{
/* check parameters */
if( out == NULL ) error("add_8tuple: invalid n-tuple input.");
if( out->dim != 8 ) error("add_8tuple: the n-tuple must be a 8-tuple.");
/* if needed, alloc more tuples to 'out' */
if( out->size == out->max_size ) enlarge_ntuple_list(out);
if( out->values == NULL ) error("add_8tuple: invalid n-tuple input.");
/* add new 8-tuple */
out->values[ out->size * out->dim + 0 ] = v1;
out->values[ out->size * out->dim + 1 ] = v2;
out->values[ out->size * out->dim + 2 ] = v3;
out->values[ out->size * out->dim + 3 ] = v4;
out->values[ out->size * out->dim + 4 ] = v5;
out->values[ out->size * out->dim + 5 ] = v6;
out->values[ out->size * out->dim + 6 ] = v7;
out->values[ out->size * out->dim + 7 ] = v8;
/* update number of tuples counter */
out->size++;
}
/** char image data type
The pixel value at (x,y) is accessed by:
image->data[ x + y * image->xsize ]
with x and y integer.
*/
typedef struct image_char_s
{
unsigned char * data;
unsigned int xsize,ysize;
} * image_char;
/*----------------------------------------------------------------------------*/
/** Free memory used in image_char 'i'.
*/
static void free_image_char(image_char i)
{
if( i == NULL || i->data == NULL )
error("free_image_char: invalid input image.");
free( (void *) i->data );
free( (void *) i );
}
/*----------------------------------------------------------------------------*/
/** Create a new image_char of size 'xsize' times 'ysize'.
*/
static image_char new_image_char(unsigned int xsize, unsigned int ysize)
{
image_char image;
/* check parameters */
if( xsize == 0 || ysize == 0 ) error("new_image_char: invalid image size.");
/* get memory */
image = (image_char) malloc( sizeof(struct image_char_s) );
if( image == NULL ) error("not enough memory.");
image->data = (unsigned char *) calloc( (size_t) (xsize*ysize),
sizeof(unsigned char) );
if( image->data == NULL ) error("not enough memory.");
/* set image size */
image->xsize = xsize;
image->ysize = ysize;
return image;
}
/*----------------------------------------------------------------------------*/
/** Create a new image_char of size 'xsize' times 'ysize',
initialized to the value 'fill_value'.
*/
static image_char new_image_char_ini( unsigned int xsize, unsigned int ysize,
unsigned char fill_value )
{
image_char image = new_image_char(xsize,ysize); /* create image */
unsigned int N = xsize*ysize;
unsigned int i;
/* check parameters */
if( image == NULL || image->data == NULL )
error("new_image_char_ini: invalid image.");
/* initialize */
for(i=0; i<N; i++) image->data[i] = fill_value;
return image;
}
/*----------------------------------------------------------------------------*/
/** int image data type
The pixel value at (x,y) is accessed by:
image->data[ x + y * image->xsize ]
with x and y integer.
*/
typedef struct image_int_s
{
int * data;
unsigned int xsize,ysize;
} * image_int;
/*----------------------------------------------------------------------------*/
/** Create a new image_int of size 'xsize' times 'ysize'.
*/
static image_int new_image_int(unsigned int xsize, unsigned int ysize)
{
image_int image;
/* check parameters */
if( xsize == 0 || ysize == 0 ) error("new_image_int: invalid image size.");
/* get memory */
image = (image_int) malloc( sizeof(struct image_int_s) );
if( image == NULL ) error("not enough memory.");
image->data = (int *) calloc( (size_t) (xsize*ysize), sizeof(int) );
if( image->data == NULL ) error("not enough memory.");
/* set image size */
image->xsize = xsize;
image->ysize = ysize;
return image;
}
/*----------------------------------------------------------------------------*/
/** Create a new image_int of size 'xsize' times 'ysize',
initialized to the value 'fill_value'.
*/
static image_int new_image_int_ini( unsigned int xsize, unsigned int ysize,
int fill_value )
{
image_int image = new_image_int(xsize,ysize); /* create image */
unsigned int N = xsize*ysize;
unsigned int i;
/* initialize */
for(i=0; i<N; i++) image->data[i] = fill_value;
return image;
}
/** Compute a Gaussian kernel of length 'kernel->dim',
standard deviation 'sigma', and centered at value 'mean'.
For example, if mean=0.5, the Gaussian will be centered
in the middle point2i between values 'kernel->values[0]'
and 'kernel->values[1]'.
*/
static void gaussian_kernel(ntuple_list kernel, double sigma, double mean)
{
double sum = 0.0;
double val;
int i;
/* check parameters */
if( kernel == NULL || kernel->values == NULL )
error("gaussian_kernel: invalid n-tuple 'kernel'.");
if( sigma <= 0.0 ) error("gaussian_kernel: 'sigma' must be positive.");
/* compute Gaussian kernel */
if( kernel->max_size < 1 ) enlarge_ntuple_list(kernel);
kernel->size = 1;
for(i=0;i<kernel->dim;i++)
{
val = ( (double) i - mean ) / sigma;
kernel->values[i] = exp( -0.5 * val * val );
sum += kernel->values[i];
}
/* normalization */
if( sum >= 0.0 ) for(i=0;i<kernel->dim;i++) kernel->values[i] /= sum;
}
/*----------------------------------------------------------------------------*/
/** Scale the input image 'in' by a factor 'scale' by Gaussian sub-sampling.
For example, scale=0.8 will give a result at 80% of the original size.
The image is convolved with a Gaussian kernel
@f[
G(x,y) = \frac{1}{2\pi\sigma^2} e^{-\frac{x^2+y^2}{2\sigma^2}}
@f]
before the sub-sampling to prevent aliasing.
The standard deviation sigma given by:
- sigma = sigma_scale / scale, if scale < 1.0
- sigma = sigma_scale, if scale >= 1.0
To be able to sub-sample at non-integer steps, some interpolation
is needed. In this implementation, the interpolation is done by
the Gaussian kernel, so both operations (filtering and sampling)
are done at the same time. The Gaussian kernel is computed
centered on the coordinates of the required sample. In this way,
when applied, it gives directly the result of convolving the image
with the kernel and interpolated to that particular position.
A fast algorithm is done using the separability of the Gaussian
kernel. Applying the 2D Gaussian kernel is equivalent to applying
first a horizontal 1D Gaussian kernel and then a vertical 1D
Gaussian kernel (or the other way round). The reason is that
@f[
G(x,y) = G(x) * G(y)
@f]
where
@f[
G(x) = \frac{1}{\sqrt{2\pi}\sigma} e^{-\frac{x^2}{2\sigma^2}}.
@f]
The algorithm first applies a combined Gaussian kernel and sampling
in the x axis, and then the combined Gaussian kernel and sampling
in the y axis.
*/
static image_double gaussian_sampler( image_double in, double scale,
double sigma_scale )
{
image_double aux,out;
ntuple_list kernel;
int N,M,h,n,x,y,i;
int xc,yc,j,double_x_size,double_y_size;
double sigma,xx,yy,sum,prec;
/* check parameters */
if( in == NULL || in->data == NULL || in->xsize == 0 || in->ysize == 0 )
error("gaussian_sampler: invalid image.");
if( scale <= 0.0 ) error("gaussian_sampler: 'scale' must be positive.");
if( sigma_scale <= 0.0 )
error("gaussian_sampler: 'sigma_scale' must be positive.");
/* compute new image size and get memory for images */
if( in->xsize * scale > (double) UINT_MAX ||
in->ysize * scale > (double) UINT_MAX )
error("gaussian_sampler: the output image size exceeds the handled size.");
N = (unsigned int) ceil( in->xsize * scale );//上取整
M = (unsigned int) ceil( in->ysize * scale );
aux = new_image_double(N,in->ysize);
out = new_image_double(N,M);
/* sigma, kernel size and memory for the kernel */
sigma = scale < 1.0 ? sigma_scale / scale : sigma_scale;
/*
The size of the kernel is selected to guarantee that the
the first discarded term is at least 10^prec times smaller
than the central value. For that, h should be larger than x, with
e^(-x^2/2sigma^2) = 1/10^prec.
Then,
x = sigma * sqrt( 2 * prec * ln(10) ).
*/
prec = 3.0;//高斯核的最外围降到10^(-3)
h = (unsigned int) ceil( sigma * sqrt( 2.0 * prec * log(10.0) ) );
n = 1+2*h; /* kernel size */
kernel = new_ntuple_list(n);
/* auxiliary double image size variables */
double_x_size = (int) (2 * in->xsize);
double_y_size = (int) (2 * in->ysize);
/* First subsampling: x axis */
for(x=0;x<aux->xsize;x++)
{
/*
x is the coordinate in the new image.
xx is the corresponding x-value in the original size image.
xc is the integer value, the pixel coordinate of xx.
*/
xx = (double) x / scale;
/* coordinate (0.0,0.0) is in the center of pixel (0,0),
so the pixel with xc=0 get the values of xx from -0.5 to 0.5 */
xc = (int) floor( xx + 0.5 );
gaussian_kernel( kernel, sigma, (double) h + xx - (double) xc );
/* the kernel must be computed for each x because the fine
offset xx-xc is different in each case */
for(y=0;y<aux->ysize;y++)
{
sum = 0.0;
for(i=0;i<kernel->dim;i++)
{
j = xc - h + i;
/* symmetry boundary condition */
while( j < 0 ) j += double_x_size;
while( j >= double_x_size ) j -= double_x_size;
if( j >= (int) in->xsize ) j = double_x_size-1-j;
sum += in->data[ j + y * in->xsize ] * kernel->values[i];
}
aux->data[ x + y * aux->xsize ] = sum;
}
}
/* Second subsampling: y axis */
for(y=0;y<out->ysize;y++)
{
/*
y is the coordinate in the new image.
yy is the corresponding x-value in the original size image.
yc is the integer value, the pixel coordinate of xx.
*/
yy = (double) y / scale;
/* coordinate (0.0,0.0) is in the center of pixel (0,0),
so the pixel with yc=0 get the values of yy from -0.5 to 0.5 */
yc = (int) floor( yy + 0.5 );
gaussian_kernel( kernel, sigma, (double) h + yy - (double) yc );
/* the kernel must be computed for each y because the fine
offset yy-yc is different in each case */
for(x=0;x<out->xsize;x++)
{
sum = 0.0;
for(i=0;i<kernel->dim;i++)
{
j = yc - h + i;
/* symmetry boundary condition */
while( j < 0 ) j += double_y_size;
while( j >= double_y_size ) j -= double_y_size;
if( j >= (int) in->ysize ) j = double_y_size-1-j;
sum += aux->data[ x + j * aux->xsize ] * kernel->values[i];
}
out->data[ x + y * out->xsize ] = sum;
}
}
/* free memory */
free_ntuple_list(kernel);
free_image_double(aux);
return out;
}
/*----------------------------------------------------------------------------*/
/*--------------------------------- Gradient ---------------------------------*/
/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/** Computes the direction of the level line of 'in' at each point2i.
The result is:
- an image_double with the angle at each pixel, or NOTDEF if not defined.
- the image_double 'modgrad' (a point2ier is passed as argument)
with the gradient magnitude at each point2i.
- a list of pixels 'list_p' roughly ordered by decreasing
gradient magnitude. (The order is made by classifying point2is
into bins by gradient magnitude. The parameters 'n_bins' and
'max_grad' specify the number of bins and the gradient modulus
at the highest bin. The pixels in the list would be in
decreasing gradient magnitude, up to a precision of the size of
the bins.)
- a point2ier 'mem_p' to the memory used by 'list_p' to be able to
free the memory when it is not used anymore.
*/
//返回一张梯度角度顺时针旋转90°后的align角度图angles,如果梯度角度是(gx,gy)->(-gy,gx),
//和梯度的模的图modgrad,然后按照n_bins进行伪排序返回链表的头指针list_p,里面存的是坐标
static image_double ll_angle( image_double in, double threshold,
struct coorlist ** list_p,
image_double * modgrad, unsigned int n_bins )
{
image_double g;
unsigned int n,p,x,y,adr,i;