入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

相似图片搜索原理二(phash—c++实现)

创建时间:2015-09-23 投稿人: 浏览次数:836

前段时间介绍过相似图片搜索原理一(ahash) http://blog.csdn.net/lu597203933/article/details/45101859,它是基于内容检索最简单的一种;这里介绍它的增强版本感知哈希算法(perceptual hash, phash)。它主要也是用缩略图搜原图并能达到较好点的效果.

理论部分:

理论部分主要包括以下几个步骤:

<1> 图像缩放—将图像缩放到32*32大小

<2>灰度化—对32*32大小的图像进行灰度化

<3>离散余弦变换(DCT)—对32*32大小图像进行DCT

<4>计算均值—用32*32大小图片前面8*8大小图片处理并计算这64个像素的均值

<4>得到8*8图像的phash—8*8的像素值中大于均值的则用1表示,小于的用0表示,这样就得到一个64位二进制码作为该图像的phash值。

<5>计算两幅图像ahash值的汉明距离,距离越小,表明两幅图像越相似;距离越大,表明两幅图像距离越大。

这样做能够避免伽马校正或者颜色直方图调整带来的影响。

更详细的理论可以参看:

1:http://www.hackerfactor.com/blog/index.php?/archives/432-Looks-Like-It.html

2:http://blog.csdn.net/luoweifu/article/details/8220992包括java代码实现

 

下面我给出自己的c++代码实现:

<1>图像灰度化与缩放

      

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1.        Mat img = imread("E:\algorithmZack\ImageSearch\image\person.jpg", 1);  
  2. if(!img.data){  
  3.     cout << "the image is not exist" << endl;  
  4.     return 0;  
  5. }  
  6. int size = 32;  // 图片缩放后大小  
  7.   
  8. resize(img, img, Size(size,size));      // 缩放到32*32  
  9. cvtColor(img, img, COLOR_BGR2GRAY);       // 灰度化  

<2>DCT变换

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. /* 
  2.     功能:获取DCT系数 
  3.     n:矩阵大小 
  4.     quotient: 系数 
  5.     quotientT: 系数转置 
  6. */  
  7. void coefficient(const int &n, double **quotient, double **quotientT){  
  8.     double sqr = 1.0/sqrt(n+0.0);  
  9.     for(int i = 0; i < n; i++){  
  10.         quotient[0][i] = sqr;  
  11.         quotientT[i][0] =  sqr;  
  12.     }  
  13.   
  14.     for(int i = 1; i < n; i++){  
  15.         for(int j = 0; j < n; j++){  
  16.             quotient[i][j] = sqrt(2.0/n)*cos(i*(j+0.5)*PI/n);  // 由公式得到  
  17.             quotientT[j][i] = quotient[i][j];  
  18.         }  
  19.     }  
  20.   
  21. }  
  22. /* 
  23.     功能:两矩阵相乘 
  24.     A和B:源输入矩阵 
  25.     result:输出矩阵 
  26. */  
  27. void matrixMultiply(double **A, double **B, int n, double **result){    
  28.     double t = 0;  
  29.     for(int i = 0; i < n; i++){  
  30.         for(int j = 0; j < n; j++){  
  31.             t = 0;  
  32.             for(int k = 0; k < n; k++)  
  33.                 t += A[i][k]*B[k][j];     
  34.             result[i][j] = t;  
  35.         }  
  36.     }  
  37. }  
  38.   
  39.   
  40. void DCT(Mat_<uchar> image, const int &n, double **iMatrix){  
  41.     for(int i = 0; i < n; i++){  
  42.         for(int j = 0; j < n; j++){  
  43.             iMatrix[i][j] = (double)image(i,j);  
  44.         }  
  45.     }  
  46.   
  47.     // 为系数分配空间  
  48.     double **quotient = new double*[n];  
  49.     double **quotientT = new double*[n];  
  50.     double **tmp = new double*[n];  
  51.     for(int i = 0; i < n; i++){  
  52.         quotient[i] = new double[n];  
  53.         quotientT[i] = new double[n];   
  54.         tmp[i] = new double[n];  
  55.     }  
  56.     // 计算系数矩阵  
  57.     coefficient(n, quotient, quotientT);  
  58.     matrixMultiply(quotient, iMatrix, n, tmp);  // 由公式成绩结果  
  59.     matrixMultiply(tmp, quotientT, n, iMatrix);  
  60.   
  61.     for(int i = 0; i < n; i++){  
  62.         delete []tmp[i];  
  63.         delete []quotient[i];  
  64.         delete []quotientT[i];  
  65.     }  
  66.     delete []tmp;  
  67.     delete []quotient;  
  68.     delete []quotientT;  
  69. }  

<3>计算均值

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. // 计算8*8图像的平均灰度  
  2. float calcAverage(double **iMatrix, const int &size){  
  3.     float sum = 0;  
  4.     for(int i = 0 ; i < size; i++){  
  5.         for(int j = 0; j < size; j++){  
  6.             sum += iMatrix[i][j];  
  7.         }  
  8.     }  
  9.     return sum/(size*size);  
  10. }  

<4>计算汉明距离

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. /* 计算hash值 
  2.     image:8*8的灰度图像 
  3.     size: 图像大小  8*8 
  4.     phash:存放64位hash值 
  5.     averagePix: 灰度值的平均值 
  6. */  
  7. void fingerPrint(double **iMatrix, const int &size, bitset<hashLength> &phash, const float &averagePix){  
  8.     for(int i = 0; i < size; i++){  
  9.         int pos = i * size;  
  10.         for(int j = 0; j < size; j++){  
  11.             phash[pos+j] = iMatrix[i][j] >= averagePix ? 1:0;  
  12.         }  
  13.     }  
  14. }  


完整源代码:

 

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. #include <iostream>  
  2. #include <bitset>  
  3. #include <string>  
  4. #include <iomanip>  
  5. #include <cmath>  
  6. #include <opencv2highguihighgui.hpp>  
  7. #include <opencv2imgprocimgproc.hpp>  
  8. #include <opencv2corecore.hpp>  
  9.   
  10. using namespace std;  
  11. using namespace cv;  
  12.   
  13. #define PI 3.1415926  
  14. #define hashLength 64  
  15.   
  16. /* 
  17.     功能:获取DCT系数 
  18.     n:矩阵大小 
  19.     quotient: 系数 
  20.     quotientT: 系数转置 
  21. */  
  22. void coefficient(const int &n, double **quotient, double **quotientT){  
  23.     double sqr = 1.0/sqrt(n+0.0);  
  24.     for(int i = 0; i < n; i++){  
  25.         quotient[0][i] = sqr;  
  26.         quotientT[i][0] =  sqr;  
  27.     }  
  28.   
  29.     for(int i = 1; i < n; i++){  
  30.         for(int j = 0; j < n; j++){  
  31.             quotient[i][j] = sqrt(2.0/n)*cos(i*(j+0.5)*PI/n);  // 由公式得到  
  32.             quotientT[j][i] = quotient[i][j];  
  33.         }  
  34.     }  
  35.   
  36. }  
  37. /* 
  38.     功能:两矩阵相乘 
  39.     A和B:源输入矩阵 
  40.     result:输出矩阵 
  41. */  
  42. void matrixMultiply(double **A, double **B, int n, double **result){    
  43.     double t = 0;  
  44.     for(int i = 0; i < n; i++){  
  45.         for(int j = 0; j < n; j++){  
  46.             t = 0;  
  47.             for(int k = 0; k < n; k++)  
  48.                 t += A[i][k]*B[k][j];     
  49.             result[i][j] = t;  
  50.         }  
  51.     }  
  52. }  
  53.   
  54.   
  55. void DCT(Mat_<uchar> image, const int &n, double **iMatrix){  
  56.     for(int i = 0; i < n; i++){  
  57.         for(int j = 0; j < n; j++){  
  58.             iMatrix[i][j] = (double)image(i,j);  
  59.         }  
  60.     }  
  61.  &n
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
  • 上一篇:没有了
  • 下一篇:没有了
未上传头像