我在 Halide 中进行模板匹配时遇到了另一个问题(已解决问题的原始链接:输出在模板匹配中移动)
现在我试图在得分最低的位置绘制一个矩形(表示最佳匹配)。
模板匹配部分:
Image<float> source = load_image("C:\\Users\\Admin\\Desktop\\templateMatchingOpenCV\\clip\\clip2.png");
Image<float> templ = load_image("C:\\Users\\Admin\\Desktop\\templateMatchingOpenCV\\clip\\object3.png");
Var x, y, xt, yt, x_outer, y_outer, x_inner, y_inner, tile_index;
RDom r(0, templ.width(), 0, templ.height());
Func limit, comparesqdiff, comparesqdiffnorm, compareccorr;
limit = BoundaryConditions::constant_exterior(source, 1.0f);
Expr function = sum(Halide::pow(templ(r.x, r.y) - limit(x + r.x, y + r.y), 2)) / (templ.width()*templ.height());
comparesqdiff(x, y) = sum(Halide::pow(templ(r.x, r.y) - limit(x + r.x, y + r.y), 2)) / (templ.width()*templ.height());
Image<float> outputsqdiff;
comparesqdiff.tile(x, y, x_outer, y_outer, x_inner, y_inner, 64,64).fuse(x_outer, y_outer, tile_index).vectorize(x_inner).unroll(y_inner).parallel(tile_index);
comparesqdiff.compile_jit();
现在我很清楚,我应该用来查找最低分数位置的函数是 argmin,但我不太明白它是如何使用的。另外我知道绘图方法将涵盖像素下方和右侧的所有内容,但我还没有到达那部分。
绘制矩形部分:
RDom wholeImage(0, source.width() - templ.width(), 0, source.height() - templ.height());
Tuple coords = argmin(r, function, "argmin");
Func show;
show(x, y) = select(x >= coords[0] && y >= coords[1] && x <= coords[0] + templ.width() && y <= coords[1] + templ.height(), 0, limit(x, y));
Image<float> test(source.width(), source.height());
test = show.realize(source.width(), source.height());
先感谢您。