Three kinds of hashing algorithms for similar image search

Source: Internet
Author: User

Presumably we all use Google or Baidu's map function, above is my search for the results of a picture of the Crown brother, to achieve a picture comparison purposes and use of information fingerprint comparison has three algorithms, these algorithms are easy to understand, the following are described below:

One, average hash algorithm (Ahash)

This algorithm is based on the comparison of each pixel and the average value of the gray scale to achieve, the most suitable for the thumbnail, enlarge the image search.

Steps:

1. Zoom image : In order to retain the structure to remove the details, remove the difference between size, cross-aspect ratio, the picture is uniformly scaled to 8*8, a total of 64 pixels of the picture.

2. Convert to Grayscale: convert the scaled image to a 256-step grayscale image.

Attach a grayscale graph-related algorithm (R = red, G = green, B = blue)

1. Floating-point algorithm: gray=r*0.3+g*0.59+b*0.112. Integer method: gray= (r*30+g*59+b*11)/1003. Shift Method: Gray = (r*76+g*151+b*28) >>8;4. Average method: gray= (r+g+b)/3;5. Green only: gray=g;

3. Calculate average : Calculates the average of all pixels of a picture after grayscale processing.

4. Compare pixel grayscale values : traverse the grayscale image each pixel, if the greater than average record is 1, otherwise 0.

5. Get information fingerprint: combine 64 bit bits, order to maintain consistency.

6. Compare fingerprints: calculate the fingerprint of two pictures, calculate Hamming distance (from one fingerprint to another need to change several times), the more the Hamming distance, the more inconsistent the picture, conversely, the smaller the Hamming distance, the more similar picture, when the distance is 0 o'clock, the description is exactly the same. (usually think distance >10 is two completely different pictures)

Here is the program I wrote in Java for this algorithm, and Eclipse can run directly.

:http://download.csdn.net/detail/nash_/5093143

Original image to compare:

Four images in the photo gallery:

Output Result:

Similar_pic.jpg is seldom similar to the original.
Google.gif is completely different from the original.
Origin.jpg is the same picture as the original.
Ohter_word.jpg is very similar to the original.

Second, perceptual hashing algorithm (Phash)

The average hashing algorithm is too strict, imprecise, and more suitable for searching thumbnails, in order to obtain more accurate results can choose the perceptual hashing algorithm, it uses DCT (discrete cosine transform) to reduce the frequency of the method

Steps:

1. Zoom Out Image:32 * 32 is a better size so that the DCT calculation is convenient

2. Convert to Grayscale: convert the scaled image to a 256-step grayscale image. (See the average hash algorithm step for the algorithm)

3. Calculate the DCT: A collection of discrete component ratios in DCT images

4. Narrowing the DCT: The DCT is 32*32, leaving the upper left corner of the 8*8, which represent the lowest frequency of the picture

5. Calculate Average: calculates the average of all pixels after narrowing the DCT.

6. Further reduce the DCT: the greater than the average record is 1, whereas the record is 0.

7. Get information fingerprint: combine 64 information bits, order to maintain consistency.

8. Compare fingerprints: calculate the fingerprint of two pictures, calculate Hamming distance (from one fingerprint to another need to change several times), the more the Hamming distance, the more inconsistent the picture, conversely, the smaller the Hamming distance, the more similar picture, when the distance is 0 o'clock, the description is exactly the same. (usually think distance >10 is two completely different pictures)

This algorithm can refer to open source projects phash,:http://www.phash.org/download/

Three, differential hashing algorithm (Dhash)

Compared to the Phash,dhash speed is much faster, compared to ahash,dhash in the efficiency of almost the same situation, the effect is better, it is based on the gradient implementation.

Steps:

1. Shrink the image: shrink to the size of the 9*8, once it has a 72 pixel point

2. Convert to Grayscale: convert the scaled image to a 256-step grayscale image. (See the average hash algorithm step for the algorithm)

3. Calculate the difference:the Dhash algorithm works between neighboring pixels, resulting in 8 different differences between 9 pixels per line, and a total of 8 rows, resulting in 64 difference values

4. Get a fingerprint: if the left pixel is brighter than the right, record 1, otherwise 0.

It should be explained that this fingerprint algorithm can not only be applied to image search, but also for other multimedia forms. In addition, there are many image search feature extraction methods, many algorithms can be improved, for example, the character can be first face recognition, and then in the face area of the local hash, or the background is a solid color can be filtered first and so on, finally in the search results can also be based on color, scenery, products, etc. to filter.

==================================================================================================

Nash_ welcome reprint, sharing with people is the source of progress!

Reprint please keep the original address: http://blog.csdn.net/nash_/article/details/8618775

Three kinds of hashing algorithms for similar image search

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.