【问题标题】:python and combining a multidimensional list (opencv)python并结合多维列表(opencv)
【发布时间】:2013-10-02 06:07:44
【问题描述】:

我对 python/opencv 有点陌生,我有点困惑。我想我的问题与opencv无关,只是python。所以我将在没有opencv的情况下解释它: 我有一个 3-dim 列表:

for contour in contours:
    contour = cv2.approxPolyDP(contour,10,True)
    print "--------------------------"
    print contour
    print "--------------------------"

我明白了:

--------------------------
[[[168 377]]

 [[250 404]]]
--------------------------
--------------------------
[[[332 153]]

 [[419 216]]]
--------------------------

但是,我真正想要的是:

--------------------------
[[[168 377]]

 [[250 404]]

 [[332 153]]

 [[419 216]]]
--------------------------

当我自己构建列表时,它也可以正常工作,它应该是这样的:

>>> np.array([[[168,377],[250,404],[332,153],[419,216]]])
array([[[168, 377],
        [250, 404],
        [332, 153],
        [419, 216]]])

我知道...尺寸不一样。我不知道为什么opencv可以处理这个!? (这些是cv2.findContours 的轮廓 任何人都知道如何重新排列这个列表?或者对此有用的文档。 谢谢和问候:)

【问题讨论】:

  • 它们是 numpy 数组,如果您使用 numpy 作为关键字或在 numpy 文档中搜索,您可以查找很多有用的东西

标签: python list opencv merge


【解决方案1】:
res = []
for contour in contours:
    contour = cv2.approxPolyDP(contour,10,True)
    print "--------------------------"
    print contour
    print "--------------------------"
    res.append(contour)
print np.vstack(res)

【讨论】:

  • 虽然发帖可能会解决问题,但最好提供一些解释。他们可以帮助未来的读者。
  • 感谢 Zaw!这解决了所描述的问题。但不幸的是,这不是我的“真正”问题;)
  • 要将这个新的组合数组用作连续轮廓,您必须将其用作cv2.drawContours(drawing,[np.vstack(combined)],0,color,1)
【解决方案2】:
for i in range(2):
    tempcnts = cv2.findContours(gray, cv2.RETR_EXTERNAL,
                            cv2.CHAIN_APPROX_SIMPLE)
    tempcnts = imutils.grab_contours(tempcnts)
    # tempcnts = cv2.approxPolyDP(tempcnts, 10, True)
    print("--------------------------")
    print(tempcnts)
    print("--------------------------")
    if i==0:
        cnts=tempcnts
    else:
        cnts[0]=np.append(cnts[0], tempcnts[0],0)
print("--------------------------")
print("--------------------------")
print("--------------------------")
print(cnts)
print("--------------------------")
print("--------------------------")
print("--------------------------")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-17
    • 2015-08-08
    • 1970-01-01
    • 1970-01-01
    • 2021-09-22
    • 1970-01-01
    • 2017-01-22
    • 2013-02-25
    相关资源
    最近更新 更多