• opencv::直方图比较


    直方图比较方法-概述
        对输入的两张图像计算得到直方图H1与H2,归一化到相同的尺度空间然后可以通过计算H1与H2的之间的距离得到两个直方图的相似程度进而比较图像本身的相似程度。
        
        Correlation 相关性比较
        Chi-Square 卡方比较
        Intersection 十字交叉性
        Bhattacharyya distance 巴氏距离

    直方图比较方法-相关性计算(CV_COMP_CORREL)

    直方图比较方法-卡方计算(CV_COMP_CHISQR)

    直方图比较方法-十字计算(CV_COMP_INTERSECT)

    直方图比较方法-巴氏距离计算(CV_COMP_BHATTACHARYYA )

    首先把图像从RGB色彩空间转换到HSV色彩空间cvtColor
    计算图像的直方图,然后归一化到[0~1]之间calcHist和normalize;
    使用上述四种比较方法之一进行比较compareHist
    compareHist(
            InputArray h1,     // 直方图数据,下同
            InputArray H2,
            int method        // 比较方法,上述四种方法之一
    )
    string convertToString(double d);
    int main(int argc, char** argv) {
        Mat base, test1, test2;
        Mat hsvbase, hsvtest1, hsvtest2;
        base = imread(STRPAHT);
        if (!base.data) {
            printf("could not load image...
    ");
            return -1;
        }
        test1 = imread(STRPAHT2);
        test2 = imread(STRPAHT3);
    
        //从RGB空间转换到HSV空间
        cvtColor(base, hsvbase, CV_BGR2HSV);
        cvtColor(test1, hsvtest1, CV_BGR2HSV);
        cvtColor(test2, hsvtest2, CV_BGR2HSV);
    
        //计算直方图并归一化
        int h_bins = 50; int 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 o-th and 1-st channels     
        int channels[] = { 0, 1 };
        MatND hist_base;
        MatND hist_test1;
        MatND hist_test2;
    
        calcHist(&hsvbase, 1, channels, Mat(), hist_base, 2, histSize, ranges, true, false);
        normalize(hist_base, hist_base, 0, 1, NORM_MINMAX, -1, Mat());
    
        calcHist(&hsvtest1, 1, channels, Mat(), hist_test1, 2, histSize, ranges, true, false);
        normalize(hist_test1, hist_test1, 0, 1, NORM_MINMAX, -1, Mat());
    
        calcHist(&hsvtest2, 1, channels, Mat(), hist_test2, 2, histSize, ranges, true, false);
        normalize(hist_test2, hist_test2, 0, 1, NORM_MINMAX, -1, Mat());
    
        //比较直方图,并返回值
        double basebase = compareHist(hist_base, hist_base, CV_COMP_INTERSECT);
        double basetest1 = compareHist(hist_base, hist_test1, CV_COMP_INTERSECT);
        double basetest2 = compareHist(hist_base, hist_test2, CV_COMP_INTERSECT);
        double tes1test2 = compareHist(hist_test1, hist_test2, CV_COMP_INTERSECT);
    
        Mat test12;
        test2.copyTo(test12);
        putText(base, convertToString(basebase), Point(50, 50), CV_FONT_HERSHEY_COMPLEX, 1, Scalar(0, 0, 255), 2, LINE_AA);
        putText(test1, convertToString(basetest1), Point(50, 50), CV_FONT_HERSHEY_COMPLEX, 1, Scalar(0, 0, 255), 2, LINE_AA);
        putText(test2, convertToString(basetest2), Point(50, 50), CV_FONT_HERSHEY_COMPLEX, 1, Scalar(0, 0, 255), 2, LINE_AA);
        putText(test12, convertToString(tes1test2), Point(50, 50), CV_FONT_HERSHEY_COMPLEX, 1, Scalar(0, 0, 255), 2, LINE_AA);
    
        namedWindow("base", CV_WINDOW_AUTOSIZE);
        namedWindow("test1", CV_WINDOW_AUTOSIZE);
        namedWindow("test2", CV_WINDOW_AUTOSIZE);
    
        imshow("base", base);
        imshow("test1", test1);
        imshow("test2", test2);
        imshow("test12", test12);
    
        waitKey(0);
        return 0;
    }
    
    string convertToString(double d) {
        ostringstream os;
        if (os << d)
            return os.str();
        return "invalid conversion";
    }
  • 相关阅读:
    Json对象处理.将对象处理成dic数组.
    asp.net core 创建允许跨域请求的api, cors.
    (转载)dotnet core 中文乱码 codepages
    (转载)Memcached和Redis简介
    发送Json数据,WebApi查看时为Null的问题(已解决)
    Vs2017获取Git空仓库后创建解决方案及项目无法推送,推送失败的问题.
    前端:Jquery 处理同一Name的Radio组时,绑定checked属性异常的问题.(已解决)
    Vs2015 本地git获取的代码目录文件修改后,启动提示error:Unable to start program “C:Program Filesdotnetdotnet.exe” 已解决.
    .net core 发布后提示Start error
    .net core vs2015 vs2017打开后errpr
  • 原文地址:https://www.cnblogs.com/osbreak/p/11496083.html
Copyright © 2020-2023  润新知