Ask Your Question

thomasfedb's profile - activity

2018-02-14 01:14:14 -0600 received badge  Famous Question (source)
2017-06-25 06:25:11 -0600 received badge  Notable Question (source)
2017-03-22 15:33:51 -0600 received badge  Popular Question (source)
2015-07-28 05:48:02 -0600 commented question Using minAreaRect with contour in Python

@StevenPuttemans The output of cv2.minAreaRect() is ((x, y), (w, h), angle). Using cv2.cv.BoxPoints() is meant to convert this to points.

2015-07-27 12:17:29 -0600 received badge  Organizer (source)
2015-07-27 12:17:13 -0600 asked a question Using minAreaRect with contour in Python

I'm attempting to use OpenCV to identify and extract a fairly obvious region from an image. So far, by using a threshold and a series of dilations and erosions, I can successfully find the contour for the area I require.

However, my attempts to use minAreaRect as a precursor to rotation and cropping are failing to generate a rectangle that contains the input contour.

contours, hierarchy = cv2.findContours(morph.copy() ,cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contour = contours[0]

draw = cv2.cvtColor(morph, cv2.COLOR_GRAY2BGR)
cv2.drawContours(draw, [contour], 0, (0,255,0), 2)

rotrect = cv2.minAreaRect(contour)
box = cv2.cv.BoxPoints(rotrect)
box = numpy.int0(box)
cv2.drawContours(draw, [box], 0, (0,0,255), 2)

cv2.imshow('image', draw); cv2.waitKey(0)

Here's and example of the output:

Output

Where the red stroke is the rect and the green is the contour. I would have expected the red stroke to encompass the green stroke.

Unfortunately I'm unable to provide the input image.