A brief introduction of image similarity--hash algorithm

Source: Internet
Author: User

One, mean value hash

The mean hash method is to generate a "fingerprint" string for each image, and then compare the fingerprint of different images to determine the similarity of the image, the closer the result, the more similar the picture.
The steps to calculate the mean hash.
1. Size reduction
The quickest way to get rid of high frequencies and details is to shrink the image to the size of 8x8, totaling 64 pixels. Don't keep the aspect ratio, just turn it into a 8*8 square. This allows you to compare images of any size, and discard the differences in the images of different sizes and proportions.
2. Simplified color
Convert small images of 8*8 into grayscale images, converting 64-pixel colors (red,green,blue) into one color (black-and-white grayscale).
3. Calculate the average
Calculates a grayscale average of all 64 pixels.
4. Compare the grayscale of pixels
The grayscale of each pixel is compared to the average. Greater than or equal to the average, recorded as 1, less than the average, recorded as 0.
5. Calculate the hash value
By combining the results of the previous step, you make up a 64-bit integer, which is the fingerprint of the image. The order of the combinations is not important, just make sure all the pictures are in the same order. (I set it from left to right, from top to bottom, with binary).

Second, perceptual hash (Phash)

Perceptual hash and mean hash are very similar, using discrete cosine (DCT) to reduce frequency.
The two-dimensional DCT transforms the two-dimensional image from the spatial domain to the frequency domain. The image of the said, is to calculate the image by which two-dimensional cosine wave composition, the calculated results are C (U, v), where u is the horizontal direction of the Hivipo frequency, V is the vertical direction of the Hivipo frequency, will eventually calculate a lot of C (U,V); Each c is called a DCT coefficient, which represents the amplitude (or energy) of the Hivipo frequency (u,v), and the superposition of all these two-dimensional waves is the original image.


Steps to calculate Phash:
1. Size reduction
Phash starts with a small picture, but the picture is better than 8*8,32*32. The purpose of this is to simplify the calculation of the DCT, rather than reducing the frequency.
2. Simplified color
Convert images into grayscale images to further simplify computational complexity.
3. Computing DCT
DCT is the decomposition of the image frequency aggregation and ladder shape, although JPEG using 8*8 DCT transform, here using the 32*32 DCT transformation.
4. Narrowing the DCT
Although the result of DCT is 32*32 size matrix, we just keep the 8*8 matrix in the upper left corner, this part presents the lowest frequency in the picture.
5. Calculate the average
Like the mean hash, computes the mean value of the DCT,
6, further reduce the DCT
This is the most important step, according to the 8*8 DCT matrix, set 0 or 1 of the 64-bit hash value, greater than or equal to the DCT mean is set to "1", less than the DCT mean is set to "0."
7. Construct hash value
Set the 64bit to a 64-bit long integer, and the order of the combinations is not important, just make sure all the pictures are in the same order. As with the mean hash, the phash can also be compared using hamming distances. (only need to compare each position and calculate the number of different bits)

A brief introduction of image similarity--hash algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.