fanfuhan OpenCV 教學019 ~ opencv-019-圖像直方圖比較

fanfuhan OpenCV 教學019 ~ opencv-019-圖像直方圖比較

fanfuhan OpenCV 教學019 ~ opencv-019-圖像直方圖比較


資料來源: https://fanfuhan.github.io/

https://fanfuhan.github.io/2019/03/30/opencv-019/

GITHUB:https://github.com/jash-git/fanfuhan_ML_OpenCV


圖像直方圖比較,就是計算兩幅圖像的直方圖數據,比較兩組數據的相似性,從而得到兩幅圖像之間的相似程度,直方圖比較在早期的CBIR(以圖搜圖)中是應用很常見的技術手段,通常會結合邊緣處理、詞袋等技術一起使用。

C++

#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;
}

Python

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()

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *