title: Template matching - cross correlation vs square diff - when choose one over the other?
body: <h1>TL;DR</h1>
  
  <p>When and why is cross correlation beneficial over square diff (when using template matching)?</p>
  
  <h1>Details</h1>
  
  <p>According to OpenCV's <a href="https://docs.opencv.org/3.2.0/de/da9/tutorial_template_matching.html" rel="nofollow noreferrer">documentation</a> of template matching (you have to scroll down a bit), square difference is defined as:</p>
  
  <p><a href="https://i.stack.imgur.com/yzwl9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yzwl9.png" alt="enter image description here"></a></p>
  
  <p>and cross correlation as:</p>
  
  <p><a href="https://i.stack.imgur.com/jjci1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jjci1.png" alt="enter image description here"></a></p>
  
  <p>(where <strong><em>T</em></strong> is the template and <strong><em>I</em></strong> the image)</p>
  
  <p>If I'm not mistaken, square diff is the only method (also compared to cross correlation coefficient) that is guaranteed to find the best match (in a numerical sense) in each image.</p>
  
  <p>If we take a look at cross correlation we notice that the multiplication yields higher results for brighter parts in the image (because bright pixels have a higher numerical value than dark pixels). This means, if we perform template matching with a dark template on a bright image we will most likely get a bad result when using cross correlation.</p>
  
  <p>For example, if we take this image:</p>
  
  <p><a href="https://i.stack.imgur.com/7LeNP.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7LeNP.jpg" alt="source image"></a></p>
  
  <p>and perfrom template matching with this template:</p>
  
  <p><a href="https://i.stack.imgur.com/S89KL.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/S89KL.jpg" alt="template"></a></p>
  
  <p>we get these results (red is cross correlation and green is square diff):</p>
  
  <p><a href="https://i.stack.imgur.com/ItD2R.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ItD2R.jpg" alt="result"></a></p>
  
  <p>Obviously, square diff is the better choice here as cross correlation yields a very bad result. The only advantage I can see in using cross correlation over square diff is the computational complexity as cross correlation should be a bit faster because it only has to calculate a product (instead of a sum and a square).</p>
  
  <p>My question is: are there any benefits that I don't see? When should I choose one method over the other? </p>