Quantcast
Viewing all articles
Browse latest Browse all 41027

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][1]][1] 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. [1]: http://i.stack.imgur.com/GyAQs.png

Viewing all articles
Browse latest Browse all 41027

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>