diff --git a/hashcompute.go b/hashcompute.go index 8e61ca9..877f3a3 100644 --- a/hashcompute.go +++ b/hashcompute.go @@ -91,6 +91,7 @@ func PerceptionHashExtend(img image.Image, hashSize int) (*ExtImageHash, error) if img == nil { return nil, errors.New("Image object can not be nil") } + var phash []uint64 imgSize := hashSize * hashSize resized := resize.Resize(uint(imgSize), uint(imgSize), img, resize.Bilinear) pixels := transforms.Rgb2Gray(resized) @@ -99,7 +100,11 @@ func PerceptionHashExtend(img image.Image, hashSize int) (*ExtImageHash, error) median := etcs.MedianOfPixels(flattens) lenOfUnit := 64 - phash := make([]uint64, imgSize/lenOfUnit) + if imgSize%lenOfUnit == 0 { + phash = make([]uint64, imgSize/lenOfUnit) + } else { + phash = make([]uint64, imgSize/lenOfUnit+1) + } for idx, p := range flattens { indexOfArray := idx / lenOfUnit indexOfBit := lenOfUnit - idx%lenOfUnit - 1 @@ -116,7 +121,7 @@ func AverageHashExtend(img image.Image, hashSize int) (*ExtImageHash, error) { if img == nil { return nil, errors.New("Image object can not be nil") } - + var ahash []uint64 imgSize := hashSize * hashSize resized := resize.Resize(uint(hashSize), uint(hashSize), img, resize.Bilinear) @@ -125,7 +130,11 @@ func AverageHashExtend(img image.Image, hashSize int) (*ExtImageHash, error) { avg := etcs.MeanOfPixels(flattens) lenOfUnit := 64 - ahash := make([]uint64, imgSize/lenOfUnit) + if imgSize%lenOfUnit == 0 { + ahash = make([]uint64, imgSize/lenOfUnit) + } else { + ahash = make([]uint64, imgSize/lenOfUnit+1) + } for idx, p := range flattens { indexOfArray := idx / lenOfUnit indexOfBit := lenOfUnit - idx%lenOfUnit - 1 @@ -143,13 +152,18 @@ func DifferenceHashExtend(img image.Image, hashSize int) (*ExtImageHash, error) return nil, errors.New("Image object can not be nil") } + var dhash []uint64 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) + if imgSize%lenOfUnit == 0 { + dhash = make([]uint64, imgSize/lenOfUnit) + } else { + dhash = make([]uint64, imgSize/lenOfUnit+1) + } idx := 0 for i := 0; i < len(pixels); i++ { for j := 0; j < len(pixels[i])-1; j++ { diff --git a/hashcompute_test.go b/hashcompute_test.go index e77f8df..5c315dc 100644 --- a/hashcompute_test.go +++ b/hashcompute_test.go @@ -164,6 +164,15 @@ func TestExtImageHashCompute(t *testing.T) { {"_examples/sample2.jpg", "_examples/sample2.jpg", 16, AverageHashExtend, "AverageHashExtend", 0}, {"_examples/sample3.jpg", "_examples/sample3.jpg", 16, AverageHashExtend, "AverageHashExtend", 0}, {"_examples/sample4.jpg", "_examples/sample4.jpg", 16, AverageHashExtend, "AverageHashExtend", 0}, + {"_examples/sample1.jpg", "_examples/sample2.jpg", 16, AverageHashExtend, "AverageHashExtend", 149}, + {"_examples/sample1.jpg", "_examples/sample3.jpg", 16, AverageHashExtend, "AverageHashExtend", 8}, + {"_examples/sample1.jpg", "_examples/sample4.jpg", 16, AverageHashExtend, "AverageHashExtend", 152}, + {"_examples/sample2.jpg", "_examples/sample3.jpg", 16, AverageHashExtend, "AverageHashExtend", 155}, + {"_examples/sample2.jpg", "_examples/sample4.jpg", 16, AverageHashExtend, "AverageHashExtend", 27}, + {"_examples/sample1.jpg", "_examples/sample1.jpg", 17, AverageHashExtend, "AverageHashExtend", 0}, + {"_examples/sample2.jpg", "_examples/sample2.jpg", 17, AverageHashExtend, "AverageHashExtend", 0}, + {"_examples/sample3.jpg", "_examples/sample3.jpg", 17, AverageHashExtend, "AverageHashExtend", 0}, + {"_examples/sample4.jpg", "_examples/sample4.jpg", 17, AverageHashExtend, "AverageHashExtend", 0}, {"_examples/sample1.jpg", "_examples/sample1.jpg", 8, PerceptionHashExtend, "PerceptionHashExtend", 0}, {"_examples/sample2.jpg", "_examples/sample2.jpg", 8, PerceptionHashExtend, "PerceptionHashExtend", 0}, {"_examples/sample3.jpg", "_examples/sample3.jpg", 8, PerceptionHashExtend, "PerceptionHashExtend", 0}, @@ -177,6 +186,15 @@ 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/sample2.jpg", 16, PerceptionHashExtend, "PerceptionHashExtend", 122}, + {"_examples/sample1.jpg", "_examples/sample3.jpg", 16, PerceptionHashExtend, "PerceptionHashExtend", 12}, + {"_examples/sample1.jpg", "_examples/sample4.jpg", 16, PerceptionHashExtend, "PerceptionHashExtend", 122}, + {"_examples/sample2.jpg", "_examples/sample3.jpg", 16, PerceptionHashExtend, "PerceptionHashExtend", 118}, + {"_examples/sample2.jpg", "_examples/sample4.jpg", 16, PerceptionHashExtend, "PerceptionHashExtend", 104}, + {"_examples/sample1.jpg", "_examples/sample1.jpg", 17, PerceptionHashExtend, "PerceptionHashExtend", 0}, + {"_examples/sample2.jpg", "_examples/sample2.jpg", 17, PerceptionHashExtend, "PerceptionHashExtend", 0}, + {"_examples/sample3.jpg", "_examples/sample3.jpg", 17, PerceptionHashExtend, "PerceptionHashExtend", 0}, + {"_examples/sample4.jpg", "_examples/sample4.jpg", 17, 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}, @@ -190,6 +208,15 @@ func TestExtImageHashCompute(t *testing.T) { {"_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}, + {"_examples/sample1.jpg", "_examples/sample2.jpg", 16, DifferenceHashExtend, "DifferenceHashExtend", 139}, + {"_examples/sample1.jpg", "_examples/sample3.jpg", 16, DifferenceHashExtend, "DifferenceHashExtend", 14}, + {"_examples/sample1.jpg", "_examples/sample4.jpg", 16, DifferenceHashExtend, "DifferenceHashExtend", 130}, + {"_examples/sample2.jpg", "_examples/sample3.jpg", 16, DifferenceHashExtend, "DifferenceHashExtend", 147}, + {"_examples/sample2.jpg", "_examples/sample4.jpg", 16, DifferenceHashExtend, "DifferenceHashExtend", 89}, + {"_examples/sample1.jpg", "_examples/sample1.jpg", 17, DifferenceHashExtend, "DifferenceHashExtend", 0}, + {"_examples/sample2.jpg", "_examples/sample2.jpg", 17, DifferenceHashExtend, "DifferenceHashExtend", 0}, + {"_examples/sample3.jpg", "_examples/sample3.jpg", 17, DifferenceHashExtend, "DifferenceHashExtend", 0}, + {"_examples/sample4.jpg", "_examples/sample4.jpg", 17, DifferenceHashExtend, "DifferenceHashExtend", 0}, } { file1, err := os.Open(tt.img1) if err != nil {