-
Notifications
You must be signed in to change notification settings - Fork 3
/
link.c
1210 lines (1061 loc) · 51.9 KB
/
link.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
/*******************************************************************************
License:
This software was developed at the National Institute of Standards and
Technology (NIST) by employees of the Federal Government in the course
of their official duties. Pursuant to title 17 Section 105 of the
United States Code, this software is not subject to copyright protection
and is in the public domain. NIST assumes no responsibility whatsoever for
its use by other parties, and makes no guarantees, expressed or implied,
about its quality, reliability, or any other characteristic.
Disclaimer:
This software was developed to promote biometric standards and biometric
technology testing for the Federal Government in accordance with the USA
PATRIOT Act and the Enhanced Border Security and Visa Entry Reform Act.
Specific hardware and software products identified in this software were used
in order to perform the software development. In no case does such
identification imply recommendation or endorsement by the National Institute
of Standards and Technology, nor does it imply that the products and equipment
identified are necessarily the best available for the purpose.
*******************************************************************************/
/***********************************************************************
LIBRARY: LFS - NIST Latent Fingerprint System
FILE: LINK.C
AUTHOR: Michael D. Garris
DATE: 08/02/1999
UPDATED: 10/04/1999 Version 2 by MDG
UPDATED: 03/16/2005 by MDG
Contains routines responsible for linking compatible minutiae
together as part of the NIST Latent Fingerprint System (LFS).
***********************************************************************
ROUTINES:
link_minutiae()
create_link_table()
update_link_table()
order_link_table()
process_link_table()
link_score()
***********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include "lfs.h"
/*************************************************************************
**************************************************************************
#cat: link_minutiae - Clusters minutiae that are sufficiently close to each
#cat: other and have compatible directions to be considered part
#cat: of the same ridge or valley and then links them together.
#cat: In linking two minutia, the respective minutia features
#cat: in the image are joined by drawing pixels and the points
#cat: are removed from the list.
Input:
minutiae - list of true and false minutiae
bdata - binary image data (0==while & 1==black)
iw - width (in pixels) of image
ih - height (in pixels) of image
nmap - IMAP ridge flow matrix with invalid, high-curvature,
and no-valid-neighbor regions identified
mw - width in blocks of the NMAP
mh - height in blocks of the NMAP
lfsparms - parameters and thresholds for controlling LFS
Output:
minutiae - list of pruned minutiae
bdata - edited binary image with breaks in ridges and valleys filled
Return Code:
Zero - successful completion
Negative - system error
**************************************************************************/
int link_minutiae(MINUTIAE *minutiae,
unsigned char *bdata, const int iw, const int ih,
int *nmap, const int mw, const int mh,
const LFSPARMS *lfsparms)
{
int i, ret, *onloop;
MINUTIA *main_min; /* Renamed by MDG on 03-16-05 */
int main_x, main_y;
int *link_table, *x_axis, *y_axis, nx_axis, ny_axis, n_entries;
/* Go through the list of minutiae and detect any small loops (ex. */
/* < 15 pixels in circumference), and remove any minutiae */
/* (bifurcations) with bad contours. */
if((ret = get_loop_list(&onloop, minutiae, lfsparms->small_loop_len,
bdata, iw, ih)))
return(ret);
i = 0;
/* Foreach minutia treated as a "main" minutia */
/* (except for the very last one) ... */
while(i < minutiae->num-1){
/* Set current minutia to "main" minutia. */
main_min = minutiae->list[i];
main_x = main_min->x;
main_y = main_min->y;
/* If the main minutia is NOT on a small loop ... */
if(!onloop[i]){
/* Then minutia is a ridge-ending OR a bifurcation */
/* which is NOT to be skipped ... */
/* We now want to build a connected graph (link table) of */
/* all those minutiae sufficiently close and complementing */
/* each other that they potentially could be merged (linked) */
/* together. (Ex. table has max dimensions of 20). */
if((ret = create_link_table(&link_table, &x_axis, &y_axis,
&nx_axis, &ny_axis, &n_entries,
lfsparms->link_table_dim,
i, minutiae, onloop, nmap, mw, mh,
bdata, iw, ih, lfsparms))){
/* Deallocate working memory. */
free(onloop);
/* Return error code. */
return(ret);
}
/* Put the link table in sorted order based on x and then y-axis */
/* entries. These minutia are sorted based on their point of */
/* perpendicular intersection with a line running from the origin */
/* at an angle equal to the average direction of all entries in */
/* the link table. */
if((ret = order_link_table(link_table, x_axis, y_axis,
nx_axis, ny_axis, n_entries,
lfsparms->link_table_dim, minutiae,
lfsparms->num_directions))){
/* Deallocate working memories. */
free(link_table);
free(x_axis);
free(y_axis);
free(onloop);
/* Return error code. */
return(ret);
}
/* Process the link table deciding which minutia pairs in */
/* the table should be linked (ie. joined in the image and */
/* removed from the minutiae list (and from onloop). */
if((ret = process_link_table(link_table, x_axis, y_axis,
nx_axis, ny_axis, n_entries,
lfsparms->link_table_dim,
minutiae, onloop, bdata, iw, ih, lfsparms))){
/* Deallocate working memories. */
free(link_table);
free(x_axis);
free(y_axis);
free(onloop);
/* Return error code. */
return(ret);
}
/* Deallocate link table buffers. */
free(link_table);
free(x_axis);
free(y_axis);
}
/* Otherwise, skip minutia on a small loop. */
/* Check to see if the current "main" minutia has been removed */
/* from the minutiae list. If it has not, then we need to */
/* advance to the next minutia in the list. If it has been */
/* removed, then we are pointing to the next minutia already. */
if((minutiae->list[i]->x == main_x) &&
(minutiae->list[i]->y == main_y))
/* Advance to the next main feature in the list. */
i++;
/* At this point 'i' is pointing to the next main minutia to be */
/* processed, so continue. */
}
free(onloop);
return(0);
}
/*************************************************************************
**************************************************************************
#cat: create_link_table - Builds a 2D minutia link table where each cell in the
#cat: table represents a potential linking of 2 different
#cat: minutia points. Minutia IDs are stored on each axes
#cat: and scores representing the degree of compatibility
#cat: between 2 minutia are stored in each cell. Note that
#cat: the table is sparsely filled with scores.
Input:
tbldim - dimension of each axes of the link table
start - index position of starting minutia point in input list
minutiae - list of minutia
onloop - list of loop flags (on flag for each minutia point in list)
nmap - IMAP ridge flow matrix with invalid, high-curvature,
and no-valid-neighbor regions identified
mw - width in blocks of the NMAP
mh - height in blocks of the NMAP
bdata - binary image data (0==while & 1==black)
iw - width (in pixels) of image
ih - height (in pixels) of image
lfsparms - parameters and thresholds for controlling LFS
Output:
olink_table - sparse 2D table containing scores of potentially
linked minutia pairs
ox_axis - minutia IDs registered along x-axis
oy_axis - minutia IDs registered along y-axis
onx_axis - number of minutia registered along x-axis
ony_axis - number of minutia registered along y-axis
on_entries - number of scores currently entered in the table
Return Code:
Zero - successful completion
Negative - system error
**************************************************************************/
int create_link_table(int **olink_table, int **ox_axis, int **oy_axis,
int *onx_axis, int *ony_axis, int *on_entries, const int tbldim,
const int start, const MINUTIAE *minutiae, const int *onloop,
int *nmap, const int mw, const int mh,
unsigned char *bdata, const int iw, const int ih,
const LFSPARMS *lfsparms)
{
int ret, first, second;
int full_ndirs, qtr_ndirs, half_ndirs, low_curve_min_deltadir, deltadir;
int *link_table, *x_axis, *y_axis, nx_axis, ny_axis, n_entries, tblalloc;
int *queue, *inqueue, head, tail;
MINUTIA *minutia1, *minutia2;
int xblk, yblk;
int nmapval, opp1dir, joindir, iscore;
double jointheta, joindist, dscore;
/* Compute number of directions on full circle. */
full_ndirs = lfsparms->num_directions<<1;
/* Compute number of directions in 45=(180/4) degrees. */
qtr_ndirs = lfsparms->num_directions>>2;
/* Compute number of directions in 90=(180/2) degrees. */
half_ndirs = lfsparms->num_directions>>1;
/* Minimum allowable deltadir to link minutia in low-curvature region. */
/* (The closer the deltadir is to 180 degrees, the more likely the link. */
/* When ndirs==16, then this value is 11=(3*4)-1 == 123.75 degrees. */
/* I chose to parameterize this threshold based on a fixed fraction of */
/* 'ndirs' rather than on passing in a parameter in degrees and doing */
/* the conversion. I doubt the difference matters. */
low_curve_min_deltadir = (3 * qtr_ndirs) - 1;
/* Allocate and initialize link table buffers. */
/* Note: The use of "calloc" initializes all table values to 0. */
tblalloc = tbldim * tbldim;
link_table = (int *)calloc(tblalloc, sizeof(int));
if(link_table == (int *)NULL){
fprintf(stderr, "ERROR : create_link_table : calloc : link_table\n");
return(-330);
}
/* Allocate horizontal axis entries in table. */
x_axis = (int *)malloc(tbldim * sizeof(int));
if(x_axis == (int *)NULL){
free(link_table);
fprintf(stderr, "ERROR : create_link_table : malloc : x_axis\n");
return(-331);
}
/* Allocate vertical axis entries in table. */
y_axis = (int *)malloc(tbldim * sizeof(int));
if(y_axis == (int *)NULL){
free(link_table);
free(x_axis);
fprintf(stderr, "ERROR : create_link_table : malloc : y_axis\n");
return(-332);
}
nx_axis = 0;
ny_axis = 0;
n_entries = 0;
/* Allocate and initalize queue buffers. As minutia are entered into */
/* the link table they are placed in the queue for subsequent matching. */
queue = (int *)malloc(minutiae->num * sizeof(int));
if(queue == (int *)NULL){
free(link_table);
free(x_axis);
free(y_axis);
fprintf(stderr, "ERROR : create_link_table : malloc : queue\n");
return(-333);
}
/* List of flags to indicate if a manutia has been entered in the queue. */
/* Once a minutia "in queue" status is set to TRUE it will not be reset. */
/* This way a minutia will only ever be processed as a primary minutia */
/* once when builing the link table. Note that the calloc() initializes */
/* the flags to FALSE (as the queue is initially empty). */
inqueue = (int *)calloc(minutiae->num, sizeof(int));
if(inqueue == (int *)NULL){
free(link_table);
free(x_axis);
free(y_axis);
free(queue);
fprintf(stderr, "ERROR : create_link_table : calloc : inqueue\n");
return(-334);
}
/* Initialize head and tail to start of queue. */
head = 0;
tail = 0;
/* Push the index of the "main" manutia point onto the queue. */
queue[tail++] = start;
/* Set "main" minutia inqueue flag to TRUE. */
inqueue[start] = TRUE;
/* While the queue is NOT empty ... */
while(head != tail){
/* Pop the next manutia point from the queue and refer to it as */
/* the primary (first) minutia. */
first = queue[head++];
minutia1 = minutiae->list[first];
/* Look for those minutia points that potentially match the */
/* "first" minutia and add them to the link table. These */
/* potentially matching minutia are secondary and refered to */
/* as the "second" minutia. Always restart the search at the */
/* original "main" manutia. */
second = start+1;
/* While secondary manutae remain to be matched to the current */
/* first minutia... */
while(second < minutiae->num){
/* Assign second minutia to temporary pointer. */
minutia2 = minutiae->list[second];
/* 1. If y-delta from second to first minutia is small (ex. */
/* <= 20 pixels) ... */
if((minutia2->y - minutia1->y) <= lfsparms->max_link_dist){
/* 2. If first and second minutia are not the same point ... */
/* (Remeber that the search for matching seconds starts */
/* all the way back to the starting "main" minutia.) */
if(first != second){
/* 3. If first and second minutia are the same type ... */
if(minutia1->type == minutia2->type){
/* 4. If |x-delta| between minutiae is small (ex. <= */
/* 20 pixels) ... */
if(abs(minutia1->x - minutia2->x) <=
lfsparms->max_link_dist){
/* 5. If second minutia is NOT on a small loop ... */
if(!onloop[second]){
/* The second minutia is ridge-ending OR a */
/* bifurcation NOT to be skipped ... */
/* Compute the "inner" distance between the */
/* first and second minutia's directions. */
deltadir = closest_dir_dist(minutia1->direction,
minutia2->direction, full_ndirs);
/* If the resulting direction is INVALID */
/* (this should never happen, but just in case)... */
if(deltadir == INVALID_DIR){
free(link_table);
free(x_axis);
free(y_axis);
free(queue);
free(inqueue);
/* Then there is a problem. */
fprintf(stderr,
"ERROR : create_link_table : INVALID direction\n");
return(-335);
}
/* Compute first minutia's block coords from */
/* its pixel coords. */
xblk = minutia1->x/lfsparms->blocksize;
yblk = minutia1->y/lfsparms->blocksize;
/* Get corresponding block's NMAP value. */
/* -3 == NO_VALID_NBRS */
/* -2 == HIGH_CURVATURE */
/* -1 == INVALID_DIR */
/* 0 <= VALID_DIR */
nmapval = *(nmap+(yblk*mw)+xblk);
/* 6. CASE I: If block has VALID_DIR and deltadir */
/* relatively close to 180 degrees (at */
/* least 123.75 deg when ndirs==16)... */
/* OR */
/* CASE II: If block is HIGH_CURVATURE and */
/* deltadir is at least 45 degrees... */
if(((nmapval >= 0) &&
(deltadir >= low_curve_min_deltadir)) ||
((nmapval == HIGH_CURVATURE) &&
(deltadir >= qtr_ndirs))){
/* Then compute direction of "joining" vector. */
/* First, compute direction of line from first */
/* to second minutia points. */
joindir = line2direction(minutia1->x, minutia1->y,
minutia2->x, minutia2->y,
lfsparms->num_directions);
/* Comptue opposite direction of first minutia. */
opp1dir = (minutia1->direction+
lfsparms->num_directions)%full_ndirs;
/* Take "inner" distance on full circle between */
/* the first minutia's opposite direction and */
/* the joining direction. */
joindir = abs(opp1dir - joindir);
joindir = min(joindir, full_ndirs - joindir);
/* 7. If join angle is <= 90 deg... */
if(joindir <= half_ndirs){
/* Convert integer join direction to angle */
/* in radians on full circle. Multiply */
/* direction by (2PI)/32==PI/16 radians per */
/* unit direction and you get radians. */
jointheta = joindir *
(M_PI/(double)lfsparms->num_directions);
/* Compute squared distance between frist */
/* and second minutia points. */
joindist = distance(minutia1->x, minutia1->y,
minutia2->x, minutia2->y);
/* 8. If the 2 minutia points are close enough */
/* (ex. thresh == 20 pixels)... */
if(joindist <= lfsparms->max_link_dist){
/* 9. Does a "free path" exist between the */
/* 2 minutia points? */
if(free_path(minutia1->x, minutia1->y,
minutia2->x, minutia2->y,
bdata, iw, ih, lfsparms)){
/* If the join distance is very small, */
/* join theta will be unreliable, so set */
/* join theta to zero. */
/* (ex. thresh == 5 pixels)... */
if(joindist < lfsparms->min_theta_dist)
/* Set the join theta to zero. */
jointheta = 0.0;
/* Combine the join theta and distance */
/* to compute a link score. */
dscore = link_score(jointheta, joindist,
lfsparms);
/* Round off the floating point score. */
/* Need to truncate so answers are same */
/* on different computers. */
dscore = trunc_dbl_precision(dscore,
TRUNC_SCALE);
iscore = sround(dscore);
/* Add minutia pair and their link score */
/* to the Link Table. */
if(iscore > 0){
if((ret = update_link_table(link_table,
x_axis, y_axis, &nx_axis, &ny_axis,
&n_entries, tbldim,
queue, &head, &tail, inqueue,
first, second, iscore))){
/* If update ERROR, deallocate */
/* working memories. */
free(link_table);
free(x_axis);
free(y_axis);
free(queue);
free(inqueue);
return(ret);
}
} /* Else score is <= 0, so skip second. */
} /* 9. Else no free path, so skip second. */
} /* 8. Else joindist too big, so skip second. */
} /* 7. Else joindir is too big, so skip second. */
} /* 6. Else INVALID DIR or deltadir too small, */
/* so skip second. */
} /* 5. Else second minutia on small loop, so skip it. */
} /* 4. Else X distance too big, so skip second. */
} /* 3. Else first and second NOT same type, so skip second. */
} /* 2. Else first and second ARE same point, so skip second. */
/* If we get here, we want to advance to the next secondary. */
second++;
}
/* 1. Otherwise, Y distnace too big, so we are done searching for */
/* secondary matches to the current frist minutia. It is time */
/* to take the next minutia in the queue and begin matching */
/* secondaries to it. */
else{
/* So, break out of the secondary minutiae while loop. */
break;
}
}
/* Done matching current first minutia to secondaries. */
}
/* Get here when queue is empty, and we have our complete link table. */
/* Deallocate working memories. */
free(queue);
free(inqueue);
/* Assign link table buffers and attributes to output pointers. */
*olink_table = link_table;
*ox_axis = x_axis;
*oy_axis = y_axis;
*onx_axis = nx_axis;
*ony_axis = ny_axis;
*on_entries = n_entries;
/* Return normally. */
return(0);
}
/*************************************************************************
**************************************************************************
#cat: update_link_table - Takes the indices of 2 minutia and their link
#cat: compatibility score and updates the 2D link table.
#cat: The input minutia are registered to positions along
#cat: different axes, if they are not already in the table,
#cat: and a queue is maintained so that a cluster of
#cat: potentially linked points may be gathered.
Input:
link_table - sparse 2D table containing scores of potentially linked
minutia pairs
x_axis - minutia IDs registered along x-axis
y_axis - minutia IDs registered along y-axis
nx_axis - number of minutia registered along x-axis
ny_axis - number of minutia registered along y-axis
n_entries - number of scores currently entered in the table
tbldim - dimension of each axes of the link table
queue - list of clustered minutiae yet to be used to locate
other compatible minutiae
head - head of the queue
tail - tail of the queue
inqueue - flag for each minutia point in minutiae list to signify if
it has been clustered with the points in this current link
table
first - index position of first minutia of current link pair
second - index position of second minutia of current link pair
score - degree of link compatibility of current link pair
Output:
link_table - updated sparse 2D table containing scores of potentially
linked minutia pairs
x_axis - updated minutia IDs registered along x-axis
y_axis - updated minutia IDs registered along y-axis
nx_axis - updated number of minutia registered along x-axis
ny_axis - updated number of minutia registered along y-axis
n_entries - updated number of scores currently entered in the table
queue - updated list of clustered minutiae yet to be used to locate
other compatible minutiae
tail - updated tail of the queue
inqueue - updated list of flags, one for each minutia point in
minutiae list to signify if it has been clustered with
the points in this current link table
Return Code:
Zero - successful completion
Negative - system error
**************************************************************************/
int update_link_table(int *link_table, int *x_axis, int *y_axis,
int *nx_axis, int *ny_axis, int *n_entries, const int tbldim,
int *queue, int *head, int *tail, int *inqueue,
const int first, const int second, const int score)
{
int x, y, *tptr;
/* If the link table is empty. */
if(*n_entries == 0){
/* Add first and second minutia to the table. */
/* If horizontal axis of table is full ... */
if(*nx_axis >= tbldim)
/* Then ignore the minutia pair and return normally. */
return(0);
/* Add first minutia to horizontal axis. */
x_axis[*nx_axis] = first;
/* If vertical axis of table is full ... */
if(*ny_axis >= tbldim)
/* Then ignore the minutia pair and return normally. */
return(0);
/* Add second minutia to vertical axis. */
y_axis[*ny_axis] = second;
/* Enter minutia pair score to the link table. */
tptr = link_table + ((*ny_axis)*tbldim) + (*nx_axis);
*tptr = score;
(*n_entries)++;
/* Bump number of entries in each axis. */
(*nx_axis)++;
(*ny_axis)++;
/* Add second minutia to queue (if not already in queue), so it */
/* can be processed to see who might link to it. */
if(!inqueue[second]){
queue[*tail] = second;
(*tail)++;
inqueue[second] = TRUE;
}
/* Done, so return normally. */
return(0);
}
/* We are filling in the table with a "faimily" or "cluster" of */
/* potentially inter-linked points. Once the first entry is */
/* made in the table, all subsequent updates will be based on */
/* at least the first minutia already being in the table. */
/* If first minutia already stored in horizontal axis */
/* of the link table. */
if((x = in_int_list(first, x_axis, *nx_axis)) >= 0){
/* If second minutia already stored in vertical axis */
/* of the link table. */
if((y = in_int_list(second, y_axis, *ny_axis)) >= 0){
/* Entry may not be set or the new score may be larger. */
tptr = link_table + (y*tbldim) + x;
if(*tptr == 0)
/* Assign the minutia pair score to the table. */
*tptr = score;
}
/* Otherwise, second minutia not in vertical axis of link table. */
else{
/* Add the second minutia to the vertical axis and */
/* the minutia pair's score to the link table. */
/* If vertical axis of table is full ... */
if(*ny_axis >= tbldim)
/* Then ignore the minutia pair and return normally. */
return(0);
/* Add second minutia to vertical axis. */
y_axis[*ny_axis] = second;
/* Enter minutia pair score to the link table. */
tptr = link_table + ((*ny_axis)*tbldim) + x;
*tptr = score;
(*n_entries)++;
/* Bump number of entries in vertical axis. */
(*ny_axis)++;
/* Add second minutia to queue (if not already in queue), so it */
/* can be processed to see who might link to it. */
if(!inqueue[second]){
queue[*tail] = second;
(*tail)++;
inqueue[second] = TRUE;
}
}
}
/* Otherwise, first minutia not in horizontal axis of link table. */
else{
/* If first minutia already stored in vertical axis */
/* of the link table. */
if((y = in_int_list(first, y_axis, *ny_axis)) >= 0){
/* If second minutia already stored in horizontal axis */
/* of the link table. */
if((x = in_int_list(second, x_axis, *nx_axis)) >= 0){
/* Entry may not be set or the new score may be larger. */
tptr = link_table + (y*tbldim) + x;
if(*tptr == 0)
/* Assign the minutia pair score to the table. */
*tptr = score;
}
/* Otherwise, second minutia not in horizontal axis of link table. */
else{
/* Add the second minutia to the horizontal axis and */
/* the minutia pair's score to the link table. */
/* If horizontal axis of table is full ... */
if(*nx_axis >= tbldim)
/* Then ignore the minutia pair and return normally. */
return(0);
/* Add second minutia to vertical axis. */
x_axis[*nx_axis] = second;
/* Enter minutia pair score to the link table. */
tptr = link_table + (y*tbldim) + (*nx_axis);
*tptr = score;
(*n_entries)++;
/* Bump number of entries in horizontal axis. */
(*nx_axis)++;
/* Add second minutia to queue (if not already in queue), so it */
/* can be processed to see who might link to it. */
if(!inqueue[second]){
queue[*tail] = second;
(*tail)++;
inqueue[second] = TRUE;
}
}
}
/* Otherwise, first minutia not in vertical or horizontal axis of */
/* link table. This is an error, as this should only happen upon */
/* the first point being entered, which is already handled above. */
else{
fprintf(stderr,
"ERROR : update_link_table : first minutia not found in table\n");
return(-340);
}
}
/* Done, so return normally. */
return(0);
}
/*************************************************************************
**************************************************************************
#cat: order_link_table - Puts the link table in sorted order based on x and
#cat: then y-axis entries. These minutia are sorted based
#cat: on their point of perpendicular intersection with a
#cat: line running from the origin at an angle equal to the
#cat: average direction of all entries in the link table.
Input:
link_table - sparse 2D table containing scores of potentially linked
minutia pairs
x_axis - minutia IDs registered along x-axis
y_axis - minutia IDs registered along y-axis
nx_axis - number of minutia registered along x-axis
ny_axis - number of minutia registered along y-axis
n_entries - number of scores currently entered in the table
tbldim - dimension of each axes of the link table
minutiae - list of minutia
ndirs - number of IMAP directions (in semicircle)
Output:
link_table - sorted sparse 2D table containing scores of potentially
linked minutia pairs
x_axis - sorted minutia IDs registered along x-axis
y_axis - sorted minutia IDs registered along y-axis
Return Code:
Zero - successful completion
Negative - system error
**************************************************************************/
int order_link_table(int *link_table, int *x_axis, int *y_axis,
const int nx_axis, const int ny_axis, const int n_entries,
const int tbldim, const MINUTIAE *minutiae,
const int ndirs)
{
int i, j, ret, sumdir, avrdir, *order;
double davrdir, avrtheta, pi_factor, cs, sn;
double *dlist;
MINUTIA *minutia;
int *tlink_table, *tx_axis, *ty_axis, tblalloc;
int *toptr, *frptr;
/* If the table is empty or if there is only one horizontal or */
/* vertical entry in the table ... */
if((nx_axis <= 1) || (ny_axis <= 1))
/* Then don't reorder the table, just return normally. */
return(0);
/* Compute average direction (on semi-circle) of all minutia entered */
/* in the link table. This gives an average ridge-flow direction */
/* among all the potentially linked minutiae in the table. */
/* Initialize direction accumulator to 0. */
sumdir = 0;
/* Accumulate directions (on semi-circle) of minutia entered in the */
/* horizontal axis of the link table. */
for(i = 0; i < nx_axis; i++)
sumdir += (minutiae->list[x_axis[i]]->direction % ndirs);
/* Accumulate directions of minutia entered in the vertical axis */
/* of the link table. */
for(i = 0; i < ny_axis; i++)
sumdir += (minutiae->list[y_axis[i]]->direction % ndirs);
/* Compute the average direction and round off to integer. */
davrdir = (sumdir / (double)(nx_axis + ny_axis));
/* Need to truncate precision so that answers are consistent */
/* on different computer architectures when rounding doubles. */
davrdir = trunc_dbl_precision(davrdir, TRUNC_SCALE);
avrdir = sround(davrdir);
/* Conversion factor from integer directions to radians. */
pi_factor = M_PI / (double)ndirs;
/* Compute sine and cosine of average direction in radians. */
avrtheta = avrdir*pi_factor;
sn = sin(avrtheta);
cs = cos(avrtheta);
/* Allocate list to hold distances to be sorted on. */
dlist = (double *)malloc(tbldim * sizeof(double));
if(dlist == (double *)NULL){
fprintf(stderr, "ERROR : order_link_table : malloc : dlist\n");
return(-350);
}
/* Allocate and initialize temporary link table buffers. */
tblalloc = tbldim * tbldim;
tlink_table = (int *)calloc(tblalloc, sizeof(int));
if(tlink_table == (int *)NULL){
free(dlist);
fprintf(stderr, "ERROR : order_link_table : calloc : tlink_table\n");
return(-351);
}
tx_axis = (int *)malloc(tbldim * sizeof(int));
if(tx_axis == (int *)NULL){
free(dlist);
free(tlink_table);
fprintf(stderr, "ERROR : order_link_table : malloc : tx_axis\n");
return(-352);
}
ty_axis = (int *)malloc(tbldim * sizeof(int));
if(ty_axis == (int *)NULL){
free(dlist);
free(tlink_table);
free(tx_axis);
fprintf(stderr, "ERROR : order_link_table : malloc : ty_axis\n");
return(-353);
}
/* Compute distance measures for each minutia entered in the */
/* horizontal axis of the link table. */
/* The measure is: dist = X*cos(avrtheta) + Y*sin(avrtheta) */
/* which measures the distance from the origin along the line */
/* at angle "avrtheta" to the point of perpendicular inter- */
/* section from the point (X,Y). */
/* Foreach minutia in horizontal axis of the link table ... */
for(i = 0; i < nx_axis; i++){
minutia = minutiae->list[x_axis[i]];
dlist[i] = (minutia->x * cs) + (minutia->y * sn);
/* Need to truncate precision so that answers are consistent */
/* on different computer architectures when rounding doubles. */
dlist[i] = trunc_dbl_precision(dlist[i], TRUNC_SCALE);
}
/* Get sorted order of distance for minutiae in horizontal axis. */
if((ret = sort_indices_double_inc(&order, dlist, nx_axis))){
free(dlist);
return(ret);
}
/* Store entries on y_axis into temporary list. */
memcpy(ty_axis, y_axis, ny_axis * sizeof(int));
/* For each horizontal entry in link table ... */
for(i = 0; i < nx_axis; i++){
/* Store next minutia in sorted order to temporary x_axis. */
tx_axis[i] = x_axis[order[i]];
/* Store corresponding column of scores into temporary table. */
frptr = link_table + order[i];
toptr = tlink_table + i;
for(j = 0; j < ny_axis; j++){
*toptr = *frptr;
toptr += tbldim;
frptr += tbldim;
}
}
/* Deallocate sorted order of distance measures. */
free(order);
/* Compute distance measures for each minutia entered in the */
/* vertical axis of the temporary link table (already sorted */
/* based on its horizontal axis entries. */
/* Foreach minutia in vertical axis of the link table ... */
for(i = 0; i < ny_axis; i++){
minutia = minutiae->list[y_axis[i]];
dlist[i] = (minutia->x * cs) + (minutia->y * sn);
/* Need to truncate precision so that answers are consistent */
/* on different computer architectures when rounding doubles. */
dlist[i] = trunc_dbl_precision(dlist[i], TRUNC_SCALE);
}
/* Get sorted order of distance for minutiae in vertical axis. */
if((ret = sort_indices_double_inc(&order, dlist, ny_axis))){
free(dlist);
return(ret);
}
/* Store entries in temporary x_axis. */
memcpy(x_axis, tx_axis, nx_axis * sizeof(int));
/* For each vertical entry in the temporary link table ... */
for(i = 0; i < ny_axis; i++){
/* Store next minutia in sorted order to y_axis. */
y_axis[i] = ty_axis[order[i]];
/* Store corresponding row of scores into link table. */
frptr = tlink_table + (order[i] * tbldim);
toptr = link_table + (i * tbldim);
for(j = 0; j < nx_axis; j++){
*toptr++ = *frptr++;
}
}
/* Deallocate sorted order of distance measures. */
free(order);
/* Link table is now sorted on x and y axes. */
/* Deallocate the working memories. */
free(dlist);
free(tlink_table);
free(tx_axis);
free(ty_axis);
/* Return normally. */
return(0);
}
/*************************************************************************
**************************************************************************
#cat: process_link_table - Processes the link table deciding which minutia
#cat: pairs in the table should be linked (ie. joined in
#cat: the image and removed from the minutiae list (and
#cat: from onloop).
Input:
link_table - sparse 2D table containing scores of potentially linked
minutia pairs
x_axis - minutia IDs registered along x-axis
y_axis - minutia IDs registered along y-axis
nx_axis - number of minutia registered along x-axis
ny_axis - number of minutia registered along y-axis
n_entries - number of scores currently entered in the table
tbldim - dimension of each axes of the link table
minutiae - list of minutia
onloop - list of flags signifying which minutia lie on small lakes
bdata - binary image data (0==while & 1==black)
iw - width (in pixels) of image
ih - height (in pixels) of image
lfsparms - parameters and thresholds for controlling LFS
Output:
minutiae - list of pruned minutiae
onloop - updated loop flags
bdata - edited image with minutia features joined
Return Code:
Zero - successful completion
Negative - system error
**************************************************************************/
int process_link_table(const int *link_table,
const int *x_axis, const int *y_axis,
const int nx_axis, const int ny_axis, const int n_entries,
const int tbldim, MINUTIAE *minutiae, int *onloop,
unsigned char *bdata, const int iw, const int ih,
const LFSPARMS *lfsparms)
{
int i, j, ret, first, second;
MINUTIA *minutia1, *minutia2;
int rm1, rm2;
int *to_remove;
int n_lines, line_len, entry_incr, line_incr;
int line_i, entry_i;
int start, end;
int max_v, max_tbl_i, max_line_i, max_x, max_y;
/* If link table is empty, just return normally. */
if(n_entries == 0)
return(0);
/* If there is only 1 entry in the table, then join the minutia pair. */
if(n_entries == 1){
/* Join the minutia pair in the image. */
first = x_axis[0];
second = y_axis[0];
minutia1 = minutiae->list[first];
minutia2 = minutiae->list[second];
/* Connect the points with a line with specified radius (ex. 1 pixel). */
if((ret = join_minutia(minutia1, minutia2, bdata, iw, ih,
WITH_BOUNDARY, lfsparms->join_line_radius)))
return(ret);
/* Need to remove minutiae from onloop first, as onloop is dependent */
/* on the length of the minutiae list. We also need to remove the */
/* highest index from the lists first or the indices will be off. */
if(first > second){
rm1 = first;
rm2 = second;
}
else{
rm1 = second;
rm2 = first;
}
if((ret = remove_from_int_list(rm1, onloop, minutiae->num)))
return(ret);
if((ret = remove_from_int_list(rm2, onloop, minutiae->num-1)))
return(ret);
/* Now, remove the minutia from the minutiae list. */
if((ret = remove_minutia(rm1, minutiae)))
return(ret);
if((ret = remove_minutia(rm2, minutiae)))
return(ret);