opencv matchTemplate精度问题?

时间:2017-09-04 12:55:03

标签: opencv matchtemplate

任何人都可以帮我理解为什么matchTemplate在模板或图像为零的区域不提供零输出?

这是一个例子

void DumpMatToFile(const Mat &in, string filename)
{
    ostringstream os;
    os << in.size() << endl; 
    for (int y = 0; y < in.size().height; y++)
    {
        for (int x = 0; x < in.size().width; x++)
        {
            os << setw(12) << in.at<float>(y, x) << " ";
        }
        os << endl;
    }

    FILE *f = fopen(filename.c_str(), "wt");
    fwrite(os.str().c_str(), 1, strlen(os.str().c_str()), f);
    fclose(f);
}

void SetTo(Mat &o, float v)
{
    for (int y = 0; y < o.size().height; y++)
    {
        for (int x = 0; x < o.size().width; x++)
            o.at<float>(y, x) = v;
    }
}
void test(float k)
{
    Mat t(2, 2, CV_32FC1, Scalar(1.0f));
    t.at<float>(0, 0) = 1.0f;
    t.at<float>(0, 1) = 1.0f;
    t.at<float>(1, 0) = 1.0f;
    t.at<float>(1, 1) = 1.0f;
    Mat in1(10, 20, CV_32FC1, Scalar(0.0f));
    SetTo(in1, k);
    in1.at<float>(5, 4) = 2.0f;
    in1.at<float>(5, 5) = 2.0f;
    in1.at<float>(6, 4) = 2.0f;
    in1.at<float>(6, 5) = 2.0f;
    Mat res1, res2;
    matchTemplate(in1, t, res1, CV_TM_CCORR);
    DumpMatToFile(t, "c:/temp/t.txt");
    DumpMatToFile(in1, "c:/temp/in1.txt");
    DumpMatToFile(res1, "c:/temp/dump21.txt");
}

调用

test(1.0f);

结果如预期:

[19 x 9]
4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4
4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4 
4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4 
4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4 
4   4   4   5   6   5   4   4   4   4   4   4   4   4   4   4   4   4   4 
4   4   4   6   8   6   4   4   4   4   4   4   4   4   4   4   4   4   4 
4   4   4   5   6   5   4   4   4   4   4   4   4   4   4   4   4   4   4 
4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4 
4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4 

相反,请致电

test(0.0f);

我得到了

[19 x 9]
           0  4.21468e-08  5.96046e-08 -4.21468e-08 -1.19209e-07 -4.21468e-08  5.96046e-08            0            0            0            0            0            0            0            0            0            0            0            0 
 2.98023e-08 -2.10734e-08            0  2.10734e-08 -2.98023e-08  2.10734e-08            0            0            0            0            0            0            0            0            0            0            0            0            0 
 5.96046e-08  4.21468e-08            0 -4.21468e-08 -5.96046e-08 -4.21468e-08            0            0            0            0            0            0            0            0            0            0            0            0            0 
 1.01751e-07  4.21468e-08 -4.21468e-08 -4.21468e-08 -1.74578e-08 -4.21468e-08 -4.21468e-08            0            0            0            0            0            0            0            0            0            0            0            0 
           0            0            0            2            4            2            0            0            0            0            0            0            0            0            0            0            0            0            0 
           0            0            0            4            8            4            0            0            0            0            0            0            0            0            0            0            0            0            0 
           0            0            0            2            4            2            0            0            0            0            0            0            0            0            0            0            0            0            0 
           0            0            0            0            0            0            0            0            0            0            0            0            0            0            0            0            0            0            0 
           0            0            0            0            0            0            0            0            0            0            0            0            0            0            0            0            0            0            0 

那么为什么结果ccor的左上角区域不会变为零? 这种不精确性在实际案例中引入了不当行为。

由于

安德烈

0 个答案:

没有答案