函数的使用很简单,直接把 OpenCV 的官方文档截取过来吧,主要是。。。懒了。。。
#include
#include
#include
#include
using namespace cv;
using namespace std;
char file[] = "1.jpg";
string convertToString(double d);
int main(int argc, char** argv)
{
Mat img1 = imread(file, -1);
pyrDown(img1, img1, Size(img1.cols/2, img1.rows/2));
imshow("1",img1);
Mat img20 = imread("img.jpg", -1);
imshow("2", img20);
//转到HSV
cvtColor(img1, img1, CV_BGR2HSV);
cvtColor(img20, img20, CV_BGR2HSV);
Mat img2;
img20.copyTo(img2);
//
int h_bins = 50;int s_bins = 60;
int histSize[] = {h_bins, s_bins};
float h_ranges[] = {0, 180};
float s_ranges[] = {0, 256};
const float* ranges[] = {h_ranges, s_ranges};
int channels[] = {0, 1};
MatND hist_base, hist_test1, hist_test2;
calcHist(&img1, 1, channels, Mat(), hist_base, 2, histSize, ranges, true, false);
normalize(hist_base, hist_base, 0, 1, NORM_MINMAX, -1, Mat());
calcHist(&img2, 1, channels, Mat(), hist_test1, 2, histSize, ranges, true, false);
normalize(hist_test1, hist_test1, 0, 1, NORM_MINMAX, -1, Mat());
double basetest1 = compareHist(hist_base, hist_base, CV_COMP_CORREL);
double basetest2 = compareHist(hist_base, hist_test1, CV_COMP_CORREL);//相关性比较
double basetest3 = compareHist(hist_base, hist_test1, CV_COMP_CHISQR);//卡方比较
double basetest4 = compareHist(hist_base, hist_test1, CV_COMP_INTERSECT);//十字交叉性
double basetest5 = compareHist(hist_base, hist_test1, CV_COMP_BHATTACHARYYA);//巴氏距离
putText(img1, convertToString(basetest1), Point(50, 90), CV_FONT_HERSHEY_COMPLEX, 1, Scalar(0,0,0), 2 );
putText(img1, "Origional CORREL", Point(50, 40), CV_FONT_HERSHEY_COMPLEX, 1, Scalar(0,0,0), 2 );
imshow("3", img1);
imwrite("13.jpg", img1);
img20.copyTo(img2);
putText(img2, convertToString(basetest2), Point(50, 90), CV_FONT_HERSHEY_COMPLEX, 1, Scalar(0,0,0), 2 );
putText(img2, "CORREL", Point(50, 40), CV_FONT_HERSHEY_COMPLEX, 1, Scalar(0,0,0), 2 );
imshow("相关性比较", img2);
imwrite("14.jpg", img2);
img20.copyTo(img2);
putText(img2, convertToString(basetest3), Point(50, 90), CV_FONT_HERSHEY_COMPLEX, 1, Scalar(0,0,0), 2 );
putText(img2, "CHISQR", Point(50, 40), CV_FONT_HERSHEY_COMPLEX, 1, Scalar(0,0,0), 2 );
imshow("卡方比较", img2);
imwrite("15.jpg", img2);
img20.copyTo(img2);
putText(img2, convertToString(basetest4), Point(50, 90), CV_FONT_HERSHEY_COMPLEX, 1, Scalar(0,0,0), 2 );
putText(img2, "INTERSECT", Point(50, 40), CV_FONT_HERSHEY_COMPLEX, 1, Scalar(0,0,0), 2 );
imshow("十字交叉", img2);
imwrite("16.jpg", img2);
img20.copyTo(img2);
putText(img2, convertToString(basetest5), Point(50, 90), CV_FONT_HERSHEY_COMPLEX, 1, Scalar(0,0,0), 2 );
putText(img2, "BHATTACHARYYA", Point(50, 40), CV_FONT_HERSHEY_COMPLEX, 1, Scalar(0,0,0), 2 );
imshow("巴氏距离", img2);
imwrite("17.jpg", img2);
waitKey();
return 1;
}
string convertToString(double d)
{
ostringstream os;
if (os << d)
{
return os.str();
}
return "invalid conversion";
}