我正在使用OpenCV documentation给出的直方图比较代码的变体来检查提交的图像与存储在我的文件系统中的其他图像的相似性。问题是我得到的值与我使用的原始比较不同。
原始代码:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
/** @function main */
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;
/// Load three images with different environment settings
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 );
/// Convert to HSV
cvtColor( src_base, hsv_base, CV_BGR2HSV );
cvtColor( src_test1, hsv_test1, CV_BGR2HSV );
cvtColor( src_test2, hsv_test2, CV_BGR2HSV );
hsv_half_down = hsv_base( Range( hsv_base.rows/2, hsv_base.rows - 1 ), Range( 0, hsv_base.cols - 1 ) );
/// Using 30 bins for hue and 32 for saturation
int h_bins = 50; int s_bins = 60;
int histSize[] = { h_bins, s_bins };
// hue varies from 0 to 256, saturation from 0 to 180
float h_ranges[] = { 0, 256 };
float s_ranges[] = { 0, 180 };
const float* ranges[] = { h_ranges, s_ranges };
// Use the o-th and 1-st channels
int channels[] = { 0, 1 };
/// Histograms
MatND hist_base;
MatND hist_half_down;
MatND hist_test1;
MatND hist_test2;
/// Calculate the histograms for the HSV images
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() );
/// Apply the histogram comparison methods
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;
}
以下是我如何更改代码(我从第二个calcHist继续):
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;
using namespace cv;
int main( int argc, char** argv )
{
Mat src_base, hsv_base;
Mat hsv_half_down;
Mat src_rest, hsv_rest;
if( argc < 2 )
{
printf("** Error. Usage: ./compareHist_Demo <image_settings0> <image_setting1> <image_settings2>\n");
return -1;
}
src_base = imread( argv[1], 1 );
/// Convert to HSV
cvtColor( src_base, hsv_base, COLOR_BGR2HSV );
hsv_half_down = hsv_base( Range( hsv_base.rows/2, hsv_base.rows - 1 ), Range( 0, hsv_base.cols - 1 ) );
/// Using 30 bins for hue and 32 for saturation
int h_bins = 50; int s_bins = 60;
int histSize[] = { h_bins, s_bins };
// hue varies from 0 to 256, saturation from 0 to 180
float s_ranges[] = { 0, 256 };
float h_ranges[] = { 0, 180 };
const float* ranges[] = { h_ranges, s_ranges };
// Use the o-th and 1-st channels
int channels[] = { 0, 1 };
/// Histograms
MatND hist_base;
MatND hist_half_down;
MatND hist_rest;
/// Calculate the histograms for the HSV images
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() );
Mat src_rest, hsv_rest;
MatND hist_rest;
std::vector<string> src;
string line;
ifstream myfile ("/Users/Dave/Desktop/foods.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
src.push_back(line);
}
myfile.close();
}
else cout << "Unable to open file";
long arrLength = src.size();
for(int i = 0;i<arrLength;i++){
src_rest = imread(src[i],1);
cvtColor(src_rest, hsv_rest, COLOR_BGR2HSV);
calcHist(&hsv_rest, 1, channels, Mat(), hist_rest, 2, histSize, ranges, true, false);
normalize(hist_rest, hist_rest, 0, 1, NORM_MINMAX, -1, Mat());
double baseComp = compareHist( hist_rest, hist_base, 0 );
cout<<"Main picture compared to "<<src[i]<<" using correlation "<<baseComp<<'\n';
}
printf("\n");
for(int i = 0;i<arrLength;i++){
src_rest = imread(src[i],1);
cvtColor(src_rest, hsv_rest, COLOR_BGR2HSV);
calcHist(&hsv_rest, 1, channels, Mat(), hist_rest, 2, histSize, ranges, true, false);
normalize(hist_rest, hist_rest, 0, 1, NORM_MINMAX, -1, Mat());
double baseComp = compareHist( hist_rest, hist_base, 1 );
cout<<"Main picture compared to "<<src[i]<<" using chi values "<<baseComp<<'\n';
}
for(int i = 0;i<arrLength;i++){
src_rest = imread(src[i],1);
cvtColor(src_rest, hsv_rest, COLOR_BGR2HSV);
calcHist(&hsv_rest, 1, channels, Mat(), hist_rest, 2, histSize, ranges, true, false);
normalize(hist_rest, hist_rest, 0, 1, NORM_MINMAX, -1, Mat());
double baseComp = compareHist( hist_rest, hist_base, 2 );
cout<<"Main picture compared to "<<src[i]<<" intersection "<<baseComp<<'\n';
}
for(int i = 0;i<arrLength;i++){
src_rest = imread(src[i],1);
cvtColor(src_rest, hsv_rest, COLOR_BGR2HSV);
calcHist(&hsv_rest, 1, channels, Mat(), hist_rest, 2, histSize, ranges, true, false);
normalize(hist_rest, hist_rest, 0, 1, NORM_MINMAX, -1, Mat());
double baseComp = compareHist( hist_base, hist_rest, 3 );
cout<<"Main picture compared to "<<src[i]<<" bhattacharyya "<<baseComp<<'\n';
}
已省略的其余代码是OpenCV版本中的其余初始化。
继承原始输出:
Method [0] Perfect, Base-Half, Base-Test(1), Base-Test(2) : 1.000000, 0.999990, 0.979741, 0.952336
Method [1] Perfect, Base-Half, Base-Test(1), Base-Test(2) : 0.000000, 0.018286, 128.338021, 130.609857
Method [2] Perfect, Base-Half, Base-Test(1), Base-Test(2) : 1.260779, 1.225753, 1.225654, 1.254846
Method [3] Perfect, Base-Half, Base-Test(1), Base-Test(2) : 0.000000, 0.052663, 0.440655, 0.494793
继续我的输出:
Main picture compared to base.jpg using correlation 1
Main picture compared to test1.jpg using correlation 0.763895
Main picture compared to test2.jpg using correlation 0.54124
Main picture compared to base.jpg using chi values 0
Main picture compared to test1.jpg using chi values 223.443
Main picture compared to test2.jpg using chi values 876.137
Main picture compared to base.jpg intersection 3.4481
Main picture compared to test1.jpg intersection 1.78523
Main picture compared to test2.jpg intersection 1.79021
Main picture compared to base.jpg bhattacharyya 1.05367e-08
Main picture compared to test1.jpg bhattacharyya 0.569303
Main picture compared to test2.jpg bhattacharyya 0.675118
因此,我在循环中为代码布局代码的方式存在问题,以便获得完全不同的结果吗?