从Python中获取OpenCV轮廓的坐标列表

问题描述 投票:0回答:3

我正在寻找一种从OpenCV生成的轮廓中检索坐标列表的方法。

例如,如果我有一个OpenCV轮廓:

cnt = [[[272 271]] 
       [[271 272]]
       [[270 272]]
       [[269 272]]
       [[268 272]]]

如何创建轮廓列表x坐标:

x = [273, 271, 270, 269, 268]
python-2.7 opencv
3个回答
1
投票

假设你有:

cnt = []
cnt.append([[272, 271]])
cnt.append([[271, 272]])
cnt.append([[270, 272]])
cnt.append([[269, 272]])
cnt.append([[268, 272]])

然后,要获得包含X值的列表,您可以使用:

xList = [x[0][0] for x in cnt] # --> [272, 271, 270, 269, 268]

看到这里工作:http://ideone.com/gWnhQ5


0
投票

从OpenCV文档:

contours是图像中所有轮廓的Python列表。每个轮廓都是(x,y)坐标的Numpy数组......

因此,您只需访问x坐标:

contour[:,0]

0
投票

我有同样的问题,我使用下面的代码解决了它:

x_waypoints = (x_y_array.flatten(1)[:length_of_contours2])
y_waypoints = (x_y_array.flatten(1)[length_of_contours2:])

您可以打印flatten(1)的结果,并进行检查。

© www.soinside.com 2019 - 2024. All rights reserved.