hashcompute: Fix 64bits unaligned size case (#23)

This commit is contained in:
Dong-hee Na 2019-03-16 23:15:32 +09:00 committed by GitHub
parent 58a4aa88ab
commit 20cafe6017
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 4 deletions

View File

@ -91,6 +91,7 @@ func PerceptionHashExtend(img image.Image, hashSize 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
imgSize := hashSize * hashSize imgSize := hashSize * hashSize
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)
@ -99,7 +100,11 @@ func PerceptionHashExtend(img image.Image, hashSize int) (*ExtImageHash, error)
median := etcs.MedianOfPixels(flattens) median := etcs.MedianOfPixels(flattens)
lenOfUnit := 64 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 { for idx, p := range flattens {
indexOfArray := idx / lenOfUnit indexOfArray := idx / lenOfUnit
indexOfBit := lenOfUnit - idx%lenOfUnit - 1 indexOfBit := lenOfUnit - idx%lenOfUnit - 1
@ -116,7 +121,7 @@ func AverageHashExtend(img image.Image, hashSize 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
imgSize := hashSize * hashSize imgSize := hashSize * hashSize
resized := resize.Resize(uint(hashSize), uint(hashSize), img, resize.Bilinear) 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) avg := etcs.MeanOfPixels(flattens)
lenOfUnit := 64 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 { for idx, p := range flattens {
indexOfArray := idx / lenOfUnit indexOfArray := idx / lenOfUnit
indexOfBit := lenOfUnit - idx%lenOfUnit - 1 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") return nil, errors.New("Image object can not be nil")
} }
var dhash []uint64
imgSize := hashSize * hashSize imgSize := hashSize * hashSize
resized := resize.Resize(uint(hashSize)+1, uint(hashSize), img, resize.Bilinear) resized := resize.Resize(uint(hashSize)+1, uint(hashSize), img, resize.Bilinear)
pixels := transforms.Rgb2Gray(resized) pixels := transforms.Rgb2Gray(resized)
lenOfUnit := 64 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 idx := 0
for i := 0; i < len(pixels); i++ { for i := 0; i < len(pixels); i++ {
for j := 0; j < len(pixels[i])-1; j++ { for j := 0; j < len(pixels[i])-1; j++ {

View File

@ -164,6 +164,15 @@ func TestExtImageHashCompute(t *testing.T) {
{"_examples/sample2.jpg", "_examples/sample2.jpg", 16, AverageHashExtend, "AverageHashExtend", 0}, {"_examples/sample2.jpg", "_examples/sample2.jpg", 16, AverageHashExtend, "AverageHashExtend", 0},
{"_examples/sample3.jpg", "_examples/sample3.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/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/sample1.jpg", "_examples/sample1.jpg", 8, PerceptionHashExtend, "PerceptionHashExtend", 0},
{"_examples/sample2.jpg", "_examples/sample2.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}, {"_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/sample2.jpg", "_examples/sample2.jpg", 16, PerceptionHashExtend, "PerceptionHashExtend", 0},
{"_examples/sample3.jpg", "_examples/sample3.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/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/sample1.jpg", "_examples/sample1.jpg", 8, DifferenceHashExtend, "DifferenceHashExtend", 0},
{"_examples/sample2.jpg", "_examples/sample2.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/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/sample2.jpg", "_examples/sample2.jpg", 16, DifferenceHashExtend, "DifferenceHashExtend", 0},
{"_examples/sample3.jpg", "_examples/sample3.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/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) file1, err := os.Open(tt.img1)
if err != nil { if err != nil {