相似图片搜索的原理和实现——颜色分布法

#1,原理

每张图片都可以生成颜色分布的直方图(color histogram)。如果两张图片的直方图很接近,就可以认为它们很相似。

任何一种颜色都是由红绿蓝三原色(RGB)构成的,所以上图共有4张直方图(三原色直方图 + 最后合成的直方图)。

如果每种原色都可以取256个值,那么整个颜色空间共有1600万种颜色(256的三次方)。针对这1600万种颜色比较直方图,计算量实在太大了,因此需要采用简化方法。可以将0~255分成四个区:0~63为第0区,64~127为第1区,128~191为第2区,192~255为第3区。这意味着红绿蓝分别有4个区,总共可以构成64种组合(4的3次方)。

任何一种颜色必然属于这64种组合中的一种,这样就可以统计每一种组合包含的像素数量。

上图是某张图片的颜色分布表,将表中最后一栏提取出来,组成一个64维向量(7414, 230, 0, 0, 8, ..., 109, 0, 0, 3415, 53929)。这个向量就是这张图片的特征值或者叫"指纹"。

于是,寻找相似图片就变成了找出与其最相似的向量。这可以用皮尔逊相关系数或者余弦相似度算出。

#2,c++代码实现

  1 #include<math.h>
  2 #include<bitset>
  3 #include<iostream>
  4 #include<vector>
  5 #include<string>
  6 #include<fstream>
  7 #include <time.h>
  8 #include<opencv2/core/core.hpp>
  9 #include<opencv2/highgui/highgui.hpp>
 10 
 11 using namespace std;
 12 using namespace cv;
 13 
 14 void getRGB(Mat &pic, vector<int > &PixVec);
 15 int bit2int(bitset<2>& bbit, bitset<2>& gbit, bitset<2>& rbit);
 16 bitset<2> classify(int val);
 17 double correlation(vector<int> &Pix1, vector<int> &Pix2);
 18 void getNameFromTxt(vector<string> &OrigNamePic, string FileName, string OrigFileName);
 19 
 20 int main(){
 21   double beginTime = clock();
 22 
 23   string FileName="rawdata";
 24   string oeder1 = "DIR .\" + FileName + "\*.jpg / B >FileNameList.TXT ";
 25   system(oeder1.c_str());
 26 
 27   vector<string> PicName;
 28   getNameFromTxt(PicName, FileName, "FileNameList.TXT");
 29 
 30   int Piclen = PicName.size();
 31   for (int m = 0; m < Piclen; m++){
 32     cout << "Compare the " << m << "-th picture with the others!" << endl;
 33     for (int n = m+1; n < Piclen; n++){
 34       Mat pic1 = imread(PicName[m], 1);
 35       Mat pic2 = imread(PicName[n], 1);
 36 
 37       //PixVec
 38       vector<int> Pix1Vec(64, 0);
 39       getRGB(pic1, Pix1Vec);
 40       vector<int> Pix2Vec(64, 0);
 41       getRGB(pic2, Pix2Vec);
 42 
 43       double correlVal = correlation(Pix1Vec, Pix2Vec);
 44       //cout << "The value of correlation coefficient is: " << correlVal << endl;
 45       if (correlVal > 0.999999){
 46         string movePic = "move .\" + PicName[m]+" DeletePic >nul";
 47         system(movePic.c_str());
 48         break;
 49       }
 50     }    
 51   }
 52 
 53   double endTime = clock();
 54   cout << "beginTime:" << beginTime << endl
 55     << "endTime:" << endTime << endl
 56     << "endTime-beginTime:" << endTime - beginTime << "ms" << endl;
 57 
 58   system("Pause");
 59   return 0;
 60 }
 61 
 62 void getNameFromTxt(vector<string> &OrigNamePic, string fileName, string OrigFileName){
 63   ifstream OrigNameIn(OrigFileName);
 64   while (!OrigNameIn.eof()){
 65     string cacheNameO;
 66     getline(OrigNameIn, cacheNameO);
 67     int len = cacheNameO.size();
 68     if (len>0){
 69       string realName = fileName + "\" + cacheNameO;
 70       OrigNamePic.push_back(realName);
 71     }
 72   }
 73 
 74   OrigNameIn.close();
 75   string order = "del " + OrigFileName;
 76   system(order.c_str());
 77 }
 78 
 79 void getRGB(Mat &pic, vector<int > &PixVec){
 80   int rowNum=pic.rows;
 81   int colNum=pic.cols;
 82   int pixNum=colNum*pic.channels();
 83 
 84   if(pic.channels()!=3)
 85     cout<<"The channel of the picture is not 3!"<<endl;
 86 
 87   Mat_<Vec3b>::iterator it=pic.begin<Vec3b>();
 88   Mat_<Vec3b>::iterator itend=pic.end<Vec3b>();
 89 
 90   for(;it!=itend;++it){
 91     bitset<2> bpix,gpix,rpix;
 92     bpix=classify((*it)[0]);
 93     gpix=classify((*it)[1]);
 94     rpix=classify((*it)[2]);
 95 
 96     int clasVal=bit2int(bpix, gpix, rpix);
 97     PixVec[clasVal]++;
 98   }
 99 
100 }
101 
102 int bit2int(bitset<2>& bbit,bitset<2>& gbit,bitset<2>& rbit){
103   bitset<6> bitval;
104   for(int i=0;i<2;i++){
105     bitval[0*2+i]=rbit[i];
106     bitval[1*2+i]=gbit[i];
107     bitval[2*2+i]=bbit[i];
108   }
109   return bitval.to_ulong();
110 }
111 
112 bitset<2> classify(int val){
113   if (val<64){
114     bitset<2> bitval(0);
115     return bitval;
116   }
117   else if (val<128){
118     bitset<2> bitval(1);
119     return bitval;
120   }
121   else if (val<192){
122     bitset<2> bitval(2);
123     return bitval;
124   }
125   else {//if(val<256)
126     bitset<2> bitval(3);
127     return bitval;
128   }
129 }
130 
131 double correlation(vector<int> &Pix1, vector<int> &Pix2){
132   double XYsum=0.0, Xsum=0.0, Ysum=0.0;
133   double Xmean=0.0, Ymean=0.0;
134 
135   int len=Pix1.size();
136 
137   for(int i=0; i<len; i++){
138     Xmean += Pix1[i];
139     Ymean += Pix2[i];
140   }
141   Xmean =(double)Xmean/(double)len;
142   Ymean =(double)Ymean/(double)len;
143 
144   for(int j=0;j<len;j++){
145     XYsum += ((double)Pix1[j]-Xmean)*((double)Pix2[j]-Ymean);
146     Xsum += ((double)Pix1[j]-Xmean)*((double)Pix1[j]-Xmean);
147     Ysum += ((double)Pix2[j]-Ymean)*((double)Pix2[j]-Ymean);
148   }
149 
150   double finalVal=(double)XYsum/(double)(sqrt(Xsum)*sqrt(Ysum));
151   return finalVal;
152 }
相似图片搜索实现

#3,程序运行结果

原文地址:https://www.cnblogs.com/sophia-hxw/p/5674686.html