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
- 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);
- uchar* data2 = matdst2.ptr<uchar> (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 <; 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 =" <<iDiffNum<<endl;
- if (Idiffnum <= 5)
- cout<<"Images is very similar!" <<endl;
- else if (Idiffnum > Ten)
- cout<<"They is different images!" <<endl;
- Else
- cout<<"Both image is somewhat similar!" <<endl;
OPENCV implementation of hash value method for image similarity calculation