gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 21:59:26 25 4
gpt4 key购买 nike

我有一个元组数组:

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

如何在 OpenCV 中绘制点与点之间的线?

这是我无法运行的代码:

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

最佳答案

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

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)

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

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)

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

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

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

关于python - 如何在 OpenCV 中的点之间画线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50671524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com