Quoted By:

Some details on how a simple perceptual hashing algorithm works:

Perceptual hashes are hashes where the closer an image resembles another, the closer one hash also is to another. Meaning, there is a metric distance between hashes, which can be measured with a distance function such as the hamming distance.

To compute the hash, we take a 32 by 32 2D discrete cosine transform (DCT) of a scaled-down grey-scale version of the image. Then, we take the top 8 by 8 section of the DCT, which gives us the low-frequency information of the image, i.e. the basic features.

Next, calculate the mean value of all values of said cropped DCT. Iterate through all 64 values of the 8x8 field, and if the value of the DCT is lower than the mean value, set the bit at said position in the 64-bit hash to 1.

I.e.:

for (int i = 0; i < 64; i++) { if (cropped_dct* > mean) { hash = hash | 1 << i; }}*

If you just have a few banned hashes you want to compare this against, you can simply do a linear search by computing the distance function between each banned hash and the hash you're checking. If you have a lot of hashes you want to compare against, and want to find the closest, a vantage point tree or a multiple vantage point tree is a more efficient solution, since the distance is in a metric space, so you only need to compare the distance between vantage points and not literally every known banned hash.