ish

package module
v0.0.0-...-bbdc45b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 14, 2016 License: BSD-2-Clause Imports: 11 Imported by: 1

README

ish

A collection of algorithms for comparing the similarity of images.

Usage

This example program will compute the difference hash of a list of images from the command-line arguments:

package main

import (
	"encoding/hex"
	"fmt"
	"os"

	"github.com/brett-lempereur/ish"
)

func main() {
	hasher := ish.NewDifferenceHash(8, 8)
	for _, filename := range os.Args[1:] {
		img, ft, err := ish.LoadFile(filename)
		if err != nil {
			fmt.Printf("%s: %s\n", filename, err.Error())
			continue
		}
		dh, err := hasher.Hash(img)
		if err != nil {
			fmt.Printf("%s <%s>: %s", filename, ft, err)
		} else {
			dhs := hex.EncodeToString(dh)
			fmt.Printf("%s <%s>: %s\n", filename, ft, dhs)
		}
	}
}

Running this program against the test data in this repository gives:

[brett@saito ish]$ go run example/dhash.go testdata/*
testdata/house-32x32-Gray.png <png>: df8681cbc31986d9
testdata/house-4MP-CMYK.png <png>: df8681cbcb19861b
testdata/house-4MP-Gray.png <png>: df8681cbc3198699
testdata/house-4MP-RGBA.jpeg <jpeg>: df8681cbcb19865b
testdata/house-4MP-RGBA.png <png>: df8681cbcb19861b
testdata/house-8MP-CMYK.png <png>: df8681cbcb19861b
testdata/house-8MP-Gray.png <png>: df8681cbc3198699
testdata/house-8MP-RGBA.jpeg <jpeg>: df8681cbcb19865b
testdata/house-8MP-RGBA.png <png>: df8681cbcb19861b

Installation

Use go get to pull the package and dependencies into your GOPATH:

go get -u github.com/brett-lempereur/ish

Thanks

Thanks to Dr. Neal Krawetz for documenting some efficient image similarity hashing algorithms over at the HackerFactor.

License

Released under the BSD license.

Documentation

Overview

Package ish implements a collection of perceptual hash algorithms for digital forensic image processing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(r io.Reader) (image.Image, string, error)

Decode reads from an io.Reader, returning an image and the name of its format. If an error occurs, the returned image is nil and the value of format is undefined.

func Grayscale

func Grayscale(img image.Image) *image.Gray

Grayscale converts an image to the grayscale colour space, using optimised algorithms for common source colour spaces.

func LoadFile

func LoadFile(filename string) (image.Image, string, error)

LoadFile loads and decodes the named file, returning an image and the name of its format. If an error occurs, the returned image is nil and the value of format is undefined.

func Resize

func Resize(img *image.Gray, width, height int) (*image.Gray, error)

Resize resizes a grayscale image using an efficient bicubic filter on a single thread.

Types

type AverageHash

type AverageHash struct {
	// contains filtered or unexported fields
}

AverageHash implements the average hash perceptual hashing algorithm described by Dr. Neal Krawetz.

func NewAverageHash

func NewAverageHash(width, height int) *AverageHash

NewAverageHash returns a new instance of AverageHash.

func (*AverageHash) Distance

func (ah *AverageHash) Distance(ha, hb []byte) int

Distance returns the hamming distance between two difference hashes.

func (*AverageHash) Hash

func (ah *AverageHash) Hash(img image.Image) ([]byte, error)

Hash computes the difference hash of an image by shrinking it and comparing the relative brightness of pixels to the mean brightness of the image.

func (*AverageHash) Length

func (ah *AverageHash) Length() int

Length returns the length of the average hash in bytes.

type DifferenceHash

type DifferenceHash struct {
	// contains filtered or unexported fields
}

DifferenceHash implements the difference hash perceptual hashing algorithm described by Dr. Neal Krawetz.

func NewDifferenceHash

func NewDifferenceHash(width, height int) *DifferenceHash

NewDifferenceHash returns a new instance of DifferenceHash.

func (*DifferenceHash) Distance

func (dh *DifferenceHash) Distance(ha, hb []byte) int

Distance returns the hamming distance between two difference hashes.

func (*DifferenceHash) Hash

func (dh *DifferenceHash) Hash(img image.Image) ([]byte, error)

Hash computes the difference hash of an image by shrinking it and comparing the relative brightness of pixels.

func (*DifferenceHash) Length

func (dh *DifferenceHash) Length() int

Length returns the length of the difference hash in bytes.

type PerceptualHash

type PerceptualHash interface {
	// Hash computes the perceptual hash of an image.
	Hash(img image.Image) ([]byte, error)
	// Length returns the length of hashes computed by this percepual hash
	// in bytes.
	Length() int
	// Distance calculates the hamming distance between two perceptual hashes.
	Distance(ha, hb []byte) int
}

PerceptualHash is the interface implemented by perceptual hash algorithms.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL