From 5f56e8609c1304c9019888e3716109654d00b80c Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Sat, 16 Mar 2019 00:47:57 +0900 Subject: [PATCH] DifferenceHashExtend: Implement DifferenceHashExtend (#21) --- hashcompute.go | 28 ++++++++++++++++++++++++++++ hashcompute_test.go | 13 +++++++++++++ 2 files changed, 41 insertions(+) diff --git a/hashcompute.go b/hashcompute.go index abf2aae..4fcd405 100644 --- a/hashcompute.go +++ b/hashcompute.go @@ -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 +} diff --git a/hashcompute_test.go b/hashcompute_test.go index b058fb2..e77f8df 100644 --- a/hashcompute_test.go +++ b/hashcompute_test.go @@ -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 {