title: output shifted in template matching
body: <p>I'm trying to create a template matching program that's using the following formula to determine the fit between the template and the image:</p>
  
  <p><a href="https://i.stack.imgur.com/Xfqgc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Xfqgc.png" alt="formula"></a></p>
  
  <p>my code is following:</p>
  
  <pre><code>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&lt;uint8_t&gt;(Halide::pow(t(0 + r.x, 0 + r.y) - limit(x + r.x, y + r.y),2));
  
  Halide::Image&lt;uint8_t&gt; output(input.width(),input.height());
  output = compare.realize(input.width(),input.height());
  </code></pre>
  
  <p>After executing the following code the result image is shifted like in the example:</p>
  
  <p>Original image: <a href="https://i.stack.imgur.com/58qsZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/58qsZ.png" alt="Original image"></a></p>
  
  <p>Template: <a href="https://i.stack.imgur.com/Pv6EE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Pv6EE.png" alt="Template"></a></p>
  
  <p>Result: <a href="https://i.stack.imgur.com/3Yeyu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3Yeyu.png" alt="Result"></a></p>
  
  <p>How can I prevent the image from shifting?</p>