Difference Hash

Difference hashing is based on four steps:

  • (1) Reduce size shrinking the image to 9x8 so that there are 72 total pixels. Since we ignore size and aspect ratio, the hash should match any similar picture regardless of how it is stretched.
  • (2) Convert the image to grayscale.
  • (3) Compute the difference between adjacent pixels, creating the relative gradient direction. When shrinked to 9x8, the 9 pixels per row create 8 differences between adjacent pixels.
  • (4) Assign each bits depending on whether the left pixel is brighter than the right pixel.

Example

Let us visualize the original image and hash and mathash of an image.

using TestImages
img = testimage("fabio_color_256.png");
img

An difference mat hash (matrix hash) can be created using difference_mathash(image, size), as follows:

using TestImages, Images, ImageHashes
img = testimage("fabio_color_256.png");
mat_hash = difference_mathash(img, 8)
mat_hash
9×8 BitMatrix:
 0  1  1  0  1  0  1  0
 1  0  0  1  0  1  1  1
 1  1  0  1  1  1  0  0
 0  0  0  1  0  1  1  1
 1  1  1  0  1  0  0  1
 1  0  1  0  1  0  0  0
 1  0  0  0  0  1  1  0
 0  0  0  1  1  1  1  1
 0  0  0  0  0  0  0  0

We can visualize the hash using Gray..

using TestImages, Images, ImageHashes
img = testimage("fabio_color_256.png");
mat_hash = difference_mathash(img, 8)
Gray.(mat_hash)

The bigger the mat hash is, the higher quality it can achive.

using TestImages, Images, ImageHashes
img = testimage("fabio_color_256.png");
mat_hash = difference_mathash(img, 28)
Gray.(mat_hash)

A hash (vector hash) for an image can be created with difference_hash function.

using TestImages, ImageHashes
img = testimage("fabio_color_256.png");
img_hash = difference_hash(img, 8)
img_hash
0x000000000000006e54630e3adb9b4cb2

Execution time and allocations

using TestImages, ImageHashes, BenchmarkTools
img = testimage("fabio_color_256.png");
benchmark = @benchmark difference_hash($img, 8)
benchmark
BenchmarkTools.Trial: 10000 samples with 9 evaluations per sample.
 Range (min … max):  2.010 μs …   5.025 μs  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     2.299 μs               ┊ GC (median):    0.00%
 Time  (mean ± σ):   2.317 μs ± 260.859 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

  ▆▇▅▃▂▁▁    ▅█▇▅▄▂▂▁▁ ▂▄▅▄▃▂▁▁▂▁                             ▂
  ████████▆▃▆█████████▇███████████▇▇█▇▆▆▆▇█▇▆▅▅▆▆▅▃▄▄▄▅▄▆▇▆▇▆ █
  2.01 μs      Histogram: log(frequency) by time      3.34 μs <

 Memory estimate: 528 bytes, allocs estimate: 4.