[英]How to concatenate 2 np.ndarray's?
I use openCV to generate a contour that looks like this: 我使用openCV生成看起来像这样的轮廓:
array([[[227, 762]],
[[228, 762]],
[[229, 763]],
[[228, 764]],
[[229, 765]],
[[228, 766]],
[[227, 766]],
[[228, 766]],
[[229, 765]],
[[228, 764]],
[[229, 763]],
[[229, 762]]], dtype=int32)
This contour has type np.ndarray 该轮廓的类型为np.ndarray
When I check cnt.shape, I get (12,1,2), indicating that this is more complex than a simple 12 x 2 matrix. 当我检查cnt.shape时,得到(12,1,2),表明它比简单的12 x 2矩阵复杂。
This means that when I try: 这意味着当我尝试:
In [104]: new = np.array([1,2])
In [105]: type(new)
Out[105]: numpy.ndarray
In [106]: X = np.vstack((ex,new))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-106-5ceb3fcd9619> in <module>()
----> 1 X = np.vstack((ex,new))
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/core/shape_base.pyc in vstack(tup)
226
227 """
--> 228 return _nx.concatenate([atleast_2d(_m) for _m in tup], 0)
229
230 def hstack(tup):
ValueError: all the input arrays must have same number of dimensions
You can see that new and ex are both of the same type and I was hoping that they both have 2 columns and thus I can stack them vertically, but alas, no.... How can I do this? 您可以看到new和ex都是相同的类型,我希望它们都具有2列,因此我可以垂直堆叠它们,但是,a,不,...我该怎么做?
To convert it to 12x2, simply: 要将其转换为12x2,只需:
yourarray.reshape(12, 2)
That gets me: 那让我:
array([[227, 762],
[228, 762],
[229, 763],
[228, 764],
[229, 765],
[228, 766],
[227, 766],
[228, 766],
[229, 765],
[228, 764],
[229, 763],
[229, 762]])
I recommend a brief readthrough of numpy's array
methods - there are lots of potentially useful stuff. 我建议对numpy的array
方法做一个简单的通读-有很多潜在有用的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.