Go Perceptual image hashing package

GitHub Action GoDoc Go Report Card

goimagehash

Inspired by imagehash

A image hashing library written in Go. ImageHash supports:

Installation

go get github.com/corona10/goimagehash

Special thanks to

Usage

func main() {
        file1, _ := os.Open("sample1.jpg")
        file2, _ := os.Open("sample2.jpg")
        defer file1.Close()
        defer file2.Close()

        img1, _ := jpeg.Decode(file1)
        img2, _ := jpeg.Decode(file2)
        hash1, _ := goimagehash.AverageHash(img1)
        hash2, _ := goimagehash.AverageHash(img2)
        distance, _ := hash1.Distance(hash2)
        fmt.Printf("Distance between images: %v\n", distance)

        hash1, _ = goimagehash.DifferenceHash(img1)
        hash2, _ = goimagehash.DifferenceHash(img2)
        distance, _ = hash1.Distance(hash2)
        fmt.Printf("Distance between images: %v\n", distance)
        width, height := 8, 8
        hash3, _ = goimagehash.ExtAverageHash(img1, width, height)
        hash4, _ = goimagehash.ExtAverageHash(img2, width, height)
        distance, _ = hash3.Distance(hash4)
        fmt.Printf("Distance between images: %v\n", distance)
        fmt.Printf("hash3 bit size: %v\n", hash3.Bits())
        fmt.Printf("hash4 bit size: %v\n", hash4.Bits())

        var b bytes.Buffer
        foo := bufio.NewWriter(&b)
        _ = hash4.Dump(foo)
        foo.Flush()
        bar := bufio.NewReader(&b)
        hash5, _ := goimagehash.LoadExtImageHash(bar)
}

Release Note

v1.0.3

  • Add workflow for GithubAction
  • Fix typo on the GoDoc for LoadImageHash

v1.0.2

  • go.mod is now used for install goimagehash

v1.0.1

  • Perception/ExtPerception hash creation times are reduced

v1.0.0

IMPORTANT goimagehash v1.0.0 does not have compatible with the before version for future features

v0.3.0

  • Support DifferenceHashExtend.
  • Support AverageHashExtend.
  • Support PerceptionHashExtend by @TokyoWolFrog.

v0.2.0

  • Perception Hash is updated.
  • Fix a critical bug of finding median value.

v0.1.0

  • Support Average hashing
  • Support Difference hashing
  • Support Perception hashing
  • Use bits.OnesCount64 for computing Hamming distance by @dominikh
  • Support hex serialization methods to ImageHash by @brunoro
Owner
Dong-hee Na
Software development engineer at Line corp, Member of the @python core-dev, Google Summer of Code 2018 mentor, Google Summer of Code 2017 student.
Dong-hee Na
Comments
  • Refactored Perceptionhash for performance

    Refactored Perceptionhash for performance

    Refactored PerceptionHash for performance. If you want me to change the name to replace existing function that would be acceptable.

    Features:

    • Significantly reduces allocations
    • Uses static DCT1D tables
    • Uses sync pool to reduce pixel data allocations
    • Uses image.Image optimizations for obtaining pixels for improved inlining of functions
    • Backward compatible with PerceptionHash, they are interchangeable. Confirmed with passing tests.
    • Performance without image resize as below.
    name                      time/op
    PerceptionHash/Regular-12         358µs ±26%
    PerceptionHash/Fast-12           84.8µs ± 7%
    PerceptionHash/Parallel-12       82.5µs ± 3%
    PerceptionHash/Fast-Parallel-12  11.5µs ± 1%
    
    name                      alloc/op
    PerceptionHash/Regular-12         193kB ± 0%
    PerceptionHash/Fast-12            17.0B ± 0%
    PerceptionHash/Parallel-12        193kB ± 0%
    PerceptionHash/Fast-Parallel-12    198B ± 0%
    
    name                      allocs/op
    PerceptionHash/Regular-12         4.68k ± 0%
    PerceptionHash/Fast-12             0.00     
    PerceptionHash/Parallel-12        4.68k ± 0%
    PerceptionHash/Fast-Parallel-12    0.00 
    
  • Use

    Use "imaging" module for image resize instead of "resize"

    Use imaging module to get the same results as Python imagehash module. Issue: https://github.com/corona10/goimagehash/issues/41

    There is no impact to the performance, but image hash results won't be compatible with previous versions since image modules produce different down scaled images.

    Performance test

    Test image

    • Resolution: 10001 x 3649
    • File size: 16.4 MB

    Test results

    resize.Bilinear
    10 images are processed in 10.001422234s
    
    imaging.Lanczos
    10 images are processed in 10.456443503s
    
    imaging.Linear
    10 images are processed in 9.56188539s
    
    Python imagehash
    10 images are processed in 4.128061s
    
  •  dct: Improve DCT1D to O(nlogn) algorithm

    dct: Improve DCT1D to O(nlogn) algorithm

    AS-IS:
    BenchmarkPerceptionHash-8  500  2893930 ns/op  456698 B/op  4455 allocs/op
    
    TO-BE:
    BenchmarkPerceptionHash-8  2000  890306 ns/op  456382 B/op  4455 allocs/op
    

    reference: DCT type II, unscaled. Algorithm by Byeong Gi Lee, 1984.

    closes: #13

  • ImageHash hex string serialization

    ImageHash hex string serialization

    Added the ToString and FromString methods, that create a string representation of an ImageHash, following the format: <first letter of type>:<hex representation of hash.

    Also fixed some go linter complaints.

  • goimagehash: Fix quickselect algorithm to pick median value.

    goimagehash: Fix quickselect algorithm to pick median value.

    There was a serious bug for picking a median value of pixels by using a quick select algorithm. By this patch, the perception hash algorithm works better. But it can affect to a generated hash which was generated by 0.1 version of the goimagehash. (It will be not identical if the hash was generated by perception hash from v0.1)

    After merging this patch, I will release v0.2 of the goimagehash.

    Fixes: #8

  • Use bits.OnesCount64 when available

    Use bits.OnesCount64 when available

    Benchmark on i7-2600k, which has the POPCNT instruction:

    name old time/op new time/op delta DistanceIdentical-8 5.08ns ± 0% 1.01ns ± 1% -80.07% (p=0.008 n=5+5) DistanceDifferent-8 81.5ns ± 2% 1.0ns ± 0% -98.76% (p=0.016 n=5+4)

    Benchmark on Cavium Octeon, a MIPS64 platform with no dedicated instruction:

    name old time/op new time/op delta DistanceIdentical-2 120ns ± 6% 144ns ± 5% +19.93% (p=0.008 n=5+5) DistanceDifferent-2 656ns ± 4% 144ns ± 4% -78.09% (p=0.008 n=5+5)

    Closes #10

  • Update dependencies from go module and dep tool.

    Update dependencies from go module and dep tool.

    As we discussed on issue #17, just updating dependencies.

    I don't think there is any performance improvements, but there is a fix for images with 0 width/height. https://github.com/nfnt/resize/commit/874f89dba47d254fa82f0efd7c5da88e309a5431

  • Performance improvement

    Performance improvement

    • Remove init function for DCT values reduces initial start up time
    • DCT2DFast256 supports a 256 bit PHash
    • DCT2DFast64 and DCTFast256 results can be directly set to flattens. This has 10-15% performance improvement.
  • feat(phash): add support to size 32

    feat(phash): add support to size 32

    Hello, mr @corona10 imagededup libraries works with phash size 32. refs: https://github.com/idealo/imagededup/blob/master/imagededup/methods/hashing.py#L436

    Could you review please?

    Thanks

  • Consider using math/bits.OnesCount64 for computing Hamming distance

    Consider using math/bits.OnesCount64 for computing Hamming distance

    math/bits contains optimized bit operations, which additionally are treated as intrinsics when platform support exists. For example, on modern x86 platforms, OnesCount64 will use the POPCNT instruction, and on unsupported platforms, it'll still use an implementation more efficient than shifting 63 times.

  • feat(phash): add suport to work with size 32

    feat(phash): add suport to work with size 32

    Hello, mr @corona10 imagededup libraries works with phash size 32. refs: https://github.com/idealo/imagededup/blob/master/imagededup/methods/hashing.py#L436

    Could you review please?

    Thanks

  • Panic !!! index out of FlattenPixels

    Panic !!! index out of FlattenPixels

    hello guys, i found some panic case details below:

    OS: Mac OS & Ubuntu 18.0.4 short description:

    • index out of range in file transforms/pixels.go line 35
    • index out of range in file hashcompute.go func ExtDifferenceHash line 177
    • index out of range in file hashcompute.go func ExtAverageHash line 146

    example error: github.com/corona10/goimagehash/transforms.FlattenPixels(0xc000f8e000, 0x3f, 0x3f, 0x38, 0x3f, 0x4a74e40, 0xc0029ec280, 0xc0000da030) /Users/maulanaoktofitriadi/go/pkg/mod/github.com/corona10/[email protected]/transforms/pixels.go:35 +0xe1 github.com/corona10/goimagehash.ExtAverageHash(0x4a74e40, 0xc00004a100, 0x38, 0x3f, 0x0, 0x0, 0xc002566780) /Users/maulanaoktofitriadi/go/pkg/mod/github.com/corona10/[email protected]/hashcompute.go:133 +0xbf gitlab.com/prakerja/worker-check-ktp/handler.ProcessingData.func2(0xc0023faf20, 0xc00241b9e0, 0xc0001c4000, 0xc00242fda0, 0x1f) /Users/maulanaoktofitriadi/go/src/gitlab.com/pr/worker-check-ktp/handler/worker.go:83 +0x5ee created by gitlab.com/pr/worker-check-ktp/handler.ProcessingData /Users/maulanaoktofitriadi/go/src/gitlab.com/pr/worker-check-ktp/handler/worker.go:48 +0x339

    somehow when iam using ExtAverageHash or ExtDifferenceHash function then i got index out of range

  • How to saved hashes for future comparison?

    How to saved hashes for future comparison?

    After I get an image hashed:

    	hash1, err := goimagehash.AverageHash(img1)
    	if err != nil {
    		log.Fatal(err)
    	}
    	fmt.Println("hash of image is:", hash1)
    	fmt.Println("hash1.ToString():", hash1.ToString())
    

    I get

    hash of image is: &{9222170938831798272 1}
    hash1.ToString(): a:7ffbbb9b93000000
    

    My problem is how to save hash1 in database, so that later on I can retrive it and compare its distance with newly computed hash2 using:

    distance, _ := hash1.Distance(hash2)

  • I recommend cutting off the minor ones.

    I recommend cutting off the minor ones.

    Not always in real life need to work so accurately. For example, if it is necessary to determine the motion in the picture such precision is excessive and the image will mean the same and the algorithm will say that change more than 10% that would be wrong.

    ExtAverageHash real live recommend add filter

    if p > avg && math.Abs(p-avg) > 12.5 {

    problem

    output_2_3 output_1_3

    no filter distance 183

    add filter distance 0

    TEST hashcompute_test.go:298: ExtAverageHash: Distance between _examples/sample1.jpg and _examples/sample2.jpg is expected 42 but got 35 hashcompute_test.go:298: ExtAverageHash: Distance between _examples/sample1.jpg and _examples/sample4.jpg is expected 38 but got 30 hashcompute_test.go:298: ExtAverageHash: Distance between _examples/sample2.jpg and _examples/sample3.jpg is expected 40 but got 38 hashcompute_test.go:298: ExtAverageHash: Distance between _examples/sample2.jpg and _examples/sample4.jpg is expected 6 but got 5 hashcompute_test.go:298: ExtAverageHash: Distance between _examples/sample1.jpg and _examples/sample2.jpg is expected 149 but got 137 hashcompute_test.go:298: ExtAverageHash: Distance between _examples/sample1.jpg and _examples/sample4.jpg is expected 152 but got 140 hashcompute_test.go:298: ExtAverageHash: Distance between _examples/sample2.jpg and _examples/sample3.jpg is expected 155 but got 150 hashcompute_test.go:298: ExtAverageHash: Distance between _examples/sample2.jpg and _examples/sample4.jpg is expected 27 but got 25

    p > avg

    I'm not sure if this is the right thing to do, you probably need to look at deviations of more than % of the average then it will work both ways.

Image - This repository holds supplementary Go image librariesThis repository holds supplementary Go image libraries

Go Images This repository holds supplementary Go image libraries. Download/Insta

Jan 5, 2022
darkroom - An image proxy with changeable storage backends and image processing engines with focus on speed and resiliency.
darkroom - An image proxy with changeable storage backends and image processing engines with focus on speed and resiliency.

Darkroom - Yet Another Image Proxy Introduction Darkroom combines the storage backend and the image processor and acts as an Image Proxy on your image

Dec 6, 2022
Easily customizable Social image (or Open graph image) generator

fancycard Easily customizable Social image (or Open graph image) generator Built with Go, Gin, GoQuery and Chromedp Build & Run Simply, Clone this rep

Jan 14, 2022
An API which allows you to upload an image and responds with the same image, stripped of EXIF data

strip-metadata This is an API which allows you to upload an image and responds with the same image, stripped of EXIF data. How to run You need to have

Nov 25, 2021
Imgpreview - Tiny image previews for HTML while the original image is loading
Imgpreview - Tiny image previews for HTML while the original image is loading

imgpreview This is a Go program that generates tiny blurry previews for images t

May 22, 2022
Go package for fast high-level image processing powered by libvips C library

bimg Small Go package for fast high-level image processing using libvips via C bindings, providing a simple programmatic API. bimg was designed to be

Jan 2, 2023
Imaging is a simple image processing package for Go
Imaging is a simple image processing package for Go

Imaging Package imaging provides basic image processing functions (resize, rotate, crop, brightness/contrast adjustments, etc.). All the image process

Dec 30, 2022
Go package for decoding and encoding TARGA image format

tga tga is a Go package for decoding and encoding TARGA image format. It supports RLE and raw TARGA images with 8/15/16/24/32 bits per pixel, monochro

Sep 26, 2022
golang package to find the K most dominant/prominent colors in an image
golang package to find the K most dominant/prominent colors in an image

prominentcolor Find the K most dominant colors in an image The Kmeans function returns the K most dominant colors in the image, ordered in the order o

Nov 7, 2022
Go package captcha implements generation and verification of image and audio CAPTCHAs.
Go package captcha implements generation and verification of image and audio CAPTCHAs.

Package captcha ⚠️ Warning: this captcha can be broken by advanced OCR captcha breaking algorithms. import "github.com/dchest/captcha" Package captch

Dec 30, 2022
package for convert DataURLs to image

convert base64 DataURLs to image

Oct 18, 2021
A Go package converting a monochrome 1-bit bitmap image into a set of vector paths.
A Go package converting a monochrome 1-bit bitmap image into a set of vector paths.

go-bmppath Overview Package bmppath converts a monochrome 1-bit bitmap image into a set of vector paths. Note that this package is by no means a sophi

Mar 22, 2022
magicimage is a simple image validation & save with rich feature package for net/http

Installation go get github.com/IndominusByte/magicimage Usage examples A few usage examples can be found below. See the documentation for the full lis

Feb 5, 2022
Pbm - Package ppm implements a Portable Bit Map (PBM) image decoder and encoder written in Go

Package pbm import "github.com/slashformotion/pbm" Package pbm implements a Portable Bit Map (PBM) image decoder and encoder. The supported image col

Jan 5, 2022
Image processing algorithms in pure Go
Image processing algorithms in pure Go

bild A collection of parallel image processing algorithms in pure Go. The aim of this project is simplicity in use and development over absolute high

Jan 6, 2023
Image processing library and rendering toolkit for Go.

blend Image processing library and rendering toolkit for Go. (WIP) Installation: This library is compatible with Go1. go get github.com/phrozen/blend

Nov 11, 2022
Decode embedded EXIF meta data from image files.

goexif Provides decoding of basic exif and tiff encoded data. Still in alpha - no guarantees. Suggestions and pull requests are welcome. Functionality

Dec 17, 2022
A lightning fast image processing and resizing library for Go

govips A lightning fast image processing and resizing library for Go This package wraps the core functionality of libvips image processing library by

Jan 8, 2023
Fast, simple, scalable, Docker-ready HTTP microservice for high-level image processing

imaginary Fast HTTP microservice written in Go for high-level image processing backed by bimg and libvips. imaginary can be used as private or public

Jan 3, 2023