灰色直方图图像匹配
#include "opencv2/highgui/highgui.hpp" #include "opencv/cv.hpp" #include "string.h" //画直方图用 int HistogramBins = 256; float HistogramRange1[2] = { 0, 255 }; float *HistogramRange[1] = { &HistogramRange1[0] }; /* * imagefile1: * imagefile2: * method: could be CV_COMP_CHISQR, CV_COMP_BHATTACHARYYA, CV_COMP_CORREL, CV_COMP_INTERSECT */ int CompareHist(const char* imagefile1, const char* imagefile2) { IplImage *image1 = cvLoadImage(imagefile1, 0); IplImage *image2 = cvLoadImage(imagefile2, 0); CvHistogram *Histogram1 = cvCreateHist(1, &HistogramBins, CV_HIST_ARRAY, HistogramRange); CvHistogram *Histogram2 = cvCreateHist(1, &HistogramBins, CV_HIST_ARRAY, HistogramRange); cvCalcHist(&image1, Histogram1); cvCalcHist(&image2, Histogram2); cvNormalizeHist(Histogram1, 1); cvNormalizeHist(Histogram2, 1); // CV_COMP_CHISQR,CV_COMP_BHATTACHARYYA这两种都可以用来做直方图的比较,值越小,说明图形越相似 printf("CV_COMP_CHISQR : %.4f\n", cvCompareHist(Histogram1, Histogram2, CV_COMP_CHISQR)); printf("CV_COMP_BHATTACHARYYA : %.4f\n", cvCompareHist(Histogram1, Histogram2, CV_COMP_BHATTACHARYYA)); // CV_COMP_CORREL, CV_COMP_INTERSECT这两种直方图的比较,值越大,说明图形越相似 printf("CV_COMP_CORREL : %.4f\n", cvCompareHist(Histogram1, Histogram2, CV_COMP_CORREL)); printf("CV_COMP_INTERSECT : %.4f\n", cvCompareHist(Histogram1, Histogram2, CV_COMP_INTERSECT)); cvReleaseImage(&image1); cvReleaseImage(&image2); cvReleaseHist(&Histogram1); cvReleaseHist(&Histogram2); return 0; } int main(int argc, char* argv[]) { char path1[111]; char path2[111]; strcpy(path1, "C:\\C_C++ code\\Photo and video\\mulpsq\\psq0032.jpg"); strcpy(path2, "C:\\C_C++ code\\Photo and video\\mulpsq\\psq0035.jpg"); CompareHist(path1,path2); //CompareHist("d:\\camera.jpg", "d:\\camera1.jpg"); return 0; }