opencv:图像直方图相似性比较

opencv:图像直方图相似性比较_第1张图片

void hist_compare(Mat src1, Mat src2) {
    int histSize[] = { 256, 256, 256 };
    int channels[] = { 0, 1, 2 };
    Mat hist1, hist2;
    float c1[] = { 0, 255 };
    float c2[] = { 0, 255 };
    float c3[] = { 0, 255 };
    const float* histRanges[] = { c1, c2, c3 };
    calcHist(&src1, 1, channels, Mat(), hist1, 3, histSize, histRanges, true, false);
    calcHist(&src2, 1, channels, Mat(), hist2, 3, histSize, histRanges, true, false);

    // 归一化
    normalize(hist1, hist1, 0, 1.0, NORM_MINMAX, -1, Mat());
    normalize(hist2, hist2, 0, 1.0, NORM_MINMAX, -1, Mat());

    // 比较 - 巴氏距离
    double h12_bhattacharyya = compareHist(hist1, hist2, HISTCMP_BHATTACHARYYA);
    double h11_bhattacharyya = compareHist(hist1, hist1, HISTCMP_BHATTACHARYYA);

    // 比较 - 相关性比较
    double h12_correl = compareHist(hist1, hist2, HISTCMP_CORREL);
    double h11_correl = compareHist(hist1, hist1, HISTCMP_CORREL);

    printf("巴氏距离: h12 = %.2f\th11 = %.2f\n", h12_bhattacharyya, h11_bhattacharyya);
    printf("相关性: h12 = %.2f\th11 = %.2f\n", h12_correl, h11_correl);
}

你可能感兴趣的:(opencv:图像直方图相似性比较)