Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

In OpenCV, after calling cv2.findContours, I'm given an array of contours.

contours, hierarchy = cv2.findContours(image, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

I want to use cv2.boundingRect to give me a rectangle that defines the contour, since the contour could be complex.

for contour in contours:
   boundRect = cv2.boundingRect(contour)

However, this gives me a BoundingRect object, which is of the form (x, y, width, height). Is there a standard way to convert this into a standard NumPy array with a helper function that is already provided, or do I need to construct this manually?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

Yes, you will have to construct such an array manually.

May be, you can do as follows :

>>> a = np.empty((0,4))
>>> for con in cont:
        rect = np.array(cv2.boundingRect(con)).reshape(1,4)
        a = np.append(a,rect,0)

In my case, final a had a shape of (166,4).

Or you can use any Numpy methods to do so.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.