DifferenceHashExtend: Implement DifferenceHashExtend (#21)

This commit is contained in:
Dong-hee Na 2019-03-16 00:47:57 +09:00 committed by GitHub
parent b535580cae
commit 5f56e8609c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 0 deletions

View File

@ -135,3 +135,31 @@ func AverageHashExtend(img image.Image, hashSize int) (*ExtImageHash, error) {
}
return NewExtImageHash(ahash, AHash), nil
}
// DifferenceHashExtend function returns dhash of which the size can be set larger than uint64
// Support 64bits dhash (hashSize=8) and 256bits dhash (hashSize=16)
func DifferenceHashExtend(img image.Image, hashSize int) (*ExtImageHash, error) {
if img == nil {
return nil, errors.New("Image object can not be nil")
}
imgSize := hashSize * hashSize
resized := resize.Resize(uint(hashSize)+1, uint(hashSize), img, resize.Bilinear)
pixels := transforms.Rgb2Gray(resized)
lenOfUnit := 64
dhash := make([]uint64, imgSize/lenOfUnit)
idx := 0
for i := 0; i < len(pixels); i++ {
for j := 0; j < len(pixels[i])-1; j++ {
indexOfArray := (imgSize - 1 - idx) / lenOfUnit
indexOfBit := idx % lenOfUnit
if pixels[i][j] < pixels[i][j+1] {
dhash[indexOfArray] |= 1 << uint(indexOfBit)
}
idx++
}
}
return NewExtImageHash(dhash, DHash), nil
}

View File

@ -177,6 +177,19 @@ func TestExtImageHashCompute(t *testing.T) {
{"_examples/sample2.jpg", "_examples/sample2.jpg", 16, PerceptionHashExtend, "PerceptionHashExtend", 0},
{"_examples/sample3.jpg", "_examples/sample3.jpg", 16, PerceptionHashExtend, "PerceptionHashExtend", 0},
{"_examples/sample4.jpg", "_examples/sample4.jpg", 16, PerceptionHashExtend, "PerceptionHashExtend", 0},
{"_examples/sample1.jpg", "_examples/sample1.jpg", 8, DifferenceHashExtend, "DifferenceHashExtend", 0},
{"_examples/sample2.jpg", "_examples/sample2.jpg", 8, DifferenceHashExtend, "DifferenceHashExtend", 0},
{"_examples/sample3.jpg", "_examples/sample3.jpg", 8, DifferenceHashExtend, "DifferenceHashExtend", 0},
{"_examples/sample4.jpg", "_examples/sample4.jpg", 8, DifferenceHashExtend, "DifferenceHashExtend", 0},
{"_examples/sample1.jpg", "_examples/sample2.jpg", 8, DifferenceHashExtend, "DifferenceHashExtend", 43},
{"_examples/sample1.jpg", "_examples/sample3.jpg", 8, DifferenceHashExtend, "DifferenceHashExtend", 0},
{"_examples/sample1.jpg", "_examples/sample4.jpg", 8, DifferenceHashExtend, "DifferenceHashExtend", 37},
{"_examples/sample2.jpg", "_examples/sample3.jpg", 8, DifferenceHashExtend, "DifferenceHashExtend", 43},
{"_examples/sample2.jpg", "_examples/sample4.jpg", 8, DifferenceHashExtend, "DifferenceHashExtend", 16},
{"_examples/sample1.jpg", "_examples/sample1.jpg", 16, DifferenceHashExtend, "DifferenceHashExtend", 0},
{"_examples/sample2.jpg", "_examples/sample2.jpg", 16, DifferenceHashExtend, "DifferenceHashExtend", 0},
{"_examples/sample3.jpg", "_examples/sample3.jpg", 16, DifferenceHashExtend, "DifferenceHashExtend", 0},
{"_examples/sample4.jpg", "_examples/sample4.jpg", 16, DifferenceHashExtend, "DifferenceHashExtend", 0},
} {
file1, err := os.Open(tt.img1)
if err != nil {