-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdistance_test.go
140 lines (110 loc) · 3.96 KB
/
distance_test.go
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
/*
Testing suite for the distance definition with all implemented algorithms.
It starts with Ahash and finish with Dhash.
1. Test that distance between a 512px image and 256px image is near to zero
2. Test that distance between a image and inverted image is not too close from zero but not to far either
3. Test that distance between a image and white image is not close from zero
4. Test maximum distance between a images
*/
package imagehash
import (
"testing"
)
// Test the distance between two size with Ahash.
func TestDistanceAHashSizes(t *testing.T) {
src1, _ := OpenImg("./testdata/lena_512.png")
src2, _ := OpenImg("./testdata/lena_256.png")
hash1, _ := Ahash(src1, 8)
hash2, _ := Ahash(src2, 8)
dist := GetDistance(hash1, hash2)
// The value should be 0
if dist > 3 {
t.Errorf("the distance between the two images is to important: %d", dist)
}
}
// Test the distance between two size with Dhash.
func TestDistanceDHashSizes(t *testing.T) {
src1, _ := OpenImg("./testdata/lena_512.png")
src2, _ := OpenImg("./testdata/lena_256.png")
hash1, _ := Dhash(src1, 8)
hash2, _ := Dhash(src2, 8)
dist := GetDistance(hash1, hash2)
// The value should be 0
if dist > 3 {
t.Errorf("the distance between the two images is to important: %d", dist)
}
}
// Test the distance between regular and inverted image with Ahash.
func TestDistanceAHashInverted(t *testing.T) {
src1, _ := OpenImg("./testdata/lena_512.png")
src2, _ := OpenImg("./testdata/lena_256.png")
hash1, _ := Ahash(src1, 8)
hash2, _ := Ahash(src2, 8)
dist := GetDistance(hash1, hash2)
// The value should be 0
if dist > 3 {
t.Errorf("the distance between the two images is to important: %d", dist)
}
}
// Test the distance between regular and inverted image with Dhash.
func TestDistanceDHashInverted(t *testing.T) {
src1, _ := OpenImg("./testdata/lena_512.png")
src2, _ := OpenImg("./testdata/lena_inverted_512.png")
hash1, _ := Dhash(src1, 8)
hash2, _ := Dhash(src2, 8)
dist := GetDistance(hash1, hash2)
// The value should be 16
if dist < 10 && dist > 20 {
t.Errorf("the distance between the two images is to important: %d", dist)
}
}
// Test the distance between regular and inverted image with Ahash.
func TestDistanceAHashWhite(t *testing.T) {
src1, _ := OpenImg("./testdata/lena_512.png")
src2, _ := OpenImg("./testdata/white_512.png")
hash1, _ := Ahash(src1, 8)
hash2, _ := Ahash(src2, 8)
dist := GetDistance(hash1, hash2)
// The value should be 7
if dist < 5 {
t.Errorf("the distance between the two images is to important: %d", dist)
}
}
// Test the distance between regular and inverted image with Dhash.
func TestDistanceDHashWhite(t *testing.T) {
src1, _ := OpenImg("./testdata/lena_512.png")
src2, _ := OpenImg("./testdata/white_512.png")
hash1, _ := Dhash(src1, 8)
hash2, _ := Dhash(src2, 8)
dist := GetDistance(hash1, hash2)
// The value should be 16
if dist < 10 {
t.Errorf("the distance between the two images is to important: %d", dist)
}
}
// Test the maximum distance between hashes of 8 and 16.
func TestMaximuimDistance(t *testing.T) {
src1, _ := OpenImg("./testdata/white_512.png")
src2, _ := OpenImg("./testdata/rand_512.png")
hash1, _ := Ahash(src1, 8)
hash2, _ := Ahash(src2, 8)
dist := GetDistance(hash1, hash2)
// The value should be 16
if distMax := GetDistanceMaxRange(hash1, hash2); distMax != dist {
t.Errorf("the maximum distance is not good. We have %d and it should be %d", distMax, dist)
}
hash1, _ = Ahash(src1, 16)
hash2, _ = Ahash(src2, 16)
dist = GetDistance(hash1, hash2)
// The value should be 32
if distMax := GetDistanceMaxRange(hash1, hash2); distMax != dist {
t.Errorf("the maximum distance is not good. We have %d and it should be %d", distMax, dist)
}
hash1, _ = Ahash(src1, 32)
hash2, _ = Ahash(src2, 16)
dist = GetDistance(hash1, hash2)
// The value should be 32
if distMax := GetDistanceMaxRange(hash1, hash2); distMax != dist {
t.Errorf("the maximum distance is not good. We have %d and it should be %d", distMax, dist)
}
}