forked from pavolmarak/openfinger-preprocessor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqualitymap.cpp
More file actions
4776 lines (4104 loc) · 186 KB
/
qualitymap.cpp
File metadata and controls
4776 lines (4104 loc) · 186 KB
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
#include "qualitymap.h"
QualityMap::QualityMap(QObject *parent) : QObject(parent)
{
}
void QualityMap::setParams(const cv::Mat &imgOriginal, QMAP_PARAMS qmapParams)
{
this->imgOriginal = imgOriginal;
this->idata = imgOriginal.data;
this->iw = imgOriginal.cols;
this->ih = imgOriginal.rows;
this->id = 8 * imgOriginal.elemSize();
this->ippi = qmapParams.ppi;
if(this->id != 8){
qDebug() << "Error: Wrong image depth. Image is not grayscale.";
}
}
void QualityMap::computeQualityMap()
{
this->computeImageMaps(); // computes image maps
this->gen_quality_map(); // computes overall image quality map
}
int *QualityMap::getQuality_map() const
{
return quality_map;
}
int QualityMap::getMap_w() const
{
return map_w;
}
int QualityMap::getMap_h() const
{
return map_h;
}
cv::Mat QualityMap::getImgQualityMap()
{
int blockSize = 8;
cv::Mat imgQualityMap = cv::Mat::zeros(this->map_h * blockSize, this->map_w * blockSize, CV_8UC1);
cv::Mat block = cv::Mat(blockSize, blockSize, CV_8UC1);
int cnt = 0;
for (int y = 0; y < this->map_h * blockSize; y += blockSize) {
for (int x = 0; x < this->map_w * blockSize; x += blockSize) {
if (this->quality_map[cnt] == 4) block.setTo(255);
else block.setTo(this->quality_map[cnt] * 63);
block.copyTo(imgQualityMap(cv::Rect(x, y, blockSize, blockSize)));
cnt++;
}
}
return imgQualityMap.rowRange(0, this->ih).colRange(0, this->iw);
}
cv::Mat QualityMap::getQualityMap()
{
int blockSize = 8;
cv::Mat qualityMap = cv::Mat::zeros(this->map_h * blockSize, this->map_w * blockSize, CV_8UC1);
cv::Mat block = cv::Mat(blockSize, blockSize, CV_8UC1);
int cnt = 0;
for (int y = 0; y < this->map_h * blockSize; y += blockSize) {
for (int x = 0; x < this->map_w * blockSize; x += blockSize) {
if (this->quality_map[cnt] == 4) block.setTo(100);
else if (this->quality_map[cnt] == 3) block.setTo(50);
else if (this->quality_map[cnt] == 2) block.setTo(25);
else if (this->quality_map[cnt] == 1) block.setTo(10);
else block.setTo(1);
block.copyTo(qualityMap(cv::Rect(x, y, blockSize, blockSize)));
cnt++;
}
}
//cv::GaussianBlur(qualityMap, qualityMap, cv::Size(121, 121), 10.0, 10.0);
return qualityMap.rowRange(0, this->ih).colRange(0, this->iw);
}
void QualityMap::fill_minutiae(MINUTIAE_VECTOR &minutiae)
{
if(this->minutiae->alloc < minutiae.size()){
qDebug() << "ERROR: Too many minutiae, exceeding max.";
}
int cntr=0;
for(std::tuple<QPoint,int,int,int> minutia : minutiae){
this->minutiae->list[cntr] = (MINUTIAQ*)malloc(sizeof(MINUTIAQ));
this->minutiae->list[cntr]->x = std::get<0>(minutia).x();
this->minutiae->list[cntr]->y = std::get<0>(minutia).y();
cntr++;
}
this->minutiae->num = cntr;
}
void QualityMap::computeImageMaps()
{
unsigned char *pdata;
int pw, ph;
DIR2RAD *dir2rad;
DFTWAVES *dftwaves;
ROTGRIDS *dftgrids;
int ret, maxpad;
/******************/
/* INITIALIZATION */
/******************/
/* Determine the maximum amount of image padding required to support */
/* LFS processes. */
maxpad = get_max_padding_V2(this->lfsparms_V2.windowsize, this->lfsparms_V2.windowoffset,
this->lfsparms_V2.dirbin_grid_w, this->lfsparms_V2.dirbin_grid_h);
/* Initialize lookup table for converting integer directions */
/* to angles in radians. */
if((ret = init_dir2rad(&dir2rad, this->lfsparms_V2.num_directions))){
/* Free memory allocated to this point. */
qDebug() << "Error: init_dir2rad()";
}
/* Initialize wave form lookup tables for DFT analyses. */
/* used for direction binarization. */
if((ret = init_dftwaves(&dftwaves, dft_coefs, this->lfsparms_V2.num_dft_waves,
this->lfsparms_V2.windowsize))){
/* Free memory allocated to this point. */
free_dir2rad(dir2rad);
qDebug() << "Error: init_dftwaves()";
}
/* Initialize lookup table for pixel offsets to rotated grids */
/* used for DFT analyses. */
if((ret = init_rotgrids(&dftgrids, iw, ih, maxpad,
this->lfsparms_V2.start_dir_angle, this->lfsparms_V2.num_directions,
this->lfsparms_V2.windowsize, this->lfsparms_V2.windowsize,
RELATIVE2ORIGIN))){
/* Free memory allocated to this point. */
free_dir2rad(dir2rad);
free_dftwaves(dftwaves);
qDebug() << "Error: init_rotgrids()";
}
/* 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, this->lfsparms_V2.pad_value))){
/* Free memory allocated to this point. */
free_dir2rad(dir2rad);
free_dftwaves(dftwaves);
free_rotgrids(dftgrids);
qDebug() << "Error: pad_uchar_image()";
}
}
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");
qDebug() << "Error: malloc()";
}
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. */
gen_image_maps(&(this->direction_map),
&(this->low_contrast_map),
&(this->low_flow_map),
&(this->high_curve_map),
&(this->map_w),
&(this->map_h),
pdata, pw, ph,
dir2rad, dftwaves, dftgrids,
&(this->lfsparms_V2));
/* Deallocate working memories. */
free_dir2rad(dir2rad);
free_dftwaves(dftwaves);
free_rotgrids(dftgrids);
/******************/
/* WRAP-UP */
/******************/
/* Deallocate working memory. */
free(pdata);
}
/***********************************************************************
************************************************************************
#cat: gen_quality_map - Takes a direction map, low contrast map, low ridge
#cat: flow map, and high curvature map, and combines them
#cat: into a single map containing 5 levels of decreasing
#cat: quality. This is done through a set of heuristics.
********************************************/
void QualityMap::gen_quality_map()
{
int *QualMap;
int thisX, thisY;
int compX, compY;
int arrayPos, arrayPos2;
int QualOffset;
QualMap = (int *)malloc(this->map_w * this->map_h * sizeof(int));
if(QualMap == (int *)NULL){
fprintf(stderr, "ERROR : gen_quality_map : malloc : QualMap\n");
qDebug() << "ERROR : gen_quality_map : malloc";
}
/* Foreach row of blocks in maps ... */
for(thisY=0; thisY<this->map_h; thisY++){
/* Foreach block in current row ... */
for(thisX=0; thisX<this->map_w; thisX++) {
/* Compute block index. */
arrayPos=(thisY*this->map_w)+thisX;
/* If current block has low contrast or INVALID direction ... */
if(this->low_contrast_map[arrayPos] || this->direction_map[arrayPos]<0)
/* Set block's quality to 0/F. */
QualMap[arrayPos]=0;
else{
/* Set baseline quality before looking at neighbors */
/* (will subtract QualOffset below) */
/* If current block has low flow or high curvature ... */
if(this->low_flow_map[arrayPos] || this->high_curve_map[arrayPos])
/* Set block's quality initially to 3/B. */
QualMap[arrayPos] = 3; /* offset will be -1..-2 */
/* Otherwise, block is NOT low flow AND NOT high curvature... */
else
/* Set block's quality to 4/A. */
QualMap[arrayPos]=4; /* offset will be 0..-2 */
/* If block within NEIGHBOR_DELTA of edge ... */
if(thisY < NEIGHBOR_DELTA || thisY > this->map_h - 1 - NEIGHBOR_DELTA ||
thisX < NEIGHBOR_DELTA || thisX > this->map_w - 1 - NEIGHBOR_DELTA)
/* Set block's quality to 1/E. */
QualMap[arrayPos]=1;
/* Otherwise, test neighboring blocks ... */
else{
/* Initialize quality adjustment to 0. */
QualOffset=0;
/* Foreach row in neighborhood ... */
for(compY=thisY-NEIGHBOR_DELTA;
compY<=thisY+NEIGHBOR_DELTA;compY++){
/* Foreach block in neighborhood */
/* (including current block)... */
for(compX=thisX-NEIGHBOR_DELTA;
compX<=thisX+NEIGHBOR_DELTA;compX++) {
/* Compute neighboring block's index. */
arrayPos2 = (compY*this->map_w)+compX;
/* If neighbor block (which might be itself) has */
/* low contrast or INVALID direction .. */
if(this->low_contrast_map[arrayPos2] ||
this->direction_map[arrayPos2]<0) {
/* Set quality adjustment to -2. */
QualOffset=-2;
/* Done with neighborhood row. */
break;
}
/* Otherwise, if neighbor block (which might be */
/* itself) has low flow or high curvature ... */
else if(this->low_flow_map[arrayPos2] ||
this->high_curve_map[arrayPos2]) {
/* Set quality to -1 if not already -2. */
QualOffset=std::min(QualOffset,-1);
}
}
}
/* Decrement minutia quality by neighborhood adjustment. */
QualMap[arrayPos]+=QualOffset;
}
}
}
}
/* Set output pointer. */
this->quality_map = QualMap;
}
/*************************************************************************
**************************************************************************
#cat: free_minutiae - Takes a minutiae list and deallocates all memory
#cat: associated with it.
Input:
minutiae - pointer to allocated list of minutia structures
*************************************************************************/
void QualityMap::free_minutiae(MINUTIAE *minutiae)
{
int i;
/* Deallocate minutia structures in the list. */
for(i = 0; i < minutiae->num; i++)
free_minutia(minutiae->list[i]);
/* Deallocate list of minutia pointers. */
free(minutiae->list);
/* Deallocate the list structure. */
free(minutiae);
}
/***********************************************************************
************************************************************************
#cat: combined_minutia_quality - Combines quality measures derived from
#cat: the quality map and neighboring pixel statistics to
#cat: infer a reliability measure on the scale [0...1].
Input:
minutiae - structure contining the detected minutia
quality_map - map with blocks assigned 1 of 5 quality levels
map_w - width (in blocks) of the map
map_h - height (in blocks) of the map
blocksize - size (in pixels) of each block in the map
idata - 8-bit grayscale fingerprint image
iw - width (in pixels) of the image
ih - height (in pixels) of the image
id - depth (in pixels) of the image
ppmm - scan resolution of the image in pixels/mm
Output:
minutiae - updated reliability members
************************************************************************/
void QualityMap::combined_minutia_quality(MINUTIAE *minutiae,
int *quality_map, const int mw, const int mh, const int blocksize,
unsigned char *idata, const int iw, const int ih, const int id,
const double ppmm)
{
int ret, i, index, radius_pix;
int *pquality_map, qmap_value;
MINUTIAQ *minutia;
double gs_reliability, reliability;
/* If image is not 8-bit grayscale ... */
if(id != 8){
fprintf(stderr, "ERROR : combined_miutia_quality : ");
fprintf(stderr, "image must pixel depth = %d must be 8 ", id);
fprintf(stderr, "to compute reliability\n");
qDebug() << "ERROR: image is not 8-bit grayscale";
}
/* Compute pixel radius of neighborhood based on image's scan resolution. */
radius_pix = sround(RADIUS_MM * ppmm);
/* Expand block map values to pixel map. */
if((ret = pixelize_map(&pquality_map, iw, ih,
quality_map, mw, mh, blocksize))){
qDebug() << "ERROR: pixelize_map()";
}
/* Foreach minutiae detected ... */
for(i = 0; i < minutiae->num; i++){
/* Assign minutia pointer. */
minutia = minutiae->list[i];
/* Compute reliability from stdev and mean of pixel neighborhood. */
gs_reliability = grayscale_reliability(minutia, idata, iw, ih, radius_pix);
/* Lookup quality map value. */
/* Compute minutia pixel index. */
index = (minutia->y * iw) + minutia->x;
/* Switch on pixel's quality value ... */
qmap_value = pquality_map[index];
/* Combine grayscale reliability and quality map value. */
switch(qmap_value){
/* Quality A : [50..99]% */
case 4 :
reliability = 0.50 + (0.49 * gs_reliability);
break;
/* Quality B : [25..49]% */
case 3 :
reliability = 0.25 + (0.24 * gs_reliability);
break;
/* Quality C : [10..24]% */
case 2 :
reliability = 0.10 + (0.14 * gs_reliability);
break;
/* Quality D : [5..9]% */
case 1 :
reliability = 0.05 + (0.04 * gs_reliability);
break;
/* Quality E : 1% */
case 0 :
reliability = 0.01;
break;
/* Error if quality value not in range [0..4]. */
default:
fprintf(stderr, "ERROR : combined_miutia_quality : ");
fprintf(stderr, "unexpected quality map value %d ", qmap_value);
fprintf(stderr, "not in range [0..4]\n");
free(pquality_map);
qDebug() << "ERROR: combined_miutia_quality, unexpected quality map value, not in range [0..4]";
}
minutia->reliability = reliability;
}
/* NEW 05-08-2002 */
free(pquality_map);
}
/*************************************************************************
**************************************************************************
#cat: pixelize_map - Takes a block image map and assigns each pixel in the
#cat: image its corresponding block value. This allows block
#cat: values in maps to be directly accessed via pixel addresses.
Input:
iw - the width (in pixels) of the corresponding image
ih - the height (in pixels) of the corresponding image
imap - input block image map
mw - the width (in blocks) of the map
mh - the height (in blocks) of the map
blocksize - the dimension (in pixels) of each block
Output:
omap - points to the resulting pixelized map
Return Code:
Zero - successful completion
Negative - system error
**************************************************************************/
int QualityMap::pixelize_map(int **omap, const int iw, const int ih,
int *imap, const int mw, const int mh, const int blocksize)
{
int *pmap;
int ret, x, y;
int *blkoffs, bw, bh, bi;
int *spptr, *pptr;
pmap = (int *)malloc(iw*ih*sizeof(int));
if(pmap == (int *)NULL){
fprintf(stderr, "ERROR : pixelize_map : malloc : pmap\n");
return(-590);
}
if((ret = block_offsets(&blkoffs, &bw, &bh, iw, ih, 0, blocksize))){
return(ret);
}
if((bw != mw) || (bh != mh)){
free(blkoffs);
fprintf(stderr,
"ERROR : pixelize_map : block dimensions do not match\n");
return(-591);
}
for(bi = 0; bi < mw*mh; bi++){
spptr = pmap + blkoffs[bi];
for(y = 0; y < blocksize; y++){
pptr = spptr;
for(x = 0; x < blocksize; x++){
*pptr++ = imap[bi];
}
spptr += iw;
}
}
/* Deallocate working memory. */
free(blkoffs);
/* Assign pixelized map to output pointer. */
*omap = pmap;
/* Return normally. */
return(0);
}
/***********************************************************************
************************************************************************
#cat: grayscale_reliability - Given a minutia point, computes a reliability
#cat: measure from the stdev and mean of its pixel neighborhood.
Code originally written by Austin Hicklin for FBI ATU
Modified by Michael D. Garris (NIST) Sept. 25, 2000
GrayScaleReliability - reasonable reliability heuristic, returns
0.0 .. 1.0 based on stdev and Mean of a localized histogram where
"ideal" stdev is >=64; "ideal" Mean is 127. In a 1 ridge radius
(11 pixels), if the bytevalue (shade of gray) in the image has a
stdev of >= 64 & a mean of 127, returns 1.0 (well defined
light & dark areas in equal proportions).
Input:
minutia - structure containing detected minutia
idata - 8-bit grayscale fingerprint image
iw - width (in pixels) of the image
ih - height (in pixels) of the image
radius_pix - pixel radius of surrounding neighborhood
Return Value:
reliability - computed reliability measure
************************************************************************/
double QualityMap::grayscale_reliability(MINUTIAQ *minutia, unsigned char *idata,
const int iw, const int ih, const int radius_pix)
{
double mean, stdev;
double reliability;
get_neighborhood_stats(&mean, &stdev, minutia, idata, iw, ih, radius_pix);
reliability = std::min((stdev>IDEALSTDEV ? 1.0 : stdev/(double)IDEALSTDEV),
(1.0-(fabs(mean-IDEALMEAN)/(double)IDEALMEAN)));
return(reliability);
}
/***********************************************************************
************************************************************************
#cat: get_neighborhood_stats - Given a minutia point, computes the mean
#cat: and stdev of the 8-bit grayscale pixels values in a
#cat: surrounding neighborhood with specified radius.
Code originally written by Austin Hicklin for FBI ATU
Modified by Michael D. Garris (NIST) Sept. 25, 2000
Input:
minutia - structure containing detected minutia
idata - 8-bit grayscale fingerprint image
iw - width (in pixels) of the image
ih - height (in pixels) of the image
radius_pix - pixel radius of surrounding neighborhood
Output:
mean - mean of neighboring pixels
stdev - standard deviation of neighboring pixels
************************************************************************/
void QualityMap::get_neighborhood_stats(double *mean, double *stdev, MINUTIAQ *minutia,
unsigned char *idata, const int iw, const int ih,
const int radius_pix)
{
int i, x, y, rows, cols;
int n = 0, sumX = 0, sumXX = 0;
int histogram[256];
/* Zero out histogram. */
memset(histogram, 0, 256 * sizeof(int));
/* Set minutia's coordinate variables. */
x = minutia->x;
y = minutia->y;
/* If minutiae point is within sampleboxsize distance of image border, */
/* a value of 0 reliability is returned. */
if ((x < radius_pix) || (x > iw-radius_pix-1) ||
(y < radius_pix) || (y > ih-radius_pix-1)) {
*mean = 0.0;
*stdev = 0.0;
return;
}
/* Foreach row in neighborhood ... */
for(rows = y - radius_pix;
rows <= y + radius_pix;
rows++){
/* Foreach column in neighborhood ... */
for(cols = x - radius_pix;
cols <= x + radius_pix;
cols++){
/* Bump neighbor's pixel value bin in histogram. */
histogram[*(idata+(rows * iw)+cols)]++;
}
}
/* Foreach grayscale pixel bin ... */
for(i = 0; i < 256; i++){
if(histogram[i]){
/* Accumulate Sum(X[i]) */
sumX += (i * histogram[i]);
/* Accumulate Sum(X[i]^2) */
sumXX += (i * i * histogram[i]);
/* Accumulate N samples */
n += histogram[i];
}
}
/* Mean = Sum(X[i])/N */
*mean = sumX/(double)n;
/* Stdev = sqrt((Sum(X[i]^2)/N) - Mean^2) */
*stdev = sqrt((sumXX/(double)n) - ((*mean)*(*mean)));
}
/*************************************************************************
**************************************************************************
#cat: get_max_padding_V2 - Deterines the maximum amount of image pixel padding
#cat: required by all LFS (Version 2) processes. Padding is currently
#cat: required by the rotated grids used in DFT analyses and in
#cat: directional binarization. The NIST generalized code enables
#cat: the parameters governing these processes to be redefined, so a
#cat: check at runtime is required to determine which process
#cat: requires the most padding. By using the maximum as the padding
#cat: factor, all processes will run safely with a single padding of
#cat: the input image avoiding the need to repad for further processes.
Input:
map_windowsize - the size (in pixels) of each window centered about
each block in the image used in DFT analyses
map_windowoffset - the offset (in pixels) from the orgin of the
surrounding window to the origin of the block
dirbin_grid_w - the width (in pixels) of the rotated grids used in
directional binarization
dirbin_grid_h - the height (in pixels) of the rotated grids used in
directional binarization
Return Code:
Non-negative - the maximum padding required for all processes
**************************************************************************/
int QualityMap::get_max_padding_V2(const int map_windowsize, const int map_windowoffset,
const int dirbin_grid_w, const int dirbin_grid_h)
{
int dft_pad, dirbin_pad, max_pad;
double diag;
double pad;
/* 1. Compute pad required for rotated windows used in DFT analyses. */
/* Explanation of DFT padding:
B---------------------
| window |
| |
| |
| A.......______|__________
| : : |
|<-C-->: block: |
<--|--D-->: : | image
| ........ |
| | |
| | |
| | |
----------------------
|
|
|
Pixel A = Origin of entire fingerprint image
= Also origin of first block in image. Each pixel in
this block gets the same DFT results computed from
the surrounding window. Note that in general
blocks are adjacent and non-overlapping.
Pixel B = Origin of surrounding window in which DFT
analysis is conducted. Note that this window is not
completely contained in the image but extends to the
top and to the right.
Distance C = Number of pixels in which the window extends
beyond the image (map_windowoffset).
Distance D = Amount of padding required to hold the entire
rotated window in memory.
*/
/* Compute pad as difference between the MAP windowsize */
/* and the diagonal distance of the window. */
/* (DFT grids are computed with pixel offsets RELATIVE2ORIGIN.) */
diag = sqrt((double)(2.0 * map_windowsize * map_windowsize));
pad = (diag-map_windowsize)/(double)2.0;
/* Need to truncate precision so that answers are consistent */
/* on different computer architectures when rounding doubles. */
pad = trunc_dbl_precision(pad, TRUNC_SCALE);
/* Must add the window offset to the rotational padding. */
dft_pad = sround(pad) + map_windowoffset;
/* 2. Compute pad required for rotated blocks used in directional */
/* binarization. Binarization blocks are applied to each pixel */
/* in the input image. */
diag = sqrt((double)((dirbin_grid_w*dirbin_grid_w)+
(dirbin_grid_h*dirbin_grid_h)));
/* Assumption: all grid centers reside in valid/allocated memory. */
/* (Dirbin grids are computed with pixel offsets RELATIVE2CENTER.) */
pad = (diag-1)/(double)2.0;
/* Need to truncate precision so that answers are consistent */
/* on different computer architectures when rounding doubles. */
pad = trunc_dbl_precision(pad, TRUNC_SCALE);
dirbin_pad = sround(pad);
max_pad = std::max(dft_pad, dirbin_pad);
/* Return the maximum of the two required paddings. This padding will */
/* be sufficiently large for all purposes, so that padding of the */
/* input image will only be required once. */
return(max_pad);
}
/*************************************************************************
**************************************************************************
#cat: init_dir2rad - Allocates and initializes a lookup table containing
#cat: cosine and sine values needed to convert integer IMAP
#cat: directions to angles in radians.
Input:
ndirs - the number of integer directions to be defined in a
semicircle
Output:
optr - points to the allocated/initialized DIR2RAD structure
Return Code:
Zero - successful completion
Negative - system error
**************************************************************************/
int QualityMap::init_dir2rad(DIR2RAD **optr, const int ndirs)
{
DIR2RAD *dir2rad;
int i;
double theta, pi_factor;
double cs, sn;
/* Allocate structure */
dir2rad = (DIR2RAD *)malloc(sizeof(DIR2RAD));
if(dir2rad == (DIR2RAD *)NULL){
fprintf(stderr, "ERROR : init_dir2rad : malloc : dir2rad\n");
return(-10);
}
/* Assign number of directions */
dir2rad->ndirs = ndirs;
/* Allocate cosine vector */
dir2rad->cos = (double *)malloc(ndirs * sizeof(double));
if(dir2rad->cos == (double *)NULL){
/* Free memory allocated to this point. */
free(dir2rad);
fprintf(stderr, "ERROR : init_dir2rad : malloc : dir2rad->cos\n");
return(-11);
}
/* Allocate sine vector */
dir2rad->sin = (double *)malloc(ndirs * sizeof(double));
if(dir2rad->sin == (double *)NULL){
/* Free memory allocated to this point. */
free(dir2rad->cos);
free(dir2rad);
fprintf(stderr, "ERROR : init_dir2rad : malloc : dir2rad->sin\n");
return(-12);
}
/* Pi_factor sets the period of the trig functions to NDIRS units in x. */
/* For example, if NDIRS==16, then pi_factor = 2(PI/16) = .3926... */
pi_factor = 2.0*M_PI_MINDTCT/(double)ndirs;
/* Now compute cos and sin values for each direction. */
for (i = 0; i < ndirs; ++i) {
theta = (double)(i * pi_factor);
cs = cos(theta);
sn = sin(theta);
/* Need to truncate precision so that answers are consistent */
/* on different computer architectures. */
cs = trunc_dbl_precision(cs, TRUNC_SCALE);
sn = trunc_dbl_precision(sn, TRUNC_SCALE);
dir2rad->cos[i] = cs;
dir2rad->sin[i] = sn;
}
*optr = dir2rad;
return(0);
}
/*************************************************************************
**************************************************************************
#cat: init_dftwaves - Allocates and initializes a set of wave forms needed
#cat: to conduct DFT analysis on blocks of the input image
Input:
dft_coefs - array of multipliers used to define the frequency for
each wave form to be computed
nwaves - number of wave forms to be computed
blocksize - the width and height of each block of image data to
be DFT analyzed
Output:
optr - points to the allocated/initialized DFTWAVES structure
Return Code:
Zero - successful completion
Negative - system error
**************************************************************************/
int QualityMap::init_dftwaves(DFTWAVES **optr, const double *dft_coefs,
const int nwaves, const int blocksize)
{
DFTWAVES *dftwaves;
int i, j;
double pi_factor, freq, x;
double *cptr, *sptr;
/* Allocate structure */
dftwaves = (DFTWAVES *)malloc(sizeof(DFTWAVES));
if(dftwaves == (DFTWAVES *)NULL){
fprintf(stderr, "ERROR : init_dftwaves : malloc : dftwaves\n");
return(-20);
}
/* Set number of DFT waves */
dftwaves->nwaves = nwaves;
/* Set wave length of the DFT waves (they all must be the same length) */
dftwaves->wavelen = blocksize;
/* Allocate list of wave pointers */
dftwaves->waves = (DFTWAVE **)malloc(nwaves * sizeof(DFTWAVE *));
if(dftwaves == (DFTWAVES *)NULL){
/* Free memory allocated to this point. */
free(dftwaves);
fprintf(stderr, "ERROR : init_dftwaves : malloc : dftwaves->waves\n");
return(-21);
}
/* Pi_factor sets the period of the trig functions to BLOCKSIZE units */
/* in x. For example, if BLOCKSIZE==24, then */
/* pi_factor = 2(PI/24) = .26179... */
pi_factor = 2.0*M_PI_MINDTCT/(double)blocksize;
/* Foreach of 4 DFT frequency coef ... */
for (i = 0; i < nwaves; ++i) {
/* Allocate wave structure */
dftwaves->waves[i] = (DFTWAVE *)malloc(sizeof(DFTWAVE));
if(dftwaves->waves[i] == (DFTWAVE *)NULL){
/* Free memory allocated to this point. */
{ int _j; for(_j = 0; _j < i; _j++){
free(dftwaves->waves[_j]->cos);
free(dftwaves->waves[_j]->sin);
free(dftwaves->waves[_j]);
}}
free(dftwaves->waves);
free(dftwaves);
fprintf(stderr,
"ERROR : init_dftwaves : malloc : dftwaves->waves[i]\n");
return(-22);
}
/* Allocate cosine vector */
dftwaves->waves[i]->cos = (double *)malloc(blocksize * sizeof(double));
if(dftwaves->waves[i]->cos == (double *)NULL){
/* Free memory allocated to this point. */
{ int _j; for(_j = 0; _j < i; _j++){
free(dftwaves->waves[_j]->cos);
free(dftwaves->waves[_j]->sin);
free(dftwaves->waves[_j]);
}}
free(dftwaves->waves[i]);
free(dftwaves->waves);
free(dftwaves);
fprintf(stderr,
"ERROR : init_dftwaves : malloc : dftwaves->waves[i]->cos\n");
return(-23);
}
/* Allocate sine vector */
dftwaves->waves[i]->sin = (double *)malloc(blocksize * sizeof(double));
if(dftwaves->waves[i]->sin == (double *)NULL){
/* Free memory allocated to this point. */
{ int _j; for(_j = 0; _j < i; _j++){
free(dftwaves->waves[_j]->cos);
free(dftwaves->waves[_j]->sin);
free(dftwaves->waves[_j]);
}}
free(dftwaves->waves[i]->cos);
free(dftwaves->waves[i]);
free(dftwaves->waves);
free(dftwaves);
fprintf(stderr,
"ERROR : init_dftwaves : malloc : dftwaves->waves[i]->sin\n");
return(-24);
}
/* Assign pointer nicknames */
cptr = dftwaves->waves[i]->cos;
sptr = dftwaves->waves[i]->sin;
/* Compute actual frequency */
freq = pi_factor * dft_coefs[i];
/* Used as a 1D DFT on a 24 long vector of pixel sums */
for (j = 0; j < blocksize; ++j) {
/* Compute sample points from frequency */
x = freq * (double)j;
/* Store cos and sin components of sample point */
*cptr++ = cos(x);
*sptr++ = sin(x);
}
}
*optr = dftwaves;
return(0);
}
/*************************************************************************
**************************************************************************
#cat: free_dir2rad - Deallocates memory associated with a DIR2RAD structure
Input:
dir2rad - pointer to memory to be freed
*************************************************************************/
void QualityMap::free_dir2rad(DIR2RAD *dir2rad)
{
free(dir2rad->cos);
free(dir2rad->sin);
free(dir2rad);
}
/*************************************************************************
**************************************************************************
#cat: free_dftwaves - Deallocates the memory associated with a DFTWAVES
#cat: structure
Input:
dftwaves - pointer to memory to be freed
**************************************************************************/
void QualityMap::free_dftwaves(DFTWAVES *dftwaves)
{
int i;
for(i = 0; i < dftwaves->nwaves; i++){
free(dftwaves->waves[i]->cos);
free(dftwaves->waves[i]->sin);
free(dftwaves->waves[i]);
}
free(dftwaves->waves);
free(dftwaves);
}
/*************************************************************************
**************************************************************************
#cat: free_rotgrids - Deallocates the memory associated with a ROTGRIDS
#cat: structure
Input:
rotgrids - pointer to memory to be freed
**************************************************************************/
void QualityMap::free_rotgrids(ROTGRIDS *rotgrids)
{
int i;
for(i = 0; i < rotgrids->ngrids; i++)
free(rotgrids->grids[i]);
free(rotgrids->grids);
free(rotgrids);
}
/*************************************************************************
**************************************************************************
#cat: free_dir_powers - Deallocate memory associated with DFT power vectors
Input:
powers - vectors of DFT power values (N Waves X M Directions)
nwaves - number of DFT wave forms used
**************************************************************************/
void QualityMap::free_dir_powers(double **powers, const int nwaves)
{
int w;
for(w = 0; w < nwaves; w++)
free(powers[w]);
free(powers);
}
/*************************************************************************
**************************************************************************
#cat: init_rotgrids - Allocates and initializes a set of offsets that address
#cat: individual rotated pixels within a grid.
#cat: These rotated grids are used to conduct DFT analyses
#cat: on blocks of input image data, and they are used
#cat: in isotropic binarization.
Input:
iw - width (in pixels) of the input image
ih - height (in pixels) of the input image
pad - designates the number of pixels to be padded to the perimeter