使用Python从OpenCV中FindContours检测到的单个轮廓中提取坐标



在这里,我的代码将(x,y)存储在cv2.findcontours中的两个中,在Opencv Python中。

我选择了一个随机轮廓

c=contour[6]

现在,我希望在单独的数组中检测到的X A y值以执行一些操作

numpy阵列以这种方式存储。

[[[ 746  997]]
 [[ 744  998]]
 [[ 742  999]]
 [[ 740 1000]]]

我尝试使用它提取x值

x = c[:,[0]]

但是我得到了相同的数组。

所以我尝试使用此循环提取

    for a in c:
      for b in a:
        s_x = np.append(s_x, b[0])
        s_y = np.append(s_y, b[1])

有没有一种简单的方法来选择X坐标,而不是通过循环而根本没有此错误?

看来您的数组有一个额外的维度,因此您可以将其删除,然后索引应起作用。

x = c.squeeze()[:, 0]
cnt = cnts[1]             # choose one 
cnt = cnt.reshape(-1,2)   # change the shape 
xs  = cnt[:,0]            # get xs

您的数组具有三个维度。细节可以用形状属性看到。

import numpy as np
c = np.array([[[ 746,  997]],
              [[ 744,  998]],
              [[ 742,  999]],
              [[ 740, 1000]]])
print(c.shape)

(4,1,2)

具有形状后,切成阵列。

x = c[:,0,0]
print(x)

[746 744 742 740]

最新更新