-
Notifications
You must be signed in to change notification settings - Fork 5
/
lsd.c
2250 lines (1884 loc) · 76.3 KB
/
lsd.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
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
/*----------------------------------------------------------------------------
LSD - Line Segment Detector on digital images
This code is part of the following publication and was subject
to peer review:
"LSD: a Line Segment Detector" by Rafael Grompone von Gioi,
Jeremie Jakubowicz, Jean-Michel Morel, and Gregory Randall,
Image Processing On Line, 2012. DOI:10.5201/ipol.2012.gjmr-lsd
http://dx.doi.org/10.5201/ipol.2012.gjmr-lsd
Copyright (c) 2007-2011 rafael grompone von gioi <[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/>.
----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/** @file lsd.c
LSD module code
@author rafael grompone von gioi <[email protected]>
*/
/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/** @mainpage LSD code documentation
This is an implementation of the Line Segment Detector described
in the paper:
"LSD: A Fast Line Segment Detector with a False Detection Control"
by Rafael Grompone von Gioi, Jeremie Jakubowicz, Jean-Michel Morel,
and Gregory Randall, IEEE Transactions on Pattern Analysis and
Machine Intelligence, vol. 32, no. 4, pp. 722-732, April, 2010.
and in more details in the CMLA Technical Report:
"LSD: A Line Segment Detector, Technical Report",
by Rafael Grompone von Gioi, Jeremie Jakubowicz, Jean-Michel Morel,
Gregory Randall, CMLA, ENS Cachan, 2010.
The version implemented here includes some further improvements
described in the following publication, of which this code is part:
"LSD: a Line Segment Detector" by Rafael Grompone von Gioi,
Jeremie Jakubowicz, Jean-Michel Morel, and Gregory Randall,
Image Processing On Line, 2012. DOI:10.5201/ipol.2012.gjmr-lsd
http://dx.doi.org/10.5201/ipol.2012.gjmr-lsd
The module's main function is lsd().
The source code is contained in two files: lsd.h and lsd.c.
HISTORY:
- version 1.6 - nov 2011:
- changes in the interface,
- max_grad parameter removed,
- the factor 11 was added to the number of test
to consider the different precision values
tested,
- a minor bug corrected in the gradient sorting
code,
- the algorithm now also returns p and log_nfa
for each detection,
- a minor bug was corrected in the image scaling,
- the angle comparison in "isaligned" changed
from < to <=,
- "eps" variable renamed "log_eps",
- "lsd_scale_region" interface was added,
- minor changes to comments.
- version 1.5 - dec 2010: Changes in 'refine', -W option added,
and more comments added.
- version 1.4 - jul 2010: lsd_scale interface added and doxygen doc.
- version 1.3 - feb 2010: Multiple bug correction and improved code.
- version 1.2 - dec 2009: First full Ansi C Language version.
- version 1.1 - sep 2009: Systematic subsampling to scale 0.8 and
correction to partially handle "angle problem".
- version 1.0 - jan 2009: First complete Megawave2 and Ansi C Language
version.
@author rafael grompone von gioi <[email protected]>
*/
/*----------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <limits.h>
#include <float.h>
#include "lsd.h"
/** ln(10) */
#ifndef M_LN10
#define M_LN10 2.30258509299404568402
#endif /* !M_LN10 */
/** PI */
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif /* !M_PI */
#ifndef FALSE
#define FALSE 0
#endif /* !FALSE */
#ifndef TRUE
#define TRUE 1
#endif /* !TRUE */
/** Label for pixels with undefined gradient. */
#define NOTDEF -1024.0
/** 3/2 pi */
#define M_3_2_PI 4.71238898038
/** 2 pi */
#define M_2__PI 6.28318530718
/** Label for pixels not used in yet. */
#define NOTUSED 0
/** Label for pixels already used in detection. */
#define USED 1
/*----------------------------------------------------------------------------*/
/** Chained list of coordinates.
*/
struct coorlist
{
int x,y;
struct coorlist * next;
};
/*----------------------------------------------------------------------------*/
/** A point (or pixel).
*/
struct point {int x,y;};
/*----------------------------------------------------------------------------*/
/*------------------------- Miscellaneous functions --------------------------*/
/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/** Fatal error, print a message to standard-error output and exit.
*/
static void error(char * msg)
{
fprintf(stderr,"LSD Error: %s\n",msg);
exit(EXIT_FAILURE);
}
/*----------------------------------------------------------------------------*/
/** Doubles relative error factor
*/
#define RELATIVE_ERROR_FACTOR 100.0
/*----------------------------------------------------------------------------*/
/** 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.
*/
static 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);
}
/*----------------------------------------------------------------------------*/
/** Computes Euclidean distance between point (x1,y1) and point (x2,y2).
*/
static double dist(double x1, double y1, double x2, double y2)
{
return sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) );
}
/*----------------------------------------------------------------------------*/
/*----------------------- 'list of n-tuple' data type ------------------------*/
/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/** 'list of n-tuple' data type
The i-th component of the j-th n-tuple of an n-tuple list 'ntl'
is accessed with:
ntl->values[ i + j * ntl->dim ]
The dimension of the n-tuple (n) is:
ntl->dim
The number of n-tuples in the list is:
ntl->size
The maximum number of n-tuples that can be stored in the
list with the allocated memory at a given time is given by:
ntl->max_size
*/
typedef struct ntuple_list_s
{
unsigned int size;
unsigned int max_size;
unsigned 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(unsigned 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++;
}
/*----------------------------------------------------------------------------*/
/*----------------------------- Image Data Types -----------------------------*/
/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/** 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;
}
/*----------------------------------------------------------------------------*/
/** double 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_double_s
{
double * data;
unsigned int xsize,ysize;
} * image_double;
/*----------------------------------------------------------------------------*/
/** Free memory used in image_double 'i'.
*/
static 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'.
*/
static image_double new_image_double(unsigned int xsize, unsigned 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'.
*/
static image_double new_image_double_ptr( unsigned int xsize,
unsigned 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;
}
/*----------------------------------------------------------------------------*/
/*----------------------------- Gaussian filter ------------------------------*/
/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/** 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 point 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;
unsigned 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;
unsigned 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;
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 point.
The result is:
- an image_double with the angle at each pixel, or NOTDEF if not defined.
- the image_double 'modgrad' (a pointer is passed as argument)
with the gradient magnitude at each point.
- a list of pixels 'list_p' roughly ordered by decreasing
gradient magnitude. (The order is made by classifying points
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 pointer 'mem_p' to the memory used by 'list_p' to be able to
free the memory when it is not used anymore.
*/
static image_double ll_angle( image_double in, double threshold,
struct coorlist ** list_p, void ** mem_p,
image_double * modgrad, unsigned int n_bins )
{
image_double g;
unsigned int n,p,x,y,adr,i;
double com1,com2,gx,gy,norm,norm2;
/* the rest of the variables are used for pseudo-ordering
the gradient magnitude values */
int list_count = 0;
struct coorlist * list;
struct coorlist ** range_l_s; /* array of pointers to start of bin list */
struct coorlist ** range_l_e; /* array of pointers to end of bin list */
struct coorlist * start;
struct coorlist * end;
double max_grad = 0.0;
/* check parameters */
if( in == NULL || in->data == NULL || in->xsize == 0 || in->ysize == 0 )
error("ll_angle: invalid image.");
if( threshold < 0.0 ) error("ll_angle: 'threshold' must be positive.");
if( list_p == NULL ) error("ll_angle: NULL pointer 'list_p'.");
if( mem_p == NULL ) error("ll_angle: NULL pointer 'mem_p'.");
if( modgrad == NULL ) error("ll_angle: NULL pointer 'modgrad'.");
if( n_bins == 0 ) error("ll_angle: 'n_bins' must be positive.");
/* image size shortcuts */
n = in->ysize;
p = in->xsize;
/* allocate output image */
g = new_image_double(in->xsize,in->ysize);
/* get memory for the image of gradient modulus */
*modgrad = new_image_double(in->xsize,in->ysize);
/* get memory for "ordered" list of pixels */
list = (struct coorlist *) calloc( (size_t) (n*p), sizeof(struct coorlist) );
*mem_p = (void *) list;
range_l_s = (struct coorlist **) calloc( (size_t) n_bins,
sizeof(struct coorlist *) );
range_l_e = (struct coorlist **) calloc( (size_t) n_bins,
sizeof(struct coorlist *) );
if( list == NULL || range_l_s == NULL || range_l_e == NULL )
error("not enough memory.");
for(i=0;i<n_bins;i++) range_l_s[i] = range_l_e[i] = NULL;
/* 'undefined' on the down and right boundaries */
for(x=0;x<p;x++) g->data[(n-1)*p+x] = NOTDEF;
for(y=0;y<n;y++) g->data[p*y+p-1] = NOTDEF;
/* compute gradient on the remaining pixels */
for(x=0;x<p-1;x++)
for(y=0;y<n-1;y++)
{
adr = y*p+x;
/*
Norm 2 computation using 2x2 pixel window:
A B
C D
and
com1 = D-A, com2 = B-C.
Then
gx = B+D - (A+C) horizontal difference
gy = C+D - (A+B) vertical difference
com1 and com2 are just to avoid 2 additions.
*/
com1 = in->data[adr+p+1] - in->data[adr];
com2 = in->data[adr+1] - in->data[adr+p];
gx = com1+com2; /* gradient x component */
gy = com1-com2; /* gradient y component */
norm2 = gx*gx+gy*gy;
norm = sqrt( norm2 / 4.0 ); /* gradient norm */
(*modgrad)->data[adr] = norm; /* store gradient norm */
if( norm <= threshold ) /* norm too small, gradient no defined */
g->data[adr] = NOTDEF; /* gradient angle not defined */
else
{
/* gradient angle computation */
g->data[adr] = atan2(gx,-gy);
/* look for the maximum of the gradient */
if( norm > max_grad ) max_grad = norm;
}
}
/* compute histogram of gradient values */
for(x=0;x<p-1;x++)
for(y=0;y<n-1;y++)
{
norm = (*modgrad)->data[y*p+x];
/* store the point in the right bin according to its norm */
i = (unsigned int) (norm * (double) n_bins / max_grad);
if( i >= n_bins ) i = n_bins-1;
if( range_l_e[i] == NULL )
range_l_s[i] = range_l_e[i] = list+list_count++;
else
{
range_l_e[i]->next = list+list_count;
range_l_e[i] = list+list_count++;
}
range_l_e[i]->x = (int) x;
range_l_e[i]->y = (int) y;
range_l_e[i]->next = NULL;
}
/* Make the list of pixels (almost) ordered by norm value.
It starts by the larger bin, so the list starts by the
pixels with the highest gradient value. Pixels would be ordered
by norm value, up to a precision given by max_grad/n_bins.
*/
for(i=n_bins-1; i>0 && range_l_s[i]==NULL; i--);
start = range_l_s[i];
end = range_l_e[i];
if( start != NULL )
while(i>0)
{
--i;
if( range_l_s[i] != NULL )
{
end->next = range_l_s[i];
end = range_l_e[i];
}
}
*list_p = start;
/* free memory */
free( (void *) range_l_s );
free( (void *) range_l_e );
return g;
}
/*----------------------------------------------------------------------------*/
/** Is point (x,y) aligned to angle theta, up to precision 'prec'?
*/
static int isaligned( int x, int y, image_double angles, double theta,
double prec )
{
double a;
/* check parameters */
if( angles == NULL || angles->data == NULL )
error("isaligned: invalid image 'angles'.");
if( x < 0 || y < 0 || x >= (int) angles->xsize || y >= (int) angles->ysize )
error("isaligned: (x,y) out of the image.");
if( prec < 0.0 ) error("isaligned: 'prec' must be positive.");
/* angle at pixel (x,y) */
a = angles->data[ x + y * angles->xsize ];
/* pixels whose level-line angle is not defined
are considered as NON-aligned */
if( a == NOTDEF ) return FALSE; /* there is no need to call the function
'double_equal' here because there is
no risk of problems related to the
comparison doubles, we are only
interested in the exact NOTDEF value */
/* it is assumed that 'theta' and 'a' are in the range [-pi,pi] */
theta -= a;
if( theta < 0.0 ) theta = -theta;
if( theta > M_3_2_PI )
{
theta -= M_2__PI;
if( theta < 0.0 ) theta = -theta;
}
return theta <= prec;
}
/*----------------------------------------------------------------------------*/
/** Absolute value angle difference.
*/
static 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.
*/
static 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;
}
/*----------------------------------------------------------------------------*/
/*----------------------------- NFA computation ------------------------------*/
/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/** Computes the natural logarithm of the absolute value of
the gamma function of x using the Lanczos approximation.
See http://www.rskey.org/gamma.htm
The formula used is
@f[
\Gamma(x) = \frac{ \sum_{n=0}^{N} q_n x^n }{ \Pi_{n=0}^{N} (x+n) }
(x+5.5)^{x+0.5} e^{-(x+5.5)}
@f]
so
@f[
\log\Gamma(x) = \log\left( \sum_{n=0}^{N} q_n x^n \right)
+ (x+0.5) \log(x+5.5) - (x+5.5) - \sum_{n=0}^{N} \log(x+n)
@f]
and
q0 = 75122.6331530,
q1 = 80916.6278952,
q2 = 36308.2951477,
q3 = 8687.24529705,
q4 = 1168.92649479,
q5 = 83.8676043424,
q6 = 2.50662827511.
*/
static double log_gamma_lanczos(double x)
{
static double q[7] = { 75122.6331530, 80916.6278952, 36308.2951477,
8687.24529705, 1168.92649479, 83.8676043424,
2.50662827511 };
double a = (x+0.5) * log(x+5.5) - (x+5.5);
double b = 0.0;
int n;
for(n=0;n<7;n++)
{
a -= log( x + (double) n );
b += q[n] * pow( x, (double) n );
}
return a + log(b);
}
/*----------------------------------------------------------------------------*/
/** Computes the natural logarithm of the absolute value of
the gamma function of x using Windschitl method.
See http://www.rskey.org/gamma.htm