如何在 OpenCV 中的点之间画线? [英] How to draw lines between points in OpenCV?

查看:145
本文介绍了如何在 OpenCV 中的点之间画线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个元组数组:

a = [(375, 193)
(364, 113)
(277, 20)
(271, 16)
(52, 106)
(133, 266)
(289, 296)
(372, 282)]

如何在 OpenCV 中的点之间画线?

How to draw lines between points in OpenCV?

这是我的代码不起作用:

Here is my code that isn't working:

for index, item in enumerate(a): 
    print (item[index]) 
    #cv2.line(image, item[index], item[index + 1], [0, 255, 0], 2) 

推荐答案

使用绘制轮廓,您可以一次绘制所有形状.

Using draw contours, you can draw the shape all at once.

img = np.zeros([512, 512, 3],np.uint8)
a = np.array([(375, 193), (364, 113), (277, 20), (271, 16), (52, 106), (133, 266), (289, 296), (372, 282)])
cv2.drawContours(img, [a], 0, (255,255,255), 2)

如果您不想关闭图像并想继续您的开始方式:

If you don't want the image closed and want to continue how you started:

image = np.zeros([512, 512, 3],np.uint8)
pointsInside = [(375, 193), (364, 113), (277, 20), (271, 16), (52, 106), (133, 266), (289, 296), (372, 282)]

for index, item in enumerate(pointsInside): 
    if index == len(pointsInside) -1:
        break
    cv2.line(image, item, pointsInside[index + 1], [0, 255, 0], 2) 

关于您当前的代码,您似乎正在尝试通过索引当前点来访问下一个点.您需要检查原始数组中的下一个点.

Regarding your current code, it looks like you are trying to access the next point by indexing the current point. You need to check for the next point in the original array.

第二个版本的更Pythonic 方法是:

A more Pythonic way of doing the second version would be:

for point1, point2 in zip(a, a[1:]): 
    cv2.line(image, point1, point2, [0, 255, 0], 2) 

这篇关于如何在 OpenCV 中的点之间画线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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