2014-01-14 50 views
3

我已經獲得了一個Mat文件的向量,我想計算它們之間的相關性,以便保持與理論相似的兩個mat文件。實際上在這個向量中存儲的是從圖像中檢測到的眼睛,所以我試圖刪除異常值。如何計算兩個Mat文件之間的相關性?OpenCV中兩個Mat文件的計算關聯

編輯:

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

我覺得它工作正常。

+1

發佈您的代碼的相關部分,請 – kkuilla

回答

4

最好計算這兩個Mat文件的特徵向量的相關性,而不是直接計算Mat數據。例如,您可以首先計算每個Mat文件的RGB/HSV顏色直方圖(如果爲每個通道使用8分箱,則爲24d向量),然後計算這兩個直方圖向量的相關性。