阅读(4058) (10)

OpenCV直方图比较

2017-09-20 09:56:53 更新

目标

在本教程中,您将学习如何:

  • 使用函数cv :: compareHist获取一个数值参数,表示两个直方图相互匹配的程度。
  • 使用不同的指标来比较直方图

理论

  1. 为了比较两个直方图( H1 和 H2 ),首先我们必须选择度量( d(H1,H2)来表示两个直方图的匹配度。
  2. OpenCV实现函数cv :: compareHist进行比较。它还提供4种不同的指标来计算匹配:
  • 相关性(CV_COMP_CORREL)

OpenCV直方图比较

where

OpenCV直方图比较

N是直方图库的总数。

  • Chi-Square(CV_COMP_CHISQR)

OpenCV直方图比较

  • 交点(method= CV_COMP_INTERSECT)

OpenCV直方图比较

  • Bhattacharyya distance ( CV_COMP_BHATTACHARYYA )

OpenCV直方图比较

Code

  • 这个程序是做什么的?加载基础图像和2个测试图像进行比较。生成基本图像的下半部分的1个图像将图像转换为HSV格式计算所有图像的HS直方图,并对它们进行归一化,以便进行比较。比较基本图像的直方图与2个测试直方图,下半部基本图像的直方图以及相同的基本图像直方图。显示获得的数值匹配参数。
  • 可下载的代码:点击这里
  • 代码一览:
#include "opencv2/imgcodecs.hpp"
#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( argv[1], IMREAD_COLOR );
    src_test1 = imread( argv[2], IMREAD_COLOR );
    src_test2 = imread( argv[3], 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 );
    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() );
    for( int i = 0; i < 4; i++ )
    {
        int compare_method = i;
        double base_base = compareHist( hist_base, hist_base, compare_method );
        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" );
    return 0;
}

说明

  • 声明诸如矩阵的变量来存储基本图像和另外两个图像进行比较(BGR和HSV)
Mat src_base,hsv_base;
Mat src_test1,hsv_test1;
Mat src_test2,hsv_test2;
Mat hsv_half_down;
  • 加载基本图像(src_base)和其他两个测试图像:
if( argc < 4 )
  { printf("** Error. Usage: ./compareHist_Demo <image_settings0> <image_setting1> <image_settings2>n");
    return -1;
  }
src_base = imread( argv[1], 1 );
src_test1 = imread( argv[2], 1 );
src_test2 = imread( argv[3], 1 );
  • 将其转换为HSV格式:
cvtColor(src_base,hsv_base,COLOR_BGR2HSV);
cvtColor(src_test1,hsv_test1,COLOR_BGR2HSV);
cvtColor(src_test2,hsv_test2,COLOR_BGR2HSV);
  • 另外,创建一半的基本图像(HSV格式):
hsv_half_down = hsv_base(Range(hsv_base.rows / 2,hsv_base.rows  -  1),Range(0,hsv_base.cols  -  1));
  • 初始化参数以计算直方图(bins, ranges and channels H and S ).
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对象以存储直方图:
MatND hist_base;
MatND hist_half_down;
MatND hist_test1;
MatND hist_test2;
  • 计算基本图像的直方图,2个测试图像和半基准图像:
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() );
  • 依次应用基本图像(hist_base)和其他直方图的直方图之间的4种比较方法:
for( int i = 0; i < 4; i++ )
   { int compare_method = i;
     double base_base = compareHist( hist_base, hist_base, compare_method );
     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 );
  }

结果

  • 我们使用以下图像作为输入:

OpenCV直方图比较

Base_0

OpenCV直方图比较

Tset_1

OpenCV直方图比较

Test_2

其中第一个是基础(要与其他人进行比较),另外2个是测试图像。我们还将比较第一幅图像与其本身和一半的基本图像。

  • 当我们比较基本图像直方图与本身时,我们应该期待一个完美的匹配。此外,与基本图像的一半的直方图相比,它应该呈现高匹配,因为它们都来自相同的源。对于其他两个测试图像,我们可以观察到它们具有非常不同的照明条件,因此匹配不应该很好:
  • 这里的数值结果:
*Method*Base - BaseBase - HalfBase - Test 1Base - Test 2
*Correlation*1.0000000.9307660.1820730.120447
*Chi-square*0.0000004.94046621.18453649.273437
*Intersection*24.39154814.9598093.8890295.775088
*Bhattacharyya*0.0000000.2226090.6465760.801869

对于相关交点方法,度量越高,匹配越准确。我们可以看到,比赛基数是预期的最高。另外我们可以看到,匹配的一半是第二好的比赛(正如我们预测的)。对于其他两个指标,结果越少,匹配越好。我们可以看到,测试1和测试2之间的相对于基数的匹配更糟,这也是预期的。