title: template matching with multiple objects opencv python
body: <p>I have a scanned image document like this:
  <a href="https://i.stack.imgur.com/1MiB9.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1MiB9.jpg" alt="document sample"></a></p>
  
  <p>That image was scanned using an ordinary scan engine so it is possible for the document to be skewed. the image has been carried out a binarization process, so there is still a little noise. I want to know where the location of this template is in the picture.
  this is the template:</p>
  
  <p><a href="https://i.stack.imgur.com/VDnXH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VDnXH.png" alt="the template I&#39;m looking for"></a></p>
  
  <p>my expected result was the location coordinate of the template inside the image document, in array form like this:</p>
  
  <pre><code>[[35,1532], [1923,20], [1923,1532]]
  </code></pre>
  
  <p>I need clues if the results are correct like adding boxes around the template matched
  I've tried this code :</p>
  
  <pre class="lang-py prettyprint-override"><code>img = cv2.imread('image_document.jpg')
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  template = cv2.imread('template.jpg',0)
  
  # run template matching, get minimum val
  res = cv2.matchTemplate(gray, template, cv2.TM_SQDIFF_NORMED)
  min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
  
  # create threshold from min val, find where sqdiff is less than thresh
  min_thresh = (min_val) * 1.5
  match_locations = np.argwhere(res&lt;=min_thresh)
  
  # draw template match boxes
  w, h = template.shape[::-1]
  for (x, y) in zip(match_locations[1], match_locations[0]):
      cv2.rectangle(img, (x, y), (x+w, y+h), [0,255,255], 2)
  
  # display result
  cv2.imwrite('result.jpg', img)
  </code></pre>
  
  <p>but the actual result was the rectangle is too big and does not match with the template</p>