-
Notifications
You must be signed in to change notification settings - Fork 3
/
detect.c
586 lines (518 loc) · 20.1 KB
/
detect.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
/*******************************************************************************
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: DETECT.C
AUTHOR: Michael D. Garris
DATE: 08/16/1999
UPDATED: 10/04/1999 Version 2 by MDG
UPDATED: 03/16/2005 by MDG
Takes an 8-bit grayscale fingerpinrt image and detects minutiae
as part of the NIST Latent Fingerprint System (LFS).
***********************************************************************
ROUTINES:
lfs_detect_minutiae()
lfs_detect_minutiae_V2()
***********************************************************************/
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include "lfs.h"
/*************************************************************************
#cat: lfs_detect_minutiae - Takes a grayscale fingerprint image (of arbitrary
#cat: size), and returns a map of directional ridge flow in the image
#cat: (2 versions), a binarized image designating ridges from valleys,
#cat: and a list of minutiae (including position, type, direction,
#cat: neighbors, and ridge counts to neighbors).
Input:
idata - input 8-bit grayscale fingerprint image data
iw - width (in pixels) of the image
ih - height (in pixels) of the image
lfsparms - parameters and thresholds for controlling LFS
Output:
ominutiae - resulting list of minutiae
oimap - resulting IMAP
{invalid (-1) or valid ridge directions}
onmap - resulting NMAP
{invalid (-1), high-curvature (-2), blanked blocks {-3} or
valid ridge directions}
omw - width (in blocks) of image maps
omh - height (in blocks) of image maps
obdata - resulting binarized image
{0 = black pixel (ridge) and 255 = white pixel (valley)}
obw - width (in pixels) of the binary image
obh - height (in pixels) of the binary image
Return Code:
Zero - successful completion
Negative - system error
**************************************************************************/
int lfs_detect_minutiae(MINUTIAE **ominutiae,
int **oimap, int **onmap, int *omw, int *omh,
unsigned char **obdata, int *obw, int *obh,
unsigned char *idata, const int iw, const int ih,
const LFSPARMS *lfsparms)
{
unsigned char *pdata, *bdata;
int pw, ph, bw, bh;
DIR2RAD *dir2rad;
DFTWAVES *dftwaves;
ROTGRIDS *dftgrids;
ROTGRIDS *dirbingrids;
int *imap, *nmap, mw, mh;
int ret, maxpad;
MINUTIAE *minutiae;
/******************/
/* INITIALIZATION */
/******************/
/* Determine the maximum amount of image padding required to support */
/* LFS processes. */
maxpad = get_max_padding(lfsparms->blocksize,
lfsparms->dirbin_grid_w, lfsparms->dirbin_grid_h,
lfsparms->isobin_grid_dim);
/* Initialize lookup table for converting integer IMAP directions */
/* to angles in radians. */
if((ret = init_dir2rad(&dir2rad, lfsparms->num_directions))){
/* Free memory allocated to this point. */
return(ret);
}
/* Initialize wave form lookup tables for DFT analyses. */
/* used for direction binarization. */
if((ret = init_dftwaves(&dftwaves, dft_coefs, lfsparms->num_dft_waves,
lfsparms->blocksize))){
/* Free memory allocated to this point. */
free_dir2rad(dir2rad);
return(ret);
}
/* Initialize lookup table for pixel offsets to rotated grids */
/* used for DFT analyses. */
if((ret = init_rotgrids(&dftgrids, iw, ih, maxpad,
lfsparms->start_dir_angle, lfsparms->num_directions,
lfsparms->blocksize, lfsparms->blocksize,
RELATIVE2ORIGIN))){
/* Free memory allocated to this point. */
free_dir2rad(dir2rad);
free_dftwaves(dftwaves);
return(ret);
}
/* Pad input image based on max padding. */
if(maxpad > 0){ /* May not need to pad at all */
if((ret = pad_uchar_image(&pdata, &pw, &ph, idata, iw, ih,
maxpad, lfsparms->pad_value))){
/* Free memory allocated to this point. */
free_dir2rad(dir2rad);
free_dftwaves(dftwaves);
free_rotgrids(dftgrids);
return(ret);
}
}
else{
/* If padding is unnecessary, then copy the input image. */
pdata = (unsigned char *)malloc(iw*ih);
if(pdata == (unsigned char *)NULL){
/* Free memory allocated to this point. */
free_dir2rad(dir2rad);
free_dftwaves(dftwaves);
free_rotgrids(dftgrids);
fprintf(stderr, "ERROR : lfs_detect_minutiae : malloc : pdata\n");
return(-430);
}
memcpy(pdata, idata, iw*ih);
pw = iw;
ph = ih;
}
/* Scale input image to 6 bits [0..63] */
/* !!! Would like to remove this dependency eventualy !!! */
/* But, the DFT computations will need to be changed, and */
/* could not get this work upon first attempt. */
bits_8to6(pdata, pw, ph);
/******************/
/* IMAP */
/******************/
/* Generate IMAP for the input image. */
if((ret = gen_imap(&imap, &mw, &mh, pdata, pw, ph, dir2rad,
dftwaves, dftgrids, lfsparms))){
/* Free memory allocated to this point. */
free_dir2rad(dir2rad);
free_dftwaves(dftwaves);
free_rotgrids(dftgrids);
free(pdata);
return(ret);
}
free_dir2rad(dir2rad);
free_dftwaves(dftwaves);
free_rotgrids(dftgrids);
/* Generate NMAP from the IMAP of the input image. */
if((ret = gen_nmap(&nmap, imap, mw, mh, lfsparms))){
/* Free memory allocated to this point. */
free(pdata);
free(imap);
return(ret);
}
/******************/
/* BINARIZARION */
/******************/
/* Initialize lookup table for pixel offsets to rotated grids */
/* used for directional binarization. */
if((ret = init_rotgrids(&dirbingrids, iw, ih, maxpad,
lfsparms->start_dir_angle, lfsparms->num_directions,
lfsparms->dirbin_grid_w, lfsparms->dirbin_grid_h,
RELATIVE2CENTER))){
/* Free memory allocated to this point. */
free(pdata);
free(imap);
free(nmap);
return(ret);
}
/* Binarize input image based on NMAP information. */
if((ret = binarize(&bdata, &bw, &bh, pdata, pw, ph, nmap, mw, mh,
dirbingrids, lfsparms))){
/* Free memory allocated to this point. */
free(pdata);
free(imap);
free(nmap);
free_rotgrids(dirbingrids);
return(ret);
}
free_rotgrids(dirbingrids);
/* Check dimension of binary image. If they are different from */
/* the input image, then ERROR. */
if((iw != bw) || (ih != bh)){
/* Free memory allocated to this point. */
free(pdata);
free(imap);
free(nmap);
free(bdata);
fprintf(stderr,
"ERROR : lfs_detect_minutiae : binary image has bad dimensions : %d, %d\n",
bw, bh);
return(-431);
}
/******************/
/* DETECTION */
/******************/
/* Convert 8-bit grayscale binary image [0,255] to */
/* 8-bit binary image [0,1]. */
gray2bin(1, 1, 0, bdata, iw, ih);
/* Allocate list of maximum number of minutia pointers. */
if((ret = alloc_minutiae(&minutiae, MAX_MINUTIAE))){
return(ret);
}
/* Detect the minutiae in the binarized image. */
if((ret = detect_minutiae(minutiae, bdata, iw, ih, imap, nmap, mw, mh,
lfsparms))){
/* Free memory allocated to this point. */
free(pdata);
free(imap);
free(nmap);
free(bdata);
return(ret);
}
if((ret = remove_false_minutia(minutiae, bdata, iw, ih, nmap, mw, mh,
lfsparms))){
/* Free memory allocated to this point. */
free(pdata);
free(imap);
free(nmap);
free(bdata);
free_minutiae(minutiae);
return(ret);
}
/******************/
/* RIDGE COUNTS */
/******************/
if((ret = count_minutiae_ridges(minutiae, bdata, iw, ih, lfsparms))){
/* Free memory allocated to this point. */
free(pdata);
free(imap);
free(nmap);
free(bdata);
free_minutiae(minutiae);
return(ret);
}
/******************/
/* WRAP-UP */
/******************/
/* Convert 8-bit binary image [0,1] to 8-bit */
/* grayscale binary image [0,255]. */
gray2bin(1, 255, 0, bdata, iw, ih);
/* Deallocate working memory. */
free(pdata);
/* Assign results to output pointers. */
*oimap = imap;
*onmap = nmap;
*omw = mw;
*omh = mh;
*obdata = bdata;
*obw = bw;
*obh = bh;
*ominutiae = minutiae;
return(0);
}
/*************************************************************************
#cat: lfs_detect_minutiae_V2 - Takes a grayscale fingerprint image (of
#cat: arbitrary size), and returns a set of image block maps,
#cat: a binarized image designating ridges from valleys,
#cat: and a list of minutiae (including position, reliability,
#cat: type, direction, neighbors, and ridge counts to neighbors).
#cat: The image maps include a ridge flow directional map,
#cat: a map of low contrast blocks, a map of low ridge flow blocks.
#cat: and a map of high-curvature blocks.
Input:
idata - input 8-bit grayscale fingerprint image data
iw - width (in pixels) of the image
ih - height (in pixels) of the image
lfsparms - parameters and thresholds for controlling LFS
Output:
ominutiae - resulting list of minutiae
odmap - resulting Direction Map
{invalid (-1) or valid ridge directions}
olcmap - resulting Low Contrast Map
{low contrast (TRUE), high contrast (FALSE)}
olfmap - resulting Low Ridge Flow Map
{low ridge flow (TRUE), high ridge flow (FALSE)}
ohcmap - resulting High Curvature Map
{high curvature (TRUE), low curvature (FALSE)}
omw - width (in blocks) of image maps
omh - height (in blocks) of image maps
obdata - resulting binarized image
{0 = black pixel (ridge) and 255 = white pixel (valley)}
obw - width (in pixels) of the binary image
obh - height (in pixels) of the binary image
Return Code:
Zero - successful completion
Negative - system error
**************************************************************************/
int lfs_detect_minutiae_V2(MINUTIAE **ominutiae,
int **odmap, int **olcmap, int **olfmap, int **ohcmap,
int *omw, int *omh,
unsigned char **obdata, int *obw, int *obh,
unsigned char *idata, const int iw, const int ih,
const LFSPARMS *lfsparms)
{
unsigned char *pdata, *bdata;
int pw, ph, bw, bh;
DIR2RAD *dir2rad;
DFTWAVES *dftwaves;
ROTGRIDS *dftgrids;
ROTGRIDS *dirbingrids;
int *direction_map, *low_contrast_map, *low_flow_map, *high_curve_map;
int mw, mh;
int ret, maxpad;
MINUTIAE *minutiae;
/******************/
/* INITIALIZATION */
/******************/
/* Determine the maximum amount of image padding required to support */
/* LFS processes. */
maxpad = get_max_padding_V2(lfsparms->windowsize, lfsparms->windowoffset,
lfsparms->dirbin_grid_w, lfsparms->dirbin_grid_h);
/* Initialize lookup table for converting integer directions */
/* to angles in radians. */
if((ret = init_dir2rad(&dir2rad, lfsparms->num_directions))){
/* Free memory allocated to this point. */
return(ret);
}
/* Initialize wave form lookup tables for DFT analyses. */
/* used for direction binarization. */
if((ret = init_dftwaves(&dftwaves, dft_coefs, lfsparms->num_dft_waves,
lfsparms->windowsize))){
/* Free memory allocated to this point. */
free_dir2rad(dir2rad);
return(ret);
}
/* Initialize lookup table for pixel offsets to rotated grids */
/* used for DFT analyses. */
if((ret = init_rotgrids(&dftgrids, iw, ih, maxpad,
lfsparms->start_dir_angle, lfsparms->num_directions,
lfsparms->windowsize, lfsparms->windowsize,
RELATIVE2ORIGIN))){
/* Free memory allocated to this point. */
free_dir2rad(dir2rad);
free_dftwaves(dftwaves);
return(ret);
}
/* Pad input image based on max padding. */
if(maxpad > 0){ /* May not need to pad at all */
if((ret = pad_uchar_image(&pdata, &pw, &ph, idata, iw, ih,
maxpad, lfsparms->pad_value))){
/* Free memory allocated to this point. */
free_dir2rad(dir2rad);
free_dftwaves(dftwaves);
free_rotgrids(dftgrids);
return(ret);
}
}
else{
/* If padding is unnecessary, then copy the input image. */
pdata = (unsigned char *)malloc(iw*ih);
if(pdata == (unsigned char *)NULL){
/* Free memory allocated to this point. */
free_dir2rad(dir2rad);
free_dftwaves(dftwaves);
free_rotgrids(dftgrids);
fprintf(stderr, "ERROR : lfs_detect_minutiae_V2 : malloc : pdata\n");
return(-580);
}
memcpy(pdata, idata, iw*ih);
pw = iw;
ph = ih;
}
/* Scale input image to 6 bits [0..63] */
/* !!! Would like to remove this dependency eventualy !!! */
/* But, the DFT computations will need to be changed, and */
/* could not get this work upon first attempt. Also, if not */
/* careful, I think accumulated power magnitudes may overflow */
/* doubles. */
bits_8to6(pdata, pw, ph);
/******************/
/* MAPS */
/******************/
/* Generate block maps from the input image. */
if((ret = gen_image_maps(&direction_map, &low_contrast_map,
&low_flow_map, &high_curve_map, &mw, &mh,
pdata, pw, ph, dir2rad, dftwaves, dftgrids, lfsparms))){
/* Free memory allocated to this point. */
free_dir2rad(dir2rad);
free_dftwaves(dftwaves);
free_rotgrids(dftgrids);
free(pdata);
return(ret);
}
/* Deallocate working memories. */
free_dir2rad(dir2rad);
free_dftwaves(dftwaves);
free_rotgrids(dftgrids);
/******************/
/* BINARIZARION */
/******************/
/* Initialize lookup table for pixel offsets to rotated grids */
/* used for directional binarization. */
if((ret = init_rotgrids(&dirbingrids, iw, ih, maxpad,
lfsparms->start_dir_angle, lfsparms->num_directions,
lfsparms->dirbin_grid_w, lfsparms->dirbin_grid_h,
RELATIVE2CENTER))){
/* Free memory allocated to this point. */
free(pdata);
free(direction_map);
free(low_contrast_map);
free(low_flow_map);
free(high_curve_map);
return(ret);
}
/* Binarize input image based on NMAP information. */
if((ret = binarize_V2(&bdata, &bw, &bh,
pdata, pw, ph, direction_map, mw, mh,
dirbingrids, lfsparms))){
/* Free memory allocated to this point. */
free(pdata);
free(direction_map);
free(low_contrast_map);
free(low_flow_map);
free(high_curve_map);
free_rotgrids(dirbingrids);
return(ret);
}
/* Deallocate working memory. */
free_rotgrids(dirbingrids);
/* Check dimension of binary image. If they are different from */
/* the input image, then ERROR. */
if((iw != bw) || (ih != bh)){
/* Free memory allocated to this point. */
free(pdata);
free(direction_map);
free(low_contrast_map);
free(low_flow_map);
free(high_curve_map);
free(bdata);
fprintf(stderr, "ERROR : lfs_detect_minutiae_V2 :");
fprintf(stderr,"binary image has bad dimensions : %d, %d\n",
bw, bh);
return(-581);
}
/******************/
/* DETECTION */
/******************/
/* Convert 8-bit grayscale binary image [0,255] to */
/* 8-bit binary image [0,1]. */
gray2bin(1, 1, 0, bdata, iw, ih);
/* Allocate initial list of minutia pointers. */
if((ret = alloc_minutiae(&minutiae, MAX_MINUTIAE))){
return(ret);
}
/* Detect the minutiae in the binarized image. */
if((ret = detect_minutiae_V2(minutiae, bdata, iw, ih,
direction_map, low_flow_map, high_curve_map,
mw, mh, lfsparms))){
/* Free memory allocated to this point. */
free(pdata);
free(direction_map);
free(low_contrast_map);
free(low_flow_map);
free(high_curve_map);
free(bdata);
return(ret);
}
if((ret = remove_false_minutia_V2(minutiae, bdata, iw, ih,
direction_map, low_flow_map, high_curve_map, mw, mh,
lfsparms))){
/* Free memory allocated to this point. */
free(pdata);
free(direction_map);
free(low_contrast_map);
free(low_flow_map);
free(high_curve_map);
free(bdata);
free_minutiae(minutiae);
return(ret);
}
/******************/
/* RIDGE COUNTS */
/******************/
if((ret = count_minutiae_ridges(minutiae, bdata, iw, ih, lfsparms))){
/* Free memory allocated to this point. */
free(pdata);
free(direction_map);
free(low_contrast_map);
free(low_flow_map);
free(high_curve_map);
free_minutiae(minutiae);
return(ret);
}
/******************/
/* WRAP-UP */
/******************/
/* Convert 8-bit binary image [0,1] to 8-bit */
/* grayscale binary image [0,255]. */
gray2bin(1, 255, 0, bdata, iw, ih);
/* Deallocate working memory. */
free(pdata);
/* Assign results to output pointers. */
*odmap = direction_map;
*olcmap = low_contrast_map;
*olfmap = low_flow_map;
*ohcmap = high_curve_map;
*omw = mw;
*omh = mh;
*obdata = bdata;
*obw = bw;
*obh = bh;
*ominutiae = minutiae;
return(0);
}