• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

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

C# 水墨上仙 1315次浏览

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

#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*)
 
    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;
}


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明Google图像对比基本算法的简单实现
喜欢 (0)
加载中……