简体   繁体   English

将旋转的矩形绘制成图像

[英]Drawing rotated rectangle into image

I am trying to draw rotatec rectangle using openCV and python so far my code: 到目前为止,我正在尝试使用openCV和python绘制rotatec矩形:

rect = cv2.minAreaRect(boxes[i][1:])
box = cv2.boxPoints(rect)
box = np.int0(box)
cv2.drawContours(image,[box],0,(0,0,255),2)

where boxes[i][1:] is array of coordinations [xmin,ymin,xmax,ymax] 其中boxes[i][1:]是协调数组[xmin,ymin,xmax,ymax]

and image is image to draw recrangle on. image是要绘制矩形的图像。

However this keeps throwing 但是,这不断抛出

   rect = cv2.minAreaRect(boxes[i][1:])
cv2.error: OpenCV(3.4.1) C:\bld\opencv_1520732670222\work\opencv-3.4.1\modules\imgproc\src\convhull.cpp:137: error: (-215) total >= 0 && (depth == 5 || depth == 4) in function cv::convexHull 

Wha does it mean and how can i fix it? 这是什么意思,我该如何解决?

According to the doc of cv2.minAreaRect , the function requires a pointset as input. 根据cv2.minAreaRect文档 ,该函数需要一个点集作为输入。 So if you give a (xmin, ymin, xmax, ymax) tuple, it will be considered as a pointset composed of 2 single points (xmin, ymin) and (xmax, ymax) . 因此,如果给出一个(xmin, ymin, xmax, ymax)元组,它将被视为由2个单点(xmin, ymin)(xmax, ymax)组成的点集。 Thus finding the rotated rectangle with minimum area, will lead to a null-area rectangle (the line segment between the 2 points), which is likely to generate the error you obtain. 因此,找到具有最小面积的旋转矩形,将导致一个空区域矩形(两点之间的线段),这很可能会产生您获得的错误。 I'm not 100% confident with that answer, but I guess that it might be right. 我对这个答案不是100%自信,但是我想这可能是正确的。

So I suggest to replace [(xmin, ymin), (xmax, ymax)] by [(xmin, ymin), (xmin, ymax), (xmax, ymin), (xmax, ymax)] and test if it solves the problem... 因此,我建议将[(xmin, ymin), (xmax, ymax)]替换为[(xmin, ymin), (xmin, ymax), (xmax, ymin), (xmax, ymax)]并测试是否解决了问题...

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

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