发布网友 发布时间:2022-05-01 19:46
共1个回答
热心网友 时间:2022-06-22 04:15
int main(int argc,char* argv[])
{
Mat src1Image,src1HalfImage,src2Image,src3Image;
Mat src1HsvImage,src1HalfHsvImage,src2HsvImage,src3HsvImage;
src1Image = imread("F:\\opencv\\OpenCVImage\\compareHist01.jpg");
src2Image = imread("F:\\opencv\\OpenCVImage\\compareHist02.jpg");
src3Image = imread("F:\\opencv\\OpenCVImage\\compareHist03.jpg");
src1HalfImage = Mat(src1Image, Range(src1Image.rows/2,src1Image.rows-1),Range(0,src1Image.cols-1));
cvtColor(src1Image, src1HsvImage, CV_RGB2HSV);
cvtColor(src2Image, src2HsvImage, CV_RGB2HSV);
cvtColor(src3Image, src3HsvImage, CV_RGB2HSV);
cvtColor(src1HalfImage, src1HalfHsvImage, CV_RGB2HSV);
int hbins = 50,sbins = 60;
int histSize[] = {hbins,sbins};
int channels[] = {0,1};
float hRange[] = {0,256};
float sRange[] = {0,180};
const float* ranges[] = {hRange,sRange};
MatND src1Hist,src2Hist,src3Hist,srcHalfHist;
calcHist(&src1HsvImage, 1, channels, Mat(), src1Hist, 2, histSize, ranges,true,false);
normalize(src1Hist, src1Hist, 0,1, NORM_MINMAX,-1,Mat());
calcHist(&src2HsvImage, 1, channels, Mat(), src2Hist, 2, histSize, ranges,true,false);
normalize(src2Hist, src2Hist, 0,1, NORM_MINMAX,-1,Mat());
calcHist(&src3HsvImage, 1, channels, Mat(), src3Hist, 2, histSize, ranges,true,false);
normalize(src3Hist, src3Hist, 0,1, NORM_MINMAX,-1,Mat());
calcHist(&src1HalfHsvImage, 1, channels, Mat(), srcHalfHist, 2, histSize, ranges,true,false);
normalize(srcHalfHist, srcHalfHist, 0,1, NORM_MINMAX,-1,Mat());
imshow("src1 image", src1Image);
imshow("src2 image", src2Image);
imshow("src3 image", src3Image);
imshow("src1Half image", src1HalfImage);
moveWindow("src1 image", 0, 0);
moveWindow("src2 image", src1Image.cols, 0);
moveWindow("src3 image", src2Image.cols+src1Image.cols, 0);
moveWindow("src1 half image", src1Image.cols+src2Image.cols+src3Image.cols, 0);
for (int i = 0; i < 4; i++) {
int compareMethod = i;
double compareResultA = compareHist(src1Hist, src1Hist, i);
double compareResultB = compareHist(src1Hist, src2Hist, i);
double compareResultC = compareHist(src1Hist, src3Hist, i);
double compareResultD = compareHist(src1Hist, srcHalfHist, i);
printf("直方图比对结果为\r compareResultA = %.3f \ncompareResultB = %.3f \ncompareResultC = %.3f \ncompareResultD = %.3f \n",compareResultA,compareResultB,compareResultC,compareResultD);
printf("比对方法为 %d\n",compareMethod);
}
waitKey(0);
return 0;
}