OpenCV2马拉松第12圈——直方图比较

收入囊中
  • 使用4种不同的方法进行直方图比较

葵花宝典
要比较两个直方图, 首先必须要选择一个衡量直方图相似度对比标准。也就是先说明要在哪个方面做对比。
我们可以想出很多办法,OpenCV采用了以下4种
OpenCV2马拉松第12圈——直方图比较_第1张图片
公式也都不难,我们自己就能实现。
d越小,表示差异越低,两幅图像越接近,越相似

初识API
C++:  double  compareHist (InputArray  H1, InputArray  H2, int  method )
C++:  double  compareHist (const SparseMat&  H1, const SparseMat&  H2, int  method )
 
  • H1 – First compared histogram.
  • H2 – Second compared histogram of the same size as H1 .
  • method –

    Comparison method that could be one of the following:

    • CV_COMP_CORREL Correlation
    • CV_COMP_CHISQR Chi-Square
    • CV_COMP_INTERSECT Intersection
    • CV_COMP_BHATTACHARYYA Bhattacharyya distance
    • CV_COMP_HELLINGER Synonym for CV_COMP_BHATTACHARYYA

荷枪实弹
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;

int main( int, char** argv )
{
	Mat src1,src2,gray1,gray2;
	src1 = imread(argv[1]);
	src2 = imread(argv[2]); 
	cvtColor(src1, gray1, CV_BGR2GRAY);
	cvtColor(src2, gray2, CV_BGR2GRAY);
	
	int histSize = 256;
  	float range[] = { 0, 256 } ;
  	const float* histRange = { range };
 	int channels[] = {0};
 	
 	Mat hist1,hist2;
	calcHist( &gray1, 1, channels, Mat(), hist1, 1, &histSize, &histRange);
	calcHist( &gray2, 1, channels, Mat(), hist2, 1, &histSize, &histRange);

	//相关:CV_COMP_CORREL    
	//卡方:CV_COMP_CHISQR
	//直方图相交:CV_COMP_INTERSECT
	//Bhattacharyya距离:CV_COMP_BHATTACHARYYA
	double diff = compareHist(hist1,hist2,CV_COMP_BHATTACHARYYA);
	cout << diff << endl;
}





举一反三
这篇直方图比较非常简单,没什么要说的了


计算机视觉讨论群:162501053
转载请注明:http://blog.csdn.net/abcd1992719g

你可能感兴趣的:(opencv,直方图比较)