W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
在本教程中,您將學(xué)習(xí)如何:
where
N是直方圖庫的總數(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;
}
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_setting1> <image_settings2>\n");
return -1;
}
src_base = imread( argv[1], 1 );
src_test1 = imread( argv[2], 1 );
src_test2 = imread( argv[3], 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};
float h_ranges [] = {0,180};
float s_ranges [] = {0,256};
const float * ranges [] = {h_ranges,s_ranges};
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 );
}
Base_0
Tset_1
Test_2
其中第一個是基礎(chǔ)(要與其他人進(jìn)行比較),另外2個是測試圖像。我們還將比較第一幅圖像與其本身和一半的基本圖像。
*Method* | Base - Base | Base - Half | Base - Test 1 | Base - Test 2 |
---|---|---|---|---|
*Correlation* | 1.000000 | 0.930766 | 0.182073 | 0.120447 |
*Chi-square* | 0.000000 | 4.940466 | 21.184536 | 49.273437 |
*Intersection* | 24.391548 | 14.959809 | 3.889029 | 5.775088 |
*Bhattacharyya* | 0.000000 | 0.222609 | 0.646576 | 0.801869 |
對于相關(guān)和交點方法,度量越高,匹配越準(zhǔn)確。我們可以看到,比賽基數(shù)是預(yù)期的最高。另外我們可以看到,匹配的一半是第二好的比賽(正如我們預(yù)測的)。對于其他兩個指標(biāo),結(jié)果越少,匹配越好。我們可以看到,測試1和測試2之間的相對于基數(shù)的匹配更糟,這也是預(yù)期的。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: