title: Using OpenCV to extract an exact region from a scanned paper?
body: <p>We basically have designed a template on paper and need OpenCV to extract an exact region from a scanned A4 size sheet (Say a 5cmx5cm region at   the point (5cm,7cm) ). </p>
  
  <p>Right now I've placed 4 objects (4 hexagons) at the 4 corners and used the template matching code (found <a href="http://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_imgproc/py_template_matching/py_template_matching.html" rel="nofollow noreferrer">here</a>) to try to get the locations of all 4 hexagons and then use that as my guide to navigate the page. But the template matching code returns quite a few points (very close to each other). </p>
  
  <p>So is this the right way to go about it? Is there (and should I use) any algorithms to find the "mean" of these points or is there a <em>better</em> method to go about this? </p>
  
  <pre><code>import cv2
  import numpy as np
  from matplotlib import pyplot as plt
  
  img_rgb = cv2.imread('Template.jpg')
  img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
  template = cv2.imread('hex.png',0)
  w, h = template.shape[::-1]
  
  res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
  threshold = 0.8
  loc = np.where( res &gt;= threshold)
  for pt in zip(*loc[::-1]):
      cv2.circle(img_rgb, pt, 1, (0,0,255))
  
  cv2.imwrite('res.png',img_rgb)
  </code></pre>
  
  <p>Here are the images: <a href="https://i.stack.imgur.com/2Jdym.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2Jdym.jpg" alt="Basic Template"></a>
  <a href="https://i.stack.imgur.com/CYxAc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CYxAc.png" alt="Image to be found"></a></p>
  
  <p>Results:
  <a href="https://i.stack.imgur.com/b11Dx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/b11Dx.png" alt="enter image description here"></a></p>