上一个教程 : 直方图计算
下一个教程 : 反向投影
原作者 | Ana Huamán |
---|---|
兼容性 | OpenCV >= 3.0 |
目标
在本教程中,你将学习如何
- 使用函数 cv::compareHist 获得一个数值参数,以表示两个直方图之间的匹配程度。
- 使用不同的指标来比较直方图
原理
- 要比较两个直方图(H1 和 H2),首先必须选择一个度量(d(H1,H2))来表示两个直方图的匹配程度。
OpenCV 实现了 cv::compareHist 函数来执行比较。它还提供了 4 种不同的指标来计算匹配度:
- 相关性 ( cv::HISTCMP_CORREL )
d ( H 1 , H 2 ) = ∑ I ( H 1 ( I ) − H 1 ‾ ) ( H 2 ( I ) − H 2 ‾ ) ∑ I ( H 1 ( I ) − H 1 ‾ ) 2 ∑ I ( H 2 ( I ) − H 2 ‾ ) 2 d\left( H_1,H_2 \right) =\frac{\sum{_I\left( H_1\left( I \right) -\overline{H_1} \right) \left( H_2\left( I \right) -\overline{H_2} \right)}}{\sqrt{\sum{_I}\left( H_1\left( I \right) -\overline{H_1} \right) ^2\sum{_I\left( H_2\left( I \right) -\overline{H_2} \right) ^2}}} d(H1,H2)=∑I(H1(I)−H1)2∑I(H2(I)−H2)2∑I(H1(I)−H1)(H2(I)−H2)
其中
H
k
‾
=
1
N
∑
J
H
(
J
)
\overline{H_k}=\frac{1}{N}\sum_J^{}{H\left( J \right)}
Hk=N1J∑H(J)
而 N 是直方图分区的总数。
-
卡方检验 ( cv::HISTCMP_CHISQR )
d ( H 1 , H 2 ) = ∑ I ( H 1 ( I ) − H 2 ( I ) ) 2 H 1 ( I ) d\left( H_1,H_2 \right) =\sum{_I\frac{\left( H_1\left( I \right) -H_2\left( I \right) \right) ^2}{H_1\left( I \right)}} d(H1,H2)=∑IH1(I)(H1(I)−H2(I))2 -
相交检测 ( method=cv::HISTCMP_INTERSECT )
d ( H 1 , H 2 ) = ∑ I min ( H 1 ( I ) , H 2 ( I ) ) d\left( H_1,H_2 \right) =\sum_I^{}{\min \left( H_1\left( I \right) ,H_2\left( I \right) \right)} d(H1,H2)=I∑min(H1(I),H2(I)) -
巴氏距离 ( cv::HISTCMP_BHATTACHARYYA )
d ( H 1 , H 2 ) = 1 − 1 H 1 H 2 N 2 ∑ I H 1 ( I ) ⋅ H 2 ( I ) d\left( H_1,H_2 \right) =\sqrt{1-\frac{1}{H_1H_2N^2}\sum_I^{}{\sqrt{H_1\left( I \right) \cdot H_2\left( I \right)}}} d(H1,H2)=1−H1H2N21I∑H1(I)⋅H2(I)
代码
- 这个程序做什么?
- 加载一张基础图像和两张测试图像,并与之进行比较。
- 生成 1 幅图像,即基础图像的下半部分
- 将图像转换为 HSV 格式
- 计算所有图像的 H-S 直方图并归一化,以便进行比较。
- 将基础图像的直方图与 2 个测试直方图、下半部分基础图像的直方图以及同一基础图像的直方图进行比较。
- 显示获得的数字匹配参数。
C++
- 可下载代码: 点击此处
- 代码一览
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
using namespace std;
using namespace cv;
const char* keys =
"{ help h| | Print help message. }"
"{ @input1 |Histogram_Comparison_Source_0.jpg | Path to input image 1. }"
"{ @input2 |Histogram_Comparison_Source_1.jpg | Path to input image 2. }"
"{ @input3 |Histogram_Comparison_Source_2.jpg | Path to input image 3. }";
int main( int argc, char** argv )
{
CommandLineParser parser( argc, argv, keys );
samples::addSamplesDataSearchSubDirectory( "doc/tutorials/imgproc/histograms/histogram_comparison/images" );
Mat src_base = imread(samples::findFile( parser.get<String>( "@input1" ) ) );
Mat src_test1 = imread(samples::findFile( parser.get<String>( "@input2" ) ) );
Mat src_test2 = imread(samples::findFile( parser.get<String>( "@input3" ) ) );
if( src_base.empty() || src_test1.empty() || src_test2.empty() )
{
cout << "Could not open or find the images!\n" << endl;
parser.printMessage();
return -1;
}
Mat hsv_base, hsv_test1, hsv_test2;
cvtColor( src_base, hsv_base, COLOR_BGR2HSV );
cvtColor( src_test1, hsv_test1, COLOR_BGR2HSV );
cvtColor( src_test2, hsv_test2, COLOR_BGR2HSV );
Mat hsv_half_down = hsv_base( Range( hsv_base.rows/2, hsv_base.rows ), Range( 0, hsv_base.cols ) );
int h_bins = 50, s_bins = 60;
int histSize[] = { h_bins, s_bins };
// 色调范围从 0 到 179,饱和度范围从 0 到 255
float h_ranges[] = { 0, 180 };
float s_ranges[] = { 0, 256 };
const float* ranges[] = { h_ranges, s_ranges };
// 使用第 0 和第 1 个通道
int channels[] = { 0, 1 };
Mat hist_base, hist_half_down, hist_test1, 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 compare_method = 0; compare_method < 4; compare_method++ )
{
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 );
cout << "Method " << compare_method << " Perfect, Base-Half, Base-Test(1), Base-Test(2) : "
<< base_base << " / " << base_half << " / " << base_test1 << " / " << base_test2 << endl;
}
cout << "Done \n";
return 0;
}
Java
- 可下载代码: 点击此处
- 代码一览
import java.util.Arrays;
import java.util.List;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfFloat;
import org.opencv.core.MatOfInt;
import org.opencv.core.Range;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
class CompareHist {
public void run(String[] args) {
if (args.length != 3) {
System.err.println("You must supply 3 arguments that correspond to the paths to 3 images.");
System.exit(0);
}
Mat srcBase = Imgcodecs.imread(args[0]);
Mat srcTest1 = Imgcodecs.imread(args[1]);
Mat srcTest2 = Imgcodecs.imread(args[2]);
if (srcBase.empty() || srcTest1.empty() || srcTest2.empty()) {
System.err.println("Cannot read the images");
System.exit(0);
}
Mat hsvBase = new Mat(), hsvTest1 = new Mat(), hsvTest2 = new Mat();
Imgproc.cvtColor( srcBase, hsvBase, Imgproc.COLOR_BGR2HSV );
Imgproc.cvtColor( srcTest1, hsvTest1, Imgproc.COLOR_BGR2HSV );
Imgproc.cvtColor( srcTest2, hsvTest2, Imgproc.COLOR_BGR2HSV );
Mat hsvHalfDown = hsvBase.submat( new Range( hsvBase.rows()/2, hsvBase.rows() - 1 ), new Range( 0, hsvBase.cols() - 1 ) );
int hBins = 50, sBins = 60;
int[] histSize = { hBins, sBins };
// 色调范围从 0 到 179,饱和度范围从 0 到 255
float[] ranges = { 0, 180, 0, 256 };
// 使用第 0 和第 1 个通道
int[] channels = { 0, 1 };
Mat histBase = new Mat(), histHalfDown = new Mat(), histTest1 = new Mat(), histTest2 = new Mat();
List<Mat> hsvBaseList = Arrays.asList(hsvBase);
Imgproc.calcHist(hsvBaseList, new MatOfInt(channels), new Mat(), histBase, new MatOfInt(histSize), new MatOfFloat(ranges), false);
Core.normalize(histBase, histBase, 0, 1, Core.NORM_MINMAX);
List<Mat> hsvHalfDownList = Arrays.asList(hsvHalfDown);
Imgproc.calcHist(hsvHalfDownList, new MatOfInt(channels), new Mat(), histHalfDown, new MatOfInt(histSize), new MatOfFloat(ranges), false);
Core.normalize(histHalfDown, histHalfDown, 0, 1, Core.NORM_MINMAX);
List<Mat> hsvTest1List = Arrays.asList(hsvTest1);
Imgproc.calcHist(hsvTest1List, new MatOfInt(channels), new Mat(), histTest1, new MatOfInt(histSize), new MatOfFloat(ranges), false);
Core.normalize(histTest1, histTest1, 0, 1, Core.NORM_MINMAX);
List<Mat> hsvTest2List = Arrays.asList(hsvTest2);
Imgproc.calcHist(hsvTest2List, new MatOfInt(channels), new Mat(), histTest2, new MatOfInt(histSize), new MatOfFloat(ranges), false);
Core.normalize(histTest2, histTest2, 0, 1, Core.NORM_MINMAX);
for( int compareMethod = 0; compareMethod < 4; compareMethod++ ) {
double baseBase = Imgproc.compareHist( histBase, histBase, compareMethod );
double baseHalf = Imgproc.compareHist( histBase, histHalfDown, compareMethod );
double baseTest1 = Imgproc.compareHist( histBase, histTest1, compareMethod );
double baseTest2 = Imgproc.compareHist( histBase, histTest2, compareMethod );
System.out.println("Method " + compareMethod + " Perfect, Base-Half, Base-Test(1), Base-Test(2) : " + baseBase + " / " + baseHalf
+ " / " + baseTest1 + " / " + baseTest2);
}
}
}
public class CompareHistDemo {
public static void main(String[] args) {
// 加载本地 OpenCV 库
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
new CompareHist().run(args);
}
}
Python
- 可下载代码: 点击此处
- 代码一览
from __future__ import print_function
from __future__ import division
import cv2 as cv
import numpy as np
import argparse
parser = argparse.ArgumentParser(description='Code for Histogram Comparison tutorial.')
parser.add_argument('--input1', help='Path to input image 1.')
parser.add_argument('--input2', help='Path to input image 2.')
parser.add_argument('--input3', help='Path to input image 3.')
args = parser.parse_args()
src_base = cv.imread(args.input1)
src_test1 = cv.imread(args.input2)
src_test2 = cv.imread(args.input3)
if src_base is None or src_test1 is None or src_test2 is None:
print('Could not open or find the images!')
exit(0)
hsv_base = cv.cvtColor(src_base, cv.COLOR_BGR2HSV)
hsv_test1 = cv.cvtColor(src_test1, cv.COLOR_BGR2HSV)
hsv_test2 = cv.cvtColor(src_test2, cv.COLOR_BGR2HSV)
hsv_half_down = hsv_base[hsv_base.shape[0]//2:,:]
h_bins = 50
s_bins = 60
histSize = [h_bins, s_bins]
histSize = [h_bins, s_bins] # 色调从 0 到 60 不等。
# 色调从 0 到 179,饱和度从 0 到 255
h_ranges = [0, 180]
s_ranges = [0, 256] # 色调在 0 至 179 之间变化,饱和度在 0 至 255 之间变化。
ranges = h_ranges + s_ranges # 连接列表
# 使用第 0 和第 1 个通道
channels = [0, 1]
hist_base = cv.calcHist([hsv_base], channels, None, histSize, ranges, accumulate=False)
cv.normalize(hist_base, hist_base, alpha=0, beta=1, norm_type=cv.NORM_MINMAX)
hist_half_down = cv.calcHist([hsv_half_down], channels, None, histSize, ranges, accumulate=False)
cv.normalize(hist_half_down, hist_half_down, alpha=0, beta=1, norm_type=cv.NORM_MINMAX)
hist_test1 = cv.calcHist([hsv_test1], channels, None, histSize, ranges, accumulate=False)
cv.normalize(hist_test1, hist_test1, alpha=0, beta=1, norm_type=cv.NORM_MINMAX)
hist_test2 = cv.calcHist([hsv_test2], channels, None, histSize, ranges, accumulate=False)
cv.normalize(hist_test2, hist_test2, alpha=0, beta=1, norm_type=cv.NORM_MINMAX)
for compare_method in range(4):
base_base = cv.compareHist(hist_base, hist_base, compare_method)
base_half = cv.compareHist(hist_base, hist_half_down, compare_method)
base_test1 = cv.compareHist(hist_base, hist_test1, compare_method)
base_test2 = cv.compareHist(hist_base, hist_test2, compare_method)
print('Method:', compare_method, 'Perfect, Base-Half, Base-Test(1), Base-Test(2) :',\
base_base, '/', base_half, '/', base_test1, '/', base_test2)
说明
- 加载基本图像 (src_base) 和另外两个测试图像:
C++
CommandLineParser parser( argc, argv, keys );
samples::addSamplesDataSearchSubDirectory( "doc/tutorials/imgproc/histograms/histogram_comparison/images" );
Mat src_base = imread(samples::findFile( parser.get<String>( "@input1" ) ) );
Mat src_test1 = imread(samples::findFile( parser.get<String>( "@input2" ) ) );
Mat src_test2 = imread(samples::findFile( parser.get<String>( "@input3" ) ) );
if( src_base.empty() || src_test1.empty() || src_test2.empty() )
{
cout << "Could not open or find the images!\n" << endl;
parser.printMessage();
return -1;
}
Java
if (args.length != 3) {
System.err.println("You must supply 3 arguments that correspond to the paths to 3 images.");
System.exit(0);
}
Mat srcBase = Imgcodecs.imread(args[0]);
Mat srcTest1 = Imgcodecs.imread(args[1]);
Mat srcTest2 = Imgcodecs.imread(args[2]);
if (srcBase.empty() || srcTest1.empty() || srcTest2.empty()) {
System.err.println("Cannot read the images");
System.exit(0);
}
Python
parser = argparse.ArgumentParser(description='Code for Histogram Comparison tutorial.')
parser.add_argument('--input1', help='Path to input image 1.')
parser.add_argument('--input2', help='Path to input image 2.')
parser.add_argument('--input3', help='Path to input image 3.')
args = parser.parse_args()
src_base = cv.imread(args.input1)
src_test1 = cv.imread(args.input2)
src_test2 = cv.imread(args.input3)
if src_base is None or src_test1 is None or src_test2 is None:
print('Could not open or find the images!')
exit(0)
- 将它们转换为 HSV 格式:
C++
Mat hsv_base, hsv_test1, hsv_test2;
cvtColor( src_base, hsv_base, COLOR_BGR2HSV );
cvtColor( src_test1, hsv_test1, COLOR_BGR2HSV );
cvtColor( src_test2, hsv_test2, COLOR_BGR2HSV );
Java
Mat hsvBase = new Mat(), hsvTest1 = new Mat(), hsvTest2 = new Mat();
Imgproc.cvtColor( srcBase, hsvBase, Imgproc.COLOR_BGR2HSV );
Imgproc.cvtColor( srcTest1, hsvTest1, Imgproc.COLOR_BGR2HSV );
Imgproc.cvtColor( srcTest2, hsvTest2, Imgproc.COLOR_BGR2HSV );
Python
hsv_base = cv.cvtColor(src_base, cv.COLOR_BGR2HSV)
hsv_test1 = cv.cvtColor(src_test1, cv.COLOR_BGR2HSV)
hsv_test2 = cv.cvtColor(src_test2, cv.COLOR_BGR2HSV)
- 另外,创建一幅半幅基础图像(HSV 格式):
C++
Mat hsv_half_down = hsv_base( Range( hsv_base.rows/2, hsv_base.rows ), Range( 0, hsv_base.cols ) );
Java
Mat hsvHalfDown = hsvBase.submat( new Range( hsvBase.rows()/2, hsvBase.rows() - 1 ), new Range( 0, hsvBase.cols() - 1 ) );
Python
hsv_half_down = hsv_base[hsv_base.shape[0]//2:,:]
- 初始化用于计算直方图的参数(bins、ranges 以及通道 H 和 S)。
C++
int h_bins = 50, 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 0-th and 1-st channels
int channels[] = { 0, 1 };
Java
int hBins = 50, sBins = 60;
int[] histSize = { hBins, sBins };
// hue varies from 0 to 179, saturation from 0 to 255
float[] ranges = { 0, 180, 0, 256 };
// Use the 0-th and 1-st channels
int[] channels = { 0, 1 };
Python
h_bins = 50
s_bins = 60
histSize = [h_bins, s_bins]
# hue varies from 0 to 179, saturation from 0 to 255
h_ranges = [0, 180]
s_ranges = [0, 256]
ranges = h_ranges + s_ranges # concat lists
# Use the 0-th and 1-st channels
channels = [0, 1]
- 计算基础图像、两幅测试图像和半下基础图像的直方图:
C++
Mat hist_base, hist_half_down, hist_test1, 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() );
Java
Mat histBase = new Mat(), histHalfDown = new Mat(), histTest1 = new Mat(), histTest2 = new Mat();
List<Mat> hsvBaseList = Arrays.asList(hsvBase);
Imgproc.calcHist(hsvBaseList, new MatOfInt(channels), new Mat(), histBase, new MatOfInt(histSize), new MatOfFloat(ranges), false);
Core.normalize(histBase, histBase, 0, 1, Core.NORM_MINMAX);
List<Mat> hsvHalfDownList = Arrays.asList(hsvHalfDown);
Imgproc.calcHist(hsvHalfDownList, new MatOfInt(channels), new Mat(), histHalfDown, new MatOfInt(histSize), new MatOfFloat(ranges), false);
Core.normalize(histHalfDown, histHalfDown, 0, 1, Core.NORM_MINMAX);
List<Mat> hsvTest1List = Arrays.asList(hsvTest1);
Imgproc.calcHist(hsvTest1List, new MatOfInt(channels), new Mat(), histTest1, new MatOfInt(histSize), new MatOfFloat(ranges), false);
Core.normalize(histTest1, histTest1, 0, 1, Core.NORM_MINMAX);
List<Mat> hsvTest2List = Arrays.asList(hsvTest2);
Imgproc.calcHist(hsvTest2List, new MatOfInt(channels), new Mat(), histTest2, new MatOfInt(histSize), new MatOfFloat(ranges), false);
Core.normalize(histTest2, histTest2, 0, 1, Core.NORM_MINMAX);
Python
hist_base = cv.calcHist([hsv_base], channels, None, histSize, ranges, accumulate=False)
cv.normalize(hist_base, hist_base, alpha=0, beta=1, norm_type=cv.NORM_MINMAX)
hist_half_down = cv.calcHist([hsv_half_down], channels, None, histSize, ranges, accumulate=False)
cv.normalize(hist_half_down, hist_half_down, alpha=0, beta=1, norm_type=cv.NORM_MINMAX)
hist_test1 = cv.calcHist([hsv_test1], channels, None, histSize, ranges, accumulate=False)
cv.normalize(hist_test1, hist_test1, alpha=0, beta=1, norm_type=cv.NORM_MINMAX)
hist_test2 = cv.calcHist([hsv_test2], channels, None, histSize, ranges, accumulate=False)
cv.normalize(hist_test2, hist_test2, alpha=0, beta=1, norm_type=cv.NORM_MINMAX)
- 在基础图像的直方图(hist_base)和其他直方图之间依次应用 4 种比较方法:
C++
for( int compare_method = 0; compare_method < 4; compare_method++ )
{
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 );
cout << "Method " << compare_method << " Perfect, Base-Half, Base-Test(1), Base-Test(2) : "
<< base_base << " / " << base_half << " / " << base_test1 << " / " << base_test2 << endl;
}
Java
for( int compareMethod = 0; compareMethod < 4; compareMethod++ ) {
double baseBase = Imgproc.compareHist( histBase, histBase, compareMethod );
double baseHalf = Imgproc.compareHist( histBase, histHalfDown, compareMethod );
double baseTest1 = Imgproc.compareHist( histBase, histTest1, compareMethod );
double baseTest2 = Imgproc.compareHist( histBase, histTest2, compareMethod );
System.out.println("Method " + compareMethod + " Perfect, Base-Half, Base-Test(1), Base-Test(2) : " + baseBase + " / " + baseHalf
+ " / " + baseTest1 + " / " + baseTest2);
}
Python
for compare_method in range(4):
base_base = cv.compareHist(hist_base, hist_base, compare_method)
base_half = cv.compareHist(hist_base, hist_half_down, compare_method)
base_test1 = cv.compareHist(hist_base, hist_test1, compare_method)
base_test2 = cv.compareHist(hist_base, hist_test2, compare_method)
print('Method:', compare_method, 'Perfect, Base-Half, Base-Test(1), Base-Test(2) :',\
base_base, '/', base_half, '/', base_test1, '/', base_test2)
结果
- 我们使用以下图像作为输入:
其中第一幅是基准图像(与其他图像进行比较),另外两幅是测试图像。我们还将比较第一张图像与它自己的关系,以及与半张基础图像的关系。
2. 当我们比较基底图像的直方图时,应该会发现两者完全匹配。此外,与半幅基础图像的直方图相比,由于两者来自同一来源,因此也应呈现高度匹配。对于另外两张测试图像,我们可以看到它们的光照条件非常不同,因此匹配度应该不会很高:
3. 以下是我们使用 OpenCV 3.4.1 得到的数值结果:
方法 | Base-Base | Base-Half | Base-Test1 | Base-Test2 |
---|---|---|---|---|
相关性(Correlation) | 1.000000 | 0.880438 | 0.20457 | 0.0664547 |
卡方(Chi-Square) | 0.000000 | 4.6834 | 2697.98 | 4763.8 |
相交(Intersection) | 18.8947 | 13.022 | 5.44085 | 2.58173 |
Bhattacharyya | 0.000000 | 0.237887 | 0.679826 | 0.874173 |
对于相关法和交集法,度量越高,匹配越准确。我们可以看到,匹配基数是所有方法中最高的。此外,我们还可以看到,"Base-Half "匹配度是第二高的(正如我们所预测的)。对于其他两个指标,结果越小,匹配度越高。我们可以发现,测试 1 和测试 2 在基数方面的匹配度较差,这也是意料之中的。