title: OpenCV C++ - Find an image contained within an image?
body: <p>I have code for searching one small image in bigger another one image:</p>
  
  <pre><code>int* MyLib::MatchingMethod(int, void*)
  {
      /// Source image to display
  
      img.copyTo(img_display);
  
      /// Create the result matrix
      int result_cols = img.cols - templ.cols + 1;
      int result_rows = img.rows - templ.rows + 1;
  
      result.create(result_rows, result_cols, CV_32FC1);
  
      match_method = 0;
  
      /// Do the Matching and Normalize
      matchTemplate(img, templ, result, match_method);
      normalize(result, result, 0, 1, cv::NORM_MINMAX, -1, cv::Mat());
  
      /// Localizing the best match with minMaxLoc
      double minVal;
      double maxVal;
      cv::Point minLoc;
      cv::Point maxLoc;
      cv::Point matchLoc;
  
      minMaxLoc(result, &amp;minVal, &amp;maxVal, &amp;minLoc, &amp;maxLoc, cv::Mat());
  
      /// For SQDIFF and SQDIFF_NORMED, the best matches are lower values. For all the other methods, the higher the better
      if (match_method == CV_TM_SQDIFF || match_method == CV_TM_SQDIFF_NORMED)
      {
          matchLoc = minLoc;
      }
      else
      {
          matchLoc = maxLoc;
      }
  
      if (showOpenCVWindow) {
          /// Show me what you got
          rectangle(img_display, matchLoc, cv::Point(matchLoc.x + templ.cols, matchLoc.y + templ.rows), cv::Scalar(255, 0, 0, 255), 2, 8, 0);
          rectangle(result, matchLoc, cv::Point(matchLoc.x + templ.cols, matchLoc.y + templ.rows), cv::Scalar(255, 0, 0, 255), 2, 8, 0);
  
          imshow(image_window, img_display);
          imshow(result_window, result);
      }
  
      double  myX = (matchLoc.x + (templ.cols) / 2);
      double  myY = (matchLoc.y + (templ.rows) / 2);
  
      static int o[2];
      o[0] = myX;
      o[1] = myY;
  
      return o;
  }
  </code></pre>
  
  <p>But this code could mistakenly "found" any area, even if bigger image doesn't contains small image.</p>
  
  <p>How to change this code, to force it to "exactly" searching of the small image. For example, if smaller image is not on the bigger image, this code must show any info message "Image not found".</p>
  
  <p><strong>Update 1.</strong> It looks, like <code>matchTemplate</code> doesn't work good. For example, I have 3 images - one template ( <a href="http://s6.postimg.org/nj2ts3lf5/image.png" rel="nofollow noreferrer">http://s6.postimg.org/nj2ts3lf5/image.png</a> ) , one image, that contains image from template ( <a href="http://s6.postimg.org/fp6tkg301/image.png" rel="nofollow noreferrer">http://s6.postimg.org/fp6tkg301/image.png</a> ), and one image, that doesn't contains template ( <a href="http://s6.postimg.org/9x23zk3sh/image.png" rel="nofollow noreferrer">http://s6.postimg.org/9x23zk3sh/image.png</a> ).</p>
  
  <p>For first image, that contains template, maxVal=0.99999994039535522 and it correctly selected area: <a href="http://s6.postimg.org/65x4qzfht/image.png" rel="nofollow noreferrer">http://s6.postimg.org/65x4qzfht/image.png</a></p>
  
  <p>But for image, that <strong>doesn't</strong> contains template, maxVal=<strong>1.0000000000000000</strong> and it <strong>incorrectly</strong> selected area, <strong>that doesn't contains template image</strong>: <a href="http://s6.postimg.org/5132llt0x/screenshot_544.png" rel="nofollow noreferrer">http://s6.postimg.org/5132llt0x/screenshot_544.png</a></p>
  
  <p>Thank you!</p>