OpenCV Python: Draw minAreaRect ( RotatedRect not implemented)
Here's a concrete example to draw the rotated rectangle. The idea is to obtain a binary image with Otsu's threshold then find contours using cv2.findContours()
. We can obtain the rotated rectangle using cv2.minAreaRect()
and the four corner vertices using cv2.boxPoints()
. To draw the rectangle we can use cv2.drawContours()
or cv2.polylines()
.
Input ->
Output
Code
import cv2
import numpy as np
# Load image, convert to grayscale, Otsu's threshold for binary image
image = cv2.imread('1.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# Find contours, find rotated rectangle, obtain four verticies, and draw
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
rect = cv2.minAreaRect(cnts[0])
box = np.int0(cv2.boxPoints(rect))
cv2.drawContours(image, [box], 0, (36,255,12), 3) # OR
# cv2.polylines(image, [box], True, (36,255,12), 3)
cv2.imshow('image', image)
cv2.waitKey()
rect = cv2.minAreaRect(cnt)
box = cv2.cv.BoxPoints(rect) # cv2.boxPoints(rect) for OpenCV 3.x
box = np.int0(box)
cv2.drawContours(im,[box],0,(0,0,255),2)
should do the trick.
sources:
1) http://opencvpython.blogspot.in/2012/06/contours-2-brotherhood.html
2) Python OpenCV Box2D