#perceptual #hash #fingerprint

no-std blockhash

A perceptual hashing algorithm for detecting similar images

6 releases (1 stable)

1.0.0 Apr 8, 2024
0.5.0 Sep 17, 2022
0.4.0 Mar 29, 2022
0.3.0 Aug 12, 2021
0.1.0 May 14, 2020

#115 in Images

Download history 5/week @ 2024-07-25 3/week @ 2024-08-01 5/week @ 2024-08-08 20/week @ 2024-08-15 11/week @ 2024-08-22 18/week @ 2024-08-29 9/week @ 2024-09-05 6/week @ 2024-09-12 25/week @ 2024-09-19 38/week @ 2024-09-26 18/week @ 2024-10-03 100/week @ 2024-10-10 23/week @ 2024-10-17 9/week @ 2024-10-24 38/week @ 2024-10-31 11/week @ 2024-11-07

85 downloads per month
Used in 3 crates

MIT license

1.5MB
779 lines

blockhash

Crates.io CI status MIT License

This is an implementation of the Blockhash algorithm for detecting similar images, and can produce 16-, 64-, 144-, and 256-bit perceptual hashes.

Support for the image crate is provided by default, but support for any image type can be easily added.

Documentation

Usage

use blockhash::blockhash64;

let img = image::open("images/example.png").unwrap();
let hash = blockhash64(&img);

assert_eq!(hash.to_string(), "c7c48f8989c77e0c");

The Blockhash algorithm

This is a basic outline of how the algorithm works. Note that this explanation uses floating-point numbers, but the library itself only uses integer operations in order to avoid any rounding errors.

To demonstrate the algorithm, we will calculate the 64-bit hash of the following image:

First we convert it to a grayscale image by taking the average of the red, green, and blue components:

Then we divide the image into 64 (8×8) blocks and calculate the average brightness for all the pixels in each block:

Next, we divide the blocks into 4 horizontal bands and find the median value for each band:

Blocks brighter than the median represent a 1 and blocks darker than the median represent a 0. This gives us the 64 bits of the hash, read from left to right, top to bottom:

We can represent the hash as a hexadecimal string:

c7c48f8989c77e0c

License

This project is licensed under the MIT license.

Dependencies