Measuring image similarity with perceptual hashes
A perceptual hash produces a small digest from an image file where slight changes in the images only produce a slight change in the hash. This can be useful to quickly compare thousands of images.
Getting ready
Install the
pHash
library from www.phash.org. On a Debian-based system, we can install it by using apt-get
as follows:
$ sudo apt-get install libphash0-dev
Install the phash
library from Cabal as follows:
$ cabal install phash
Find three nearly identical images. We will use the following image:
This is the second image that we will be using
And the following image is the third:
How to do it…
Import the
phash
library as follows:import Data.PHash import Data.Maybe (fromJust, isJust)
Hash an image as follows:
main = do phash1 <- imageHash "image1.jpg" putStrLn $ "image1: " ++ show phash1
Hash a similar image as follows:
phash2 <- imageHash "image2.jpg" putStrLn $ "image2: " ++ show phash2
Hash a slightly different image as follows...