title: How to template match a simple 2D shape in OpenCV?
body: <p>I want to detect all pawns on a simple 2D chessboard. The problem is, my code only detects pawns on dark squares. I need all white pawns detected. Here's my set up:</p>
  
  <p>Chessboard (<code>full_board.png</code>):
  <a href="https://i.stack.imgur.com/EKJhm.png" rel="noreferrer"><img src="https://i.stack.imgur.com/EKJhm.png" alt="enter image description here"></a></p>
  
  <p>Pawn (<code>wp.png</code>):
  <a href="https://i.stack.imgur.com/gpkrb.png" rel="noreferrer"><img src="https://i.stack.imgur.com/gpkrb.png" alt="enter image description here"></a></p>
  
  <p><strong>Expected output</strong> (detects all pawns):
  <a href="https://i.stack.imgur.com/gCCfz.png" rel="noreferrer"><img src="https://i.stack.imgur.com/gCCfz.png" alt="enter image description here"></a></p>
  
  <p><strong>Actual output</strong> (not all pawns are detected):
  <a href="https://i.stack.imgur.com/O6Gc3.png" rel="noreferrer"><img src="https://i.stack.imgur.com/O6Gc3.png" alt="enter image description here"></a></p>
  
  <p>Code:</p>
  
  <pre><code>import cv2
  import numpy as np
  import imutils
  
  def main():
      img = cv2.imread('full_board.png', 0)   
      # Piece templates:
      img_rgb = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
      img_gray = cv2.cvtColor(img_rgb,cv2.COLOR_BGR2GRAY)
  
      pawn_white_template = cv2.imread('wp.png', 0)
  
      cv2.imshow("Template", pawn_white_template)
      cv2.waitKey(0)
  
      w_pawn_white, h_pawn_white = pawn_white_template.shape[::-1]
  
      res_pawn_white = cv2.matchTemplate(img_gray,pawn_white_template,cv2.TM_CCOEFF_NORMED)
  
      threshhold = 0.6
      loc = np.where(res_pawn_white &gt;= threshhold)
  
      for pt in zip(*loc[::-1]):
          cv2.rectangle(img_rgb,pt,(pt[0]+w_pawn_white, pt[1]+h_pawn_white),(0,255,255),1)
  
      cv2.imshow('detected',img_rgb)
      cv2.waitKey(0)
      cv2.destroyAllWindows()
  </code></pre>
  
  <p>Changing threshold doesn't help me. I'm not sure why it is only detecting dark square pawns.
  Any ideas how to detect all pawns?</p>