imagehash icon indicating copy to clipboard operation
imagehash copied to clipboard

Differences using ImageHash Class and MySQL DB comparision

Open Ahnungsloser opened this issue 4 years ago • 3 comments

I've got three test images .. ImageHash gives following hashes ..

Image 1: 0405794952704d4a Image 2: 0405714972704d48 Image 3: 0445714972704d4a

Distances in ImageHash are always 3 using example:

use Jenssegers\ImageHash\ImageHash; use Jenssegers\ImageHash\Implementations\DifferenceHash ;

$hasher = new ImageHash(new DifferenceHash()); $distance = $hasher->distance($hash1, $hash2); // = 3 $distance = $hasher->distance($hash1, $hash3); // = 3

If the hashes are inserted into MySQL DB in column "hash" ( data type varchar() ) ...

SELECT images.*, BIT_COUNT('0405794952704d4a' ^ hash) as hamming_distance FROM images HAVING hamming_distance < 50

Distances are as followed ..

Image 1: 0405794952704d4a -> 0 Image 2: 0405714972704d48 -> 12 Image 3: 0445714972704d4a -> 21

I am expecting hamming distance of 3 like using the ImageHash DifferenceHash() Class. Where is my fault?

Ahnungsloser avatar Apr 21 '20 11:04 Ahnungsloser

your issue solved ? i am having same issue

gorkijatt avatar Apr 28 '20 17:04 gorkijatt

okae i fixed this issue :)

  1. create a new column in your database name it hexdec or anything you want.. set type as bigint and attributes unsigned.
  2. now convert your hash column values to hexdec using this function
<?php 
 $hash_format = "1d1f094b99d97933";
 $hexdec_format = hexdec($hash_format);
?>
  1. after updating hexdec column you can run this query SELECT images.*, BIT_COUNT(2098406171686304051^ hexdec) as hamming_distance FROM images HAVING hamming_distance < 30 ORDER BY hamming_distance ASC LIMIT 10

image

gorkijatt avatar Apr 28 '20 18:04 gorkijatt

laravel Eloquent seems to be unavailable

xiahe-debug avatar Jul 13 '21 09:07 xiahe-debug