Opencv直方图匹配错误输出

时间:2017-01-05 01:14:58

标签: c++ opencv histogram

我使用了两个直方图。在两个向量中有不同的数据值。我使用 opencv直方图匹配函数。但它返回不正确的值。 (我使用了CV_COMP_CORREL我收到了1 。(完全匹配的价值。)我不知道我在哪里弄错了。作为参考,我附上了下面的代码。谢谢。

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代码:

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;

}

主要功能代码:((返回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;
}

1 个答案:

答案 0 :(得分:0)

您的问题是 CV_32F

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

需要更改 CV_8UC1

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

CV_8U是无符号的8位/像素 - 即一个像素可以具有0-255的值,CV_32F是平的 - 像素可以具有0-1.0之间的任何值,因此你的直方图范围填充到0-1.0的范围内。然后两个直方图图像将是相同的。因此返回匹配参数。