Hash Value Calculation for image similarity

Source: Internet
Author: User

Hash Value Calculation for image similarity

 

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

Steps:

1. Zoom out: scale down the image to 8x8, with a total of 64 pixels. This step is used to remove image details, retain only basic information such as structure and brightness, and discard the image differences caused by different sizes and proportions;

2. Simplified color: Convert the reduced image to a 64-level gray scale. That is, all pixels have only 64 colors;

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

4. Compare the gray scale of pixels: Compare the gray scale of each pixel with the average value. If the gray scale is greater than or equal to the average value, it is recorded as 1. If the gray scale is less than the average value, it is recorded as 0;

5. Calculate the hash value: Combine the comparison results in the previous step to form a 64-bit integer, which is the fingerprint of the image. The order of the combination is not important, as long as all images are in the same order;

6. After obtaining the fingerprint, you can compare different images to see how many digits are different in the 64-bit format. In theory, this is equivalent to "Hamming distance" (Hamming distance. In information theory, the Hamming distance between two equal-length strings is the number of different characters at the corresponding positions of two strings ). If the number of different data digits does not exceed 5, the two images are very similar. If the number is greater than 10, the two images are different.

 

 

 

The following 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
 
  (i);uchar* data2 = matDst2.ptr
  
   (i);int tmp = i * 8;for (int j = 0; j < 8; j++) {int tmp1 = tmp + j;arr1[tmp1] = data1[j] / 4 * 4;arr2[tmp1] = data2[j] / 4 * 4;iAvg1 += arr1[tmp1];iAvg2 += arr2[tmp1];}}iAvg1 /= 64;iAvg2 /= 64;for (int i = 0; i < 64; i++) {arr1[i] = (arr1[i] >= iAvg1) ? 1 : 0;arr2[i] = (arr2[i] >= iAvg2) ? 1 : 0;}int iDiffNum = 0;for (int i = 0; i < 64; i++)if (arr1[i] != arr2[i])++iDiffNum;cout<
   
     10)cout<
    
     

 

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.