直方图对比,显示两张图片的相似度
函数:compareHist();
c++: double compareHist(InputArray H1,InputArray H2,int method);
method:
1.相关, Correlation (method=CV_COMP_CORREL) 方法1越趋近于1的时候基本图与样例图的相似度越近;
2.卡方, Chi-Square (method=CV_COMP_CHISQR) 方2法越趋近于0的时候基本图与样例图的相似度越近;
3.直方相交, Intersection (method=CV_COMP_INTERSETC) 方法3越趋近于58.69的时候基本图与样例图的相似度越近;
4.距离, Bhattacharyya (method=CV_COMP_BHATTACHARYYA) 方法4越趋近于0的时候基本图与样例图的相似度越近;
实例程序代码:
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/videoio/videoio.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace std;
using namespace cv;
int main(int argc, char const *argv[])
{
Mat frame,hsv,srcImage_base,hsv_base;
VideoCapture video;
video.open(0);
while(1)
{
srcImage_base=imread("base.jpg",1);
video.read(frame);
cvtColor(frame,hsv,COLOR_BGR2HSV);
cvtColor(srcImage_base,hsv_base,COLOR_BGR2HSV);
int h_bins=50;
int s_bins=60;
int histsize[]={h_bins,s_bins};
float h_ranges[]={0, 256};
float s_ranges[]={0, 180};
const float* ranges[]={h_ranges,s_ranges};
int channels[]={0,1};
MatND frame_hist;
MatND srcImage_base_hist;
calcHist( &hsv_base, 1, channels, Mat(), srcImage_base_hist, 2, histSize, ranges, true,false );
normalize( srcImage_base_hist, srcImage_base_hist, 0, 1, NORM_MINMAX, -1, Mat() );
calcHist( &hsv, 1, channels, Mat(), frame_hist, 2, histSize, ranges, true,false );
normalize( frame_hist, frame_hist, 0, 1, NORM_MINMAX, -1, Mat() );
double base_frame=compareHist( srcImage_base_hist,frame_hist,0);
count<<"base_frame="<<base_frame<<endl;
waitKey(0);
}
return 0;
}
注释:其实base.jpg是基础图片,本例程采用的在实时视频流中检测: