OpenCV直方圖比較

2018-09-20 10:22 更新

目標(biāo)

在本教程中,您將學(xué)習(xí)如何:

  • 使用函數(shù)cv :: compareHist獲取一個數(shù)值參數(shù),表示兩個直方圖相互匹配的程度。
  • 使用不同的指標(biāo)來比較直方圖

理論

  1. 為了比較兩個直方圖( H1 和 H2 ),首先我們必須選擇度量( d(H1,H2)來表示兩個直方圖的匹配度。
  2. OpenCV實現(xiàn)函數(shù)cv :: compareHist進(jìn)行比較。它還提供4種不同的指標(biāo)來計算匹配:
  • 相關(guān)性(CV_COMP_CORREL)

OpenCV直方圖比較

where

OpenCV直方圖比較

N是直方圖庫的總數(shù)。

  • Chi-Square(CV_COMP_CHISQR)

OpenCV直方圖比較

  • 交點(method= CV_COMP_INTERSECT)

OpenCV直方圖比較

  • Bhattacharyya distance ( CV_COMP_BHATTACHARYYA )

OpenCV直方圖比較

Code

  • 這個程序是做什么的?加載基礎(chǔ)圖像和2個測試圖像進(jìn)行比較。生成基本圖像的下半部分的1個圖像將圖像轉(zhuǎn)換為HSV格式計算所有圖像的HS直方圖,并對它們進(jìn)行歸一化,以便進(jìn)行比較。比較基本圖像的直方圖與2個測試直方圖,下半部基本圖像的直方圖以及相同的基本圖像直方圖。顯示獲得的數(shù)值匹配參數(shù)。
  • 可下載的代碼:點擊這里
  • 代碼一覽:
#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;
}

說明

  • 聲明諸如矩陣的變量來存儲基本圖像和另外兩個圖像進(jìn)行比較(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 );
  • 將其轉(zhuǎn)換為HSV格式:
cvtColor(src_base,hsv_base,COLOR_BGR2HSV);
cvtColor(src_test1,hsv_test1,COLOR_BGR2HSV);
cvtColor(src_test2,hsv_test2,COLOR_BGR2HSV);
  • 另外,創(chuàng)建一半的基本圖像(HSV格式):
hsv_half_down = hsv_base(Range(hsv_base.rows / 2,hsv_base.rows  -  1),Range(0,hsv_base.cols  -  1));
  • 初始化參數(shù)以計算直方圖(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};
  • 創(chuàng)建MatND對象以存儲直方圖:
MatND hist_base;
MatND hist_half_down;
MatND hist_test1;
MatND hist_test2;
  • 計算基本圖像的直方圖,2個測試圖像和半基準(zhǔn)圖像:
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() );
  • 依次應(yīng)用基本圖像(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 );
  }

結(jié)果

  • 我們使用以下圖像作為輸入:

OpenCV直方圖比較

Base_0

OpenCV直方圖比較

Tset_1

OpenCV直方圖比較

Test_2

其中第一個是基礎(chǔ)(要與其他人進(jìn)行比較),另外2個是測試圖像。我們還將比較第一幅圖像與其本身和一半的基本圖像。

  • 當(dāng)我們比較基本圖像直方圖與本身時,我們應(yīng)該期待一個完美的匹配。此外,與基本圖像的一半的直方圖相比,它應(yīng)該呈現(xiàn)高匹配,因為它們都來自相同的源。對于其他兩個測試圖像,我們可以觀察到它們具有非常不同的照明條件,因此匹配不應(yīng)該很好:
  • 這里的數(shù)值結(jié)果:
*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

對于相關(guān)交點方法,度量越高,匹配越準(zhǔn)確。我們可以看到,比賽基數(shù)是預(yù)期的最高。另外我們可以看到,匹配的一半是第二好的比賽(正如我們預(yù)測的)。對于其他兩個指標(biāo),結(jié)果越少,匹配越好。我們可以看到,測試1和測試2之間的相對于基數(shù)的匹配更糟,這也是預(yù)期的。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號