-
Notifications
You must be signed in to change notification settings - Fork 3
/
loop.c
1228 lines (1074 loc) · 51 KB
/
loop.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: LOOP.C
AUTHOR: Michael D. Garris
DATE: 05/11/1999
UPDATED: 10/04/1999 Version 2 by MDG
UPDATED: 03/16/2005 by MDG
Contains routines responsible for analyzing and filling
lakes and islands within a binary image as part of the
NIST Latent Fingerprint System (LFS).
***********************************************************************
ROUTINES:
get_loop_list()
on_loop()
on_island_lake()
on_hook()
is_loop_clockwise()
process_loop()
process_loop_V2()
get_loop_aspect()
fill_loop()
fill_partial_row()
flood_loop()
flood_fill4()
***********************************************************************/
#include <stdio.h>
#include <malloc.h>
#include "lfs.h"
/*************************************************************************
**************************************************************************
#cat: get_loop_list - Takes a list of minutia points and determines which
#cat: ones lie on loops around valleys (lakes) of a specified
#cat: maximum circumference. The routine returns a list of
#cat: flags, one for each minutia in the input list, and if
#cat: the minutia is on a qualifying loop, the corresponding
#cat: flag is set to TRUE, otherwise it is set to FALSE.
#cat: If for some reason it was not possible to trace the
#cat: minutia's contour, then it is removed from the list.
#cat: This can occur due to edits dynamically taking place
#cat: in the image by other routines.
Input:
minutiae - list of true and false minutiae
loop_len - maximum size of loop searched for
bdata - binary image data (0==while & 1==black)
iw - width (in pixels) of image
ih - height (in pixels) of image
Output:
oonloop - loop flags: TRUE == loop, FALSE == no loop
Return Code:
Zero - successful completion
Negative - system error
**************************************************************************/
int get_loop_list(int **oonloop, MINUTIAE *minutiae, const int loop_len,
unsigned char *bdata, const int iw, const int ih)
{
int i, ret;
int *onloop;
MINUTIA *minutia;
/* Allocate a list of onloop flags (one for each minutia in list). */
onloop = (int *)malloc(minutiae->num * sizeof(int));
if(onloop == (int *)NULL){
fprintf(stderr, "ERROR : get_loop_list : malloc : onloop\n");
return(-320);
}
i = 0;
/* Foreach minutia remaining in list ... */
while(i < minutiae->num){
/* Assign a temporary pointer. */
minutia = minutiae->list[i];
/* If current minutia is a bifurcation ... */
if(minutia->type == BIFURCATION){
/* Check to see if it is on a loop of specified length. */
ret = on_loop(minutia, loop_len, bdata, iw, ih);
/* If minutia is on a loop... */
if(ret == LOOP_FOUND){
/* Then set the onloop flag to TRUE. */
onloop[i] = TRUE;
/* Advance to next minutia in the list. */
i++;
}
/* If on loop test IGNORED ... */
else if (ret == IGNORE){
/* Remove the current minutia from the list. */
if((ret = remove_minutia(i, minutiae))){
/* Deallocate working memory. */
free(onloop);
/* Return error code. */
return(ret);
}
/* No need to advance because next minutia has "slid" */
/* into position pointed to by 'i'. */
}
/* If the minutia is NOT on a loop... */
else if (ret == FALSE){
/* Then set the onloop flag to FALSE. */
onloop[i] = FALSE;
/* Advance to next minutia in the list. */
i++;
}
/* Otherwise, an ERROR occurred while looking for loop. */
else{
/* Deallocate working memory. */
free(onloop);
/* Return error code. */
return(ret);
}
}
/* Otherwise, the current minutia is a ridge-ending... */
else{
/* Ridge-endings will never be on a loop, so set flag to FALSE. */
onloop[i] = FALSE;
/* Advance to next minutia in the list. */
i++;
}
}
/* Store flag list to output pointer. */
*oonloop = onloop;
/* Return normally. */
return(0);
}
/*************************************************************************
**************************************************************************
#cat: on_loop - Determines if a minutia point lies on a loop (island or lake)
#cat: of specified maximum circumference.
Input:
minutiae - list of true and false minutiae
max_loop_len - maximum size of loop searched for
bdata - binary image data (0==while & 1==black)
iw - width (in pixels) of image
ih - height (in pixels) of image
Return Code:
IGNORE - minutia contour could not be traced
LOOP_FOUND - minutia determined to lie on qualifying loop
FALSE - minutia determined not to lie on qualifying loop
Negative - system error
**************************************************************************/
int on_loop(const MINUTIA *minutia, const int max_loop_len,
unsigned char *bdata, const int iw, const int ih)
{
int ret;
int *contour_x, *contour_y, *contour_ex, *contour_ey, ncontour;
/* Trace the contour of the feature starting at the minutia point */
/* and stepping along up to the specified maximum number of steps. */
ret = trace_contour(&contour_x, &contour_y,
&contour_ex, &contour_ey, &ncontour, max_loop_len,
minutia->x, minutia->y, minutia->x, minutia->y,
minutia->ex, minutia->ey,
SCAN_CLOCKWISE, bdata, iw, ih);
/* If trace was not possible ... */
if(ret == IGNORE)
return(ret);
/* If the trace completed a loop ... */
if(ret == LOOP_FOUND){
free_contour(contour_x, contour_y, contour_ex, contour_ey);
return(LOOP_FOUND);
}
/* If the trace successfully followed the minutia's contour, but did */
/* not complete a loop within the specified number of steps ... */
if(ret == 0){
free_contour(contour_x, contour_y, contour_ex, contour_ey);
return(FALSE);
}
/* Otherwise, the trace had an error in following the contour ... */
return(ret);
}
/*************************************************************************
**************************************************************************
#cat: on_island_lake - Determines if two minutia points lie on the same loop
#cat: (island or lake). If a loop is detected, the contour
#cat: points of the loop are returned.
Input:
minutia1 - first minutia point
minutia2 - second minutia point
max_half_loop - maximum size of half the loop circumference searched for
bdata - binary image data (0==while & 1==black)
iw - width (in pixels) of image
ih - height (in pixels) of image
Output:
ocontour_x - x-pixel coords of loop contour
ocontour_y - y-pixel coords of loop contour
ocontour_x - x coord of each contour point's edge pixel
ocontour_y - y coord of each contour point's edge pixel
oncontour - number of points in the contour.
Return Code:
IGNORE - contour could not be traced
LOOP_FOUND - minutiae determined to lie on same qualifying loop
FALSE - minutiae determined not to lie on same qualifying loop
Negative - system error
**************************************************************************/
int on_island_lake(int **ocontour_x, int **ocontour_y,
int **ocontour_ex, int **ocontour_ey, int *oncontour,
const MINUTIA *minutia1, const MINUTIA *minutia2,
const int max_half_loop,
unsigned char *bdata, const int iw, const int ih)
{
int i, l, ret;
int *contour1_x, *contour1_y, *contour1_ex, *contour1_ey, ncontour1;
int *contour2_x, *contour2_y, *contour2_ex, *contour2_ey, ncontour2;
int *loop_x, *loop_y, *loop_ex, *loop_ey, nloop;
/* Trace the contour of the feature starting at the 1st minutia point */
/* and stepping along up to the specified maximum number of steps or */
/* until 2nd mintuia point is encountered. */
ret = trace_contour(&contour1_x, &contour1_y,
&contour1_ex, &contour1_ey, &ncontour1, max_half_loop,
minutia2->x, minutia2->y, minutia1->x, minutia1->y,
minutia1->ex, minutia1->ey,
SCAN_CLOCKWISE, bdata, iw, ih);
/* If trace was not possible, return IGNORE. */
if(ret == IGNORE)
return(ret);
/* If the trace encounters 2nd minutia point ... */
if(ret == LOOP_FOUND){
/* Now, trace the contour of the feature starting at the 2nd minutia, */
/* continuing to search for edge neighbors clockwise, and stepping */
/* along up to the specified maximum number of steps or until 1st */
/* mintuia point is encountered. */
ret = trace_contour(&contour2_x, &contour2_y,
&contour2_ex, &contour2_ey, &ncontour2, max_half_loop,
minutia1->x, minutia1->y, minutia2->x, minutia2->y,
minutia2->ex, minutia2->ey,
SCAN_CLOCKWISE, bdata, iw, ih);
/* If trace was not possible, return IGNORE. */
if(ret == IGNORE){
free_contour(contour1_x, contour1_y, contour1_ex, contour1_ey);
return(ret);
}
/* If the 2nd trace encounters 1st minutia point ... */
if(ret == LOOP_FOUND){
/* Combine the 2 half loop contours into one full loop. */
/* Compute loop length (including the minutia pair). */
nloop = ncontour1 + ncontour2 + 2;
/* Allocate loop contour. */
if((ret = allocate_contour(&loop_x, &loop_y, &loop_ex, &loop_ey,
nloop))){
free_contour(contour1_x, contour1_y, contour1_ex, contour1_ey);
free_contour(contour2_x, contour2_y, contour2_ex, contour2_ey);
return(ret);
}
/* Store 1st minutia. */
l = 0;
loop_x[l] = minutia1->x;
loop_y[l] = minutia1->y;
loop_ex[l] = minutia1->ex;
loop_ey[l++] = minutia1->ey;
/* Store first contour. */
for(i = 0; i < ncontour1; i++){
loop_x[l] = contour1_x[i];
loop_y[l] = contour1_y[i];
loop_ex[l] = contour1_ex[i];
loop_ey[l++] = contour1_ey[i];
}
/* Store 2nd minutia. */
loop_x[l] = minutia2->x;
loop_y[l] = minutia2->y;
loop_ex[l] = minutia2->ex;
loop_ey[l++] = minutia2->ey;
/* Store 2nd contour. */
for(i = 0; i < ncontour2; i++){
loop_x[l] = contour2_x[i];
loop_y[l] = contour2_y[i];
loop_ex[l] = contour2_ex[i];
loop_ey[l++] = contour2_ey[i];
}
/* Deallocate the half loop contours. */
free_contour(contour1_x, contour1_y, contour1_ex, contour1_ey);
free_contour(contour2_x, contour2_y, contour2_ex, contour2_ey);
/* Assign loop contour to return pointers. */
*ocontour_x = loop_x;
*ocontour_y = loop_y;
*ocontour_ex = loop_ex;
*ocontour_ey = loop_ey;
*oncontour = nloop;
/* Then return that an island/lake WAS found (LOOP_FOUND). */
return(LOOP_FOUND);
}
/* If the trace successfully followed 2nd minutia's contour, but */
/* did not encounter 1st minutia point within the specified number */
/* of steps ... */
if(ret == 0){
/* Deallocate the two contours. */
free_contour(contour1_x, contour1_y, contour1_ex, contour1_ey);
free_contour(contour2_x, contour2_y, contour2_ex, contour2_ey);
/* Then return that an island/lake was NOT found (FALSE). */
return(FALSE);
}
/* Otherwise, the 2nd trace had an error in following the contour ... */
free_contour(contour1_x, contour1_y, contour1_ex, contour1_ey);
return(ret);
}
/* If the 1st trace successfully followed 1st minutia's contour, but */
/* did not encounter the 2nd minutia point within the specified number */
/* of steps ... */
if(ret == 0){
free_contour(contour1_x, contour1_y, contour1_ex, contour1_ey);
/* Then return that an island/lake was NOT found (FALSE). */
return(FALSE);
}
/* Otherwise, the 1st trace had an error in following the contour ... */
return(ret);
}
/*************************************************************************
**************************************************************************
#cat: on_hook - Determines if two minutia points lie on a hook on the side
#cat: of a ridge or valley.
Input:
minutia1 - first minutia point
minutia2 - second minutia point
max_hook_len - maximum length of contour searched along for a hook
bdata - binary image data (0==while & 1==black)
iw - width (in pixels) of image
ih - height (in pixels) of image
Return Code:
IGNORE - contour could not be traced
HOOK_FOUND - minutiae determined to lie on same qualifying hook
FALSE - minutiae determined not to lie on same qualifying hook
Negative - system error
**************************************************************************/
int on_hook(const MINUTIA *minutia1, const MINUTIA *minutia2,
const int max_hook_len,
unsigned char *bdata, const int iw, const int ih)
{
int ret;
int *contour_x, *contour_y, *contour_ex, *contour_ey, ncontour;
/* NOTE: This routine should only be called when the 2 minutia points */
/* are of "opposite" type. */
/* Trace the contour of the feature starting at the 1st minutia's */
/* "edge" point and stepping along up to the specified maximum number */
/* of steps or until the 2nd minutia point is encountered. */
/* First search for edge neighbors clockwise. */
ret = trace_contour(&contour_x, &contour_y,
&contour_ex, &contour_ey, &ncontour, max_hook_len,
minutia2->x, minutia2->y, minutia1->ex, minutia1->ey,
minutia1->x, minutia1->y,
SCAN_CLOCKWISE, bdata, iw, ih);
/* If trace was not possible, return IGNORE. */
if(ret == IGNORE)
return(ret);
/* If the trace encountered the second minutia point ... */
if(ret == LOOP_FOUND){
free_contour(contour_x, contour_y, contour_ex, contour_ey);
return(HOOK_FOUND);
}
/* If trace had an error in following the contour ... */
if(ret != 0)
return(ret);
/* Otherwise, the trace successfully followed the contour, but did */
/* not encounter the 2nd minutia point within the specified number */
/* of steps. */
/* Deallocate previously extracted contour. */
free_contour(contour_x, contour_y, contour_ex, contour_ey);
/* Try searching contour from 1st minutia "edge" searching for */
/* edge neighbors counter-clockwise. */
ret = trace_contour(&contour_x, &contour_y,
&contour_ex, &contour_ey, &ncontour, max_hook_len,
minutia2->x, minutia2->y, minutia1->ex, minutia1->ey,
minutia1->x, minutia1->y,
SCAN_COUNTER_CLOCKWISE, bdata, iw, ih);
/* If trace was not possible, return IGNORE. */
if(ret == IGNORE)
return(ret);
/* If the trace encountered the second minutia point ... */
if(ret == LOOP_FOUND){
free_contour(contour_x, contour_y, contour_ex, contour_ey);
return(HOOK_FOUND);
}
/* If the trace successfully followed the 1st minutia's contour, but */
/* did not encounter the 2nd minutia point within the specified number */
/* of steps ... */
if(ret == 0){
free_contour(contour_x, contour_y, contour_ex, contour_ey);
/* Then return hook NOT found (FALSE). */
return(FALSE);
}
/* Otherwise, the 2nd trace had an error in following the contour ... */
return(ret);
}
/*************************************************************************
**************************************************************************
#cat: is_loop_clockwise - Takes a feature's contour points and determines if
#cat: the points are ordered clockwise or counter-clockwise about
#cat: the feature. The routine also requires a default return
#cat: value be specified in the case the the routine is not able
#cat: to definitively determine the contour's order. This allows
#cat: the default response to be application-specific.
Input:
contour_x - x-coord list for feature's contour points
contour_y - y-coord list for feature's contour points
ncontour - number of points in contour
default_ret - default return code (used when we can't tell the order)
Return Code:
TRUE - contour determined to be ordered clockwise
FALSE - contour determined to be ordered counter-clockwise
Default - could not determine the order of the contour
Negative - system error
**************************************************************************/
int is_loop_clockwise(const int *contour_x, const int *contour_y,
const int ncontour, const int default_ret)
{
int ret;
int *chain, nchain;
/* Derive chain code from contour points. */
if((ret = chain_code_loop(&chain, &nchain,
contour_x, contour_y, ncontour)))
/* If there is a system error, return the error code. */
return(ret);
/* If chain is empty... */
if(nchain == 0){
/* There wasn't enough contour points to tell, so return the */
/* the default return value. No chain needs to be deallocated */
/* in this case. */
return(default_ret);
}
/* If the chain code for contour is clockwise ... pass default return */
/* value on to this routine to correctly handle the case where we can't */
/* tell the direction of the chain code. */
ret = is_chain_clockwise(chain, nchain, default_ret);
/* Free the chain code and return result. */
free(chain);
return(ret);
}
/*************************************************************************
**************************************************************************
#cat: process_loop - Takes a contour list that has been determined to form
#cat: a complete loop, and processes it. If the loop is sufficiently
#cat: large and elongated, then two minutia points are calculated
#cat: along the loop's longest aspect axis. If it is determined
#cat: that the loop does not contain minutiae, it is filled in the
#cat: binary image.
Input:
contour_x - x-coord list for loop's contour points
contour_y - y-coord list for loop's contour points
contour_ex - x-coord list for loop's edge points
contour_ey - y-coord list for loop's edge points
ncontour - number of points in contour
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 - points to a list of detected minutia structures
OR
bdata - binary image data with loop filled
Return Code:
Zero - loop processed successfully
Negative - system error
**************************************************************************/
int process_loop(MINUTIAE *minutiae,
const int *contour_x, const int *contour_y,
const int *contour_ex, const int *contour_ey, const int ncontour,
unsigned char *bdata, const int iw, const int ih,
const LFSPARMS *lfsparms)
{
int halfway;
int idir, type, appearing;
double min_dist, max_dist;
int min_fr, max_fr, min_to, max_to;
int mid_x, mid_y, mid_pix;
int feature_pix;
int ret;
MINUTIA *minutia;
/* If contour is empty, then just return. */
if(ncontour <= 0)
return(0);
/* If loop is large enough ... */
if(ncontour > lfsparms->min_loop_len){
/* Get pixel value of feature's interior. */
feature_pix = *(bdata + (contour_y[0] * iw) + contour_x[0]);
/* Compute half the perimeter of the loop. */
halfway = ncontour>>1;
/* Get the aspect dimensions of the loop in units of */
/* squared distance. */
get_loop_aspect(&min_fr, &min_to, &min_dist,
&max_fr, &max_to, &max_dist,
contour_x, contour_y, ncontour);
/* If loop passes aspect ratio tests ... loop is sufficiently */
/* narrow or elongated ... */
if((min_dist < lfsparms->min_loop_aspect_dist) ||
((max_dist/min_dist) >= lfsparms->min_loop_aspect_ratio)){
/* Update minutiae list with opposite points of max distance */
/* on the loop. */
/* First, check if interior point has proper pixel value. */
mid_x = (contour_x[max_fr]+contour_x[max_to])>>1;
mid_y = (contour_y[max_fr]+contour_y[max_to])>>1;
mid_pix = *(bdata + (mid_y * iw) + mid_x);
/* If interior point is the same as the feature... */
if(mid_pix == feature_pix){
/* 1. Treat maximum distance point as a potential minutia. */
/* Compute direction from maximum loop point to its */
/* opposite point. */
idir = line2direction(contour_x[max_fr], contour_y[max_fr],
contour_x[max_to], contour_y[max_to],
lfsparms->num_directions);
/* Get type of minutia: BIFURCATION or RIDGE_ENDING. */
type = minutia_type(feature_pix);
/* Determine if minutia is appearing or disappearing. */
if((appearing = is_minutia_appearing(
contour_x[max_fr], contour_y[max_fr],
contour_ex[max_fr], contour_ey[max_fr])) < 0){
/* Return system error code. */
return(appearing);
}
/* Create new minutia object. */
if((ret = create_minutia(&minutia,
contour_x[max_fr], contour_y[max_fr],
contour_ex[max_fr], contour_ey[max_fr],
idir, DEFAULT_RELIABILITY,
type, appearing, LOOP_ID))){
/* Return system error code. */
return(ret);
}
/* Update the minutiae list with potential new minutia. */
ret = update_minutiae(minutiae, minutia, bdata, iw, ih, lfsparms);
/* If minuitia IGNORED and not added to the minutia list ... */
if(ret == IGNORE)
/* Deallocate the minutia. */
free_minutia(minutia);
/* 2. Treat point opposite of maximum distance point as */
/* a potential minutia. */
/* Flip the direction 180 degrees. Make sure new direction */
/* is on the range [0..(ndirsX2)]. */
idir += lfsparms->num_directions;
idir %= (lfsparms->num_directions<<1);
/* The type of minutia will stay the same. */
/* Determine if minutia is appearing or disappearing. */
if((appearing = is_minutia_appearing(
contour_x[max_to], contour_y[max_to],
contour_ex[max_to], contour_ey[max_to])) < 0){
/* Return system error code. */
return(appearing);
}
/* Create new minutia object. */
if((ret = create_minutia(&minutia,
contour_x[max_to], contour_y[max_to],
contour_ex[max_to], contour_ey[max_to],
idir, DEFAULT_RELIABILITY,
type, appearing, LOOP_ID))){
/* Return system error code. */
return(ret);
}
/* Update the minutiae list with potential new minutia. */
ret = update_minutiae(minutiae, minutia, bdata, iw, ih, lfsparms);
/* If minuitia IGNORED and not added to the minutia list ... */
if(ret == IGNORE)
/* Deallocate the minutia. */
free_minutia(minutia);
/* Done successfully processing this loop, so return normally. */
return(0);
} /* Otherwise, loop interior has problems. */
} /* Otherwise, loop is not the right shape for minutiae. */
} /* Otherwise, loop's perimeter is too small for minutiae. */
/* If we get here, we have a loop that is assumed to not contain */
/* minutiae, so remove the loop from the image. */
ret = fill_loop(contour_x, contour_y, ncontour, bdata, iw, ih);
/* Return either an error code from fill_loop or return normally. */
return(ret);
}
/*************************************************************************
**************************************************************************
#cat: process_loop_V2 - Takes a contour list that has been determined to form
#cat: a complete loop, and processes it. If the loop is sufficiently
#cat: large and elongated, then two minutia points are calculated
#cat: along the loop's longest aspect axis. If it is determined
#cat: that the loop does not contain minutiae, it is filled in the
#cat: binary image.
Input:
contour_x - x-coord list for loop's contour points
contour_y - y-coord list for loop's contour points
contour_ex - x-coord list for loop's edge points
contour_ey - y-coord list for loop's edge points
ncontour - number of points in contour
bdata - binary image data (0==while & 1==black)
iw - width (in pixels) of image
ih - height (in pixels) of image
plow_flow_map - pixelized Low Ridge Flow Map
lfsparms - parameters and thresholds for controlling LFS
Output:
minutiae - points to a list of detected minutia structures
OR
bdata - binary image data with loop filled
Return Code:
Zero - loop processed successfully
Negative - system error
**************************************************************************/
int process_loop_V2(MINUTIAE *minutiae,
const int *contour_x, const int *contour_y,
const int *contour_ex, const int *contour_ey, const int ncontour,
unsigned char *bdata, const int iw, const int ih,
int *plow_flow_map, const LFSPARMS *lfsparms)
{
int halfway;
int idir, type, appearing;
double min_dist, max_dist;
int min_fr, max_fr, min_to, max_to;
int mid_x, mid_y, mid_pix;
int feature_pix;
int ret;
MINUTIA *minutia;
int fmapval;
double reliability;
/* If contour is empty, then just return. */
if(ncontour <= 0)
return(0);
/* If loop is large enough ... */
if(ncontour > lfsparms->min_loop_len){
/* Get pixel value of feature's interior. */
feature_pix = *(bdata + (contour_y[0] * iw) + contour_x[0]);
/* Compute half the perimeter of the loop. */
halfway = ncontour>>1;
/* Get the aspect dimensions of the loop in units of */
/* squared distance. */
get_loop_aspect(&min_fr, &min_to, &min_dist,
&max_fr, &max_to, &max_dist,
contour_x, contour_y, ncontour);
/* If loop passes aspect ratio tests ... loop is sufficiently */
/* narrow or elongated ... */
if((min_dist < lfsparms->min_loop_aspect_dist) ||
((max_dist/min_dist) >= lfsparms->min_loop_aspect_ratio)){
/* Update minutiae list with opposite points of max distance */
/* on the loop. */
/* First, check if interior point has proper pixel value. */
mid_x = (contour_x[max_fr]+contour_x[max_to])>>1;
mid_y = (contour_y[max_fr]+contour_y[max_to])>>1;
mid_pix = *(bdata + (mid_y * iw) + mid_x);
/* If interior point is the same as the feature... */
if(mid_pix == feature_pix){
/* 1. Treat maximum distance point as a potential minutia. */
/* Compute direction from maximum loop point to its */
/* opposite point. */
idir = line2direction(contour_x[max_fr], contour_y[max_fr],
contour_x[max_to], contour_y[max_to],
lfsparms->num_directions);
/* Get type of minutia: BIFURCATION or RIDGE_ENDING. */
type = minutia_type(feature_pix);
/* Determine if minutia is appearing or disappearing. */
if((appearing = is_minutia_appearing(
contour_x[max_fr], contour_y[max_fr],
contour_ex[max_fr], contour_ey[max_fr])) < 0){
/* Return system error code. */
return(appearing);
}
/* Is the new point in a LOW RIDGE FLOW block? */
fmapval = *(plow_flow_map+(contour_y[max_fr]*iw)+
contour_x[max_fr]);
/* If current minutia is in a LOW RIDGE FLOW block ... */
if(fmapval)
reliability = MEDIUM_RELIABILITY;
else
/* Otherwise, minutia is in a reliable block. */
reliability = HIGH_RELIABILITY;
/* Create new minutia object. */
if((ret = create_minutia(&minutia,
contour_x[max_fr], contour_y[max_fr],
contour_ex[max_fr], contour_ey[max_fr],
idir, reliability,
type, appearing, LOOP_ID))){
/* Return system error code. */
return(ret);
}
/* Update the minutiae list with potential new minutia. */
/* NOTE: Deliberately using version one of this routine. */
ret = update_minutiae(minutiae, minutia, bdata, iw, ih, lfsparms);
/* If minuitia IGNORED and not added to the minutia list ... */
if(ret == IGNORE)
/* Deallocate the minutia. */
free_minutia(minutia);
/* 2. Treat point opposite of maximum distance point as */
/* a potential minutia. */
/* Flip the direction 180 degrees. Make sure new direction */
/* is on the range [0..(ndirsX2)]. */
idir += lfsparms->num_directions;
idir %= (lfsparms->num_directions<<1);
/* The type of minutia will stay the same. */
/* Determine if minutia is appearing or disappearing. */
if((appearing = is_minutia_appearing(
contour_x[max_to], contour_y[max_to],
contour_ex[max_to], contour_ey[max_to])) < 0){
/* Return system error code. */
return(appearing);
}
/* Is the new point in a LOW RIDGE FLOW block? */
fmapval = *(plow_flow_map+(contour_y[max_to]*iw)+
contour_x[max_to]);
/* If current minutia is in a LOW RIDGE FLOW block ... */
if(fmapval)
reliability = MEDIUM_RELIABILITY;
else
/* Otherwise, minutia is in a reliable block. */
reliability = HIGH_RELIABILITY;
/* Create new minutia object. */
if((ret = create_minutia(&minutia,
contour_x[max_to], contour_y[max_to],
contour_ex[max_to], contour_ey[max_to],
idir, reliability,
type, appearing, LOOP_ID))){
/* Return system error code. */
return(ret);
}
/* Update the minutiae list with potential new minutia. */
/* NOTE: Deliberately using version one of this routine. */
ret = update_minutiae(minutiae, minutia, bdata, iw, ih, lfsparms);
/* If minuitia IGNORED and not added to the minutia list ... */
if(ret == IGNORE)
/* Deallocate the minutia. */
free_minutia(minutia);
/* Done successfully processing this loop, so return normally. */
return(0);
} /* Otherwise, loop interior has problems. */
} /* Otherwise, loop is not the right shape for minutiae. */
} /* Otherwise, loop's perimeter is too small for minutiae. */
/* If we get here, we have a loop that is assumed to not contain */
/* minutiae, so remove the loop from the image. */
ret = fill_loop(contour_x, contour_y, ncontour, bdata, iw, ih);
/* Return either an error code from fill_loop or return normally. */
return(ret);
}
/*************************************************************************
**************************************************************************
#cat: get_loop_aspect - Takes a contour list (determined to form a complete
#cat: loop) and measures the loop's aspect (the largest and smallest
#cat: distances across the loop) and returns the points on the
#cat: loop where these distances occur.
Input:
contour_x - x-coord list for loop's contour points
contour_y - y-coord list for loop's contour points
ncontour - number of points in contour
Output:
omin_fr - contour point index where minimum aspect occurs
omin_to - opposite contour point index where minimum aspect occurs
omin_dist - the minimum distance across the loop
omax_fr - contour point index where maximum aspect occurs
omax_to - contour point index where maximum aspect occurs
omax_dist - the maximum distance across the loop
**************************************************************************/
void get_loop_aspect(int *omin_fr, int *omin_to, double *omin_dist,
int *omax_fr, int *omax_to, double *omax_dist,
const int *contour_x, const int *contour_y, const int ncontour)
{
int halfway, limit;
int i, j;
double dist;
double min_dist, max_dist;
int min_i, max_i, min_j, max_j;
/* Compute half the perimeter of the loop. */
halfway = ncontour>>1;
/* Take opposite points on the contour and walk half way */
/* around the loop. */
i = 0;
j = halfway;
/* Compute squared distance between opposite points on loop. */
dist = squared_distance(contour_x[i], contour_y[i],
contour_x[j], contour_y[j]);
/* Initialize running minimum and maximum distances along loop. */
min_dist = dist;
min_i = i;
min_j = j;
max_dist = dist;
max_i = i;
max_j = j;
/* Bump to next pair of opposite points. */
i++;
/* Make sure j wraps around end of list. */
j++;
j %= ncontour;
/* If the loop is of even length, then we only need to walk half */
/* way around as the other half will be exactly redundant. If */
/* the loop is of odd length, then the second half will not be */
/* be exactly redundant and the difference "may" be meaningful. */
/* If execution speed is an issue, then probably get away with */
/* walking only the fist half of the loop under ALL conditions. */
/* If loop has odd length ... */
if(ncontour % 2)
/* Walk the loop's entire perimeter. */
limit = ncontour;
/* Otherwise the loop has even length ... */
else
/* Only walk half the perimeter. */
limit = halfway;
/* While we have not reached our perimeter limit ... */
while(i < limit){
/* Compute squared distance between opposite points on loop. */
dist = squared_distance(contour_x[i], contour_y[i],
contour_x[j], contour_y[j]);
/* Check the running minimum and maximum distances. */
if(dist < min_dist){
min_dist = dist;
min_i = i;
min_j = j;
}
if(dist > max_dist){
max_dist = dist;
max_i = i;
max_j = j;
}
/* Bump to next pair of opposite points. */
i++;
/* Make sure j wraps around end of list. */
j++;
j %= ncontour;
}
/* Assign minimum and maximum distances to output pointers. */
*omin_fr = min_i;
*omin_to = min_j;
*omin_dist = min_dist;
*omax_fr = max_i;
*omax_to = max_j;
*omax_dist = max_dist;
}
/*************************************************************************
**************************************************************************
#cat: fill_loop - Takes a contour list that has been determined to form
#cat: a complete loop, and fills the loop accounting for
#cat: complex/concaved shapes.
#cat: NOTE, I tried using a flood-fill in place of this routine,
#cat: but the contour (although 8-connected) is NOT guaranteed to
#cat: be "complete" surrounded (in an 8-connected sense) by pixels
#cat: of opposite color. Therefore, the flood would occasionally
#cat: escape the loop and corrupt the binary image!
Input:
contour_x - x-coord list for loop's contour points
contour_y - y-coord list for loop's contour points
ncontour - number of points in contour
bdata - binary image data (0==while & 1==black)
iw - width (in pixels) of image
ih - height (in pixels) of image
Output:
bdata - binary image data with loop filled
Return Code:
Zero - loop filled successfully
Negative - system error
**************************************************************************/
int fill_loop(const int *contour_x, const int *contour_y,
const int ncontour, unsigned char *bdata,
const int iw, const int ih)
{
SHAPE *shape;
int ret, i, j, x, nx, y;
int lastj;
int next_pix, feature_pix, edge_pix;
/* Create a shape structure from loop's contour. */
if((ret = shape_from_contour(&shape, contour_x, contour_y, ncontour)))
/* If system error, then return error code. */
return(ret);
/* Get feature pixel value (the value on the interior of the loop */
/* to be filled). */
feature_pix = *(bdata+(contour_y[0]*iw)+contour_x[0]);
/* Now get edge pixel value (the value on the exterior of the loop */
/* to be used to filled the loop). We can get this value by flipping */
/* the feature pixel value. */
if(feature_pix)
edge_pix = 0;
else
edge_pix = 1;
/* Foreach row in shape... */
for(i = 0; i < shape->nrows; i++){
/* Get y-coord of current row in shape. */
y = shape->rows[i]->y;