OPENCV implementation of hash value method for image similarity calculation

Source: Internet
Author: User

http://blog.csdn.net/fengbingchun/article/details/42153261

OPENCV Implementation of hash value method for image similarity calculation2014-12-25 21:27 2959 People read comments (0) favorite reports Classification:OpenCV (a)Image Processing (+)

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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:

[CPP]View PlainCopy 
  1. String strsrcimagename = "Src.jpg";
  2. Cv::mat matsrc, MatSrc1, MATSRC2;
  3. MATSRC = Cv::imread (Strsrcimagename, Cv_load_image_color);
  4. Cv_assert (matsrc.channels () = = 3);
  5. Cv::resize (MATSRC, MatSrc1, Cv::size (357, 419), 0, 0, cv::inter_nearest);
  6. Cv::flip (MatSrc1, MATSRC1, 1);
  7. Cv::resize (MATSRC, MatSrc2, Cv::size (2177, 3233), 0, 0, CV::INTER_LANCZOS4);
  8. Cv::mat MatDst1, MatDst2;
  9. Cv::resize (MatSrc1, MatDst1, Cv::size (8, 8), 0, 0, cv::inter_cubic);
  10. Cv::resize (MatSrc2, MatDst2, Cv::size (8, 8), 0, 0, cv::inter_cubic);
  11. Cv::cvtcolor (MatDst1, MatDst1, Cv_bgr2gray);
  12. Cv::cvtcolor (MatDst2, MatDst2, Cv_bgr2gray);
  13. int iAvg1 = 0, iAvg2 = 0;
  14. int arr1[64], arr2[64];
  15. for (int i = 0; i < 8; i++) {
  16. uchar* data1 = matdst1.ptr<uchar> (i);
  17. uchar* data2 = matdst2.ptr<uchar> (i);
  18. int tmp = i * 8;
  19. For (int j = 0; J < 8; J + +) {
  20. int TMP1 = tmp + j;
  21. ARR1[TMP1] = data1[j]/4 * 4;
  22. ARR2[TMP1] = data2[j]/4 * 4;
  23. IAVG1 + = Arr1[tmp1];
  24. IAVG2 + = Arr2[tmp1];
  25. }
  26. }
  27. IAVG1/= 64;
  28. IAVG2/= 64;
  29. for (int i = 0; i <; i++) {
  30. Arr1[i] = (Arr1[i] >= iAvg1)? 1:0;
  31. Arr2[i] = (Arr2[i] >= iAvg2)? 1:0;
  32. }
  33. int idiffnum = 0;
  34. for (int i = 0; i <; i++)
  35. if (arr1[i]! = Arr2[i])
  36. ++idiffnum;
  37. cout<<"Idiffnum =" <<iDiffNum<<endl;
  38. if (Idiffnum <= 5)
  39. cout<<"Images is very similar!"  <<endl;
  40. else if (Idiffnum > Ten)
  41. cout<<"They is different images!"  <<endl;
  42. Else
  43. cout<<"Both image is somewhat similar!"  <<endl;

OPENCV implementation of 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.