Python NumPy数组到列表的转换
在OpenCV 3中,函数Python NumPy数组到列表的转换,python,python-3.x,opencv,Python,Python 3.x,Opencv,在OpenCV 3中,函数goodFeaturesToTrack返回以下形式的数组 [[[1, 2]] [[3, 4]] [[5, 6]] [[7, 8]]] 将该数组转换为Python列表后,我得到 [[[1, 2]], [[3, 4]], [[5, 6]], [[7, 8]]] 虽然这是一个列表,但如果您看到它比应该的多了一对括号,并且当我尝试通过[0][1]访问元素时,我会得到一个错误。为什么数组和列表具有这种形式?我应该如何修复它?因为您有一个3d阵列,其中一个元素位于第二个轴
goodFeaturesToTrack
返回以下形式的数组
[[[1, 2]]
[[3, 4]]
[[5, 6]]
[[7, 8]]]
将该数组转换为Python列表后,我得到
[[[1, 2]], [[3, 4]], [[5, 6]], [[7, 8]]]
虽然这是一个列表,但如果您看到它比应该的多了一对括号,并且当我尝试通过[0][1]访问元素时,我会得到一个错误。为什么数组和列表具有这种形式?我应该如何修复它?因为您有一个3d阵列,其中一个元素位于第二个轴:
In [26]: A = [[[1, 2]], [[3, 4]], [[5, 6]], [[7, 8]]]
In [27]: A[0]
Out[27]: [[1, 2]]
当您想通过A[0][1]
访问第二个项目时,它会引发一个索引器:
In [28]: A[0][1]
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-28-99c47bb3f368> in <module>()
----> 1 A[0][1]
IndexError: list index out of range
非常感谢。这正是我想要的。一个出于好奇的问题。为什么这个函数创建了一个3D数组?@Adam我想这就是
goodFeaturesToTrack
的功能,你可以在文档中找到更多细节。我会的。再次感谢您的详细回答。
In [21]: import numpy as np
In [22]: A = np.array([[[1, 2]], [[3, 4]], [[5, 6]], [[7, 8]]])
In [33]: A = np.squeeze(A)
In [34]: A
Out[34]:
array([[1, 2],
[3, 4],
[5, 6],
[7, 8]])