hashcompute: Redesign XXXHashExtend API interface (#27)
This commit is contained in:
parent
0876a2adc2
commit
161b6f5676
@ -38,9 +38,10 @@ func main() {
|
|||||||
hash1, _ = goimagehash.DifferenceHash(img1)
|
hash1, _ = goimagehash.DifferenceHash(img1)
|
||||||
hash2, _ = goimagehash.DifferenceHash(img2)
|
hash2, _ = goimagehash.DifferenceHash(img2)
|
||||||
distance, _ = hash1.Distance(hash2)
|
distance, _ = hash1.Distance(hash2)
|
||||||
fmt.Printf("Distance between images: %v\n", distance)
|
fmt.Printf("Distance between images: %v\n", distance)ls
|
||||||
hash3, _ = goimagehash.AverageHashExtend(img1, 16)
|
width, height := 8, 8
|
||||||
hash4, _ = goimagehash.AverageHashExtend(img2, 16)
|
hash3, _ = goimagehash.AverageHashExtend(img1, width, height)
|
||||||
|
hash4, _ = goimagehash.AverageHashExtend(img2, width, height)
|
||||||
distance, _ = hash3.Distance(hash4)
|
distance, _ = hash3.Distance(hash4)
|
||||||
fmt.Printf("Distance between images: %v\n", distance)
|
fmt.Printf("Distance between images: %v\n", distance)
|
||||||
fmt.Printf("hash3 bit size: %v\n", hash3.Bits())
|
fmt.Printf("hash3 bit size: %v\n", hash3.Bits())
|
||||||
|
@ -19,9 +19,10 @@ func main() {
|
|||||||
foo := bufio.NewWriter(&b)
|
foo := bufio.NewWriter(&b)
|
||||||
img1, _ := jpeg.Decode(file1)
|
img1, _ := jpeg.Decode(file1)
|
||||||
img2, _ := jpeg.Decode(file2)
|
img2, _ := jpeg.Decode(file2)
|
||||||
hash1, _ := goimagehash.AverageHashExtend(img1, 15)
|
width, height := 15, 15
|
||||||
hash2, _ := goimagehash.AverageHashExtend(img2, 15)
|
hash1, _ := goimagehash.AverageHashExtend(img1, width, height)
|
||||||
hash1024, _ := goimagehash.AverageHashExtend(img2, 32)
|
hash2, _ := goimagehash.AverageHashExtend(img2, width, height)
|
||||||
|
hash1024, _ := goimagehash.AverageHashExtend(img2, 32, 32)
|
||||||
distance, _ := hash1.Distance(hash2)
|
distance, _ := hash1.Distance(hash2)
|
||||||
fmt.Printf("Distance between images: %v\n", distance)
|
fmt.Printf("Distance between images: %v\n", distance)
|
||||||
err := hash1.Dump(foo)
|
err := hash1.Dump(foo)
|
||||||
|
@ -86,17 +86,17 @@ func PerceptionHash(img image.Image) (*ImageHash, error) {
|
|||||||
|
|
||||||
// PerceptionHashExtend function returns phash of which the size can be set larger than uint64
|
// PerceptionHashExtend function returns phash of which the size can be set larger than uint64
|
||||||
// Some variable name refer to https://github.com/JohannesBuchner/imagehash/blob/master/imagehash/__init__.py
|
// Some variable name refer to https://github.com/JohannesBuchner/imagehash/blob/master/imagehash/__init__.py
|
||||||
// Support 64bits phash (hashSize=8) and 256bits phash (hashSize=16)
|
// Support 64bits phash (width=8, height=8) and 256bits phash (width=16, height=16)
|
||||||
func PerceptionHashExtend(img image.Image, hashSize int) (*ExtImageHash, error) {
|
func PerceptionHashExtend(img image.Image, width, height int) (*ExtImageHash, error) {
|
||||||
if img == nil {
|
if img == nil {
|
||||||
return nil, errors.New("Image object can not be nil")
|
return nil, errors.New("Image object can not be nil")
|
||||||
}
|
}
|
||||||
var phash []uint64
|
var phash []uint64
|
||||||
imgSize := hashSize * hashSize
|
imgSize := width * height
|
||||||
resized := resize.Resize(uint(imgSize), uint(imgSize), img, resize.Bilinear)
|
resized := resize.Resize(uint(imgSize), uint(imgSize), img, resize.Bilinear)
|
||||||
pixels := transforms.Rgb2Gray(resized)
|
pixels := transforms.Rgb2Gray(resized)
|
||||||
dct := transforms.DCT2D(pixels, imgSize, imgSize)
|
dct := transforms.DCT2D(pixels, imgSize, imgSize)
|
||||||
flattens := transforms.FlattenPixels(dct, hashSize, hashSize)
|
flattens := transforms.FlattenPixels(dct, width, height)
|
||||||
median := etcs.MedianOfPixels(flattens)
|
median := etcs.MedianOfPixels(flattens)
|
||||||
|
|
||||||
lenOfUnit := 64
|
lenOfUnit := 64
|
||||||
@ -116,17 +116,17 @@ func PerceptionHashExtend(img image.Image, hashSize int) (*ExtImageHash, error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// AverageHashExtend function returns ahash of which the size can be set larger than uint64
|
// AverageHashExtend function returns ahash of which the size can be set larger than uint64
|
||||||
// Support 64bits ahash (hashSize=8) and 256bits ahash (hashSize=16)
|
// Support 64bits ahash (width=8, height=8) and 256bits ahash (width=16, height=16)
|
||||||
func AverageHashExtend(img image.Image, hashSize int) (*ExtImageHash, error) {
|
func AverageHashExtend(img image.Image, width, height int) (*ExtImageHash, error) {
|
||||||
if img == nil {
|
if img == nil {
|
||||||
return nil, errors.New("Image object can not be nil")
|
return nil, errors.New("Image object can not be nil")
|
||||||
}
|
}
|
||||||
var ahash []uint64
|
var ahash []uint64
|
||||||
imgSize := hashSize * hashSize
|
imgSize := width * height
|
||||||
|
|
||||||
resized := resize.Resize(uint(hashSize), uint(hashSize), img, resize.Bilinear)
|
resized := resize.Resize(uint(width), uint(height), img, resize.Bilinear)
|
||||||
pixels := transforms.Rgb2Gray(resized)
|
pixels := transforms.Rgb2Gray(resized)
|
||||||
flattens := transforms.FlattenPixels(pixels, hashSize, hashSize)
|
flattens := transforms.FlattenPixels(pixels, width, height)
|
||||||
avg := etcs.MeanOfPixels(flattens)
|
avg := etcs.MeanOfPixels(flattens)
|
||||||
|
|
||||||
lenOfUnit := 64
|
lenOfUnit := 64
|
||||||
@ -146,16 +146,16 @@ func AverageHashExtend(img image.Image, hashSize int) (*ExtImageHash, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DifferenceHashExtend function returns dhash of which the size can be set larger than uint64
|
// DifferenceHashExtend function returns dhash of which the size can be set larger than uint64
|
||||||
// Support 64bits dhash (hashSize=8) and 256bits dhash (hashSize=16)
|
// Support 64bits dhash (width=8, height=8) and 256bits dhash (width=16, height=16)
|
||||||
func DifferenceHashExtend(img image.Image, hashSize int) (*ExtImageHash, error) {
|
func DifferenceHashExtend(img image.Image, width, height int) (*ExtImageHash, error) {
|
||||||
if img == nil {
|
if img == nil {
|
||||||
return nil, errors.New("Image object can not be nil")
|
return nil, errors.New("Image object can not be nil")
|
||||||
}
|
}
|
||||||
|
|
||||||
var dhash []uint64
|
var dhash []uint64
|
||||||
imgSize := hashSize * hashSize
|
imgSize := width * height
|
||||||
|
|
||||||
resized := resize.Resize(uint(hashSize)+1, uint(hashSize), img, resize.Bilinear)
|
resized := resize.Resize(uint(width)+1, uint(height), img, resize.Bilinear)
|
||||||
pixels := transforms.Rgb2Gray(resized)
|
pixels := transforms.Rgb2Gray(resized)
|
||||||
|
|
||||||
lenOfUnit := 64
|
lenOfUnit := 64
|
||||||
|
@ -146,77 +146,78 @@ func TestExtImageHashCompute(t *testing.T) {
|
|||||||
for _, tt := range []struct {
|
for _, tt := range []struct {
|
||||||
img1 string
|
img1 string
|
||||||
img2 string
|
img2 string
|
||||||
hashSize int
|
width int
|
||||||
method func(img image.Image, hashSize int) (*ExtImageHash, error)
|
height int
|
||||||
|
method func(img image.Image, width, height int) (*ExtImageHash, error)
|
||||||
name string
|
name string
|
||||||
distance int
|
distance int
|
||||||
}{
|
}{
|
||||||
{"_examples/sample1.jpg", "_examples/sample1.jpg", 8, AverageHashExtend, "AverageHashExtend", 0},
|
{"_examples/sample1.jpg", "_examples/sample1.jpg", 8, 8, AverageHashExtend, "AverageHashExtend", 0},
|
||||||
{"_examples/sample2.jpg", "_examples/sample2.jpg", 8, AverageHashExtend, "AverageHashExtend", 0},
|
{"_examples/sample2.jpg", "_examples/sample2.jpg", 8, 8, AverageHashExtend, "AverageHashExtend", 0},
|
||||||
{"_examples/sample3.jpg", "_examples/sample3.jpg", 8, AverageHashExtend, "AverageHashExtend", 0},
|
{"_examples/sample3.jpg", "_examples/sample3.jpg", 8, 8, AverageHashExtend, "AverageHashExtend", 0},
|
||||||
{"_examples/sample4.jpg", "_examples/sample4.jpg", 8, AverageHashExtend, "AverageHashExtend", 0},
|
{"_examples/sample4.jpg", "_examples/sample4.jpg", 8, 8, AverageHashExtend, "AverageHashExtend", 0},
|
||||||
{"_examples/sample1.jpg", "_examples/sample2.jpg", 8, AverageHashExtend, "AverageHashExtend", 42},
|
{"_examples/sample1.jpg", "_examples/sample2.jpg", 8, 8, AverageHashExtend, "AverageHashExtend", 42},
|
||||||
{"_examples/sample1.jpg", "_examples/sample3.jpg", 8, AverageHashExtend, "AverageHashExtend", 4},
|
{"_examples/sample1.jpg", "_examples/sample3.jpg", 8, 8, AverageHashExtend, "AverageHashExtend", 4},
|
||||||
{"_examples/sample1.jpg", "_examples/sample4.jpg", 8, AverageHashExtend, "AverageHashExtend", 38},
|
{"_examples/sample1.jpg", "_examples/sample4.jpg", 8, 8, AverageHashExtend, "AverageHashExtend", 38},
|
||||||
{"_examples/sample2.jpg", "_examples/sample3.jpg", 8, AverageHashExtend, "AverageHashExtend", 40},
|
{"_examples/sample2.jpg", "_examples/sample3.jpg", 8, 8, AverageHashExtend, "AverageHashExtend", 40},
|
||||||
{"_examples/sample2.jpg", "_examples/sample4.jpg", 8, AverageHashExtend, "AverageHashExtend", 6},
|
{"_examples/sample2.jpg", "_examples/sample4.jpg", 8, 8, AverageHashExtend, "AverageHashExtend", 6},
|
||||||
{"_examples/sample1.jpg", "_examples/sample1.jpg", 16, AverageHashExtend, "AverageHashExtend", 0},
|
{"_examples/sample1.jpg", "_examples/sample1.jpg", 16, 16, AverageHashExtend, "AverageHashExtend", 0},
|
||||||
{"_examples/sample2.jpg", "_examples/sample2.jpg", 16, AverageHashExtend, "AverageHashExtend", 0},
|
{"_examples/sample2.jpg", "_examples/sample2.jpg", 16, 16, AverageHashExtend, "AverageHashExtend", 0},
|
||||||
{"_examples/sample3.jpg", "_examples/sample3.jpg", 16, AverageHashExtend, "AverageHashExtend", 0},
|
{"_examples/sample3.jpg", "_examples/sample3.jpg", 16, 16, AverageHashExtend, "AverageHashExtend", 0},
|
||||||
{"_examples/sample4.jpg", "_examples/sample4.jpg", 16, AverageHashExtend, "AverageHashExtend", 0},
|
{"_examples/sample4.jpg", "_examples/sample4.jpg", 16, 16, AverageHashExtend, "AverageHashExtend", 0},
|
||||||
{"_examples/sample1.jpg", "_examples/sample2.jpg", 16, AverageHashExtend, "AverageHashExtend", 149},
|
{"_examples/sample1.jpg", "_examples/sample2.jpg", 16, 16, AverageHashExtend, "AverageHashExtend", 149},
|
||||||
{"_examples/sample1.jpg", "_examples/sample3.jpg", 16, AverageHashExtend, "AverageHashExtend", 8},
|
{"_examples/sample1.jpg", "_examples/sample3.jpg", 16, 16, AverageHashExtend, "AverageHashExtend", 8},
|
||||||
{"_examples/sample1.jpg", "_examples/sample4.jpg", 16, AverageHashExtend, "AverageHashExtend", 152},
|
{"_examples/sample1.jpg", "_examples/sample4.jpg", 16, 16, AverageHashExtend, "AverageHashExtend", 152},
|
||||||
{"_examples/sample2.jpg", "_examples/sample3.jpg", 16, AverageHashExtend, "AverageHashExtend", 155},
|
{"_examples/sample2.jpg", "_examples/sample3.jpg", 16, 16, AverageHashExtend, "AverageHashExtend", 155},
|
||||||
{"_examples/sample2.jpg", "_examples/sample4.jpg", 16, AverageHashExtend, "AverageHashExtend", 27},
|
{"_examples/sample2.jpg", "_examples/sample4.jpg", 16, 16, AverageHashExtend, "AverageHashExtend", 27},
|
||||||
{"_examples/sample1.jpg", "_examples/sample1.jpg", 17, AverageHashExtend, "AverageHashExtend", 0},
|
{"_examples/sample1.jpg", "_examples/sample1.jpg", 17, 17, AverageHashExtend, "AverageHashExtend", 0},
|
||||||
{"_examples/sample2.jpg", "_examples/sample2.jpg", 17, AverageHashExtend, "AverageHashExtend", 0},
|
{"_examples/sample2.jpg", "_examples/sample2.jpg", 17, 17, AverageHashExtend, "AverageHashExtend", 0},
|
||||||
{"_examples/sample3.jpg", "_examples/sample3.jpg", 17, AverageHashExtend, "AverageHashExtend", 0},
|
{"_examples/sample3.jpg", "_examples/sample3.jpg", 17, 17, AverageHashExtend, "AverageHashExtend", 0},
|
||||||
{"_examples/sample4.jpg", "_examples/sample4.jpg", 17, AverageHashExtend, "AverageHashExtend", 0},
|
{"_examples/sample4.jpg", "_examples/sample4.jpg", 17, 17, AverageHashExtend, "AverageHashExtend", 0},
|
||||||
{"_examples/sample1.jpg", "_examples/sample1.jpg", 8, PerceptionHashExtend, "PerceptionHashExtend", 0},
|
{"_examples/sample1.jpg", "_examples/sample1.jpg", 8, 8, PerceptionHashExtend, "PerceptionHashExtend", 0},
|
||||||
{"_examples/sample2.jpg", "_examples/sample2.jpg", 8, PerceptionHashExtend, "PerceptionHashExtend", 0},
|
{"_examples/sample2.jpg", "_examples/sample2.jpg", 8, 8, PerceptionHashExtend, "PerceptionHashExtend", 0},
|
||||||
{"_examples/sample3.jpg", "_examples/sample3.jpg", 8, PerceptionHashExtend, "PerceptionHashExtend", 0},
|
{"_examples/sample3.jpg", "_examples/sample3.jpg", 8, 8, PerceptionHashExtend, "PerceptionHashExtend", 0},
|
||||||
{"_examples/sample4.jpg", "_examples/sample4.jpg", 8, PerceptionHashExtend, "PerceptionHashExtend", 0},
|
{"_examples/sample4.jpg", "_examples/sample4.jpg", 8, 8, PerceptionHashExtend, "PerceptionHashExtend", 0},
|
||||||
{"_examples/sample1.jpg", "_examples/sample2.jpg", 8, PerceptionHashExtend, "PerceptionHashExtend", 32},
|
{"_examples/sample1.jpg", "_examples/sample2.jpg", 8, 8, PerceptionHashExtend, "PerceptionHashExtend", 32},
|
||||||
{"_examples/sample1.jpg", "_examples/sample3.jpg", 8, PerceptionHashExtend, "PerceptionHashExtend", 2},
|
{"_examples/sample1.jpg", "_examples/sample3.jpg", 8, 8, PerceptionHashExtend, "PerceptionHashExtend", 2},
|
||||||
{"_examples/sample1.jpg", "_examples/sample4.jpg", 8, PerceptionHashExtend, "PerceptionHashExtend", 30},
|
{"_examples/sample1.jpg", "_examples/sample4.jpg", 8, 8, PerceptionHashExtend, "PerceptionHashExtend", 30},
|
||||||
{"_examples/sample2.jpg", "_examples/sample3.jpg", 8, PerceptionHashExtend, "PerceptionHashExtend", 34},
|
{"_examples/sample2.jpg", "_examples/sample3.jpg", 8, 8, PerceptionHashExtend, "PerceptionHashExtend", 34},
|
||||||
{"_examples/sample2.jpg", "_examples/sample4.jpg", 8, PerceptionHashExtend, "PerceptionHashExtend", 20},
|
{"_examples/sample2.jpg", "_examples/sample4.jpg", 8, 8, PerceptionHashExtend, "PerceptionHashExtend", 20},
|
||||||
{"_examples/sample1.jpg", "_examples/sample1.jpg", 16, PerceptionHashExtend, "PerceptionHashExtend", 0},
|
{"_examples/sample1.jpg", "_examples/sample1.jpg", 16, 16, PerceptionHashExtend, "PerceptionHashExtend", 0},
|
||||||
{"_examples/sample2.jpg", "_examples/sample2.jpg", 16, PerceptionHashExtend, "PerceptionHashExtend", 0},
|
{"_examples/sample2.jpg", "_examples/sample2.jpg", 16, 16, PerceptionHashExtend, "PerceptionHashExtend", 0},
|
||||||
{"_examples/sample3.jpg", "_examples/sample3.jpg", 16, PerceptionHashExtend, "PerceptionHashExtend", 0},
|
{"_examples/sample3.jpg", "_examples/sample3.jpg", 16, 16, PerceptionHashExtend, "PerceptionHashExtend", 0},
|
||||||
{"_examples/sample4.jpg", "_examples/sample4.jpg", 16, PerceptionHashExtend, "PerceptionHashExtend", 0},
|
{"_examples/sample4.jpg", "_examples/sample4.jpg", 16, 16, PerceptionHashExtend, "PerceptionHashExtend", 0},
|
||||||
{"_examples/sample1.jpg", "_examples/sample2.jpg", 16, PerceptionHashExtend, "PerceptionHashExtend", 122},
|
{"_examples/sample1.jpg", "_examples/sample2.jpg", 16, 16, PerceptionHashExtend, "PerceptionHashExtend", 122},
|
||||||
{"_examples/sample1.jpg", "_examples/sample3.jpg", 16, PerceptionHashExtend, "PerceptionHashExtend", 12},
|
{"_examples/sample1.jpg", "_examples/sample3.jpg", 16, 16, PerceptionHashExtend, "PerceptionHashExtend", 12},
|
||||||
{"_examples/sample1.jpg", "_examples/sample4.jpg", 16, PerceptionHashExtend, "PerceptionHashExtend", 122},
|
{"_examples/sample1.jpg", "_examples/sample4.jpg", 16, 16, PerceptionHashExtend, "PerceptionHashExtend", 122},
|
||||||
{"_examples/sample2.jpg", "_examples/sample3.jpg", 16, PerceptionHashExtend, "PerceptionHashExtend", 118},
|
{"_examples/sample2.jpg", "_examples/sample3.jpg", 16, 16, PerceptionHashExtend, "PerceptionHashExtend", 118},
|
||||||
{"_examples/sample2.jpg", "_examples/sample4.jpg", 16, PerceptionHashExtend, "PerceptionHashExtend", 104},
|
{"_examples/sample2.jpg", "_examples/sample4.jpg", 16, 16, PerceptionHashExtend, "PerceptionHashExtend", 104},
|
||||||
{"_examples/sample1.jpg", "_examples/sample1.jpg", 17, PerceptionHashExtend, "PerceptionHashExtend", 0},
|
{"_examples/sample1.jpg", "_examples/sample1.jpg", 17, 17, PerceptionHashExtend, "PerceptionHashExtend", 0},
|
||||||
{"_examples/sample2.jpg", "_examples/sample2.jpg", 17, PerceptionHashExtend, "PerceptionHashExtend", 0},
|
{"_examples/sample2.jpg", "_examples/sample2.jpg", 17, 17, PerceptionHashExtend, "PerceptionHashExtend", 0},
|
||||||
{"_examples/sample3.jpg", "_examples/sample3.jpg", 17, PerceptionHashExtend, "PerceptionHashExtend", 0},
|
{"_examples/sample3.jpg", "_examples/sample3.jpg", 17, 17, PerceptionHashExtend, "PerceptionHashExtend", 0},
|
||||||
{"_examples/sample4.jpg", "_examples/sample4.jpg", 17, PerceptionHashExtend, "PerceptionHashExtend", 0},
|
{"_examples/sample4.jpg", "_examples/sample4.jpg", 17, 17, PerceptionHashExtend, "PerceptionHashExtend", 0},
|
||||||
{"_examples/sample1.jpg", "_examples/sample1.jpg", 8, DifferenceHashExtend, "DifferenceHashExtend", 0},
|
{"_examples/sample1.jpg", "_examples/sample1.jpg", 8, 8, DifferenceHashExtend, "DifferenceHashExtend", 0},
|
||||||
{"_examples/sample2.jpg", "_examples/sample2.jpg", 8, DifferenceHashExtend, "DifferenceHashExtend", 0},
|
{"_examples/sample2.jpg", "_examples/sample2.jpg", 8, 8, DifferenceHashExtend, "DifferenceHashExtend", 0},
|
||||||
{"_examples/sample3.jpg", "_examples/sample3.jpg", 8, DifferenceHashExtend, "DifferenceHashExtend", 0},
|
{"_examples/sample3.jpg", "_examples/sample3.jpg", 8, 8, DifferenceHashExtend, "DifferenceHashExtend", 0},
|
||||||
{"_examples/sample4.jpg", "_examples/sample4.jpg", 8, DifferenceHashExtend, "DifferenceHashExtend", 0},
|
{"_examples/sample4.jpg", "_examples/sample4.jpg", 8, 8, DifferenceHashExtend, "DifferenceHashExtend", 0},
|
||||||
{"_examples/sample1.jpg", "_examples/sample2.jpg", 8, DifferenceHashExtend, "DifferenceHashExtend", 43},
|
{"_examples/sample1.jpg", "_examples/sample2.jpg", 8, 8, DifferenceHashExtend, "DifferenceHashExtend", 43},
|
||||||
{"_examples/sample1.jpg", "_examples/sample3.jpg", 8, DifferenceHashExtend, "DifferenceHashExtend", 0},
|
{"_examples/sample1.jpg", "_examples/sample3.jpg", 8, 8, DifferenceHashExtend, "DifferenceHashExtend", 0},
|
||||||
{"_examples/sample1.jpg", "_examples/sample4.jpg", 8, DifferenceHashExtend, "DifferenceHashExtend", 37},
|
{"_examples/sample1.jpg", "_examples/sample4.jpg", 8, 8, DifferenceHashExtend, "DifferenceHashExtend", 37},
|
||||||
{"_examples/sample2.jpg", "_examples/sample3.jpg", 8, DifferenceHashExtend, "DifferenceHashExtend", 43},
|
{"_examples/sample2.jpg", "_examples/sample3.jpg", 8, 8, DifferenceHashExtend, "DifferenceHashExtend", 43},
|
||||||
{"_examples/sample2.jpg", "_examples/sample4.jpg", 8, DifferenceHashExtend, "DifferenceHashExtend", 16},
|
{"_examples/sample2.jpg", "_examples/sample4.jpg", 8, 8, DifferenceHashExtend, "DifferenceHashExtend", 16},
|
||||||
{"_examples/sample1.jpg", "_examples/sample1.jpg", 16, DifferenceHashExtend, "DifferenceHashExtend", 0},
|
{"_examples/sample1.jpg", "_examples/sample1.jpg", 16, 16, DifferenceHashExtend, "DifferenceHashExtend", 0},
|
||||||
{"_examples/sample2.jpg", "_examples/sample2.jpg", 16, DifferenceHashExtend, "DifferenceHashExtend", 0},
|
{"_examples/sample2.jpg", "_examples/sample2.jpg", 16, 16, DifferenceHashExtend, "DifferenceHashExtend", 0},
|
||||||
{"_examples/sample3.jpg", "_examples/sample3.jpg", 16, DifferenceHashExtend, "DifferenceHashExtend", 0},
|
{"_examples/sample3.jpg", "_examples/sample3.jpg", 16, 16, DifferenceHashExtend, "DifferenceHashExtend", 0},
|
||||||
{"_examples/sample4.jpg", "_examples/sample4.jpg", 16, DifferenceHashExtend, "DifferenceHashExtend", 0},
|
{"_examples/sample4.jpg", "_examples/sample4.jpg", 16, 16, DifferenceHashExtend, "DifferenceHashExtend", 0},
|
||||||
{"_examples/sample1.jpg", "_examples/sample2.jpg", 16, DifferenceHashExtend, "DifferenceHashExtend", 139},
|
{"_examples/sample1.jpg", "_examples/sample2.jpg", 16, 16, DifferenceHashExtend, "DifferenceHashExtend", 139},
|
||||||
{"_examples/sample1.jpg", "_examples/sample3.jpg", 16, DifferenceHashExtend, "DifferenceHashExtend", 14},
|
{"_examples/sample1.jpg", "_examples/sample3.jpg", 16, 16, DifferenceHashExtend, "DifferenceHashExtend", 14},
|
||||||
{"_examples/sample1.jpg", "_examples/sample4.jpg", 16, DifferenceHashExtend, "DifferenceHashExtend", 130},
|
{"_examples/sample1.jpg", "_examples/sample4.jpg", 16, 16, DifferenceHashExtend, "DifferenceHashExtend", 130},
|
||||||
{"_examples/sample2.jpg", "_examples/sample3.jpg", 16, DifferenceHashExtend, "DifferenceHashExtend", 147},
|
{"_examples/sample2.jpg", "_examples/sample3.jpg", 16, 16, DifferenceHashExtend, "DifferenceHashExtend", 147},
|
||||||
{"_examples/sample2.jpg", "_examples/sample4.jpg", 16, DifferenceHashExtend, "DifferenceHashExtend", 89},
|
{"_examples/sample2.jpg", "_examples/sample4.jpg", 16, 16, DifferenceHashExtend, "DifferenceHashExtend", 89},
|
||||||
{"_examples/sample1.jpg", "_examples/sample1.jpg", 17, DifferenceHashExtend, "DifferenceHashExtend", 0},
|
{"_examples/sample1.jpg", "_examples/sample1.jpg", 17, 17, DifferenceHashExtend, "DifferenceHashExtend", 0},
|
||||||
{"_examples/sample2.jpg", "_examples/sample2.jpg", 17, DifferenceHashExtend, "DifferenceHashExtend", 0},
|
{"_examples/sample2.jpg", "_examples/sample2.jpg", 17, 17, DifferenceHashExtend, "DifferenceHashExtend", 0},
|
||||||
{"_examples/sample3.jpg", "_examples/sample3.jpg", 17, DifferenceHashExtend, "DifferenceHashExtend", 0},
|
{"_examples/sample3.jpg", "_examples/sample3.jpg", 17, 17, DifferenceHashExtend, "DifferenceHashExtend", 0},
|
||||||
{"_examples/sample4.jpg", "_examples/sample4.jpg", 17, DifferenceHashExtend, "DifferenceHashExtend", 0},
|
{"_examples/sample4.jpg", "_examples/sample4.jpg", 17, 17, DifferenceHashExtend, "DifferenceHashExtend", 0},
|
||||||
} {
|
} {
|
||||||
file1, err := os.Open(tt.img1)
|
file1, err := os.Open(tt.img1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -240,11 +241,11 @@ func TestExtImageHashCompute(t *testing.T) {
|
|||||||
t.Errorf("%s", err)
|
t.Errorf("%s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
hash1, err := tt.method(img1, tt.hashSize)
|
hash1, err := tt.method(img1, tt.width, tt.height)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("%s", err)
|
t.Errorf("%s", err)
|
||||||
}
|
}
|
||||||
hash2, err := tt.method(img2, tt.hashSize)
|
hash2, err := tt.method(img2, tt.width, tt.height)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("%s", err)
|
t.Errorf("%s", err)
|
||||||
}
|
}
|
||||||
|
@ -103,14 +103,14 @@ func TestSerialization(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// test for ExtIExtImageHash
|
// test for ExtIExtImageHash
|
||||||
hashSizeList := []int{8, 16}
|
sizeList := []int{8, 16}
|
||||||
for _, hashSize := range hashSizeList {
|
for _, size := range sizeList {
|
||||||
hash, err := PerceptionHashExtend(img, hashSize)
|
hash, err := PerceptionHashExtend(img, size, size)
|
||||||
checkErr(err)
|
checkErr(err)
|
||||||
|
|
||||||
hex := hash.ToString()
|
hex := hash.ToString()
|
||||||
// len(kind) == 1, len(":") == 1
|
// len(kind) == 1, len(":") == 1
|
||||||
if len(hex) != hashSize*hashSize/4+2 {
|
if len(hex) != size*size/4+2 {
|
||||||
t.Errorf("Got invalid hex string '%v'; %v of '%v'", hex, "PerceptionHashExtend", ex)
|
t.Errorf("Got invalid hex string '%v'; %v of '%v'", hex, "PerceptionHashExtend", ex)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -140,13 +140,13 @@ func TestDifferentBitSizeHash(t *testing.T) {
|
|||||||
img, _, err := image.Decode(file)
|
img, _, err := image.Decode(file)
|
||||||
checkErr(err)
|
checkErr(err)
|
||||||
|
|
||||||
hash1, _ := AverageHashExtend(img, 32)
|
hash1, _ := AverageHashExtend(img, 32, 32)
|
||||||
hash2, _ := DifferenceHashExtend(img, 32)
|
hash2, _ := DifferenceHashExtend(img, 32, 32)
|
||||||
_, err = hash1.Distance(hash2)
|
_, err = hash1.Distance(hash2)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("Should got error with different kinds of hashes")
|
t.Errorf("Should got error with different kinds of hashes")
|
||||||
}
|
}
|
||||||
hash3, _ := AverageHashExtend(img, 31)
|
hash3, _ := AverageHashExtend(img, 31, 31)
|
||||||
_, err = hash1.Distance(hash3)
|
_, err = hash1.Distance(hash3)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("Should got error with different bits of hashes")
|
t.Errorf("Should got error with different bits of hashes")
|
||||||
@ -200,14 +200,14 @@ func TestDumpAndLoad(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// test for ExtIExtImageHash
|
// test for ExtIExtImageHash
|
||||||
extMethods := []func(img image.Image, hashSize int) (*ExtImageHash, error){
|
extMethods := []func(img image.Image, width, height int) (*ExtImageHash, error){
|
||||||
AverageHashExtend, PerceptionHashExtend, DifferenceHashExtend,
|
AverageHashExtend, PerceptionHashExtend, DifferenceHashExtend,
|
||||||
}
|
}
|
||||||
|
|
||||||
hashSizeList := []int{8, 16}
|
sizeList := []int{8, 16}
|
||||||
for _, hashSize := range hashSizeList {
|
for _, size := range sizeList {
|
||||||
for _, method := range extMethods {
|
for _, method := range extMethods {
|
||||||
hash, err := method(img, hashSize)
|
hash, err := method(img, size, size)
|
||||||
checkErr(err)
|
checkErr(err)
|
||||||
var b bytes.Buffer
|
var b bytes.Buffer
|
||||||
foo := bufio.NewWriter(&b)
|
foo := bufio.NewWriter(&b)
|
||||||
@ -225,7 +225,7 @@ func TestDumpAndLoad(t *testing.T) {
|
|||||||
t.Errorf("Original and unserialized objects should be identical, got distance=%v", distance)
|
t.Errorf("Original and unserialized objects should be identical, got distance=%v", distance)
|
||||||
}
|
}
|
||||||
|
|
||||||
if hash.Bits() != hashSize*hashSize || reHash.Bits() != hashSize*hashSize {
|
if hash.Bits() != size*size || reHash.Bits() != size*size {
|
||||||
t.Errorf("Hash bits should be 64 but got, %v, %v", hash.Bits(), reHash.Bits())
|
t.Errorf("Hash bits should be 64 but got, %v, %v", hash.Bits(), reHash.Bits())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user