OPV*_*OPV 2 python opencv opencv3.0 opencv-contour
我有一组元组:
a = [(375, 193)
(364, 113)
(277, 20)
(271, 16)
(52, 106)
(133, 266)
(289, 296)
(372, 282)]
Run Code Online (Sandbox Code Playgroud)
如何在 OpenCV 中的点之间画线?
这是我的代码不起作用:
for index, item in enumerate(a):
print (item[index])
#cv2.line(image, item[index], item[index + 1], [0, 255, 0], 2)
Run Code Online (Sandbox Code Playgroud)
Zev*_*Zev 12
使用绘制轮廓,您可以一次绘制所有形状。
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)
Run Code Online (Sandbox Code Playgroud)
如果您不想关闭图像并希望继续开始的方式:
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)
Run Code Online (Sandbox Code Playgroud)
关于您当前的代码,您似乎正在尝试通过索引当前点来访问下一个点。您需要检查原始数组中的下一个点。
执行第二个版本的更 Pythonic 的方法是:
for point1, point2 in zip(a, a[1:]):
cv2.line(image, point1, point2, [0, 255, 0], 2)
Run Code Online (Sandbox Code Playgroud)
如果你只是想画线,那么cv2.polylines怎么样?cv2.drawContours当您已经有一个轮廓对象时,将是首选。
cv2.polylines(image,
a,
isClosed = False,
color = (0,255,0),
thickness = 3,
linetype = cv2.LINE_AA)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10454 次 |
| 最近记录: |