-
Notifications
You must be signed in to change notification settings - Fork 10
/
similarity_test.go
170 lines (154 loc) · 4.69 KB
/
similarity_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
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
// Copyright 2018 Vitali Fedulov. All rights reserved. Use of this source code
// is governed by a MIT-style license that can be found in the LICENSE file.
package images
import (
"image"
"path"
"testing"
)
func TestMasks(t *testing.T) {
ms := masks()
numMasks := (maskSize - 2) * (maskSize - 2)
expectedNumMasks := len(ms)
if len(ms) != numMasks {
t.Errorf("Number of masks %d does not match expected value %d.",
numMasks, expectedNumMasks)
}
for i := range ms {
if len(ms[i]) > 3*3 {
t.Errorf("Number of mask white pixels %d is more than 3*3.",
len(ms[i]))
}
}
}
func TestHash(t *testing.T) {
testDir := "testdata"
testFile := "small.jpg"
img, err := Open(path.Join(testDir, testFile))
if err != nil {
t.Error("Error opening image:", err)
return
}
h, imgSize := Hash(img)
if len(h) == 0 {
t.Errorf("Number of h %d must not be 0.", len(h))
}
if imgSize != (image.Point{267, 200}) {
t.Errorf(
"Calculated imgSize %d is not equal to the size from image properties %d.",
imgSize, image.Point{267, 200})
}
var allZeroOrLessCounter int
for i := range h {
if h[i] > 255 {
t.Errorf("h[i] %f is larger than 255.", h[i])
break
}
if h[i] <= 0 {
allZeroOrLessCounter++
}
}
if allZeroOrLessCounter == len(h) {
t.Error("All h[i] are 0 or less.")
}
}
func TestSimilar(t *testing.T) {
testDir := "testdata"
imgFiles := []string{
"flipped.jpg", "large.jpg", "small.jpg", "distorted.jpg"}
hashes := make([][]float32, len(imgFiles))
imgSizeAll := make([]image.Point, len(imgFiles))
for i := range imgFiles {
img, err := Open(path.Join(testDir, imgFiles[i]))
if err != nil {
t.Error("Error opening image:", err)
return
}
hashes[i], imgSizeAll[i] = Hash(img)
}
if !Similar(hashes[1], hashes[2], imgSizeAll[1], imgSizeAll[2]) {
t.Errorf("Expected similarity between %s and %s.",
imgFiles[1], imgFiles[2])
}
if Similar(hashes[1], hashes[0], imgSizeAll[1], imgSizeAll[0]) {
t.Errorf("Expected non-similarity between %s and %s.",
imgFiles[1], imgFiles[0])
}
if Similar(hashes[1], hashes[3], imgSizeAll[1], imgSizeAll[3]) {
t.Errorf("Expected non-similarity between %s and %s.",
imgFiles[1], imgFiles[3])
}
}
func testProportions(fA, fB string, isSimilar bool,
t *testing.T) {
p := path.Join("testdata", "proportions")
imgA, err := Open(path.Join(p, fA))
if err != nil {
t.Error("Error opening image:", err)
}
imgB, err := Open(path.Join(p, fB))
if err != nil {
t.Error("Error opening image:", err)
}
hA, sA := Hash(imgA)
hB, sB := Hash(imgB)
if isSimilar == true {
if !Similar(hA, hB, sA, sB) {
t.Errorf("Expecting similarity of %v to %v.", fA, fB)
}
}
if isSimilar == false {
if Similar(hA, hB, sA, sB) {
t.Errorf("Expecting non-similarity of %v to %v.", fA, fB)
}
}
}
func TestSimilarByProportions(t *testing.T) {
testProportions("100x130.png", "100x124.png", true, t)
testProportions("100x130.png", "100x122.png", true, t)
testProportions("130x100.png", "260x200.png", true, t)
testProportions("200x200.png", "260x200.png", false, t)
testProportions("130x100.png", "124x100.png", true, t)
testProportions("130x100.png", "122x100.png", true, t)
testProportions("130x100.png", "130x100.png", true, t)
testProportions("100x130.png", "130x100.png", false, t)
testProportions("124x100.png", "260x200.png", true, t)
testProportions("122x100.png", "260x200.png", true, t)
testProportions("100x124.png", "100x130.png", true, t)
}
func TestSimilarCustom(t *testing.T) {
testDir := "testdata"
imgFiles := []string{
"flipped.jpg", "large.jpg", "small.jpg", "distorted.jpg"}
hashes := make([][]float32, len(imgFiles))
imgSizeAll := make([]image.Point, len(imgFiles))
for i := range imgFiles {
img, err := Open(path.Join(testDir, imgFiles[i]))
if err != nil {
t.Error("Error opening image:", err)
return
}
hashes[i], imgSizeAll[i] = Hash(img)
}
delta, euc, eucNorm, corr := SimilarCustom(
hashes[1], hashes[2], imgSizeAll[1], imgSizeAll[2])
// Expected similarity.
if delta > 0.1 || euc > 242000 || eucNorm > 242000 || corr < 340 {
t.Errorf("Expected delta, euc, eucNorm, corr got %v, %v, %v, %v",
delta, euc, eucNorm, corr)
}
delta, euc, eucNorm, corr = SimilarCustom(
hashes[1], hashes[0], imgSizeAll[1], imgSizeAll[0])
// Expected non-similarity.
if !(delta > 0.1 || euc > 242000 || eucNorm > 242000 || corr < 340) {
t.Errorf("Expected delta, euc, eucNorm, corr got %v, %v, %v, %v",
delta, euc, eucNorm, corr)
}
delta, euc, eucNorm, corr = SimilarCustom(
hashes[1], hashes[3], imgSizeAll[1], imgSizeAll[3])
// Expected non-similarity.
if !(delta > 0.1 || euc > 242000 || eucNorm > 242000 || corr < 340) {
t.Errorf("Expected delta, euc, eucNorm, corr got %v, %v, %v, %v",
delta, euc, eucNorm, corr)
}
}