Skip to content

Develious/simimage

 
 

Repository files navigation

Comparing images in Go ➔ LATEST version

Near duplicates and resized images can be found with the module.

Demo: similar image search and clustering (deployed from).

Use this popular legacy repo, or try recommended latest major version in a new repository.

Changes in the new repo vs this one:

  1. Hashes get proper "hashy" meaning. If you work with millions of images, do preliminary image comparison with hash tables.
  2. Renamed functions. What used to be Hash now becomes Icon to reflect (1).

About this repo

There are no dependencies: only the Golang standard library is used. Supported image types: GIF, JPEG and PNG (golang.org/pkg/image/ as in October 2018).

Similar function gives a verdict whether 2 images are similar or not. The library also contains wrapper functions to open/save images and basic image resampling/resizing.

SimilarCustom function allows your own similarity metric thresholds.

Documentation: godoc.

Example of comparing 2 photos

To test this example go-file, you need to initialize modules from command line, because the latest version (v2) uses them:

go mod init foo

Here foo can be anything for testing purposes. Then get the required import:

go get github.com/vitali-fedulov/images/v2

Now you are ready to run or build the example.

package main

import (
	"fmt"

	// Notice v2, which is module-based most recent version.
	// Explanation: https://go.dev/blog/v2-go-modules
	"github.com/vitali-fedulov/images/v2"
)

func main() {
	
	// Open photos.
	imgA, err := images.Open("photoA.jpg")
	if err != nil {
		panic(err)
	}
	imgB, err := images.Open("photoB.jpg")
	if err != nil {
		panic(err)
	}
	
	// Calculate hashes and image sizes.
	hashA, imgSizeA := images.Hash(imgA)
	hashB, imgSizeB := images.Hash(imgB)
	
	// Image comparison.
	if images.Similar(hashA, hashB, imgSizeA, imgSizeB) {
		fmt.Println("Images are similar.")
	} else {
		fmt.Println("Images are distinct.")
	}
}

Algorithm for image comparison

Detailed explanation, also as a PDF.

Summary: In the algorithm images are resized to small squares of fixed size. A number of masks representing several sample pixels are run against the resized images to calculate average color values. Then the values are compared to give the similarity verdict. Also image proportions are used to avoid matching images of distinct shape.

Packages

No packages published

Languages

  • Go 100.0%