首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OpenCV中两个Mat文件的相关性计算

OpenCV中两个Mat文件的相关性计算
EN

Stack Overflow用户
提问于 2014-01-14 15:15:01
回答 1查看 2.7K关注 0票数 2

我有一个Mat文件的向量,我想计算它们之间的相关性,以保持这两个mat文件在理论上是相似的。实际上,在这个向量中存储的是从图像中检测到的眼睛,所以我试图删除异常值。如何计算两个Mat文件之间的相关性?

编辑:

代码语言:javascript
复制
Mat Detection::hist_calculation(Mat image){

    // Establish the number of bins
    int histSize = 256;

    // Set the ranges
    float range[] = { 0, 256 } ;
    const float* histRange = { range };

    bool uniform = true; bool accumulate = false;

    Mat hist;

    // Compute the histograms:
    calcHist( &image, 1, 0, Mat(), hist, 1, &histSize, &histRange, uniform, accumulate );


    // Draw the histograms for B, G and R
    int hist_w = 512; int hist_h = 400;
    int bin_w = cvRound( (double) hist_w/histSize );

    Mat histImage( hist_h, hist_w, CV_8UC3, Scalar( 0,0,0) );

    normalize(hist, hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );

    for( int i = 1; i < histSize; i++ )
    {
        line( histImage, Point( bin_w*(i-1), hist_h - cvRound(hist.at<float>(i-1)) ) ,
            Point( bin_w*(i),   hist_h - cvRound(hist.at<float>(i))   ) ,
            Scalar( 255, 0, 0), 2, 8, 0  );
    }

    //// Display
    //namedWindow("calcHist Demo", CV_WINDOW_AUTOSIZE );
    //imshow("calcHist Demo", histImage );
    //waitKey(0);

    return hist;
}

double Detection::cvMatHistCorrelation(Mat file1, Mat file2) {

    cvtColor(file1, file1, CV_BGR2GRAY); cvtColor(file2, file2, CV_BGR2GRAY);
    Mat hist1 = hist_calculation(file1);
    Mat hist2 = hist_calculation(file2);

    double autoCorrelation1 = compareHist( hist1, hist1, CV_COMP_BHATTACHARYYA );
    double autoCorrelation2 = compareHist( hist1, hist1, CV_COMP_BHATTACHARYYA );
    double correlation  = compareHist( hist1, hist2, CV_COMP_BHATTACHARYYA );

    cout << "autocorrelation of his1: "<< autoCorrelation1 << endl;
    cout << "autocorrelation of hist2: "<< autoCorrelation2 << endl;
    cout << "correlation between hist1 and hist2: "<< autoCorrelation << endl;

    return correlation;
}

我觉得挺好的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-14 15:17:43

更好的方法是计算这两个Mat文件的特征向量的相关性,而不是直接对Mat数据进行特征向量计算。

例如,您可以首先计算每个Mat文件的RGB/HSV颜色直方图(如果每个通道使用8桶的话是24d向量),然后计算这两个直方图向量的相关性。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21117025

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档