简体   繁体   English

无法使用opencv minAreaRect获取对象的边界

[英]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, 我正在尝试使用openCV在图像中的对象周围绘制最小边界矩形,

the cv2.minAreaRect is returning the rectangle of correct size but its orientation is off cv2.minAreaRect返回正确大小的矩形,但其方向已关闭

Following is my codeSnippet 以下是我的代码片段

The following screentshot shows the image i am working 以下屏幕截图显示了我正在处理的图像 在此处输入图片说明

In the next screenshot it shows the image with the detected border 在下一个屏幕截图中,它显示带有检测到的边框的图像 在此处输入图片说明

According to the opencv documentation linked here: https://docs.opencv.org/3.4/dd/d49/tutorial_py_contour_features.html this should work 根据此处链接的opencv文档: https : //docs.opencv.org/3.4/dd/d49/tutorial_py_contour_features.html,这应该可行

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). np.where()以正常数组索引(行,列)的顺序返回内容,但是OpenCV期望点以(x,y)顺序与(行,列)相反。 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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM