cv2.cv.BoxPoints(rect) 返回什么? [英] What does cv2.cv.BoxPoints(rect) return?

查看:181
本文介绍了cv2.cv.BoxPoints(rect) 返回什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

rect = cv2.minAreaRect(largest_contour)
rect = ((rect[0][0] * self.scale_down, rect[0][1] * self.scale_down), (rect[1][0] * self.scale_down, rect[1][1] * self.scale_down), rect[2])
box = cv2.cv.BoxPoints(rect)
print box 
box = np.int0(box)
cv2.drawContours(frame,[box], 0, (0, 0, 255), 2)

这就是我的代码的样子.我试着打印盒子看看那是什么,我得到了一些打印结果,比如 ((200.0, 472.0), (200.0, 228.0), (420.0, 228.0), (420.0, 472.0)).它应该与 x 和 y 坐标有关吗?我猜那是矩形的四个角?那么它们究竟是什么呢?谢谢!

This is how my code looks like. I tried to print the box to see what's that and I got some printing result such as ((200.0, 472.0), (200.0, 228.0), (420.0, 228.0), (420.0, 472.0)). It should have something to do x and y coordinates right? I guess that's the four corners of the rectangle? So what are they exactly? Thanks!

推荐答案

box"值的常见误解是box"ndarray 的第一个子列表总是矩形的左下角.例如,在下图所示的矩形中,box"ndarray 的第一个子列表不需要总是代表点 A.

The common misconception of the "box" values is that the first sub-list of the "box" ndarray is always the bottom-left point of the rectangle. For example, in the rectangle shown below, the first sub-list of "box" ndarray need not represent point A always.

这里是box"值代表的含义:

So here is what "box" values represent:

正如问题正确指出的那样,当您打印 box 时,您将得到一个如下所示的 ndarray:

As the question rightly points out, when you print box, you will get a ndarray that looks something like this:

然后我加倍努力进行描述并编写了这个简单的 for 循环来真正理解框"值实际代表什么:

And then I went an extra mile for description and wrote this simple for loop to really understand what "box" values actually represent:

for i in box:
    cv2.circle(image,(i[0],i[1]), 3, (0,255,0), -1)
    imgplot = plt.imshow(image)
    plt.show()

结果是:(图片按顺序)

And the results are: (the images are in order)

我认为这些图像应该消除了任何人对框"值的怀疑,但无论如何这里是一个摘要:

I think the images should have cleared anybody's doubt about "box" values, but here is a summary anyway:

矩形的最低点(左右无关)将始终是box"ndarray 的第一个子列表.所以在我给出的例子中,第一个子列表 [169 144] 代表这个矩形的右下角".现在这个点将成为决定下一个子列表代表什么的参考点.这意味着,下一个子列表将始终代表您沿顺时针方向移动时首先获得的点.(如for循环的第二张图片所示)

The lowest point of the rectangle(does not matter left or right) will always be the first sub-list of the "box" ndarray. So in the example I have given, the first sub-list [169 144] represents the "bottom right of this rectangle". Now this point will be the reference point to decide what the next sub-list represents. Meaning, the next sub-list will always represent the point that you first get when you move in the clockwise direction. (as shown in the second image of the for loop)

继续顺时针方向移动,看看下一个子列表代表什么.

And keep moving in the clockwise direction to see what the next sub-lists represent.

PS:有时很难阅读 OpenCV 文档(顺便说一句,这不是世界上最好的文档)并正确理解函数及其返回值.所以我建议搅动一小段代码,比如上面的 for 循环和 cv2.circle,来真正可视化函数的返回值.这应该真正消除您对 OpenCV 中遇到的任何函数的所有疑虑.毕竟,OpenCV 是关于可视化"的!

PS: It is sometimes very hard to read the OpenCV documentation(which is not the best in the world btw) and understand a function and its return values properly. So I suggest churn up little chunks of code, like the for loop and cv2.circle above, to really visualize the return values of a function. That should really clear all your doubts about any functions that you come across in OpenCV. After all, OpenCV is all about "visual"izing!

这篇关于cv2.cv.BoxPoints(rect) 返回什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆