title: Drawing angled rectangles in OpenCV
body: <p>I am using OpenCV and python to work on a project that involves body tracking, and I am using HSV values to find a skin tone then draw a box around it.</p>
  <p>However although I can find the tracked object and draw a box around it the rectangles are always vertical, and I would like to know if there is anyway angle the rectangles so they better show the detected object, somewhat like the minEnclosingCircle function, but using a rectangle</p>
  <p>The images probably explain what I am looking for better. The boxes that I am getting are green, and what I'm looking for I have drawn on in yellow. As you can see the mask shows and angled rectangle would also better encompass the selected area. I have also included the original image.</p>
  <p>My code is:</p>
  <pre class="lang-py prettyprint-override"><code>import numpy as np
  import cv2
  
  # Input image
  image = cv2.imread('TestIn.png')
  
  # Converts to grey for better reulsts
  gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  
  # Converts to HSV
  hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
  
  # HSV values
  lower_skin = np.array([5,36,53])
  upper_skin = np.array([19,120,125])
  
  mask = cv2.inRange(hsv, lower_skin, upper_skin)
  
  mask = cv2.erode(mask, None, iterations=2)
  mask = cv2.dilate(mask, None, iterations=2)
  
  # Finds contours
  im2, cnts, hierarchy = cv2.findContours(mask.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  
  # Draws contours
  for c in cnts:
      if cv2.contourArea(c) &lt; 3000:
          continue
  
      (x, y, w, h) = cv2.boundingRect(c)
      cv2.rectangle(image, (x,y), (x+w,y+h), (0, 255, 0), 2)
  
  cv2.imshow('mask', mask)
  cv2.imshow('image', image)
  cv2.waitKey(0)
  cv2.destroyAllWindows()
  </code></pre>
  
  <p>Input image:</p>
  <p><a href="https://i.stack.imgur.com/oaHUs.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oaHUs.jpg" alt="Input Image" /></a></p>
  <p>Output image (output boxes in green, desired boxes in yellow):</p>
  <p><a href="https://i.stack.imgur.com/5C482.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5C482.jpg" alt="output image. output boxes in green, desired boxes in yellow" /></a></p>