简体   繁体   English

OpenCV直方图匹配不正确的输出

[英]Opencv histogram matching incorrect output

I used two histograms . 我使用了两个直方图 There have different data values in two vectors .I used opencv histogram matching functions . 两个向量中的数据值不同。我使用了opencv直方图匹配函数 But it returns incorrect value. 但是它返回不正确的值。 (I used CV_COMP_CORREL but I'm receiving 1 . (that value for a perfect match.) I have no idea where I have mistaken. For your reference I have attached the code below. Thank you. (我使用了CV_COMP_CORREL我收到了1. (该值是一个完美的匹配。)我不知道自己在哪里弄错了。下面的代码供您参考,谢谢。

histo 1code : histo 1code:

Mat hitograme_one()
{
    vector<double>direction_vector_test;
    direction_vector_test.push_back(10.2);
    direction_vector_test.push_back(10.2);
    direction_vector_test.push_back(10.2);
    direction_vector_test.push_back(10.2);
    direction_vector_test.push_back(10.2);
    direction_vector_test.push_back(10.2);
    direction_vector_test.push_back(10.2);
    direction_vector_test.push_back(10.2);
    direction_vector_test.push_back(10.2);
    direction_vector_test.push_back(10.2);
    direction_vector_test.push_back(10.2);
    direction_vector_test.push_back(10.2);
    direction_vector_test.push_back(10.2);
    direction_vector_test.push_back(10.2);
    direction_vector_test.push_back(10.2);
    direction_vector_test.push_back(10.2);
    cv::Mat dummy_query = cv::Mat(4, 4, CV_32F, &direction_vector_test.front());
    Mat b_hist;
    float range[] = { 0, 151 };
    const float* histRange = { range };
    /// Establish the number of bins
    int histSize = 16;   // *IMPORATANT
    bool uniform = true; bool accumulate = false;
    int hist_w = 512; int hist_h = 400;
    int bin_w = cvRound((double)hist_w / histSize);

    calcHist(&dummy_query, 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate);

    return b_hist;
}

histo 2 code: histo 2代码:

Mat histo_two()
{
    vector<double>direction_vector_test;
    direction_vector_test.push_back(20.2);
    direction_vector_test.push_back(20.2);
    direction_vector_test.push_back(20.2);
    direction_vector_test.push_back(10.2);
    direction_vector_test.push_back(40.2);
    direction_vector_test.push_back(40.2);
    direction_vector_test.push_back(40.2);
    direction_vector_test.push_back(77.2);
    direction_vector_test.push_back(88.2);
    direction_vector_test.push_back(99.2);
    direction_vector_test.push_back(100.2);
    direction_vector_test.push_back(100.2);
    direction_vector_test.push_back(100.2);
    direction_vector_test.push_back(100.2);
    direction_vector_test.push_back(100.2);
    direction_vector_test.push_back(100.2);
    cv::Mat dummy_query = cv::Mat(4, 4, CV_32F, &direction_vector_test.front());
    Mat b_hist;
    float range[] = { 0, 151 };
    const float* histRange = { range };
    /// Establish the number of bins
    int histSize = 16;   // *IMPORATANT
    bool uniform = true; bool accumulate = false;
    int hist_w = 512; int hist_h = 400;
    int bin_w = cvRound((double)hist_w / histSize);

    calcHist(&dummy_query, 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate);
    return b_hist;

}

Main function code: ((which returns 1 which is incorrect)) 主要功能代码:((返回不正确的1))

int main(int, char** argv)
{
        double compare = compareHist(hitograme_one(), histo_two(), CV_COMP_CORREL);
        cout << compare; // returns 1 which is incorrect since input vectors //have different values
        system("pause");
    return 0;
}

Your problem is CV_32F 您的问题是CV_32F

cv::Mat dummy_query = cv::Mat(4, 4, CV_32F, &direction_vector_test.front());

Need to change CV_8UC1 需要更改CV_8UC1

cv::Mat dummy_query = cv::Mat(4, 4, CV_8UC1, &direction_vector_test.front());

CV_8U is unsigned 8bit/pixel - ie a pixel can have values 0-255, CV_32F is flat - the pixel can have any value between 0-1.0 so you're histogram range is populating to range from 0-1.0. CV_8U是无符号的8位/像素-即像素可以具有0-255的值,CV_32F是平坦的-像素可以具有0-1.0之间的任何值,因此您的直方图范围填充为0-1.0的范围。 Then two histogram images would be the same. 然后,两个直方图图像将是相同的。 So which returns the matching parameter. 因此,它返回匹配的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM