title: How can I find the angle between two contours in Python+Opencv
body: <p>I'm trying to find the angle between the two contours in <a href="https://i.stack.imgur.com/Fw1Aa.jpg" rel="nofollow noreferrer">this</a> image. However the center point of the contour far right is twitching all over the place. I'm using this code:</p>
  
  <pre><code>if len(contours) &gt; 1:
      cnt1 = contours[0]
      cnt2 = contours[1]
      area = cv2.contourArea(cnt1)
      M1 = cv2.moments(cnt1)
      M2 = cv2.moments(cnt2)
      if area &gt; 30:
          cv2.drawContours(frame, contours, -1, (0, 255, 255), 3)
          X1_px = int(M1['m10'] / M1['m00'])
          Y1_px = int(M1['m01'] / M1['m00'])
          X2_px = int(M2['m10'] / M1['m00'])
          Y2_px = int(M2['m01'] / M1['m00'])
          cv2.circle(frame, (X1_px, Y1_px), 3, (0, 0, 255), -1)
          cv2.circle(frame, (X2_px, Y2_px), 3, (0, 0, 255), -1)
  
          if (X2_px-X1_px) != 0:
              angle = math.atan((Y2_px-Y1_px)/(X2_px-X1_px))
              angle = int(angle * (180 / math.pi))
              print(angle)
  </code></pre>
  
  <p>I think the cnt1, cnt2 part of my code is causing the problem, I don't know how openCV orders the contours in this array.</p>
  
  <p>Can anyone help me fix this code?</p>