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

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
#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;
}

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇: jquery实现的时间轴

下一篇: JS 实现时间倒计时