Hash value method for image similarity calculation

Source: Internet
Author: User

Perceptual hashing algorithm (perceptual hash algorithm), which is used to generate a "fingerprint" (fingerprint) string for each image, and then compare the fingerprints of different images. The closer the result is, the more similar the image is.

Implementation steps:

1. Reduce the size: Reduce the image to 8*8 size, a total of 64 pixels. This step is to remove the details of the image, only the structure/shading and other basic information, discard the different size/proportion of the image differences;

2. Simplify the color: the reduced image to 64 levels of gray, that is, all the pixels of a total of only 64 colors;

3. Calculate average: Calculates the gray average of all 64 pixels;

4. Compare the grayscale of a pixel: compare the grayscale of each pixel to the average, greater than or equal to the average of 1, and less than the average to 0;

5. Calculate the hash value: Combining the results of the previous step, together, constitutes a 64-bit integer, which is the fingerprint of this image. The order of the combinations is not important, just ensure that all images are in the same order;

6. After getting the fingerprint, you can compare different images and see how many of the 64 bits are not the same. Theoretically, this equates to "Hamming distance" (Hamming distance, in information theory, the Hamming distance between two equal-length strings is the number of different characters in the corresponding position of two strings). If the number of different data bits is not more than 5, it means that two images are very similar, and if they are greater than 10, it means that they are two distinct images.

The above excerpt from: http://www.ruanyifeng.com/blog/2011/07/principle_of_similar_image_search.html


Here is the test code implemented with OPENCV:

String strsrcimagename = "Src.jpg"; Cv::mat matsrc, matSrc1, matsrc2;matsrc = Cv::imread (Strsrcimagename, Cv_load_image_ COLOR); Cv_assert (matsrc.channels () = = 3); Cv::resize (MATSRC, MatSrc1, Cv::size (357, 419), 0, 0, cv::inter_nearest);//cv::flip ( MatSrc1, MATSRC1, 1); Cv::resize (MATSRC, MatSrc2, Cv::size (2177, 3233), 0, 0, cv::inter_lanczos4); Cv::mat MatDst1, Matdst2;cv::resize (MatSrc1, MatDst1, Cv::size (8, 8), 0, 0, cv::inter_cubic); Cv::resize (MatSrc2, MatDst2, Cv::size (8, 8) , 0, 0, cv::inter_cubic); Cv::cvtcolor (MatDst1, MatDst1, Cv_bgr2gray); Cv::cvtcolor (MatDst2, MatDst2, cv_bgr2gray); int IAVG1 = 0, iAvg2 = 0;int arr1[64], arr2[64];for (int i = 0; i < 8; i++) {uchar* data1 = matdst1.ptr<uchar> (i); uch ar* data2 = matdst2.ptr<uchar> (i); int tmp = i * 8;for (int j = 0; J < 8; J + +) {int TMP1 = tmp + J;ARR1[TMP1] = da TA1[J]/4 * 4;ARR2[TMP1] = data2[j]/4 * 4;IAVG1 + = arr1[tmp1];iavg2 + ARR2[TMP1];}} IAVG1/= 64;iavg2/= 64;for (int i = 0; i <; i++) {Arr1[i] = (Arr1[i] >= iAvg1)? 1:0;arr2[i] = (Arr2[i] >= iAvg2)? 1:0;} int idiffnum = 0;for (int i = 0; i <; i++) if (arr1[i]! = Arr2[i]) ++idiffnum;cout<< "idiffnum =" <<idiffnu M<<endl;if (Idiffnum <= 5) cout<< "The images is very similar!" <<endl;else if (Idiffnum >) cout<< "They is" different images! " <<endl;elsecout<< "Both image is somewhat similar!" <<endl;


Hash value method for image similarity calculation

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.