-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcontour.c
1275 lines (1141 loc) · 53.8 KB
/
contour.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: CONTOUR.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 extracting and analyzing
minutia feature contour lists as part of the NIST Latent
Fingerprint System (LFS).
***********************************************************************
ROUTINES:
allocate_contour()
free_contour()
get_high_curvature_contour()
get_centered_contour()
trace_contour()
search_contour()
next_contour_pixel()
start_scan_nbr()
next_scan_nbr()
min_contour_theta()
contour_limits()
***********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include "lfs.h"
/*************************************************************************
**************************************************************************
#cat: allocate_contour - Allocates the lists needed to represent the
#cat: contour of a minutia feature (a ridge or valley-ending).
#cat: This includes two lists of coordinate pairs. The first is
#cat: the 8-connected chain of points interior to the feature
#cat: and are called the feature's "contour points".
#cat: The second is a list or corresponding points each
#cat: adjacent to its respective feature contour point in the first
#cat: list and on the exterior of the feature. These second points
#cat: are called the feature's "edge points". Don't be confused,
#cat: both lists of points are on the "edge". The first set is
#cat: guaranteed 8-connected and the color of the feature. The
#cat: second set is NOT guaranteed to be 8-connected and its points
#cat: are opposite the color of the feature. Remeber that "feature"
#cat: means either ridge-ending (black pixels) or valley-ending
#cat: (white pixels).
Input:
ncontour - number of items in each coordinate list to be allocated
Output:
ocontour_x - allocated x-coord list for feature's contour points
ocontour_y - allocated y-coord list for feature's contour points
ocontour_ex - allocated x-coord list for feature's edge points
ocontour_ey - allocated y-coord list for feature's edge points
Return Code:
Zero - lists were successfully allocated
Negative - system (allocation) error
**************************************************************************/
int allocate_contour(int **ocontour_x, int **ocontour_y,
int **ocontour_ex, int **ocontour_ey, const int ncontour)
{
int *contour_x, *contour_y, *contour_ex, *contour_ey;
/* Allocate contour's x-coord list. */
contour_x = (int *)malloc(ncontour*sizeof(int));
/* If allocation error... */
if(contour_x == (int *)NULL){
fprintf(stderr, "ERROR : allocate_contour : malloc : contour_x\n");
return(-180);
}
/* Allocate contour's y-coord list. */
contour_y = (int *)malloc(ncontour*sizeof(int));
/* If allocation error... */
if(contour_y == (int *)NULL){
/* Deallocate memory allocated to this point in this routine. */
free(contour_x);
fprintf(stderr, "ERROR : allocate_contour : malloc : contour_y\n");
return(-181);
}
/* Allocate contour's edge x-coord list. */
contour_ex = (int *)malloc(ncontour*sizeof(int));
/* If allocation error... */
if(contour_ex == (int *)NULL){
/* Deallocate memory allocated to this point in this routine. */
free(contour_x);
free(contour_y);
fprintf(stderr, "ERROR : allocate_contour : malloc : contour_ex\n");
return(-182);
}
/* Allocate contour's edge y-coord list. */
contour_ey = (int *)malloc(ncontour*sizeof(int));
/* If allocation error... */
if(contour_ey == (int *)NULL){
/* Deallocate memory allocated to this point in this routine. */
free(contour_x);
free(contour_y);
free(contour_ex);
fprintf(stderr, "ERROR : allocate_contour : malloc : contour_ey\n");
return(-183);
}
/* Otherwise, allocations successful, so assign output pointers. */
*ocontour_x = contour_x;
*ocontour_y = contour_y;
*ocontour_ex = contour_ex;
*ocontour_ey = contour_ey;
/* Return normally. */
return(0);
}
/*************************************************************************
**************************************************************************
#cat: free_contour - Deallocates the lists used to represent the
#cat: contour of a minutia feature (a ridge or valley-ending).
#cat: This includes two lists of coordinate pairs. The first is
#cat: the 8-connected chain of points interior to the feature
#cat: and are called the feature's "contour points".
#cat: The second is a list or corresponding points each
#cat: adjacent to its respective feature contour point in the first
#cat: list and on the exterior of the feature. These second points
#cat: are called the feature's "edge points".
Input:
contour_x - x-coord list for feature's contour points
contour_y - y-coord list for feature's contour points
contour_ex - x-coord list for feature's edge points
contour_ey - y-coord list for feature's edge points
**************************************************************************/
void free_contour(int *contour_x, int *contour_y,
int *contour_ex, int *contour_ey)
{
free(contour_x);
free(contour_y);
free(contour_ex);
free(contour_ey);
}
/*************************************************************************
**************************************************************************
#cat: get_high_curvature_contour - Takes the pixel coordinate of a detected
#cat: minutia feature point and its corresponding/adjacent edge
#cat: pixel and attempts to extract a contour of specified length
#cat: of the feature's edge. The contour is extracted by walking
#cat: the feature's edge a specified number of steps clockwise and
#cat: then counter-clockwise. If a loop is detected while
#cat: extracting the contour, the contour of the loop is returned
#cat: with a return code of (LOOP_FOUND). If the process fails
#cat: to extract a contour of total specified length, then
#cat: the returned contour length is set to Zero, NO allocated
#cat: memory is returned in this case, and the return code is set
#cat: to Zero. An alternative implementation would be to return
#cat: the incomplete contour with a return code of (INCOMPLETE).
#cat: For now, NO allocated contour is returned in this case.
Input:
half_contour - half the length of the extracted contour
(full-length non-loop contour = (half_contourX2)+1)
x_loc - starting x-pixel coord of feature (interior to feature)
y_loc - starting y-pixel coord of feature (interior to feature)
x_edge - x-pixel coord of corresponding edge pixel
(exterior to feature)
y_edge - y-pixel coord of corresponding edge pixel
(exterior to feature)
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 contour (interior to feature)
ocontour_y - y-pixel coords of contour (interior to feature)
ocontour_ex - x-pixel coords of corresponding edge (exterior to feature)
ocontour_ey - y-pixel coords of corresponding edge (exterior to feature)
oncontour - number of contour points returned
Return Code:
Zero - resulting contour was successfully extracted or is empty
LOOP_FOUND - resulting contour forms a complete loop
Negative - system error
**************************************************************************/
int get_high_curvature_contour(int **ocontour_x, int **ocontour_y,
int **ocontour_ex, int **ocontour_ey, int *oncontour,
const int half_contour,
const int x_loc, const int y_loc,
const int x_edge, const int y_edge,
unsigned char *bdata, const int iw, const int ih)
{
int max_contour;
int *half1_x, *half1_y, *half1_ex, *half1_ey, nhalf1;
int *half2_x, *half2_y, *half2_ex, *half2_ey, nhalf2;
int *contour_x, *contour_y, *contour_ex, *contour_ey, ncontour;
int i, j, ret;
/* Compute maximum length of complete contour */
/* (2 half contours + feature point). */
max_contour = (half_contour<<1) + 1;
/* Initialize output contour length to 0. */
*oncontour = 0;
/* Get 1st half contour with clockwise neighbor trace. */
if((ret = trace_contour(&half1_x, &half1_y, &half1_ex, &half1_ey, &nhalf1,
half_contour, x_loc, y_loc, x_loc, y_loc, x_edge, y_edge,
SCAN_CLOCKWISE, bdata, iw, ih))){
/* If trace was not possible ... */
if(ret == IGNORE)
/* Return, with nothing allocated and contour length equal to 0. */
return(0);
/* If 1st half contour forms a loop ... */
if(ret == LOOP_FOUND){
/* Need to reverse the 1st half contour so that the points are */
/* in consistent order. */
/* We need to add the original feature point to the list, so */
/* set new contour length to one plus length of 1st half contour. */
ncontour = nhalf1+1;
/* Allocate new contour list. */
if((ret = allocate_contour(&contour_x, &contour_y,
&contour_ex, &contour_ey, ncontour))){
/* If allcation error, then deallocate memory allocated to */
/* this point in this routine. */
free_contour(half1_x, half1_y, half1_ex, half1_ey);
/* Return error code. */
return(ret);
}
/* Otherwise, we have the new contour allocated, so store the */
/* original feature point. */
contour_x[0] = x_loc;
contour_y[0] = y_loc;
contour_ex[0] = x_edge;
contour_ey[0] = y_edge;
/* Now store the first half contour in reverse order. */
for(i = 1, j = nhalf1-1; i < ncontour; i++, j--){
contour_x[i] = half1_x[j];
contour_y[i] = half1_y[j];
contour_ex[i] = half1_ex[j];
contour_ey[i] = half1_ey[j];
}
/* Deallocate the first half contour. */
free_contour(half1_x, half1_y, half1_ex, half1_ey);
/* Assign the output pointers. */
*ocontour_x = contour_x;
*ocontour_y = contour_y;
*ocontour_ex = contour_ex;
*ocontour_ey = contour_ey;
*oncontour = ncontour;
/* Return LOOP_FOUND for further processing. */
return(LOOP_FOUND);
}
/* Otherwise, return the system error code from the first */
/* call to trace_contour. */
return(ret);
}
/* If 1st half contour not complete ... */
if(nhalf1 < half_contour){
/* Deallocate the partial contour. */
free_contour(half1_x, half1_y, half1_ex, half1_ey);
/* Return, with nothing allocated and contour length equal to 0. */
return(0);
}
/* Otherwise, we have a complete 1st half contour... */
/* Get 2nd half contour with counter-clockwise neighbor trace. */
/* Use the last point from the first contour trace as the */
/* point to test for a loop when tracing the second contour. */
if((ret = trace_contour(&half2_x, &half2_y, &half2_ex, &half2_ey, &nhalf2,
half_contour, half1_x[nhalf1-1], half1_y[nhalf1-1],
x_loc, y_loc, x_edge, y_edge,
SCAN_COUNTER_CLOCKWISE, bdata, iw, ih))){
/* If 2nd trace was not possible ... */
if(ret == IGNORE){
/* Deallocate the 1st half contour. */
free_contour(half1_x, half1_y, half1_ex, half1_ey);
/* Return, with nothing allocated and contour length equal to 0. */
return(0);
}
/* If non-zero return code is NOT LOOP_FOUND, then system error ... */
if(ret != LOOP_FOUND){
/* Deallocate the 1st half contour. */
free_contour(half1_x, half1_y, half1_ex, half1_ey);
/* Return system error. */
return(ret);
}
}
/* If 2nd half NOT a loop AND not complete ... */
if((ret != LOOP_FOUND) && (nhalf2 < half_contour)){
/* Deallocate both half contours. */
free_contour(half1_x, half1_y, half1_ex, half1_ey);
free_contour(half2_x, half2_y, half2_ex, half2_ey);
/* Return, with nothing allocated and contour length equal to 0. */
return(0);
}
/* Otherwise we have a full 1st half contour and a 2nd half contour */
/* that is either a loop or complete. In either case we need to */
/* concatenate the two half contours into one longer contour. */
/* Allocate output contour list. Go ahead and allocate the */
/* "max_contour" amount even though the resulting contour will */
/* likely be shorter if it forms a loop. */
if((ret = allocate_contour(&contour_x, &contour_y,
&contour_ex, &contour_ey, max_contour))){
/* If allcation error, then deallocate memory allocated to */
/* this point in this routine. */
free_contour(half1_x, half1_y, half1_ex, half1_ey);
free_contour(half2_x, half2_y, half2_ex, half2_ey);
/* Return error code. */
return(ret);
}
/* Set the current contour point counter to 0 */
ncontour = 0;
/* Copy 1st half contour into output contour buffers. */
/* This contour was collected clockwise, so it's points */
/* are entered in reverse order of the trace. The result */
/* is the first point in the output contour if farthest */
/* from the starting feature point. */
for(i = 0, j = nhalf1-1; i < nhalf1; i++, j--){
contour_x[i] = half1_x[j];
contour_y[i] = half1_y[j];
contour_ex[i] = half1_ex[j];
contour_ey[i] = half1_ey[j];
ncontour++;
}
/* Deallocate 1st half contour. */
free_contour(half1_x, half1_y, half1_ex, half1_ey);
/* Next, store starting feature point into output contour buffers. */
contour_x[nhalf1] = x_loc;
contour_y[nhalf1] = y_loc;
contour_ex[nhalf1] = x_edge;
contour_ey[nhalf1] = y_edge;
ncontour++;
/* Now, append 2nd half contour to permanent contour buffers. */
for(i = 0, j = nhalf1+1; i < nhalf2; i++, j++){
contour_x[j] = half2_x[i];
contour_y[j] = half2_y[i];
contour_ex[j] = half2_ex[i];
contour_ey[j] = half2_ey[i];
ncontour++;
}
/* Deallocate 2nd half contour. */
free_contour(half2_x, half2_y, half2_ex, half2_ey);
/* Assign outputs contour to output ponters. */
*ocontour_x = contour_x;
*ocontour_y = contour_y;
*ocontour_ex = contour_ex;
*ocontour_ey = contour_ey;
*oncontour = ncontour;
/* Return the resulting return code form the 2nd call to trace_contour */
/* (the value will either be 0 or LOOP_FOUND). */
return(ret);
}
/*************************************************************************
**************************************************************************
#cat: get_centered_contour - Takes the pixel coordinate of a detected
#cat: minutia feature point and its corresponding/adjacent edge
#cat: pixel and attempts to extract a contour of specified length
#cat: of the feature's edge. The contour is extracted by walking
#cat: the feature's edge a specified number of steps clockwise and
#cat: then counter-clockwise. If a loop is detected while
#cat: extracting the contour, no contour is returned with a return
#cat: code of (LOOP_FOUND). If the process fails to extract a
#cat: a complete contour, a code of INCOMPLETE is returned.
Input:
half_contour - half the length of the extracted contour
(full-length non-loop contour = (half_contourX2)+1)
x_loc - starting x-pixel coord of feature (interior to feature)
y_loc - starting y-pixel coord of feature (interior to feature)
x_edge - x-pixel coord of corresponding edge pixel
(exterior to feature)
y_edge - y-pixel coord of corresponding edge pixel
(exterior to feature)
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 contour (interior to feature)
ocontour_y - y-pixel coords of contour (interior to feature)
ocontour_ex - x-pixel coords of corresponding edge (exterior to feature)
ocontour_ey - y-pixel coords of corresponding edge (exterior to feature)
oncontour - number of contour points returned
Return Code:
Zero - resulting contour was successfully extracted or is empty
LOOP_FOUND - resulting contour forms a complete loop
IGNORE - contour could not be traced due to problem starting
conditions
INCOMPLETE - resulting contour was not long enough
Negative - system error
**************************************************************************/
int get_centered_contour(int **ocontour_x, int **ocontour_y,
int **ocontour_ex, int **ocontour_ey, int *oncontour,
const int half_contour,
const int x_loc, const int y_loc,
const int x_edge, const int y_edge,
unsigned char *bdata, const int iw, const int ih)
{
int max_contour;
int *half1_x, *half1_y, *half1_ex, *half1_ey, nhalf1;
int *half2_x, *half2_y, *half2_ex, *half2_ey, nhalf2;
int *contour_x, *contour_y, *contour_ex, *contour_ey, ncontour;
int i, j, ret;
/* Compute maximum length of complete contour */
/* (2 half contours + feature point). */
max_contour = (half_contour<<1) + 1;
/* Initialize output contour length to 0. */
*oncontour = 0;
/* Get 1st half contour with clockwise neighbor trace. */
ret = trace_contour(&half1_x, &half1_y, &half1_ex, &half1_ey, &nhalf1,
half_contour, x_loc, y_loc, x_loc, y_loc, x_edge, y_edge,
SCAN_CLOCKWISE, bdata, iw, ih);
/* If system error occurred ... */
if(ret < 0){
/* Return error code. */
return(ret);
}
/* If trace was not possible ... */
if(ret == IGNORE)
/* Return IGNORE, with nothing allocated. */
return(IGNORE);
/* If 1st half contour forms a loop ... */
if(ret == LOOP_FOUND){
/* Deallocate loop's contour. */
free_contour(half1_x, half1_y, half1_ex, half1_ey);
/* Return LOOP_FOUND, with nothing allocated. */
return(LOOP_FOUND);
}
/* If 1st half contour not complete ... */
if(nhalf1 < half_contour){
/* Deallocate the partial contour. */
free_contour(half1_x, half1_y, half1_ex, half1_ey);
/* Return, with nothing allocated and contour length equal to 0. */
return(INCOMPLETE);
}
/* Otherwise, we have a complete 1st half contour... */
/* Get 2nd half contour with counter-clockwise neighbor trace. */
/* Use the last point from the first contour trace as the */
/* point to test for a loop when tracing the second contour. */
ret = trace_contour(&half2_x, &half2_y, &half2_ex, &half2_ey, &nhalf2,
half_contour, half1_x[nhalf1-1], half1_y[nhalf1-1],
x_loc, y_loc, x_edge, y_edge,
SCAN_COUNTER_CLOCKWISE, bdata, iw, ih);
/* If system error occurred on 2nd trace ... */
if(ret < 0){
/* Return error code. */
return(ret);
}
/* If 2nd trace was not possible ... */
if(ret == IGNORE){
/* Deallocate the 1st half contour. */
free_contour(half1_x, half1_y, half1_ex, half1_ey);
/* Return, with nothing allocated and contour length equal to 0. */
return(IGNORE);
}
/* If 2nd trace forms a loop ... */
if(ret == LOOP_FOUND){
/* Deallocate 1st and 2nd half contours. */
free_contour(half1_x, half1_y, half1_ex, half1_ey);
free_contour(half2_x, half2_y, half2_ex, half2_ey);
/* Return LOOP_FOUND, with nothing allocated. */
return(LOOP_FOUND);
}
/* If 2nd half contour not complete ... */
if(nhalf2 < half_contour){
/* Deallocate 1st and 2nd half contours. */
free_contour(half1_x, half1_y, half1_ex, half1_ey);
free_contour(half2_x, half2_y, half2_ex, half2_ey);
/* Return, with nothing allocated and contour length equal to 0. */
return(INCOMPLETE);
}
/* Otherwise we have a full 1st half contour and a 2nd half contour */
/* that do not form a loop and are complete. We now need to */
/* concatenate the two half contours into one longer contour. */
/* Allocate output contour list. */
if((ret = allocate_contour(&contour_x, &contour_y,
&contour_ex, &contour_ey, max_contour))){
/* If allcation error, then deallocate memory allocated to */
/* this point in this routine. */
free_contour(half1_x, half1_y, half1_ex, half1_ey);
free_contour(half2_x, half2_y, half2_ex, half2_ey);
/* Return error code. */
return(ret);
}
/* Set the current contour point counter to 0 */
ncontour = 0;
/* Copy 1st half contour into output contour buffers. */
/* This contour was collected clockwise, so it's points */
/* are entered in reverse order of the trace. The result */
/* is the first point in the output contour if farthest */
/* from the starting feature point. */
for(i = 0, j = nhalf1-1; i < nhalf1; i++, j--){
contour_x[i] = half1_x[j];
contour_y[i] = half1_y[j];
contour_ex[i] = half1_ex[j];
contour_ey[i] = half1_ey[j];
ncontour++;
}
/* Deallocate 1st half contour. */
free_contour(half1_x, half1_y, half1_ex, half1_ey);
/* Next, store starting feature point into output contour buffers. */
contour_x[nhalf1] = x_loc;
contour_y[nhalf1] = y_loc;
contour_ex[nhalf1] = x_edge;
contour_ey[nhalf1] = y_edge;
ncontour++;
/* Now, append 2nd half contour to permanent contour buffers. */
for(i = 0, j = nhalf1+1; i < nhalf2; i++, j++){
contour_x[j] = half2_x[i];
contour_y[j] = half2_y[i];
contour_ex[j] = half2_ex[i];
contour_ey[j] = half2_ey[i];
ncontour++;
}
/* Deallocate 2nd half contour. */
free_contour(half2_x, half2_y, half2_ex, half2_ey);
/* Assign outputs contour to output ponters. */
*ocontour_x = contour_x;
*ocontour_y = contour_y;
*ocontour_ex = contour_ex;
*ocontour_ey = contour_ey;
*oncontour = ncontour;
/* Return normally. */
return(0);
}
/*************************************************************************
**************************************************************************
#cat: trace_contour - Takes the pixel coordinate of a detected minutia
#cat: feature point and its corresponding/adjacent edge pixel
#cat: and extracts a contour (up to a specified maximum length)
#cat: of the feature's edge in either a clockwise or counter-
#cat: clockwise direction. A second point is specified, such that
#cat: if this point is encounted while extracting the contour,
#cat: it is to be assumed that a loop has been found and a code
#cat: of (LOOP_FOUND) is returned with the contour. By independently
#cat: specifying this point, successive calls can be made to
#cat: this routine from the same starting point, and loops across
#cat: successive calls can be detected.
Input:
max_len - maximum length of contour to be extracted
x_loop - x-pixel coord of point, if encountered, triggers LOOP_FOUND
y_loop - y-pixel coord of point, if encountered, triggers LOOP_FOUND
x_loc - starting x-pixel coord of feature (interior to feature)
y_loc - starting y-pixel coord of feature (interior to feature)
x_edge - x-pixel coord of corresponding edge pixel (exterior to feature)
y_edge - y-pixel coord of corresponding edge pixel (exterior to feature)
scan_clock - direction in which neighboring pixels are to be scanned
for the next contour pixel
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 contour (interior to feature)
ocontour_y - y-pixel coords of contour (interior to feature)
ocontour_ex - x-pixel coords of corresponding edge (exterior to feature)
ocontour_ey - y-pixel coords of corresponding edge (exterior to feature)
oncontour - number of contour points returned
Return Code:
Zero - resulting contour was successfully allocated and extracted
LOOP_FOUND - resulting contour forms a complete loop
IGNORE - trace is not possible due to state of inputs
Negative - system error
**************************************************************************/
int trace_contour(int **ocontour_x, int **ocontour_y,
int **ocontour_ex, int **ocontour_ey, int *oncontour,
const int max_len, const int x_loop, const int y_loop,
const int x_loc, const int y_loc,
const int x_edge, const int y_edge,
const int scan_clock,
unsigned char *bdata, const int iw, const int ih)
{
int *contour_x, *contour_y, *contour_ex, *contour_ey, ncontour;
int cur_x_loc, cur_y_loc;
int cur_x_edge, cur_y_edge;
int next_x_loc, next_y_loc;
int next_x_edge, next_y_edge;
int i, ret;
/* Check to make sure that the feature and edge values are opposite. */
if(*(bdata+(y_loc*iw)+x_loc) ==
*(bdata+(y_edge*iw)+x_edge))
/* If not opposite, then the trace will not work, so return IGNORE. */
return(IGNORE);
/* Allocate contour buffers. */
if((ret = allocate_contour(&contour_x, &contour_y,
&contour_ex, &contour_ey, max_len))){
/* If allocation error, return code. */
return(ret);
}
/* Set pixel counter to 0. */
ncontour = 0;
/* Set up for finding first contour pixel. */
cur_x_loc = x_loc;
cur_y_loc = y_loc;
cur_x_edge = x_edge;
cur_y_edge = y_edge;
/* Foreach pixel to be collected on the feature's contour... */
for(i = 0; i < max_len; i++){
/* Find the next contour pixel. */
if(next_contour_pixel(&next_x_loc, &next_y_loc,
&next_x_edge, &next_y_edge,
cur_x_loc, cur_y_loc,
cur_x_edge, cur_y_edge,
scan_clock, bdata, iw, ih)){
/* If we trace back around to the specified starting */
/* feature location... */
if((next_x_loc == x_loop) && (next_y_loc == y_loop)){
/* Then we have found a loop, so return what we */
/* have traced to this point. */
*ocontour_x = contour_x;
*ocontour_y = contour_y;
*ocontour_ex = contour_ex;
*ocontour_ey = contour_ey;
*oncontour = ncontour;
return(LOOP_FOUND);
}
/* Otherwise, we found another point on our feature's contour, */
/* so store the new contour point. */
contour_x[i] = next_x_loc;
contour_y[i] = next_y_loc;
contour_ex[i] = next_x_edge;
contour_ey[i] = next_y_edge;
/* Bump the number of points stored. */
ncontour++;
/* Set up for finding next contour pixel. */
cur_x_loc = next_x_loc;
cur_y_loc = next_y_loc;
cur_x_edge = next_x_edge;
cur_y_edge = next_y_edge;
}
/* Otherwise, no new contour point found ... */
else{
/* So, stop short and return the number of pixels found */
/* on the contour to this point. */
*ocontour_x = contour_x;
*ocontour_y = contour_y;
*ocontour_ex = contour_ex;
*ocontour_ey = contour_ey;
*oncontour = ncontour;
/* Return normally. */
return(0);
}
}
/* If we get here, we successfully found the maximum points we */
/* were looking for on the feature contour, so assign the contour */
/* buffers to the output pointers and return. */
*ocontour_x = contour_x;
*ocontour_y = contour_y;
*ocontour_ex = contour_ex;
*ocontour_ey = contour_ey;
*oncontour = ncontour;
/* Return normally. */
return(0);
}
/*************************************************************************
**************************************************************************
#cat: search_contour - Walk the contour of a minutia feature starting at a
#cat: specified point on the feature and walking N steps in the
#cat: specified direction (clockwise or counter-clockwise), looking
#cat: for a second specified point. In this code, "feature" is
#cat: consistently referring to either the black interior edge of
#cat: a ridge-ending or the white interior edge of a valley-ending
#cat: (bifurcation). The term "edge of the feature" refers to
#cat: neighboring pixels on the "exterior" edge of the feature.
#cat: So "edge" pixels are opposite in color from the interior
#cat: feature pixels.
Input:
x_search - x-pixel coord of point being searched for
y_search - y-pixel coord of point being searched for
search_len - number of step to walk contour in search
x_loc - starting x-pixel coord of feature (interior to feature)
y_loc - starting y-pixel coord of feature (interior to feature)
x_edge - x-pixel coord of corresponding edge pixel
(exterior to feature)
y_edge - y-pixel coord of corresponding edge pixel
(exterior to feature)
scan_clock - direction in which neighbor pixels are to be scanned
(clockwise or counter-clockwise)
bdata - binary image data (0==while & 1==black)
iw - width (in pixels) of image
ih - height (in pixels) of image
Return Code:
NOT_FOUND - desired pixel not found along N steps of feature's contour
FOUND - desired pixel WAS found along N steps of feature's contour
**************************************************************************/
int search_contour(const int x_search, const int y_search,
const int search_len,
const int x_loc, const int y_loc,
const int x_edge, const int y_edge,
const int scan_clock,
unsigned char *bdata, const int iw, const int ih)
{
int cur_x_loc, cur_y_loc;
int cur_x_edge, cur_y_edge;
int next_x_loc, next_y_loc;
int next_x_edge, next_y_edge;
int i;
/* Set up for finding first contour pixel. */
cur_x_loc = x_loc;
cur_y_loc = y_loc;
cur_x_edge = x_edge;
cur_y_edge = y_edge;
/* Foreach point to be collected on the feature's contour... */
for(i = 0; i < search_len; i++){
/* Find the next contour pixel. */
if(next_contour_pixel(&next_x_loc, &next_y_loc,
&next_x_edge, &next_y_edge,
cur_x_loc, cur_y_loc,
cur_x_edge, cur_y_edge,
scan_clock, bdata, iw, ih)){
/* If we find the point we are looking for on the contour... */
if((next_x_loc == x_search) && (next_y_loc == y_search)){
/* Then return FOUND. */
return(FOUND);
}
/* Otherwise, set up for finding next contour pixel. */
cur_x_loc = next_x_loc;
cur_y_loc = next_y_loc;
cur_x_edge = next_x_edge;
cur_y_edge = next_y_edge;
}
/* Otherwise, no new contour point found ... */
else{
/* So, stop searching, and return NOT_FOUND. */
return(NOT_FOUND);
}
}
/* If we get here, we successfully searched the maximum points */
/* without finding our desired point, so return NOT_FOUND. */
return(NOT_FOUND);
}
/*************************************************************************
**************************************************************************
#cat: next_contour_pixel - Takes a pixel coordinate of a point determined
#cat: to be on the interior edge of a feature (ridge or valley-
#cat: ending), and attempts to locate a neighboring pixel on the
#cat: feature's contour. Neighbors of the current feature pixel
#cat: are searched in a specified direction (clockwise or counter-
#cat: clockwise) and the first pair of adjacent/neigboring pixels
#cat: found with the first pixel having the color of the feature
#cat: and the second the opposite color are returned as the next
#cat: point on the contour. One exception happens when the new
#cat: point is on an "exposed" corner.
Input:
cur_x_loc - x-pixel coord of current point on feature's
interior contour
cur_y_loc - y-pixel coord of current point on feature's
interior contour
cur_x_edge - x-pixel coord of corresponding edge pixel
(exterior to feature)
cur_y_edge - y-pixel coord of corresponding edge pixel
(exterior to feature)
scan_clock - direction in which neighboring pixels are to be scanned
for the next contour pixel
bdata - binary image data (0==while & 1==black)
iw - width (in pixels) of image
ih - height (in pixels) of image
Output:
next_x_loc - x-pixel coord of next point on feature's interior contour
next_y_loc - y-pixel coord of next point on feature's interior contour
next_x_edge - x-pixel coord of corresponding edge (exterior to feature)
next_y_edge - y-pixel coord of corresponding edge (exterior to feature)
Return Code:
TRUE - next contour point found and returned
FALSE - next contour point NOT found
**************************************************************************/
/*************************************************************************/
int next_contour_pixel(int *next_x_loc, int *next_y_loc,
int *next_x_edge, int *next_y_edge,
const int cur_x_loc, const int cur_y_loc,
const int cur_x_edge, const int cur_y_edge,
const int scan_clock,
unsigned char *bdata, const int iw, const int ih)
{
int feature_pix, edge_pix;
int prev_nbr_pix, prev_nbr_x, prev_nbr_y;
int cur_nbr_pix, cur_nbr_x, cur_nbr_y;
int ni, nx, ny, npix;
int nbr_i, i;
/* Get the feature's pixel value. */
feature_pix = *(bdata + (cur_y_loc * iw) + cur_x_loc);
/* Get the feature's edge pixel value. */
edge_pix = *(bdata + (cur_y_edge * iw) + cur_x_edge);
/* Get the nieghbor position of the feature's edge pixel in relationship */
/* to the feature's actual position. */
/* REMEBER: The feature's position is always interior and on a ridge */
/* ending (black pixel) or (for bifurcations) on a valley ending (white */
/* pixel). The feature's edge pixel is an adjacent pixel to the feature */
/* pixel that is exterior to the ridge or valley ending and opposite in */
/* pixel value. */
nbr_i = start_scan_nbr(cur_x_loc, cur_y_loc, cur_x_edge, cur_y_edge);
/* Set current neighbor scan pixel to the feature's edge pixel. */
cur_nbr_x = cur_x_edge;
cur_nbr_y = cur_y_edge;
cur_nbr_pix = edge_pix;
/* Foreach pixel neighboring the feature pixel ... */
for(i = 0; i < 8; i++){
/* Set current neighbor scan pixel to previous scan pixel. */
prev_nbr_x = cur_nbr_x;
prev_nbr_y = cur_nbr_y;
prev_nbr_pix = cur_nbr_pix;
/* Bump pixel neighbor index clockwise or counter-clockwise. */
nbr_i = next_scan_nbr(nbr_i, scan_clock);
/* Set current scan pixel to the new neighbor. */
/* REMEMBER: the neighbors are being scanned around the original */
/* feature point. */
cur_nbr_x = cur_x_loc + nbr8_dx[nbr_i];
cur_nbr_y = cur_y_loc + nbr8_dy[nbr_i];
/* If new neighbor is not within image boundaries... */
if((cur_nbr_x < 0) || (cur_nbr_x >= iw) ||
(cur_nbr_y < 0) || (cur_nbr_y >= ih))
/* Return (FALSE==>Failure) if neighbor out of bounds. */
return(FALSE);
/* Get the new neighbor's pixel value. */
cur_nbr_pix = *(bdata + (cur_nbr_y * iw) + cur_nbr_x);
/* If the new neighbor's pixel value is the same as the feature's */
/* pixel value AND the previous neighbor's pixel value is the same */
/* as the features's edge, then we have "likely" found our next */
/* contour pixel. */
if((cur_nbr_pix == feature_pix) && (prev_nbr_pix == edge_pix)){
/* Check to see if current neighbor is on the corner of the */
/* neighborhood, and if so, test to see if it is "exposed". */
/* The neighborhood corners have odd neighbor indicies. */
if(nbr_i % 2){
/* To do this, look ahead one more neighbor pixel. */
ni = next_scan_nbr(nbr_i, scan_clock);
nx = cur_x_loc + nbr8_dx[ni];
ny = cur_y_loc + nbr8_dy[ni];
/* If new neighbor is not within image boundaries... */
if((nx < 0) || (nx >= iw) ||
(ny < 0) || (ny >= ih))
/* Return (FALSE==>Failure) if neighbor out of bounds. */
return(FALSE);
npix = *(bdata + (ny * iw) + nx);
/* If the next neighbor's value is also the same as the */
/* feature's pixel, then corner is NOT exposed... */
if(npix == feature_pix){
/* Assign the current neighbor pair to the output pointers. */
*next_x_loc = cur_nbr_x;
*next_y_loc = cur_nbr_y;
*next_x_edge = prev_nbr_x;
*next_y_edge = prev_nbr_y;
/* Return TRUE==>Success. */
return(TRUE);
}
/* Otherwise, corner pixel is "exposed" so skip it. */
else{
/* Skip current corner neighbor by resetting it to the */
/* next neighbor, which upon the iteration will immediately */
/* become the previous neighbor. */
cur_nbr_x = nx;
cur_nbr_y = ny;
cur_nbr_pix = npix;
/* Advance neighbor index. */
nbr_i = ni;
/* Advance neighbor count. */
i++;
}
}
/* Otherwise, current neighbor is not a corner ... */
else{
/* Assign the current neighbor pair to the output pointers. */
*next_x_loc = cur_nbr_x;
*next_y_loc = cur_nbr_y;
*next_x_edge = prev_nbr_x;
*next_y_edge = prev_nbr_y;
/* Return TRUE==>Success. */
return(TRUE);
}
}
}
/* If we get here, then we did not find the next contour pixel */
/* within the 8 neighbors of the current feature pixel so */
/* return (FALSE==>Failure). */
/* NOTE: This must mean we found a single isolated pixel. */
/* Perhaps this should be filled? */
return(FALSE);
}
/*************************************************************************
**************************************************************************
#cat: start_scan_nbr - Takes a two pixel coordinates that are either
#cat: aligned north-to-south or east-to-west, and returns the
#cat: position the second pixel is in realtionship to the first.
#cat: The positions returned are based on 8-connectedness.
#cat: NOTE, this routine does NOT account for diagonal positions.
Input:
x_prev - x-coord of first point
y_prev - y-coord of first point
x_next - x-coord of second point
y_next - y-coord of second point
Return Code:
NORTH - second pixel above first
SOUTH - second pixel below first
EAST - second pixel right of first
WEST - second pixel left of first
**************************************************************************/