-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclsd.c
executable file
·7393 lines (6384 loc) · 247 KB
/
clsd.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
/*----------------------------------------------------------------------------
CLSD - Conditional line segment detection, using detections on prior noise models to inform observational detections, assuming non-uniformity of background noise.
This code is with the publication below:
[TO BE ADDED]
*NOTICE: This program is modified from the source code of LSDSAR:
*"LSDSAR, a Markovian a contrario framework for line segment detection in SAR images"
*by Chenguang Liu, Rémy Abergel, Yann Gousseau and Florence Tupin.
*(Pattern Recognition, 2019).
*https://doi.org/10.1016/j.patcog.2019.107034
*Date of Modification: October 8, 2020.
*NOTICE: This program is released under [INSERT LICENSING STATEMENT FROM GIT]
--------------------------------------------------------------------------------------------
*NOTICE: This program is modified from the source code of LSDSAR:
*"LSDSAR, a Markovian a contrario framework for line segment detection in SAR images"
*by Chenguang Liu, Rémy Abergel, Yann Gousseau and Florence Tupin.
*(Pattern Recognition, 2019).
*https://doi.org/10.1016/j.patcog.2019.107034
*Date of Modification: October 8, 2020.
Below, unmarked numbers indicate significantly new functions, otherwise:
* Edited from 'double * LineSegmentDetection'
** Edited from similarly-named 'double * lsd', 'lsd_scale', 'lsd_scale_region'
*** Edited from 'static void add-7tuple'
Major modifications are:
1) double * CLSD_Pipeline
2*) double * Conditinal_LineSegmentDetection
3*) static void make_markov
4) double * surf_grid
5) double * sort_list
Minor modifications are:
6**) double * c_lsd
7**) double * c_lsd_scale
8**) double * c_lsd_scale_region
9***) static void add_4tuple
10) struct sortstr
11) int cmp
12) static double line_angle
13) double calc_mean
14) double calc_std
New Python wrapper functions are:
15) PyObject * clsd( PyObject * self, PyObject * args)
16) static PyMethodDef clsdMethods[]
17) static struct PyModuleDef moduledef
18) PyMODINIT_FUNC PyInit_clsd(void)
The Python interface requires building the program using 'setup.py'
to link against the 'gsl' and 'mir' libraries for conditional interpolation.
Building has been confirmed for Python 3.7.6 on Linux 5.4.0-48
The other functions of the code are kept unchanged.
Benjamin Miller
The University of Texas at Austin
Email: benjamin.g.miller@utexas.edu
*/
/*
*****************************************************************************
*****************************************************************************
**Here is the header file of the original LSDSAR.
**-------------------------------------------------------------------------------------------------------
**----------------------------------------------------------------------------
LSDSAR-line segment detector for SAR images.
This code is with the publication below:
"LSDSAR, a Markovian a contrario framework for line segment detection in SAR images",
by Chenguang Liu, Rémy Abergel, Yann Gousseau and Florence Tupin. (Pattern Recognition, 2019).
*NOTICE: This program is modified from the source code of LSD:
*"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
*Date of Modification: 27/06/2018.
*NOTICE: This program is released under GNU Affero General Public License
*and any conditions added under section 7 in the link:
*https://www.gnu.org/licenses/agpl-3.0.en.html
Copyright (c) 2017, 2018 Chenguang Liu <chenguangl@whu.edu.cn>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU 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/>.
--------------------------------------------------------------------------------------------
*NOTICE: This code is modified from the source code of LSD:
*"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
*Date of Modification: 27/06/2018.
The modifications lie in functions:
1) double * lsd(int * n_out, double * img, int X, int Y),
2) double * lsd_scale(int * n_out, double * img, int X, int Y, double scale),
3) double * lsd_scale_region( int * n_out,
double * img, int X, int Y, double scale,
int ** reg_img, int * reg_x, int * reg_y ),
4)double * LineSegmentDetection( int * n_out,
double * img, int X, int Y,
double scale, double sigma_scale, double quant,
double ang_th, double log_eps, double density_th,
int n_bins,
int ** reg_img, int * reg_x, int * reg_y ),
5) static image_double ll_angle( image_double in, double threshold,
struct coorlist ** list_p, void ** mem_p,
image_double * modgrad, unsigned int n_bins ),
6) static int refine( struct point * reg, int * reg_size, image_double modgrad,
double reg_angle, double prec, double p, struct rect * rec,
image_char used, image_double angles, double density_th ),
7) static int reduce_region_radius( struct point * reg, int * reg_size,
image_double modgrad, double reg_angle,
double prec, double p, struct rect * rec,
image_char used, image_double angles,
double density_th ),
8) static double rect_improve( struct rect * rec, image_double angles,
double logNT, double log_eps ),
9) static double rect_nfa(struct rect * rec, image_double angles, double logNT),
10) static double nfa(int n, int k, double p, double logNT).
The other functions of the code are kept unchanged.
I would be grateful to receive any advices or possible erros in the source code.
Chenguang Liu
Telecom ParisTech
Email: chenguang.liu@telecom-paristech.fr
Email: chenguangl@whu.edu.cn (permanent)
*/
/*
*****************************************************************************
*****************************************************************************
**Here is the header file of the original LSD.
**-------------------------------------------------------------------------------------------------------
**-------------------------------------------------------------------------------------------------------
**
** 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 <grompone@gmail.com>
**
** 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/>.
**
** ----------------------------------------------------------------------------*/
//Headers for LSD
#include <Python.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <limits.h>
#include <float.h>
#include<string.h>
#include<gsl/gsl_eigen.h>
#include<gsl/gsl_randist.h>
#include<gsl/gsl_rng.h>
#include<gsl/gsl_qrng.h>
#include<mir/mir.h>
#include<sys/mman.h>
#include "clsd.h"
//Header for interpolant
/** 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 */
#define RADIANS_TO_DEGREES (180.0/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.
*/
int max1(int x, int y)
{if(x<y)
return y;
else
return x;}
int min1(int x, int y)
{return x<y?x:y;}
struct coorlist
{
int x,y;
struct coorlist * next;
};
struct coorlist3
{
int x,y,z;
struct coorlist3 * next;
};
/*----------------------------------------------------------------------------*/
/** A point (or pixel).
*/
struct point {int x,y;};
struct point3 {int x,y,z;};
/*----------------------------------------------------------------------------*/
/*------------------------- 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) );
}
static double dist3(double x1, double y1, double z1, double x2, double y2, double z2)
{
return sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) + (z2-z1)*(z2-z1) );
}
/*----------------------------------------------------------------------------*/
/* Orientation of a line*/
static double line_angle(double x1, double y1, double x2, double y2)
{
return atan2( y2-y1 , x2-x1 );
}
/*[azimuth,elevation,quaterion] structure
* For loop structure, one of these may have its az/el data incrementally changed,
* and fed to calc_quat below*/
typedef struct angles3_s
{
double az,el;
double quat[4];
} * angles3;
/*Given an pointer to a angles3 structure with az/el data, compute its quaternion*/
static void calc_quat(angles3 * angles)
{
//double * quat = (double*) malloc(4*sizeof(double));
//printf("\t\t\t\t\t Access angles...\n");
fflush(stdout);
/*
double quat[4];
double az = (double) (*angles) -> az;
double el = (double) (*angles) -> el;
//azimuth==yaw, pitch==elevation, roll==0
//printf("\t\t\t\t\t Calculate...\n");
//fflush(stdout);
double cy = cos(az/2.);
double sy = sin(az/2.);
double cp = cos(el/2.);
double sp = sin(el/2.);
double cr = 1;//cos(0);
double sr = 0;//sin(0);
quat[0] = cr * cp * cy + sr * sp * sy;
quat[1] = sr * cp * cy - cr * sp * sy;
quat[2] = cr * sp * cy + sr * cp * sy;
quat[3] = cr * cp * sy - sr * sp * cy;
//printf("\t\t\t\t\t Storage...\n");
//fflush(stdout);
for(int i=0;i<4;i++)
(*angles)->quat[i] = (double)quat[i];
*/
}
/** Create a new image_double of size 'xsize' times 'ysize'.
*/
static angles3 new_angles3(double az, double el)
{
angles3 image;
/* get memory */
image = (angles3) malloc( sizeof(struct angles3_s) );
if( image == NULL ) error("not enough memory.");
image->az = az;
image->el = el;
calc_quat(&image);
return image;
}
/*----------------------------------------------------------------------------*/
/** Free memory used in image_double 'i'.
*/
static void free_angles3(angles3 i)
{
if( i == NULL)
error("free_angles3: invalid input image.");
free( (void *) i );
}
/*Given line endpoints, return a structure [azimuth, elevation, quaternion] */
static angles3 line_angle3(double x1, double y1, double z1,
double x2, double y2, double z2)
{
double az = line_angle(x1,y1,x2,y2);
double el = acos((z2-z1)/dist3(x1,y1,z1,x2,y2,z2));
angles3 azel=new_angles3(az,el);
//azel->az = az;
//azel->el = el;
//calc_quat(&azel);
return azel;
}
/*----------------------------------------------------------------------------*/
/** 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;
}
/*----------------------------------------------------------------------------*/
/** Unsigned angle difference, -pi/2,pi/2, orthogonality at 0
*/
static double quat_diff(angles3 a, angles3 b)
{
//printf("\t\t\t\t\t Compute quaternions: az %.2f, el %.2f, quat [%.2f,%.2f,%.2f,%.2f]...\n",a->az,a->el,a->quat[0],a->quat[1],a->quat[2],a->quat[3]);
fflush(stdout);
if(a->quat==NULL) calc_quat(&a);
if(b->quat==NULL) calc_quat(&b);
//calc_quat(&a);
//calc_quat(&b);
double dot=0.;
double dl1[3] = {cos(a->az)*sin(a->el), sin(a->az)*sin(a->el),cos(a->el)};
double dl2[3] = {cos(b->az)*sin(b->el), sin(b->az)*sin(b->el),cos(b->el)};
for(int i=0;i<3;i++) dot+=dl1[i]*dl2[i];
//dot = sin(a->el)*sin(b->el)*cos(a->az - b->az) + cos(a->el)*cos(b->el);
//printf("\t\t\t\t\t Compute metric...\n");
//fflush(stdout);
//for(int i=0;i<4;i++) dot+=(a->quat[i])*(b->quat[i]);
//printf("\t\t\t\t\t %.2f\n",fabs(dot));
//fflush(stdout);
return fabs(dot);
}
static double quat_diff_signed(angles3 a, angles3 b)
{
fflush(stdout);
if(a->quat==NULL) calc_quat(&a);
if(b->quat==NULL) calc_quat(&b);
double dot=0.;
double dl1[3] = {cos(a->az)*sin(a->el), sin(a->az)*sin(a->el),cos(a->el)};
double dl2[3] = {cos(b->az)*sin(b->el), sin(b->az)*sin(b->el),cos(b->el)};
for(int i=0;i<3;i++) dot+=dl1[i]*dl2[i];
//for(int i=0;i<4;i++) dot+=(a->quat[i])*(b->quat[i]);
return dot;
}
/*----------------------------------------------------------------------------*/
/* Parameter-wise mean calculation for ndim-by-nv vector of observationse*/
double calc_mean(double* xv, int nv, int ndim,int param)
{
double mean=0;
int i;
for(i=0;i<nv;i++){mean+=xv[i*ndim+param];}
mean/=nv;
return mean;
}
/*----------------------------------------------------------------------------*/
/* Parameter-wise deviation calculation for ndim-by-nv vector of observationse*/
double calc_std(double* xv, int nv, int ndim,int param)
{
double std=0;
int i;
double mean = calc_mean(xv,nv,ndim,param);
for(i=0;i<nv;i++){std+=pow(xv[i*ndim+param]-mean,2);}
return sqrt(std/nv);
}
/*----------------------------------------------------------------------------*/
/*----------------------- '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++;
}
static void add_10tuple( ntuple_list out, double v1, double v2, double v3,
double v4, double v5, double v6, double v7,
double v8, double v9, double v10)
{
/* check parameters */
if( out == NULL ) error("add_10tuple: invalid n-tuple input.");
if( out->dim != 10 ) error("add_10tuple: the n-tuple must be a 10-tuple.");
/* if needed, alloc more tuples to 'out' */
if( out->size == out->max_size ) enlarge_ntuple_list(out);
if( out->values == NULL ) error("add_10tuple: 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;
out->values[ out->size * out->dim + 7 ] = v8;
out->values[ out->size * out->dim + 8 ] = v9;
out->values[ out->size * out->dim + 9 ] = v10;
/* update number of tuples counter */
out->size++;
}
/*----------------------------------------------------------------------------*/
/** Add a 4-tuple to an n-tuple list.
*/
static void add_4tuple( ntuple_list out, double v1, double v2, double v3, double v4 )
{
/* check parameters */
if( out == NULL ) error("add_4tuple: invalid n-tuple input.");
if( out->dim != 4 ) error("add_4tuple: 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_4tuple: 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;
/* 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 imagsavelines=zeros(size(lines,1),5);
for xx=1:size(lines,1)
savelines(xx,1:4)=lines(xx,1:4);
savelines(xx,5)=angleline(xx);
ende 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;
}
/*----------------------------------------------------------------------------*/
/*-------------------------- 3D 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 image3_char_s
{
unsigned char * data;
unsigned int xsize,ysize,zsize;
} * image3_char;
/*----------------------------------------------------------------------------*/
/** Free memory used in image_char 'i'.
*/
static void free_image3_char(image3_char i)
{
if( i == NULL || i->data == NULL )
error("free_image3_char: invalid input image.");
free( (void *) i->data );
free( (void *) i );
}
/*----------------------------------------------------------------------------*/
/** Create a new image_char of size 'xsize' times 'ysize'.
*/
static image3_char new_image3_char(unsigned int xsize, unsigned int ysize, unsigned int zsize)
{
image3_char image;
/* check parameters */
if( xsize == 0 || ysize == 0 || zsize == 0) error("new_image3_char: invalid image size.");
/* get memory */
image = (image3_char) malloc( sizeof(struct image3_char_s) );
if( image == NULL ) error("not enough memory.");
image->data = (unsigned char *) calloc( (size_t) (xsize*ysize*zsize),
sizeof(unsigned char) );
if( image->data == NULL ) error("not enough memory.");
/* set image size */
image->xsize = xsize;
image->ysize = ysize;
image->zsize = zsize;
return image;
}
/*----------------------------------------------------------------------------*/
/** Create a new image_char of size 'xsize' times 'ysize',
initialized to the value 'fill_value'.
*/
static image3_char new_image3_char_ini( unsigned int xsize, unsigned int ysize, unsigned int zsize,
unsigned char fill_value )
{
image3_char image = new_image3_char(xsize,ysize,zsize); /* create image */
unsigned int N = xsize*ysize*zsize;
unsigned int i;
/* check parameters */
if( image == NULL || image->data == NULL )
error("new_image3_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 image3_int_s
{
int * data;
unsigned int xsize,ysize,zsize;
} * image3_int;
/*----------------------------------------------------------------------------*/
/** Create a new image_int of size 'xsize' times 'ysize'.
*/
static image3_int new_image3_int(unsigned int xsize, unsigned int ysize, unsigned int zsize)
{
image3_int image;
/* check parameters */
if( xsize == 0 || ysize == 0 || zsize == 0 ) error("new_image3_int: invalid image size.");
/* get memory */
image = (image3_int) malloc( sizeof(struct image3_int_s) );
if( image == NULL ) error("not enough memory.");
image->data = (int *) calloc( (size_t) (xsize*ysize*zsize), sizeof(int) );
if( image->data == NULL ) error("not enough memory.");
/* set imagsavelines=zeros(size(lines,1),5);
for xx=1:size(lines,1)
savelines(xx,1:4)=lines(xx,1:4);
savelines(xx,5)=angleline(xx);
ende size */
image->xsize = xsize;