#include"stdafx.h"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include<opencv2/photo/photo.hpp>
#include <string>
#include <vector>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
system("color 2F");
Mat srcImage_base, hsvImage_base;
Mat srcImage_test1, hsvImage_test1;
Mat srcImage_test2, hsvImage_test2;
Mat hsvImage_halfDown;
//读取基准图像和两张测试图象
srcImage_base = imread("E:\\pictures\\For_Project\\1.jpg", 1);
srcImage_test1 = imread("E:\\pictures\\For_Project\\2.jpg", 1);
srcImage_test2 = imread("E:\\pictures\\For_Project\\3.jpg", 1);
imshow("基准图像", srcImage_base);
imshow("测试图像1", srcImage_test1);
imshow("测试图像2", srcImage_test2);
//将图像由BGR转换为HSV
cvtColor(srcImage_base, hsvImage_base, COLOR_BGR2HSV);
cvtColor(srcImage_test1, hsvImage_test1, COLOR_BGR2HSV);
cvtColor(srcImage_test2, hsvImage_test2, COLOR_BGR2HSV);
//创建包含基准图像下半部分的半身图像HSV格式
hsvImage_halfDown = hsvImage_base(Range(hsvImage_base.rows / 2,
hsvImage_base.rows / 2), Range(0, hsvImage_base.cols - 1));
int h_bins = 50;
int s_bins = 60;
int histSize[] = { h_bins,s_bins };
float h_ranges[] = { 0,256 };
float s_ranges[] = { 0,256 };
const float*ranges[] = { h_ranges,s_ranges };
int channels[] = { 0,1 };
MatND baseHist;
MatND halfDownHist;
MatND testHist1;
MatND testHist2;
//计算基准图像,两张测试图像,半身基准图像的HSV直方图,并归一化
calcHist(&hsvImage_base, 1, channels, Mat(), baseHist, 2, histSize, ranges, true, false);
normalize(baseHist, baseHist, 0, 1, NORM_MINMAX, -1, Mat());
calcHist(&hsvImage_halfDown, 1, channels, Mat(), halfDownHist, 2, histSize, ranges, true, false);
normalize(halfDownHist, halfDownHist, 0, 1, NORM_MINMAX, -1, Mat());
calcHist(&hsvImage_test1, 1, channels, Mat(), testHist1, 2, histSize, ranges, true, false);
normalize(testHist1, testHist1, 0, 1, NORM_MINMAX, -1, Mat());
calcHist(&hsvImage_test2, 1, channels, Mat(), testHist2, 2, histSize, ranges, true, false);
normalize(testHist2, testHist2, 0, 1, NORM_MINMAX, -1, Mat());
//按顺序依次比较,其中method分别为:CV_COMP_CORREL,CV_COMP_CHISQR,CV_COMP_INTERSECT,CV_COMP_BHATTACHARYYA
for (int i = 0;i < 4;i++) {
int compare_method = i;
double base_base = compareHist(baseHist, baseHist, compare_method);
double base_half = compareHist(baseHist, halfDownHist, compare_method);
double base_test1 = compareHist(baseHist, testHist1, compare_method);
double base_test2 = compareHist(baseHist, testHist2, compare_method);
cout << "方法" << i << "的匹配结果为:" << "【基准图-基准图】" << base_base <<
",【基准图-半身图】:" << base_half << ",【基准图-测试图1】" << base_test1 <<
",【基准图-测试图2】" << base_test2 << endl;
}
while((char)waitKey(0)!='q'){}
return 0;
}
opencv学习之例子直方图对比
最新推荐文章于 2024-05-28 18:11:54 发布