opencv-019-图像直方图比较

知识点

图像直方图比较,就是计算两幅图像的直方图数据,比较两组数据的相似性,从而得到两幅图像之间的相似程度,直方图比较在早期的CBIR(以图搜图)中是应用很常见的技术手段,通常会结合边缘处理、词袋等技术一起使用。
API
compareHist(hist1, hist2, method)
常见比较方法有

  • 相关性(常用)
  • 卡方
  • 交叉
  • 巴氏(常用)

代码(c++,python)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

/*
* 图像直方图比较
*/
int main() {
Mat src1 = imread("../images/left01.jpg");
Mat src2 = imread("../images/left13.jpg");
if (src1.empty() || src2.empty()) {
cout << "could not load image.." << endl;
}
imshow("input1", src1);
imshow("input2", src2);

// 一般在HSV色彩空间进行计算
Mat hsv1, hsv2;
cvtColor(src1, hsv1, COLOR_BGR2HSV);
cvtColor(src2, hsv2, COLOR_BGR2HSV);

int h_bins = 60, s_bins = 64;
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};
Mat hist1, hist2;
calcHist(&hsv1, 1, channels, Mat(), hist1, 2, histSize, ranges);
calcHist(&hsv2, 1, channels, Mat(), hist2, 2, histSize, ranges);

normalize(hist1, hist1, 0, 1, NORM_MINMAX, -1, Mat());
normalize(hist2, hist2, 0, 1, NORM_MINMAX, -1, Mat());

// 比较
double src1_src2_1 = compareHist(hist1, hist2, HISTCMP_CORREL);
double src1_src2_2 = compareHist(hist1, hist2, HISTCMP_BHATTACHARYYA);
printf("HISTCMP_CORREL : %.2f\n", src1_src2_1);
printf("HISTCMP_BHATTACHARYYA : %.2f\n", src1_src2_1);

waitKey(0);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import cv2 as cv
import numpy as np

src1 = cv.imread("D:/vcprojects/images/m1.png")
src2 = cv.imread("D:/vcprojects/images/m2.png")
src3 = cv.imread("D:/vcprojects/images/flower.png")
src4 = cv.imread("D:/vcprojects/images/wm_test.png")

cv.imshow("input1", src1)
cv.imshow("input2", src2)
cv.imshow("input3", src3)
cv.imshow("input4", src4)

hsv1 = cv.cvtColor(src1, cv.COLOR_BGR2HSV)
hsv2 = cv.cvtColor(src2, cv.COLOR_BGR2HSV)
hsv3 = cv.cvtColor(src3, cv.COLOR_BGR2HSV)
hsv4 = cv.cvtColor(src4, cv.COLOR_BGR2HSV)

hist1 = cv.calcHist([hsv1], [0, 1], None, [60, 64], [0, 180, 0, 256])
hist2 = cv.calcHist([hsv2], [0, 1], None, [60, 64], [0, 180, 0, 256])
hist3 = cv.calcHist([hsv3], [0, 1], None, [60, 64], [0, 180, 0, 256])
hist4 = cv.calcHist([hsv4], [0, 1], None, [60, 64], [0, 180, 0, 256])

cv.normalize(hist1, hist1, 0, 1.0, cv.NORM_MINMAX, dtype=np.float32)
cv.normalize(hist2, hist2, 0, 1.0, cv.NORM_MINMAX)
cv.normalize(hist3, hist3, 0, 1.0, cv.NORM_MINMAX)
cv.normalize(hist4, hist4, 0, 1.0, cv.NORM_MINMAX)

methods = [cv.HISTCMP_CORREL, cv.HISTCMP_CHISQR,
cv.HISTCMP_INTERSECT, cv.HISTCMP_BHATTACHARYYA]
str_method = ""
for method in methods:
src1_src2 = cv.compareHist(hist1, hist2, method)
src3_src4 = cv.compareHist(hist3, hist4, method)
if method == cv.HISTCMP_CORREL:
str_method = "Correlation"
if method == cv.HISTCMP_CHISQR:
str_method = "Chi-square"
if method == cv.HISTCMP_INTERSECT:
str_method = "Intersection"
if method == cv.HISTCMP_BHATTACHARYYA:
str_method = "Bhattacharyya"

print("%s src1_src2 = %.2f, src3_src4 = %.2f"%(str_method, src1_src2, src3_src4))


cv.waitKey(0)
cv.destroyAllWindows()

结果

代码地址

github