title: OpenCV : Fast Template matching algorithm
body: <p>I am currently trying to look for an image inside a video. The main goal is to follow some actions on the video like a pressed button or a pop-up window displayed on the screen.</p>
  
  <p>The code I'm using uses OpenCV Template Matching function:</p>
  
  <pre><code>    // For SQDIFF and SQDIFF_NORMED, the best matches are lower values. For all the other methods, the higher the better
      if( matchingMethod  == CV_TM_SQDIFF || matchingMethod == CV_TM_SQDIFF_NORMED )
          matchLoc = minLoc;
      else
          matchLoc = maxLoc;
  
      if( !((matchLoc.x == 0) &amp;&amp; (matchLoc.y == 0)) || maxVal &gt;= 0.8)
          return TRUE;
  
      return FALSE;
  }
  </code></pre>
  
  <p>The test is done with these two templates : </p>
  
  <p><a href="https://i.stack.imgur.com/Hj5e8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Hj5e8.png" alt="Template n°1"></a>
  <a href="https://i.stack.imgur.com/idpD4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/idpD4.png" alt="Template n°2"></a></p>
  
  <p>And the full image is a 3840x2160 image (i can't but the whole image since it is too big in bmp):</p>
  
  <p>1) The question is how is it possible that for two templates with very few pixels of difference the algorithm can detect the first one but completely skip the second one ?
  2) Is it possible that color depth could cause problems in the detection ? </p>
  
  <ul>
  <li>Both templates are loaded as BMP files in 24 bits depth. The source image is converted in 24 bits depth.</li>
  <li>Threshold is set to 0.92 for good accuracy</li>
  <li>MaxLevels is set to 1 for a very good accuracy since 2 does not find any matches</li>
  </ul>
  
  <p>Thank your for your help and advices</p>