Suvrajyoti Chatterjee
Suvrajyoti Chatterjee

Reputation: 107

Unable to get the boundary of an object using opencv minAreaRect

I am trying to draw a minimum bounding rectangle around an object in an image using openCV,

the cv2.minAreaRect is returning the rectangle of correct size but its orientation is off

Following is my codeSnippet

The following screentshot shows the image i am working enter image description here

In the next screenshot it shows the image with the detected border enter image description here

According to the opencv documentation linked here: https://docs.opencv.org/3.4/dd/d49/tutorial_py_contour_features.html this should work

Upvotes: 1

Views: 196

Answers (1)

alkasm
alkasm

Reputation: 23032

np.where() returns things in the normal array index (row, column) order, but OpenCV expects points in (x, y) order, which is opposite to (row, column). This has the effect that you are flipping the points about the diagonal of the image.

Simply reverse the points by swapping the two columns. Better yet would just be to be more explicit with variables and not do everything on one line:

y, x = np.where(binary == 0)
coords = np.column_stack((x, y))

Upvotes: 2

Related Questions