python - 如何在 OpenCV 中的点之间画线?
问题描述
我有一个元组数组:
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)
推荐阅读
- reporting-services - 亚马逊 MWS 报告 API 始终将请求报告更改为状态“已取消”
- node.js - 如何将字符串转换为 ObjectId 类型并在 $lookup 中用于 mongodb?
- xml - xsd 模式中的常量键
- c# - 仅在超过-1/1 时如何对 Vector3.normalized 进行标准化?
- c# - 发布到 IIS ASP.Net Core 导致“值不能为空”错误
- entity-framework - 如何使用 where 语句在两个表之间创建 Map?
- laravel - Laravel Route 给出 Object not found 错误
- python - 处理高度不平衡数据的正确方法 - 二进制分类
- javascript - 如何拆分javascript函数
- php - 如何在php中创建一个嵌套数组