forked from vitali-fedulov/images4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
similarity_test.go
83 lines (77 loc) · 2.44 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
package images4
import (
"path"
"testing"
)
func testPropSimilar(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)
}
iconA := Icon(imgA)
iconB := Icon(imgB)
if isSimilar == true {
if !propSimilar(iconA, iconB) {
t.Errorf("Expecting similarity of %v to %v.", fA, fB)
}
}
if isSimilar == false {
if propSimilar(iconA, iconB) {
t.Errorf("Expecting non-similarity of %v to %v.", fA, fB)
}
}
}
func TestPropSimilar(t *testing.T) {
testPropSimilar("100x130.png", "100x124.png", true, t)
testPropSimilar("100x130.png", "100x122.png", false, t)
testPropSimilar("130x100.png", "260x200.png", true, t)
testPropSimilar("200x200.png", "260x200.png", false, t)
testPropSimilar("130x100.png", "124x100.png", true, t)
testPropSimilar("130x100.png", "122x100.png", false, t)
testPropSimilar("130x100.png", "130x100.png", true, t)
testPropSimilar("100x130.png", "130x100.png", false, t)
testPropSimilar("124x100.png", "260x200.png", true, t)
testPropSimilar("122x100.png", "260x200.png", false, t)
testPropSimilar("100x124.png", "100x130.png", true, t)
}
func testEucSimilar(fA, fB string, isSimilar bool,
t *testing.T) {
p := path.Join("testdata", "euclidean")
imgA, err := Open(path.Join(p, fA))
if err != nil {
t.Error("Error opening image:", err)
}
iconA := Icon(imgA)
imgB, err := Open(path.Join(p, fB))
if err != nil {
t.Error("Error opening image:", err)
}
iconB := Icon(imgB)
if isSimilar == true {
if !eucSimilar(iconA, iconB) {
t.Errorf("Expecting similarity of %v to %v.", fA, fB)
}
}
if isSimilar == false {
if eucSimilar(iconA, iconB) {
t.Errorf("Expecting non-similarity of %v to %v.", fA, fB)
}
}
}
func TestEucSimilar(t *testing.T) {
testEucSimilar("large.jpg", "distorted.jpg", true, t)
testEucSimilar("large.jpg", "flipped.jpg", false, t)
testEucSimilar("large.jpg", "small.jpg", true, t)
testEucSimilar("small.gif", "small.jpg", true, t) // GIF test.
testEucSimilar("uniform-black.png", "uniform-black.png", true, t)
testEucSimilar("uniform-black.png", "uniform-white.png", false, t)
testEucSimilar("uniform-green.png", "uniform-green.png", true, t)
testEucSimilar("uniform-green.png", "uniform-white.png", false, t)
testEucSimilar("uniform-white.png", "uniform-white.png", true, t)
}