Update docs.
This commit is contained in:
parent
7aba8954cd
commit
2b9c664be8
@ -1,5 +1,6 @@
|
|||||||
[![Build Status](https://travis-ci.org/corona10/goimagehash.svg?branch=master)](https://travis-ci.org/corona10/goimagehash)
|
[![Build Status](https://travis-ci.org/corona10/goimagehash.svg?branch=master)](https://travis-ci.org/corona10/goimagehash)
|
||||||
[![GoDoc](https://godoc.org/github.com/corona10/goimagehash?status.svg)](https://godoc.org/github.com/corona10/goimagehash)
|
[![GoDoc](https://godoc.org/github.com/corona10/goimagehash?status.svg)](https://godoc.org/github.com/corona10/goimagehash)
|
||||||
|
[![Go Report Card](https://goreportcard.com/badge/github.com/corona10/goimagehash)](https://goreportcard.com/report/github.com/corona10/goimagehash)
|
||||||
|
|
||||||
# goimagehash
|
# goimagehash
|
||||||
> Inspired by [imagehash](https://github.com/JohannesBuchner/imagehash)
|
> Inspired by [imagehash](https://github.com/JohannesBuchner/imagehash)
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
package etcs
|
package etcs
|
||||||
|
|
||||||
// Calculate mean of pixels.
|
// MeanOfPixels function returns a mean of pixels.
|
||||||
func MeanOfPixels(pixels []float64) float64 {
|
func MeanOfPixels(pixels []float64) float64 {
|
||||||
m := 0.0
|
m := 0.0
|
||||||
lens := len(pixels)
|
lens := len(pixels)
|
||||||
@ -19,8 +19,8 @@ func MeanOfPixels(pixels []float64) float64 {
|
|||||||
return m / float64(lens)
|
return m / float64(lens)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get a median value of pixels.
|
// MedianOfPixels function returns a median value of pixels.
|
||||||
// Use quick selection algorithm.
|
// It uses quick selection algorithm.
|
||||||
func MedianOfPixels(pixels []float64) float64 {
|
func MedianOfPixels(pixels []float64) float64 {
|
||||||
tmp := make([]float64, len(pixels))
|
tmp := make([]float64, len(pixels))
|
||||||
copy(tmp, pixels)
|
copy(tmp, pixels)
|
||||||
|
@ -13,8 +13,7 @@ import (
|
|||||||
"github.com/nfnt/resize"
|
"github.com/nfnt/resize"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Average Hash computation.
|
// AverageHash fuction returns a hash computation of average hash.
|
||||||
// Return 64bits hash.
|
|
||||||
// Implementation follows
|
// Implementation follows
|
||||||
// http://www.hackerfactor.com/blog/index.php?/archives/432-Looks-Like-It.html
|
// http://www.hackerfactor.com/blog/index.php?/archives/432-Looks-Like-It.html
|
||||||
func AverageHash(img image.Image) (*ImageHash, error) {
|
func AverageHash(img image.Image) (*ImageHash, error) {
|
||||||
@ -38,7 +37,7 @@ func AverageHash(img image.Image) (*ImageHash, error) {
|
|||||||
return ahash, nil
|
return ahash, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Difference Hash computation.
|
// DifferenceHash function returns a hash computation of difference hash.
|
||||||
// Implementation follows
|
// Implementation follows
|
||||||
// http://www.hackerfactor.com/blog/?/archives/529-Kind-of-Like-That.html
|
// http://www.hackerfactor.com/blog/?/archives/529-Kind-of-Like-That.html
|
||||||
func DifferenceHash(img image.Image) (*ImageHash, error) {
|
func DifferenceHash(img image.Image) (*ImageHash, error) {
|
||||||
@ -62,7 +61,7 @@ func DifferenceHash(img image.Image) (*ImageHash, error) {
|
|||||||
return dhash, nil
|
return dhash, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Perceptual Hash computation.
|
// PerceptionHash function returns a hash computation of phash.
|
||||||
// Implementation follows
|
// Implementation follows
|
||||||
// http://www.hackerfactor.com/blog/index.php?/archives/432-Looks-Like-It.html
|
// http://www.hackerfactor.com/blog/index.php?/archives/432-Looks-Like-It.html
|
||||||
func PerceptionHash(img image.Image) (*ImageHash, error) {
|
func PerceptionHash(img image.Image) (*ImageHash, error) {
|
||||||
|
13
imagehash.go
13
imagehash.go
@ -11,25 +11,26 @@ import (
|
|||||||
// hashKind describes the kinds of hash.
|
// hashKind describes the kinds of hash.
|
||||||
type hashKind int
|
type hashKind int
|
||||||
|
|
||||||
|
// ImageHash is a struct of hash computation.
|
||||||
type ImageHash struct {
|
type ImageHash struct {
|
||||||
hash uint64
|
hash uint64
|
||||||
kind hashKind
|
kind hashKind
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Unknown hashKind = iota
|
Unknown hashKind = iota // Unknown Hash
|
||||||
AHash // Average Hash
|
AHash // Average Hash
|
||||||
PHash // Perceptual Hash
|
PHash // Perceptual Hash
|
||||||
DHash // Difference Hash
|
DHash // Difference Hash
|
||||||
WHash // Wavlet Hash
|
WHash // Wavlet Hash
|
||||||
)
|
)
|
||||||
|
|
||||||
// Create a new image hash.
|
// NewImageHash function creates a new image hash.
|
||||||
func NewImageHash(hash uint64, kind hashKind) *ImageHash {
|
func NewImageHash(hash uint64, kind hashKind) *ImageHash {
|
||||||
return &ImageHash{hash: hash, kind: kind}
|
return &ImageHash{hash: hash, kind: kind}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return distance between hashes.
|
// Distance method returns a distance between two hashes.
|
||||||
func (h *ImageHash) Distance(other *ImageHash) (int, error) {
|
func (h *ImageHash) Distance(other *ImageHash) (int, error) {
|
||||||
if h.GetKind() != other.GetKind() {
|
if h.GetKind() != other.GetKind() {
|
||||||
return -1, errors.New("Image hashes's kind should be identical.")
|
return -1, errors.New("Image hashes's kind should be identical.")
|
||||||
@ -48,17 +49,17 @@ func (h *ImageHash) Distance(other *ImageHash) (int, error) {
|
|||||||
return diff, nil
|
return diff, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return hash values.
|
// GetHash method returns a 64bits hash value.
|
||||||
func (h *ImageHash) GetHash() uint64 {
|
func (h *ImageHash) GetHash() uint64 {
|
||||||
return h.hash
|
return h.hash
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get kind of a hash.
|
// GetKind method returns a kind of image hash.
|
||||||
func (h *ImageHash) GetKind() hashKind {
|
func (h *ImageHash) GetKind() hashKind {
|
||||||
return h.kind
|
return h.kind
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set index of bits.
|
// Set method sets a bit of index.
|
||||||
func (h *ImageHash) Set(idx int) {
|
func (h *ImageHash) Set(idx int) {
|
||||||
h.hash |= 1 << uint(idx)
|
h.hash |= 1 << uint(idx)
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Get result of DCT-II.
|
// DCT1D function returns result of DCT-II.
|
||||||
// Follows Matlab dct().
|
// Follows Matlab dct().
|
||||||
// Implementation reference:
|
// Implementation reference:
|
||||||
// https://unix4lyfe.org/dct-1d/
|
// https://unix4lyfe.org/dct-1d/
|
||||||
@ -30,7 +30,7 @@ func DCT1D(input []float64) []float64 {
|
|||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get result of DCT2D by using the seperable property.
|
// DCT2D function returns a result of DCT2D by using the seperable property.
|
||||||
func DCT2D(input [][]float64, w int, h int) [][]float64 {
|
func DCT2D(input [][]float64, w int, h int) [][]float64 {
|
||||||
output := make([][]float64, h)
|
output := make([][]float64, h)
|
||||||
for i := range output {
|
for i := range output {
|
||||||
|
@ -8,7 +8,7 @@ import (
|
|||||||
"image"
|
"image"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Convert RGB to a gray scale flatten array.
|
// Rgb2Gray function converts RGB to a gray scale array.
|
||||||
func Rgb2Gray(colorImg image.Image) [][]float64 {
|
func Rgb2Gray(colorImg image.Image) [][]float64 {
|
||||||
bounds := colorImg.Bounds()
|
bounds := colorImg.Bounds()
|
||||||
w, h := bounds.Max.X, bounds.Max.Y
|
w, h := bounds.Max.X, bounds.Max.Y
|
||||||
@ -27,6 +27,7 @@ func Rgb2Gray(colorImg image.Image) [][]float64 {
|
|||||||
return pixels
|
return pixels
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FlattenPixels function flattens 2d array into 1d array.
|
||||||
func FlattenPixels(pixels [][]float64, x int, y int) []float64 {
|
func FlattenPixels(pixels [][]float64, x int, y int) []float64 {
|
||||||
flattens := make([]float64, x*y)
|
flattens := make([]float64, x*y)
|
||||||
for i := 0; i < y; i++ {
|
for i := 0; i < y; i++ {
|
||||||
|
Loading…
Reference in New Issue
Block a user