Skip to content
Image comparison by perceptual similarity in Golang
Branch: master
Clone or download
Latest commit 6003d20 May 29, 2019

Comparing images in Go

Demo: similar image search (JavaScript version of this Go algorithm).

The library compares images by perceptual similarity to find near duplicates and resized images. Supported image types: GIF, JPEG and PNG ( 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.

Documentation: godoc.

Example of comparing 2 photos

package main

import (

func main() {
	// Open and decode photos.
	imgA, err := images.Open("photoA.jpg")
	if err != nil {
	imgB, err := images.Open("photoB.jpg")
	if err != nil {
	// Define masks.
	masks := images.Masks()
	// Calculate hashes.
	hA, imgSizeA := images.Hash(imgA, masks)
	hB, imgSizeB := images.Hash(imgB, masks)
	// Image comparison.
	if images.Similar(hA, hB, imgSizeA, imgSizeB) {
		fmt.Println("Images are similar.")
	} else {
		fmt.Println("Images are distinct.")

Algorithm for image comparison

Detailed explanation with illustrations.

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.

You can’t perform that action at this time.