zoukankan      html  css  js  c++  java
  • opencv —— compareHist 直方图比较

    对比直方图:compareHist 函数

    double compareHist(InputArray H1, InputArray H2, int method);

    • H1,H2,是要进行比较的直方图。
    • method,比较方法。有如下选择:
    方法名 标识符 计算公式

    相关 

    Correlation

    HISTCMP_CORREL
    d(H_1,H_2) =  frac{sum_I (H_1(I) - ar{H_1}) (H_2(I) - ar{H_2})}{sqrt{sum_I(H_1(I) - ar{H_1})^2 sum_I(H_2(I) - ar{H_2})^2}}

    其中 ar{H_k} =  frac{1}{N} sum _J H_k(J)

    N 是直方图中bin的数目。

    卡方

    Chi-square

    HISTCMP_CHISQR
    d(H_1,H_2) =  sum _I  frac{left(H_1(I)-H_2(I)
ight)^2}{H_1(I)+H_2(I)}

    相交 

    Intersection

    HISTCMP_INTERSECT

    d(H_1,H_2) =  sum _I  min (H_1(I), H_2(I))

    巴氏距离 

    Bhattacharyya

    HISTCMP_BHATTACHARYYA
    d(H_1,H_2) =  sqrt{1 - frac{1}{sqrt{ar{H_1} ar{H_2} N^2}} sum_I sqrt{H_1(I) cdot H_2(I)}}

     

    代码示例:

    #include<opencv.hpp>
    #include<iostream>
    #include<string>
    using namespace std;
    using namespace cv;
    int main() {
        Mat src1 = imread("C:/Users/齐明洋/Desktop/证件照/6.jpg");
        Mat src2 = imread("C:/Users/齐明洋/Desktop/证件照/8.jpg");
        Mat src3 = imread("C:/Users/齐明洋/Desktop/证件照/10.jpg");
        imshow("src1", src1);
        imshow("src2", src2);
        imshow("src3", src3);
    
        //将图片转换成 HSV 类型
        //对 HS 两通道进行直方图统计
        cvtColor(src1, src1, COLOR_BGR2HSV);
        cvtColor(src2, src2, COLOR_BGR2HSV);
        cvtColor(src3, src3, COLOR_BGR2HSV);
    
        int channels[] = { 0,1 };
        int histsize[] = { 180,255 };
        float r1[] = { 0,180 };
        float r2[] = { 0,255 };
        const float *ranges[] = { r1,r2 };
        Mat hist1, hist2, hist3;
        calcHist(&src1, 3, channels, Mat(), hist1, 2, histsize, ranges, true);
        //https://www.cnblogs.com/bjxqmy/p/12292421.html
        normalize(hist1, hist1, 1, 0, NORM_L1);
    
        calcHist(&src2, 3, channels, Mat(), hist2, 2, histsize, ranges, true);
        normalize(hist2, hist2, 1, 0, NORM_L1);
    
        calcHist(&src3, 3, channels, Mat(), hist3, 2, histsize, ranges, true);
        normalize(hist3, hist3, 1, 0, NORM_L1);
        
        int method[] = { HISTCMP_CORREL,HISTCMP_CHISQR,HISTCMP_INTERSECT,HISTCMP_BHATTACHARYYA };
        string names[] = { "HISTCMP_CORREL","HISTCMP_CHISQR","HISTCMP_INTERSECT","HISTCMP_BHATTACHARYYA" };
        for (int i = 0; i < 4; i++) {
            cout << names[i] << ":" << endl;
            cout << "src1 and src1 :" << compareHist(hist1, hist1, method[i]) << endl;
            cout << "src1 and src2 :" << compareHist(hist1, hist2, method[i]) << endl;
            cout << "src1 and src3 :" << compareHist(hist1, hist3, method[i]) << endl;
            cout << endl;
        }
        waitKey(0);
    }

    效果演示:

     

     

    借鉴博客:https://blog.csdn.net/qq_18343569/article/details/48028457

     

  • 相关阅读:
    401. Binary Watch
    46. Permutations
    61. Rotate List
    142. Linked List Cycle II
    86. Partition List
    234. Palindrome Linked List
    19. Remove Nth Node From End of List
    141. Linked List Cycle
    524. Longest Word in Dictionary through Deleting
    android ListView详解
  • 原文地址:https://www.cnblogs.com/bjxqmy/p/12379974.html
Copyright © 2011-2022 走看看