我在Objective-C中实现了OpenCV CompareHist方法来比较两个图像的相似性:
图片1
和
图片2
)
但它会返回错误的结果。
当我预计它会低于0.9时,它返回 0.999971 。
- (bool) someMethod:(UIImage *)image :(UIImage *)temp {
cv::Mat src_base, hsv_base;
cv::Mat src_test1, hsv_test1;
src_base = [self cvMatWithImage:image];
src_test1 = [self cvMatWithImage:temp];
cv::cvtColor(src_base, hsv_base, cv::COLOR_BGR2HSV);
cv::cvtColor(src_test1, hsv_test1, cv::COLOR_BGR2HSV);
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 };
// Use the o-th and 1-st channels
int channels[] = { 0, 1 };
/// Histograms
cv::MatND hist_base;
cv::MatND hist_half_down;
cv::MatND hist_test1;
cv::MatND hist_test2;
calcHist( &hsv_base, 1, channels, cv::Mat(), hist_base, 2, histSize, ranges, true, false );
normalize( hist_base, hist_base, 0, 1, cv::NORM_MINMAX, -1, cv::Mat() );
calcHist( &hsv_test1, 1, channels, cv::Mat(), hist_test1, 2, histSize, ranges, true, false );
normalize( hist_test1, hist_test1, 0, 1, cv::NORM_MINMAX, -1, cv::Mat() );
double base_test1 = compareHist( hist_base, hist_test1, 0);
NSLog(@"%f", base_test1);
if (base_test1>0.9) {
return true;
}
return false;
}