Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wavelet hash implementation #60

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ A image hashing library written in Go. ImageHash supports:
* [Average hashing](http://www.hackerfactor.com/blog/index.php?/archives/432-Looks-Like-It.html)
* [Difference hashing](http://www.hackerfactor.com/blog/index.php?/archives/529-Kind-of-Like-That.html)
* [Perception hashing](http://www.hackerfactor.com/blog/index.php?/archives/432-Looks-Like-It.html)
* [Wavelet hashing](https://fullstackml.com/wavelet-image-hash-in-python-3504fdd282b5) [TODO]
* [Wavelet hashing](https://fullstackml.com/wavelet-image-hash-in-python-3504fdd282b5)

## Installation
```
Expand Down
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
module github.com/corona10/goimagehash

go 1.19

require github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.8.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
16 changes: 16 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,18 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
70 changes: 70 additions & 0 deletions hashcompute.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package goimagehash
import (
"errors"
"image"
"math"
"math/bits"
"sync"

"github.com/corona10/goimagehash/etcs"
Expand Down Expand Up @@ -91,6 +93,34 @@ func PerceptionHash(img image.Image) (*ImageHash, error) {
return phash, nil
}

func WaveletHash(img image.Image) (*ImageHash, error) {
if img == nil {
return nil, errors.New("image object can not be nil")
}
bounds := img.Bounds()
if transforms.Min(bounds.Max.X, bounds.Max.Y) < 8 {
return nil, errors.New("image width and height should be more than 8")
}
whash := NewImageHash(0, WHash)

imgScale := transforms.Floorp2(transforms.Min(bounds.Max.X, bounds.Max.Y))
resized := resize.Resize(imgScale, imgScale, img, resize.Bilinear)
pixels := transforms.Rgb2Gray(resized)
maxlevel := bits.Len(imgScale) - 1
transforms.DWT2D(pixels, maxlevel)
pixels[0][0] = 0.0
transforms.IDWT2D(pixels, maxlevel)
transforms.DWT2D(pixels, maxlevel-3)
flattens := transforms.FlattenPixels(pixels, 8, 8)
median := etcs.MedianOfPixelsFast64(flattens[:])
for idx, p := range flattens {
if p > median {
whash.leftShiftSet(64 - idx - 1)
}
}
return whash, nil
}

var pixelPool64 = sync.Pool{
New: func() interface{} {
p := make([]float64, 4096)
Expand Down Expand Up @@ -195,3 +225,43 @@ func ExtDifferenceHash(img image.Image, width, height int) (*ExtImageHash, error
}
return NewExtImageHash(dhash, DHash, imgSize), nil
}

// ExtWaveletHash function returns whash of which the size can be set larger than uint64
// Support 64bits whash (width=8, height=8) and 256bits whash (width=16, height=16)
// Important: width * height should be the power of 2
func ExtWaveletHash(img image.Image, width, height int) (*ExtImageHash, error) {
if img == nil {
return nil, errors.New("image object can not be nil")
}
imgSize := width * height
if imgSize <= 0 || imgSize&(imgSize-1) != 0 {
return nil, errors.New("width * height should be power of 2")
}
var whash []uint64
bounds := img.Bounds()
imgScale := transforms.Floorp2(transforms.Min(bounds.Max.X, bounds.Max.Y))
resized := resize.Resize(imgScale, imgScale, img, resize.Bilinear)
pixels := transforms.Rgb2Gray(resized)
maxlevel := bits.Len(imgScale) - 1
hashlevel := bits.Len(uint(math.Sqrt(float64(imgSize)))) - 1
transforms.DWT2D(pixels, maxlevel)
pixels[0][0] = 0.0
transforms.IDWT2D(pixels, maxlevel)
transforms.DWT2D(pixels, maxlevel-hashlevel)
flattens := transforms.FlattenPixels(pixels, width, height)
median := etcs.MedianOfPixelsFast64(flattens[:])
lenOfUnit := 64
if imgSize%lenOfUnit == 0 {
whash = make([]uint64, imgSize/lenOfUnit)
} else {
whash = make([]uint64, imgSize/lenOfUnit+1)
}
for idx, p := range flattens {
indexOfArray := idx / lenOfUnit
indexOfBit := lenOfUnit - idx%lenOfUnit - 1
if p > median {
whash[indexOfArray] |= 1 << uint(indexOfBit)
}
}
return NewExtImageHash(whash, WHash, imgSize), nil
}
94 changes: 94 additions & 0 deletions hashcompute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ func TestHashCompute(t *testing.T) {
{"_examples/sample1.jpg", "_examples/sample4.jpg", PerceptionHash, "PerceptionHash", 30},
{"_examples/sample2.jpg", "_examples/sample3.jpg", PerceptionHash, "PerceptionHash", 34},
{"_examples/sample2.jpg", "_examples/sample4.jpg", PerceptionHash, "PerceptionHash", 20},
{"_examples/sample1.jpg", "_examples/sample1.jpg", WaveletHash, "WaveletHash", 0},
{"_examples/sample2.jpg", "_examples/sample2.jpg", WaveletHash, "WaveletHash", 0},
{"_examples/sample3.jpg", "_examples/sample3.jpg", WaveletHash, "WaveletHash", 0},
{"_examples/sample4.jpg", "_examples/sample4.jpg", WaveletHash, "WaveletHash", 0},
{"_examples/sample1.jpg", "_examples/sample2.jpg", WaveletHash, "WaveletHash", 38},
{"_examples/sample1.jpg", "_examples/sample3.jpg", WaveletHash, "WaveletHash", 2},
{"_examples/sample1.jpg", "_examples/sample4.jpg", WaveletHash, "WaveletHash", 34},
{"_examples/sample2.jpg", "_examples/sample3.jpg", WaveletHash, "WaveletHash", 40},
{"_examples/sample2.jpg", "_examples/sample4.jpg", WaveletHash, "WaveletHash", 6},
{"_examples/sample3.jpg", "_examples/sample4.jpg", WaveletHash, "WaveletHash", 36},
Comment on lines +49 to +58
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PavelVGordeev

Would you like to provide the result of python version?

} {
file1, err := os.Open(tt.img1)
if err != nil {
Expand Down Expand Up @@ -122,6 +132,15 @@ func TestNilHashCompute(t *testing.T) {
if hash != nil {
t.Errorf("Nil hash should be got. but got %v", hash)
}

hash, err = WaveletHash(nil)
if err == nil {
t.Errorf("Error should be got.")
}
if hash != nil {
t.Errorf("Nil hash should be got. but got %v", hash)

}
}

func TestNilExtendHashCompute(t *testing.T) {
Expand Down Expand Up @@ -156,6 +175,45 @@ func TestNilExtendHashCompute(t *testing.T) {
if hash != nil {
t.Errorf("Nil hash should be got. but got %v", hash)
}

hash, err = ExtWaveletHash(nil, 8, 8)
if err == nil {
t.Errorf("Error should be got.")
}
if hash != nil {
t.Errorf("Nil hash should be got. but got %v", hash)
}
}

func TestNotAPowerOf2(t *testing.T) {
for _, tt := range []struct {
img string
width int
height int
method func(img image.Image, width, height int) (*ExtImageHash, error)
name string
}{
{"_examples/sample1.jpg", 8, 17, ExtPerceptionHash, "ExtPerceptionHash"},
{"_examples/sample1.jpg", 8, 17, ExtWaveletHash, "ExtWaveletHash"},
} {
file, err := os.Open(tt.img)
if err != nil {

}
defer file.Close()

img, err := jpeg.Decode(file)
if err != nil {
t.Errorf("%s", err)
}
hash, err := tt.method(img, tt.width, tt.height)
if err == nil {
t.Errorf("Error should be got.")
}
if hash != nil {
t.Errorf("Nil hash should be got. but got %v", hash)
}
}
}

func BenchmarkDistanceIdentical(b *testing.B) {
Expand Down Expand Up @@ -248,6 +306,24 @@ func TestExtImageHashCompute(t *testing.T) {
{"_examples/sample2.jpg", "_examples/sample2.jpg", 17, 17, ExtDifferenceHash, "ExtDifferenceHash", 0},
{"_examples/sample3.jpg", "_examples/sample3.jpg", 17, 17, ExtDifferenceHash, "ExtDifferenceHash", 0},
{"_examples/sample4.jpg", "_examples/sample4.jpg", 17, 17, ExtDifferenceHash, "ExtDifferenceHash", 0},
{"_examples/sample1.jpg", "_examples/sample1.jpg", 8, 8, ExtWaveletHash, "ExtWaveletHash", 0},
{"_examples/sample2.jpg", "_examples/sample2.jpg", 8, 8, ExtWaveletHash, "ExtWaveletHash", 0},
{"_examples/sample3.jpg", "_examples/sample3.jpg", 8, 8, ExtWaveletHash, "ExtWaveletHash", 0},
{"_examples/sample4.jpg", "_examples/sample4.jpg", 8, 8, ExtWaveletHash, "ExtWaveletHash", 0},
{"_examples/sample1.jpg", "_examples/sample2.jpg", 8, 8, ExtWaveletHash, "ExtWaveletHash", 38},
{"_examples/sample1.jpg", "_examples/sample3.jpg", 8, 8, ExtWaveletHash, "ExtWaveletHash", 2},
{"_examples/sample1.jpg", "_examples/sample4.jpg", 8, 8, ExtWaveletHash, "ExtWaveletHash", 34},
{"_examples/sample2.jpg", "_examples/sample3.jpg", 8, 8, ExtWaveletHash, "ExtWaveletHash", 40},
{"_examples/sample2.jpg", "_examples/sample4.jpg", 8, 8, ExtWaveletHash, "ExtWaveletHash", 6},
{"_examples/sample1.jpg", "_examples/sample1.jpg", 16, 16, ExtWaveletHash, "ExtWaveletHash", 0},
{"_examples/sample2.jpg", "_examples/sample2.jpg", 16, 16, ExtWaveletHash, "ExtWaveletHash", 0},
{"_examples/sample3.jpg", "_examples/sample3.jpg", 16, 16, ExtWaveletHash, "ExtWaveletHash", 0},
{"_examples/sample4.jpg", "_examples/sample4.jpg", 16, 16, ExtWaveletHash, "ExtWaveletHash", 0},
{"_examples/sample1.jpg", "_examples/sample2.jpg", 16, 16, ExtWaveletHash, "ExtWaveletHash", 166},
{"_examples/sample1.jpg", "_examples/sample3.jpg", 16, 16, ExtWaveletHash, "ExtWaveletHash", 14},
{"_examples/sample1.jpg", "_examples/sample4.jpg", 16, 16, ExtWaveletHash, "ExtWaveletHash", 164},
{"_examples/sample2.jpg", "_examples/sample3.jpg", 16, 16, ExtWaveletHash, "ExtWaveletHash", 168},
{"_examples/sample2.jpg", "_examples/sample4.jpg", 16, 16, ExtWaveletHash, "ExtWaveletHash", 40},
} {
file1, err := os.Open(tt.img1)
if err != nil {
Expand Down Expand Up @@ -365,3 +441,21 @@ func BenchmarkDiffrenceHash(b *testing.B) {
}
}
}

func BenchmarkWaveletHash(b *testing.B) {
file1, err := os.Open("_examples/sample3.jpg")
if err != nil {
b.Errorf("%s", err)
}
defer file1.Close()
img1, err := jpeg.Decode(file1)
if err != nil {
b.Errorf("%s", err)
}
for i := 0; i < b.N; i++ {
_, err := ExtWaveletHash(img1, 8, 8)
if err != nil {
b.Errorf("%s", err)
}
}
}
98 changes: 98 additions & 0 deletions transforms/dwt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright 2017 The goimagehash Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package transforms

const (
coeff1 = 0.5
coeff2 = -0.5
)

func DWT1D(data []float64) {
temp := make([]float64, len(data))
half := len(data) / 2
for i := 0; i < half; i++ {
k := i * 2
temp[i] = coeff1*data[k] + coeff1*data[k+1]
temp[i+half] = coeff1*data[k] + coeff2*data[k+1]
}
copy(data, temp)
}

func DWT2D(data [][]float64, level int) {
dims := len(data)
for k := 0; k < level; k++ {
curlvl := 1 << k
curdims := dims / curlvl
row := make([]float64, curdims)
for i := 0; i < curdims; i++ {
copy(row, data[i])
DWT1D(row)
copy(data[i], row)
}
col := make([]float64, curdims)
for j := 0; j < curdims; j++ {
for i := 0; i < curdims; i++ {
col[i] = data[i][j]
}
DWT1D(col)
for i := 0; i < curdims; i++ {
data[i][j] = col[i]
}
}
}
}

func IDWT1D(data []float64) {
temp := make([]float64, len(data))
half := len(data) / 2
for i := 0; i < half; i++ {
k := i * 2
temp[k] = (coeff1*data[i] + coeff1*data[i+half]) / coeff1
temp[k+1] = (coeff1*data[i] + coeff2*data[i+half]) / coeff1
}
copy(data, temp)
}

func IDWT2D(data [][]float64, level int) {
dims := len(data)
for k := level - 1; k >= 0; k-- {
curlvl := 1 << k
curdims := dims / curlvl
col := make([]float64, curdims)
for j := 0; j < curdims; j++ {
for i := 0; i < curdims; i++ {
col[i] = data[i][j]
}
IDWT1D(col)
for i := 0; i < curdims; i++ {
data[i][j] = col[i]
}
}
row := make([]float64, curdims)
for i := 0; i < curdims; i++ {
copy(row, data[i])
IDWT1D(row)
copy(data[i], row)
}
}
}

// Floorp2 computes closest to val power of 2 (less or equal)
func Floorp2(val int) uint {
val |= val >> 1
val |= val >> 2
val |= val >> 4
val |= val >> 8
val |= val >> 16
val |= val >> 32
return uint(val - (val >> 1))
}

func Min(x, y int) int {
if x < y {
return x
}
return y
}
Loading