此方法比较准确
还行
此方法不怎么行
此方法比较准确
#include<opencv2\highgui\highgui.hpp>
#include<opencv2\opencv.hpp>
#include<iostream>
#include<math.h>
using namespace std;
using namespace cv;
string convertToString(double d);
int main()
{Mat base, test1, test2;Mat hsvbase, hsvtest1, hsvtest2;base = imread("E:\\vs2015\\opencvstudy\\24police2.png");if (base.empty()){cout << "could not load the src image!" << endl;return -1;}test1 = imread("E:\\vs2015\\opencvstudy\\24police3.png");test2 = imread("E:\\vs2015\\opencvstudy\\24police4.png");cvtColor(base, hsvbase, CV_BGR2HSV);cvtColor(test1, hsvtest1, CV_BGR2HSV);cvtColor(test2, hsvtest2, CV_BGR2HSV);int h_bins = 50;int s_bins = 50;int histSize[] = { h_bins,s_bins };//hue varies from 0 to 179,saturation from 0 to 255float h_ranges[] = { 0,180 };float s_ranges[] = { 0,256 };const float* range[] = { h_ranges,s_ranges };//Use the 0-th or 1-st channelsint channels[] = { 0,1 };MatND hist_base; //MatND表示多通道MatND hist_test1;MatND hist_test2;calcHist(&hsvbase,1, channels, Mat(), hist_base,2, histSize, range, true, false);normalize(hist_base, hist_base, 0, 1, NORM_MINMAX, -1, Mat()); //Mat() 代表mask为空calcHist(&hsvtest1, 1, channels, Mat(), hist_test1, 2, histSize, range, true, false);normalize(hist_test1, hist_test1, 0, 1, NORM_MINMAX, -1, Mat()); //Mat() 代表mask为空calcHist(&hsvtest2, 1, channels, Mat(), hist_test2, 2, histSize, range, true, false);normalize(hist_test2, hist_test2, 0, 1, NORM_MINMAX, -1, Mat()); //Mat() 代表mask为空double basebase = compareHist(hist_base, hist_base, CV_COMP_CORREL);double basetest1 = compareHist(hist_base, hist_test1, CV_COMP_CORREL);double basetest2 = compareHist(hist_base, hist_test1, CV_COMP_CORREL);double test1test2 = compareHist(hist_test1, hist_test2, CV_COMP_CORREL);Mat test12;test2.copyTo(test12);putText(base, convertToString(basebase), Point(50, 50), CV_FONT_HERSHEY_COMPLEX, 1, Scalar(0, 0, 255), 2, LINE_AA);putText(test1, convertToString(basetest1), Point(50, 50), CV_FONT_HERSHEY_COMPLEX, 1, Scalar(0, 0, 255), 2, LINE_AA);putText(test2, convertToString(basetest2), Point(50, 50), CV_FONT_HERSHEY_COMPLEX, 1, Scalar(0, 0, 255), 2, LINE_AA);putText(test12, convertToString(basebase), Point(50, 50), CV_FONT_HERSHEY_COMPLEX, 1, Scalar(0, 0, 255), 2, LINE_AA);imshow("base", base);imshow("test1", test1);imshow("test2", test2);imshow("test12", test12);waitKey();return 0;
}string convertToString(double d)
{ostringstream os;if (os << d){return os.str();}return "invalid conversion";
}