1直方图计算样例如下:
vector<Mat> getimhist(Mat im)
{
vector<Mat> ims;
split(im, ims);
const int binnum[1] = {256};
float valuerange[2] = { 0,255 };
const float* hrange[1] = { valuerange };
vector<Mat> imhist;
int chnum=ims.size();
for(int i = 0; i <chnum; i++)
{
Mat hist;
calcHist(&ims[i],1,0, Mat(), hist,1,binnum,hrange);
normalize(hist, hist, 1,0, NORM_L2, -1);
imhist.push_back(hist);
}
return imhist;
}
这个函数既可以计算灰度图的也可以计算彩色图.
2直方图比较如下:
Mat vectmat2mat(vector<Mat> ims)
{
Mat bigim=ims[0];
for (int i = 1; i <ims.size(); i++)
{
bigim.push_back(ims[i]);
}
return bigim;
}
float get2imhistsim(Mat im1,Mat im2)
{
vector<Mat> imhist1=getimhist(im1);
vector<Mat> imhist2=getimhist(im2);
Mat hist1=vectmat2mat(imhist1);
Mat hist2=vectmat2mat(imhist2);
float histsim = compareHist(hist1, hist2, 0);
return histsim;
}