【问题标题】:output shifted in template matching输出在模板匹配中移动
【发布时间】:2016-07-18 08:44:17
【问题描述】:

我正在尝试创建一个模板匹配程序,它使用以下公式来确定模板和图像之间的匹配度:

我的代码如下:

Halide::Var x, y, xt, yt;
Halide::RDom r(0, t.width(), 0, t.height());
Halide::Func limit, compare;
limit = Halide::BoundaryConditions::constant_exterior(input,255);
compare(x, y) = limit(x,y);
compare(x, y) = Halide::cast<uint8_t>(Halide::pow(t(0 + r.x, 0 + r.y) - limit(x + r.x, y + r.y),2));

Halide::Image<uint8_t> output(input.width(),input.height());
output = compare.realize(input.width(),input.height());

执行以下代码后,结果图像会像示例中一样移动:

原图:

模板:

结果:

如何防止图像移动?

【问题讨论】:

    标签: halide


    【解决方案1】:

    不确定 t 和 input 的类型是什么,因此以下内容可能会溢出,但我认为您想要类似的内容:

    Halide::Var x, y, xt, yt;
    Halide::RDom r(0, t.width(), 0, t.height());
    Halide::Func limit, compare;
    limit = Halide::BoundaryConditions::constant_exterior(input,255);
    compare(x, y) = limit(x,y);
    compare(x, y) = Halide::cast<uint8_t>(sum(Halide::pow(t(r.x, r.y) - limit(x + r.x - t.width()/2, y + r.y - t.height()/2),2))/(t.width()*t.height()));
    
    Halide::Image<uint8_t> output(input.width(),input.height());
    output = compare.realize(input.width(),input.height());
    

    【讨论】:

    • 您的解决方案确实有效(除了我必须删除最初的 compare(x,y) = limit(x,y) 部分)但您能解释一下为什么要将总和除以模板尺寸的乘积吗?
    【解决方案2】:

    没有总和。您只存储模板图像右下角像素的平方差。还有一些其他的东西,我评论了:

    Halide::Var x, y, xt, yt;
    Halide::RDom r(0, t.width(), 0, t.height());
    Halide::Func limit, compare;
    
    // There's no point comparing the template to pixels not in the input.
    // If you really wanted to do that, you should start at
    // -t.width(), -t.height() and wd, ht will be plus the template size
    // instead of minus.
    int wd = input.width () - t.width ();
    int ht = input.height() - t.height();
    
    // constant_exterior returns a Func.
    // We can copy all dimensions with an underscore.
    limit(_) = Halide::BoundaryConditions::constant_exterior(input,255)(_) / 255.f;
    
    Func tf;
    tf(_) = t(_) / 255.f;
    
    // Not necessary now and even so, should have been set to undef< uint8_t >().
    // compare(x, y) = limit(x,y);
    
    // Expr are basically cut and pasted to where they are used.
    Expr sq_dif = Halide::pow(tf(r.x, r.x) - limit(x + r.x, y + r.y), 2);
    Expr t_count = t.width() * t.height();
    Expr val = Halide::sum(sq_dif) / t_count;
    
    compare(x, y) = Halide::cast<uint8_t>(Halide::clamp(255 * val, 0, 255));
    
    // The size of output is set by realize().
    Halide::Image<uint8_t> output;
    output = compare.realize(wd, ht);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-02
      • 2012-06-17
      • 1970-01-01
      • 2016-10-07
      • 2012-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多