Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
648 lines (567 loc) · 22.6 KB
/
Copy pathMain.java
File metadata and controls
648 lines (567 loc) · 22.6 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
package cs107;
import java.util.ArrayList;
import java.util.List;
/**
* This class will not be graded. You can use it to test your program.
*/
public class Main {
/**
* Main entry point of the program.
*
* @param args the command lines arguments of the program.
*/
public static void main(String[] args) {
//---------------------------
// Tests functions separately
//---------------------------
//testGetNeighbours();
//testTransitions();
//testIdentical();
//testThin();
//testConnectedPixels1();
//testConnectedPixels2();
//testConnectedPixels3();
testConnectedPixels4();
//testComputeSlope();
//testComputeSlope2();
//testComputeSlope3();
//testComputeAngle1();
//testComputeAngle2();
//testOrientation();
//testOrientation2();
//testOrientation3();
//testApplyRotation();
//testApplyTranslation();
//testWithSkeleton();
//testDrawSkeleton("1_1"); //draw skeleton of fingerprint 1_1.png
//testDrawSkeleton("1_2"); //draw skeleton of fingerprint 1_2.png
//testDrawSkeleton("2_1"); //draw skeleton of fingerprint 2_1.png
//testDrawMinutiae("1_1"); //draw minutiae of fingerprint 1_1.png
//testDrawMinutiae2("1_1_small");
//testDrawMinutiae2("skeletonTest");
//testDrawMinutiae("1_2"); //draw minutiae of fingerprint 1_2.png
//testDrawMinutiae("2_1"); //draw minutiae of fingerprint 2_1.png
//---------------------------
// Test overall functionality
//---------------------------
//compare 1_1.png with 1_2.png: they are supposed to match
//testCompareFingerprints("1_1", "1_2", true); //expected match: true
//compare 1_1.png with 2_1.png: they are not supposed to match
//testCompareFingerprints("1_1", "2_1", false); //expected match: false
//testCompareFingerprints("1_1", "1_2", true);
//compare 1_1 with all other images of the same finger
//testCompareAllFingerprints("1_1", 1, true);
//compare 1_1 with all images of finger 2
//testCompareAllFingerprints("1_1", 2, false);
//compare 1_1 with all images of finger 3 to 16
// for (int f = 3; f <= 16; f++) {
// testCompareAllFingerprints("1_1", f, false);
// }
}
/**
* This function is here to help you test the functionalities of
* getNeighbours. You are free to modify and/or delete it.
*/
public static void testGetNeighbours() {
//1: Array is one black pixel
System.out.print("testGetNeighbours 1: ");
boolean[][] image = {{true}};
boolean[] neighbours = Fingerprint.getNeighbours(image, 0, 0);
boolean[] expected = {false, false, false, false,
false, false, false, false};
if (arrayEqual(neighbours, expected)) {
System.out.println("OK");
} else {
System.out.println("ERROR");
System.out.print("Expected: ");
printArray(expected);
System.out.print("Computed: ");
printArray(neighbours);
}
//2: Array is 2 black pixels
System.out.print("testGetNeighbours 2: ");
boolean[][] image2 = {{true, true}};
boolean[] neighbours2 = Fingerprint.getNeighbours(image2, 0, 0);
boolean[] expected2 = {false, false, true, false,
false, false, false, false};
if (arrayEqual(neighbours2, expected2)) {
System.out.println("OK");
} else {
System.out.println("ERROR");
System.out.print("Expected: ");
printArray(expected2);
System.out.print("Computed: ");
printArray(neighbours2);
}
}
/**
* This function is here to test the functionalities of transitions
*/
public static void testTransitions() {
//1: pixel surrounded by white pixels
System.out.print("testTransitions1: ");
boolean[] listNeighbours1 = {false,false,false,false,false,false,false,false};
int transitions1 = Fingerprint.transitions(listNeighbours1);
int expected1 = 0;
if (expected1 == transitions1) {
System.out.println("OK");
} else {
System.out.println("ERROR");
System.out.println("Expected: " + expected1);
System.out.println("Computed: " + transitions1);
}
//2: pixel surrounded by black pixels
System.out.print("testTransitions2: ");
boolean[] listNeighbours2 = {true,true,true,true,true,true,true,true};
int transitions2 = Fingerprint.transitions(listNeighbours2);
int expected2 = 0;
if (expected2 == transitions2) {
System.out.println("OK");
} else {
System.out.println("ERROR");
System.out.println("Expected: " + expected2);
System.out.println("Computed: " + transitions2);
}
//3: random assortment
System.out.print("testTransitions3: ");
boolean[] listNeighbours3 = {true,false,false,false,true,false,true,false};
int transitions3 = Fingerprint.transitions(listNeighbours3);
int expected3 = 3;
if (expected3 == transitions3) {
System.out.println("OK");
} else {
System.out.println("ERROR");
System.out.println("Expected: " + expected3);
System.out.println("Computed: " + transitions3);
}
}
/**
* This function is here to test the functionalities of identical
*/
public static void testIdentical() {
//1: different sized images
System.out.print("testIdentical1: ");
boolean[][] image1_1 = {{false},{false}};
boolean[][] image1_2 = {{false}};
boolean isIdentical1 = Fingerprint.identical(image1_1, image1_2);
boolean expected1 = false;
if (expected1 == isIdentical1) {
System.out.println("OK");
} else {
System.out.println("ERROR");
System.out.println("Expected: " + expected1);
System.out.println("Computed: " + isIdentical1);
}
}
/**
* This function is here to help you test the functionalities of
* connectedPixels. You are free to modify and/or delete it.
*/
public static void testConnectedPixels1() {
System.out.print("testConnectedPixels1: ");
boolean[][] image = {{true, false, false, true},
{false, false, true, true},
{false, true, true, false},
{false, false, false, false}};
boolean[][] expected = {{false, false, false, true},
{false, false, true, true},
{false, true, true, false},
{false, false, false, false}};
boolean[][] connectedPixels = Fingerprint.connectedPixels(image, 2, 1, 10);
if (arrayEqual(connectedPixels, expected)) {
System.out.println("OK");
} else {
System.out.println("ERROR");
System.out.print("Expected: ");
printArray(expected);
System.out.print("Computed: ");
printArray(connectedPixels);
}
}
public static void testConnectedPixels2() {
System.out.print("testConnectedPixels2: ");
boolean[][] image = {{true, false, false, true},
{false, false, true, true},
{false, true, true, false},
{false, false, false, false}};
boolean[][] expected = {{false, false, false, false},
{false, false, true, false},
{false, true, true, false},
{false, false, false, false}};
boolean[][] connectedPixels = Fingerprint.connectedPixels(image, 2, 1, 1);
if (arrayEqual(connectedPixels, expected)) {
System.out.println("OK");
} else {
System.out.println("ERROR");
System.out.print("Expected: ");
printArray(expected);
System.out.print("Computed: ");
printArray(connectedPixels);
}
}
public static void testConnectedPixels3() {
System.out.print("testConnectedPixels3: ");
boolean[][] image = {{true, false, false, true, true},
{true, false, true, true, false},
{true, true, false, false, false},
{false, true, false, true, false}};
boolean[][] expected = {{true, false, false, true, false},
{true, false, true, true, false},
{true, true, false, false, false},
{false, true, false, false, false}};
boolean[][] connectedPixels = Fingerprint.connectedPixels(image, 2, 1, 2);
if (arrayEqual(connectedPixels, expected)) {
System.out.println("OK");
} else {
System.out.println("ERROR");
System.out.print("Expected: ");
printArray(expected);
System.out.print("Computed: ");
printArray(connectedPixels);
}
}
public static void testConnectedPixels4() {
System.out.print("testConnectedPixels4: ");
boolean[][] image = {{false, false, false, false, false},
{false, false, false, false, false},
{true, true, true, true, false},
{false, false, false, false, false}};
boolean[][] expected = {{false, false, false, false, false},
{false, false, false, false, false},
{true, true, true, true, false},
{false, false, false, false, false}};
boolean[][] connectedPixels = Fingerprint.connectedPixels(image, 2, 1, 2);
if (arrayEqual(connectedPixels, expected)) {
System.out.println("OK");
} else {
System.out.println("ERROR");
System.out.print("Expected: ");
printArray(expected);
System.out.print("Computed: ");
printArray(connectedPixels);
}
}
/**
* This function is here to test the functionalities of computeSlope
*/
public static void testComputeSlope1() {
System.out.print("testComputeSlope1: ");
boolean[][] image = {{false, false, false, true, false},
{false, false, true, false, false},
{false, true, false, false, false},
{false, false, false, false, false}};
double expected = 1.0;
double slope = Fingerprint.computeSlope(image, 2, 1);
if (expected == slope) {
System.out.println("OK");
} else {
System.out.println("ERROR");
System.out.print("Expected: ");
System.out.println(expected);
System.out.print("Computed: ");
System.out.println(slope);
}
}
//vertical slope
public static void testComputeSlope2() {
System.out.print("testComputeSlope2: ");
boolean[][] image2 = {{false, true, false, false, false},
{false, true, false, false, false},
{false, true, false, false, false},
{false, false, false, false, false}};
double expected = Double.POSITIVE_INFINITY;
double slope = Fingerprint.computeSlope(image2, 2, 1);
if (expected == slope) {
System.out.println("OK");
} else {
System.out.println("ERROR");
System.out.print("Expected: ");
System.out.println(expected);
System.out.print("Computed: ");
System.out.println(slope);
}
}
//arbitrary
public static void testComputeSlope3() {
System.out.print("testComputeSlope3: ");
boolean[][] image3 = {{false, false, false, false, false},
{false, false, false, false, true},
{false, false, false, true, false},
{true, true, true, false, false}};
double expected = 11.0/30.0;
double slope = Fingerprint.computeSlope(image3, 3, 0);
if (expected == slope) {
System.out.println("OK");
} else {
System.out.println("ERROR");
System.out.print("Expected: ");
System.out.println(expected);
System.out.print("Computed: ");
System.out.println(slope);
}
}
/**
* This function is here to test the functionalities of computeSlope
*/
//vertical lines of connected pixels
public static void testComputeAngle() {
System.out.print("testComputeAngle1: ");
boolean[][] image1 = {{false, true, false, false, false},
{false, true, false, false, false},
{false, true, false, false, false},
{false, false, false, false, false}};
double expected = Math.PI/2;
double angle = Fingerprint.computeAngle(image1, 2, 1, Double.POSITIVE_INFINITY);
if (expected == angle) {
System.out.println("OK");
} else {
System.out.println("ERROR");
System.out.print("Expected: ");
System.out.println(expected);
System.out.print("Computed: ");
System.out.println(angle);
}
}
//horizontal line of connected pixels
public static void testComputeAngle2() {
System.out.print("testComputeAngle2: ");
boolean[][] image = {{false, false, false, false, false},
{false, false, false, false, false},
{false, true, true, true, false},
{false, false, false, false, false}};
double expected = 0.0;
double angle = Fingerprint.computeAngle(image, 2, 1, 0);
if (expected == angle) {
System.out.println("OK");
} else {
System.out.println("ERROR");
System.out.print("Expected: ");
System.out.println(expected);
System.out.print("Computed: ");
System.out.println(angle);
}
}
/**
* This function is here to help you test the functionalities of
* computeOrientation. You are free to modify and/or delete it.
*/
public static void testOrientation() {
boolean[][] image = {{true, false, false, true},
{false, false, true, true},
{false, true, true, false},
{false, false, false, false}};
int angle = Fingerprint.computeOrientation(image, 2, 1, 3);
System.out.println("Expected angle: 35\t Computed angle: " + angle);
}
public static void testOrientation2() {
boolean[][] image = {{true, false, false, true},
{false, false, true, false},
{false, true, false, false},
{false, false, false, false}};
int angle = Fingerprint.computeOrientation(image, 2, 1, 3);
System.out.println("Expected angle: 45\t Computed angle: " + angle);
}
public static void testOrientation3() {
boolean[][] image = {{true, false, false, true},
{false, false, true, false},
{false, true, false, false},
{false, false, false, false}};
int angle = Fingerprint.computeOrientation(image, 0, 3, 3);
System.out.println("Expected angle: 45+180\t Computed angle: " + angle);
}
/**
* This function is here to help you test the functionalities of
* applyRotation. You are free to modify and/or delete it.
*/
public static void testApplyRotation() {
// minutia, centerRow, centerCol, rotation)
int[] minutia = new int[] {1, 3, 10};
int[] result = Fingerprint.applyRotation(minutia, 0, 0, 0);
System.out.println("Expected: 1,3,10");
System.out.print("Computed: ");
printArray(result);
result = Fingerprint.applyRotation(minutia, 10, 5, 0);
System.out.println("Expected: 1,3,10");
System.out.print("Computed: ");
printArray(result);
result = Fingerprint.applyRotation(minutia, 0, 0, 90);
System.out.println("Expected: -3,1,100");
System.out.print("Computed: ");
printArray(result);
result = Fingerprint.applyRotation(new int[] {0, 3, 10}, 0, 0, 90);
System.out.println("Expected: -3,0,100");
System.out.print("Computed: ");
printArray(result);
result = Fingerprint.applyRotation(new int[] {3, 0, 10}, 0, 0, 90);
System.out.println("Expected: 0,3,100");
System.out.print("Computed: ");
printArray(result);
}
/**
* This function is here to help you test the functionalities of
* applyTranslation. You are free to modify and/or delete it.
*/
public static void testApplyTranslation() {
// minutia, rowTranslation, colTranslation)
int[] result = Fingerprint.applyTranslation(new int[] {1, 3, 10}, 0, 0);
System.out.println("Expected: 1,3,10");
System.out.print("Computed: ");
printArray(result);
result = Fingerprint.applyTranslation(new int[] {1, 3, 10}, 10, 5);
System.out.println("Expected: -9,-2,10");
System.out.print("Computed: ");
printArray(result);
}
/**
* This function is here to help you test the functionalities of extract.
* It will read the first fingerprint and extract the minutiae. It will save
* the thinned version as skeleton_1_1.png and a version where the minutiae
* are drawn on top as minutiae_1_1.png. You are free to modify and/or delete
* it.
*/
public static void testThin() {
boolean[][] image1 = Helper.readBinary("resources/test_inputs/1_1_small.png");
boolean[][] skeleton1 = Fingerprint.thin(image1);
Helper.writeBinary("skeleton_1_1_small.png", skeleton1);
}
public static void testDrawSkeleton(String name) {
boolean[][] image1 = Helper.readBinary("resources/fingerprints/" + name + ".png");
boolean[][] skeleton1 = Fingerprint.thin(image1);
Helper.writeBinary("skeleton_" + name + ".png", skeleton1);
}
public static void testDrawMinutiae(String name) {
boolean[][] image1 = Helper.readBinary("resources/fingerprints/" + name + ".png");
boolean[][] skeleton1 = Fingerprint.thin(image1);
List<int[]> minutia1 = Fingerprint.extract(skeleton1);
int[][] colorImageSkeleton1 = Helper.fromBinary(skeleton1);
Helper.drawMinutia(colorImageSkeleton1, minutia1);
Helper.writeARGB("minutiae_" + name + ".png", colorImageSkeleton1);
}
public static void testDrawMinutiae2(String name) {
boolean[][] image1 = Helper.readBinary("resources/test_inputs/" + name + ".png");
boolean[][] skeleton1 = Fingerprint.thin(image1);
List<int[]> minutia1 = Fingerprint.extract(skeleton1);
int[][] colorImageSkeleton1 = Helper.fromBinary(skeleton1);
Helper.drawMinutia(colorImageSkeleton1, minutia1);
Helper.writeARGB("minutiae_" + name + ".png", colorImageSkeleton1);
}
/**
* This function is here to help you test the functionalities of extract
* without using the function thin. It will read the first fingerprint and
* extract the minutiae. It will save a version where the minutiae are drawn
* on top as minutiae_skeletonTest.png. You are free to modify and/or delete
* it.
*/
public static void testWithSkeleton() {
boolean[][] skeleton1 = Helper.readBinary("resources/test_inputs/skeletonTest.png");
List<int[]> minutiae1 = Fingerprint.extract(skeleton1);
List<int[]> expected = new ArrayList<int[]>();
expected.add(new int[] {39, 21, 264});
expected.add(new int[] {53, 33, 270});
System.out.print("Expected minutiae: ");
printMinutiae(expected);
System.out.print("Computed minutiae: ");
printMinutiae(minutiae1);
// Draw the minutiae on top of the thinned image
int[][] colorImageSkeleton1 = Helper.fromBinary(skeleton1);
Helper.drawMinutia(colorImageSkeleton1, minutiae1);
Helper.writeARGB("minutiae_skeletonTest.png", colorImageSkeleton1);
}
public static void printMinutiae(List<int[]> minutiae) {
for (int[] minutia : minutiae) {
System.out.print("[");
for (int j = 0; j < minutia.length; j++) {
System.out.print(minutia[j]);
if (j != minutia.length - 1)
System.out.print(", ");
}
System.out.println("],");
}
}
/**
* This function is here to help you test the overall functionalities. It will
* compare the fingerprint in the file name1.png with the fingerprint in the
* file name2.png. The third parameter indicates if we expected a match or not.
*/
public static void testCompareFingerprints(String name1, String name2, boolean expectedResult) {
boolean[][] image1 = Helper.readBinary("resources/fingerprints/" + name1 + ".png");
// Helper.show(Helper.fromBinary(image1), "Image1");
boolean[][] skeleton1 = Fingerprint.thin(image1);
//Helper.writeBinary("skeleton_" + name1 + ".png", skeleton1);
List<int[]> minutiae1 = Fingerprint.extract(skeleton1);
//printMinutiae(minutiae1);
//int[][] colorImageSkeleton1 = Helper.fromBinary(skeleton1);
//Helper.drawMinutia(colorImageSkeleton1, minutiae1);
//Helper.writeARGB("./minutiae_" + name1 + ".png", colorImageSkeleton1);
boolean[][] image2 = Helper.readBinary("resources/fingerprints/" + name2 + ".png");
boolean[][] skeleton2 = Fingerprint.thin(image2);
List<int[]> minutiae2 = Fingerprint.extract(skeleton2);
//int[][] colorImageSkeleton2 = Helper.fromBinary(skeleton2);
//Helper.drawMinutia(colorImageSkeleton2, minutiae2);
//Helper.writeARGB("./minutiae_" + name2 + ".png", colorImageSkeleton2);
boolean isMatch = Fingerprint.match(minutiae1, minutiae2);
System.out.print("Compare " + name1 + " with " + name2);
System.out.print(". Expected match: " + expectedResult);
System.out.println(" Computed match: " + isMatch);
}
/**
* This function is here to help you test the overall functionalities. It will
* compare the fingerprint in the file <code>name1.png</code> with all the eight
* fingerprints of the given finger (second parameter).
* The third parameter indicates if we expected a match or not.
*/
public static void testCompareAllFingerprints(String name1, int finger, boolean expectedResult) {
for (int i = 1; i <= 8; i++) {
testCompareFingerprints(name1, finger + "_" + i, expectedResult);
}
}
/*
* Helper functions to print and compare arrays
*/
public static boolean arrayEqual(boolean[] array1, boolean[] array2) {
if (array1 == null && array2 == null)
return true;
if (array1 == null || array2 == null)
return false;
if (array1.length != array2.length)
return false;
for (int i = 0; i < array1.length; i++) {
if (array1[i] != array2[i])
return false;
}
return true;
}
public static boolean arrayEqual(boolean[][] array1, boolean[][] array2) {
if (array1 == null && array2 == null)
return true;
if (array1 == null || array2 == null)
return false;
if (array1.length != array2.length)
return false;
for (int i = 0; i < array1.length; i++) {
if (!arrayEqual(array1[i], array2[i]))
return false;
}
return true;
}
public static void printArray(boolean[][] array) {
for (boolean[] row : array) {
for (boolean pixel : row) {
System.out.print(pixel + ",");
}
System.out.println();
}
}
public static void printArray(boolean[] array) {
for (boolean pixel : array) {
System.out.print(pixel + ",");
}
System.out.println();
}
public static void printArray(int[] array) {
for (int pixel : array) {
System.out.print(pixel + ",");
}
System.out.println();
}
}