title: Detect centre and angle of rectangles in an image using Opencv
body: <p>I have an image as below :</p>
  
  <p><a href="https://i.stack.imgur.com/1jXgB.png" rel="noreferrer"><img src="https://i.stack.imgur.com/1jXgB.png" alt="Sample image containing many rectangle contours"></a></p>
  
  <p>I need to find out the number of rectangles,centre of each rectangle and the measure the angle between the axis parallel to the longer edge of the rectangle passing  through centre and measure the angle in anti­clockwise direction from the horizontal.I found out the number of rectangles in the image.I'm struck in finding out the centre and angle of reflection.Finding the centre through moments is not giving me the correct answer.</p>
  
  <p>My code :</p>
  
  <pre><code>import cv2
  import numpy as np 
  import sys
  
  img = cv2.imread(str(sys.argv[1]),0)
  ret,thresh = cv2.threshold(img,127,255,0)
  contours,hierarchy = cv2.findContours(thresh,1,2)
  
  
  
  for contour in contours:
      area = cv2.contourArea(contour)
      if area&gt;100000:
          contours.remove(contour)
  
  
  
  
  cnt = contours[0]
  
  epsilon = 0.02*cv2.arcLength(cnt,True)
  approx = cv2.approxPolyDP(cnt,epsilon,True)
  
  print 'No of rectangles',len(approx)
  
  
  #finding the centre of the contour
  M = cv2.moments(cnt)
  
  cx = int(M['m10']/M['m00'])
  cy = int(M['m01']/M['m00'])
  
  print cx,cy
  </code></pre>