Google图像对比基本算法的简单实现

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

#include <cv.h>
#include <highgui.h>
#include <stdlib.h>
  
//计算图像感知hash值。详情看:http://blog.csdn.net/lhfslhfs/article/details/9157845
int64 CalcImagePerceptualHashKey(const IplImage* pImage)
{
    IplImage* pTheImage8X8 = cvCreateImage(cvSize(8, 8), pImage->depth, pImage->nChannels);
    IplImage* pGrayscaleImage = cvCreateImage(cvSize(8, 8), 8, 1);
  
    cvResize(pImage, pTheImage8X8, CV_INTER_AREA);
    cvConvertImage(pTheImage8X8, pGrayscaleImage);
    cvReleaseImage(&pTheImage8X8);
      
    //计算平均值
    float fElementMean = 0;
    for (int y = 0; y < 8; ++y)
    {
        for (int x = 0; x < 8; ++x)
        {
            unsigned char& cElem = *(unsigned char*)(pGrayscaleImage->imageData + x + y * pGrayscaleImage->widthStep);
            cElem = (unsigned char)(cElem / (float)255 * 64);
            fElementMean += cElem;
        }
    }
  
    fElementMean /= 64;
    unsigned char cElementKey = 0;
    int64 key = 0;
    unsigned char* pKeyPtr = (unsigned char*)&key;
  
    for (int y = 0; y < 8; ++y)
    {
        for (int x = 0; x < 8; ++x)
        {
            if (fElementMean > *(unsigned char*)(pGrayscaleImage->imageData + x + y * pGrayscaleImage->widthStep))
            {
                //小于平均值即为0。
                cElementKey <<= 1;
            }
            else
            {
                //否则即为1
                cElementKey <<= 1;
                cElementKey |= 1;
            }
        }
  
        pKeyPtr[y] = cElementKey;
    }
  
    cvReleaseImage(&pGrayscaleImage);
    return key;
}
  
//比较2个key的相似度,结果以1为最相似,0为不相似。
float CompareImageKeySimilarity(int64 key1, int64 key2)
{
    int64 keyResult = key1 ^ key2;
    int nOneCount = 0;
    int i = 64;
    while(i--)
    {
        if ((keyResult & 1) == 1)
            nOneCount++;
  
        keyResult >>= 1;
    }
  
    printf("nOneCount = %dn", nOneCount);
    return nOneCount == 0 ? 1 : (64 - nOneCount) / (float)64;
}
  
int main(int argc, char* argv[])
{
    IplImage* pSrc1 = cvLoadImage("D:\cvImg\gujinlin1.jpg");
    IplImage* pSrc2 = cvLoadImage("D:\cvImg\gujinlin2.jpg");
    int64 key1 = CalcImagePerceptualHashKey(pSrc1);
    int64 key2 = CalcImagePerceptualHashKey(pSrc2);
  
    printf("key1 = 0x%llX, key2 = 0x%llX, Similarity = %.2fn", key1, key2, CompareImageKeySimilarity(key1, key2));
    return 0;
}