Image Processing20(Histogram Comparison )
Goal
In this tutorial you will learn how to:
- Use the function cv::compareHist to get a numerical parameter that express how well two histograms match with each other.使用函数cv::compareHist。得到一个表示两个直方图相似程度的数值。
- Use different metrics to compare histograms。使用不同的指标比较直方图
Theory
- To compare two histograms ( H1 and H2 ), first we have to choose a metric ( d(H1,H2)) to express how well both histograms match.
- 比较直方图,首先需要选择一个指标来表示它们的相似程度。
- OpenCV implements the function cv::compareHist to perform a comparison. It also offers 4 different metrics to compute the matching:
- OpenCV函数提供了函数:cv::compareHist进行直方图的比较。这个函数提供了四种可用的指标。
-
Correlation ( CV_COMP_CORREL )相关性。
d(H1,H2)=∑I(H1(I)−H1¯)(H2(I)−H2¯)∑I(H1(I)−H1¯)2∑I(H2(I)−H2¯)2−−−−−−−−−−−−−−−−−−−−−−−−−−−−√whereHk¯=1N∑JHk(J)平均值and N is the total number of histogram bins.
-
Chi-Square ( CV_COMP_CHISQR )卡方。
d(H1,H2)=∑I(H1(I)−H2(I))2H1(I)
-
Intersection ( method=CV_COMP_INTERSECT )交集。
-
d(H1,H2)=∑Imin(H1(I),H2(I))Bhattacharyya distance ( CV_COMP_BHATTACHARYYA )d(H1,H2)=1−1H1¯H2¯N2−−−−−−−√∑IH1(I)⋅H2(I)−−−−−−−−−−−√−−−−−−−−−−−−−−−−−−−−−−−−−−−−√
-
Correlation ( CV_COMP_CORREL )相关性。
Code
-
What does this program do?
- Loads a base image and 2 test images to be compared with it.(加载一个基本图像,和两个测试图像)
- Generate 1 image that is the lower half of the base image(生成一个图像,是基本图像的下半部分)
- Convert the images to HSV format(转化为HSV格式)
- Calculate the H-S histogram for all the images and normalize them in order to compare them.(计算H-S直方图,并且归一化,然后比较他们)
- Compare the histogram of the base image with respect to the 2 test histograms, the histogram of the lower half base image and with the same base image histogram.(比较基本图像以及另外的两个的测试图像,下半部分的图像以及基本图像进行比较)
- Display the numerical matching parameters obtained.(显示得到的相似程度数据)
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
using namespace std;
using namespace cv;
int main( int argc, char** argv )
{
Mat src_base, hsv_base;
Mat src_test1, hsv_test1;
Mat src_test2, hsv_test2;
Mat hsv_half_down;
// if( argc < 4 )
// {
// printf("** Error. Usage: ./compareHist_Demo <image_settings0> <image_settings1> <image_settings2>\n");
// return -1;
// }
src_base = imread( "lena.jpg", IMREAD_COLOR );
src_test1 = imread( "test1.jpg", IMREAD_COLOR );
src_test2 = imread( "test2.jpg", IMREAD_COLOR );
if(src_base.empty() || src_test1.empty() || src_test2.empty())
{
cout << "Can't read one of the images" << endl;
return -1;
}
cvtColor( src_base, hsv_base, COLOR_BGR2HSV );//HSV
cvtColor( src_test1, hsv_test1, COLOR_BGR2HSV );
cvtColor( src_test2, hsv_test2, COLOR_BGR2HSV );
hsv_half_down = hsv_base( Range( hsv_base.rows/2, hsv_base.rows - 1 ), Range( 0, hsv_base.cols - 1 ) );//行和列
int h_bins = 50; int s_bins = 60;
int histSize[] = { h_bins, s_bins };
// hue varies from 0 to 179, saturation from 0 to 255
float h_ranges[] = { 0, 180 };
float s_ranges[] = { 0, 256 };
const float* ranges[] = { h_ranges, s_ranges };
// Use the o-th and 1-st channels
int channels[] = { 0, 1 };
MatND hist_base;
MatND hist_half_down;
MatND hist_test1;
MatND hist_test2;
calcHist( &hsv_base, 1, channels, Mat(), hist_base, 2, histSize, ranges, true, false );
//输入,输入数据的个数,通道数,掩码,目标输出,直方图的维度,每个维度分割数据多少,每个维度的范围,默认,默认
normalize( hist_base, hist_base, 0, 1, NORM_MINMAX, -1, Mat() );
calcHist( &hsv_half_down, 1, channels, Mat(), hist_half_down, 2, histSize, ranges, true, false );
normalize( hist_half_down, hist_half_down, 0, 1, NORM_MINMAX, -1, Mat() );
calcHist( &hsv_test1, 1, channels, Mat(), hist_test1, 2, histSize, ranges, true, false );
normalize( hist_test1, hist_test1, 0, 1, NORM_MINMAX, -1, Mat() );
calcHist( &hsv_test2, 1, channels, Mat(), hist_test2, 2, histSize, ranges, true, false );
normalize( hist_test2, hist_test2, 0, 1, NORM_MINMAX, -1, Mat() );
imshow("base",src_base);
imshow("test1",src_test1);
imshow("test2",src_test2);
for( int i = 0; i < 4; i++ )
{
int compare_method = i;
double base_base = compareHist( hist_base, hist_base, compare_method );//输入1,输入2,方法
double base_half = compareHist( hist_base, hist_half_down, compare_method );
double base_test1 = compareHist( hist_base, hist_test1, compare_method );
double base_test2 = compareHist( hist_base, hist_test2, compare_method );
printf( " Method [%d] Perfect, Base-Half, Base-Test(1), Base-Test(2) : %f, %f, %f, %f \n", i, base_base, base_half , base_test1, base_test2 );
}
printf( "Done \n" );
// imshow("base",src_base);
// imshow("test1",src_test1);
// imshow("test2",src_test2);
waitKey(0);
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( DisplayImage main.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )
install(TARGETS DisplayImage RUNTIME DESTINATION bin