perceptive

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

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

Go to latest
Published: Oct 26, 2015 License: MIT Imports: 6 Imported by: 1

README

Perceptive

Perceptive is a Go library which implements perceptual hash algorithms for comparing images.

Perceptual hash algorithms are a family of comparable hash functions which generate distinct (but not unique) fingerprints, these fingerprints are then comparable.

Perceptual hash algorithms are mainly used for detecting duplicates of the same files, in a way that standard and cryptographic hashes generally fail.

Note: This library can only compute hashes for images, it does not work on audio or video files.

Currently, the following perceptual hash algorithms are implemented:

  • Average Hash (Ahash) - Fast but generates a huge number of false positives.
  • Difference Hash (Dhash) - Fast and very few false positives.

Perceptual hash algorithms can give false positives, but there main aim is to give you a sense of similarity between files.

Perceptual hash algorithms tend to return a distance score. When comparing the two identical images below, we would receive a distance of 0:

A distance of zero means that the images are likely the same.

When comparing the two similar images below we would receive a distance between 1-10 (depending on the hashing technique used):

A distance between 1-10 indicates the images are likely a variation of each other.

When comparing the two different images below we would receive a distance greater than 10:

A distance greater than 10 indicates the images are likely different.

Remember perceptual hash algorithms can give false positives.

Installation

go get github.com/umahmood/perceptive

cd $GOPATH/src/github.com/umahmood/perceptive

go test ./...

Dependencies

Usage

package main

import (
    "log"

    "github.com/disintegration/imaging"
    "github.com/umahmood/perceptive"
)

func openImage(filePath string) image.Image {
    img, err := imaging.Open(filePath)
    if err != nil {
        log.Fatalln(err)
    }
    return img
}

func main() {
    imgA := openImage("lena.jpg")
    imgB := openImage("lena.jpg")

    distance, err := perceptive.CompareImages(imgA, imgB, perceptive.Difference)

    if distance == 0 {
        // images are likely the same
    } else if distance >= 1 && distance <= 10 {
        // images are potentially a variation
    } else {
        // images are likely different
    }
}

To do

  • Implement Phash algorithm targeting images.
  • Compute perceptual hashes for audio files.
  • Compute perceptual hashes for video files.

Documentation

http://godoc.org/github.com/umahmood/perceptive

References

License

See the LICENSE file for license rights and limitations (MIT).

Documentation

Overview

Package perceptive implements perceptual hash algorithms for comparing images.

Perceptual hash algorithms are a family of comparable hash functions which generate distinct (but not unique) fingerprints, these fingerprints are then comparable.

Perceptual hash algorithms are mainly used for detecting duplicates of the same files, in a way that standard and cryptographic hashes generally fail.

The following perceptual hash algorithms are implemented:

- Average Hash (Ahash) - Fast but generates a huge number of false positives.

- Difference Hash (Dhash) - Fast and very few false positives.

Below are some examples on how to use the library:

package main

import (
    "log"

    "github.com/disintegration/imaging"
    "github.com/umahmood/perceptive"
)

func openImage(filePath string) image.Image {
    img, err := imaging.Open(filePath)
    if err != nil {
        log.Fatalln(err)
    }
    return img
}

func main() {
    imgA := openImage("lena.jpg")
    imgB := openImage("lena.jpg")

    distance, err := perceptive.CompareImages(imgA, imgB, perceptive.Difference)

    if distance == 0 {
        // images are likely the same
    } else if distance >= 1 && distance <= 10 {
        // images are potentially a variation
    } else {
        // images are likely different
    }
}

You can also use the perceptual hash algorithms directly, this is good if you want to store the hashes in a database or some look up table:

func main() {
    imgA := openImage("lena.jpg")
    imgB := openImage("lena.jpg")

    hash1, err := perceptive.Dhash(imgA)

    if err != nil {
        // handle error
    }

    hash2, err := perceptive.Dhash(imgB)

    if err != nil {
        // handle error
    }

    // hash1 and hash2 can be stored (in a database, etc...) for later use

    // hash1 and hash2 can be compared directly
    distance := perceptive.HammingDistance(hash1, hash2)

    ...
}

When performing a Hamming distance on two hashes from Ahash or Dhash, the distance output has the following meaning:

- A distance of 0 means that the images are likely the same.

- A distance between 1-10 indicates the images are likely a variation of each other.

- A distance greater than 10 indicates the images are likely different.

Index

Constants

View Source
const (
	Major = 1
	Minor = 0
	Patch = 1
)

Semantic versioning - http://semver.org/

Variables

View Source
var (
	ErrNilImage    = errors.New("perceptive: nil image")
	ErrInvalidHash = errors.New("perceptive: invalid perceptual hash")
)

Errors

Functions

func Ahash

func Ahash(img image.Image) (uint64, error)

Ahash implements the average hash algorithm.

func CompareImages

func CompareImages(img1, img2 image.Image, hash PerceptualHash) (int, error)

CompareImages compares two images using the given perceptual hash algorithm. The function returns a distance value indicating how similar the two images are. The distance value differs depending on the perceptual hash algorithm used. If perceptual hash type is Average or Difference:

- A distance of 0 indicates the same hash and likely a similar image.

- A distance between 1 and 10 the image is potentially a variation.

- A distance greater than 10 the image is likely a different image.

func Dhash

func Dhash(img image.Image) (uint64, error)

Dhash implements the difference hash algorithm.

func HammingDistance

func HammingDistance(x, y uint64) int

HammingDistance computes the Hamming distance of two integers.

func Version

func Version() string

Version returns library version.

Types

type PerceptualHash

type PerceptualHash uint8

PerceptualHash perceptual hash algorithms

const (
	Average    PerceptualHash = iota // Average hash (Ahash)
	Difference                       // Difference hash (Dhash)
)

Valid perceptual hashes

Jump to

Keyboard shortcuts

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