有人可以向我解释这段代码的第二行是做什么的吗?
objp = np.zeros((48,3), np.float32)
objp[:,:2] = np.mgrid[0:8,0:6].T.reshape(-1,2)
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释代码的 np.mgrid[0:8,0:6] 部分到底在做什么以及代码的 T.reshape(-1,2) 部分到底在做什么吗?
谢谢,干得好!
查看这些的最简单方法是对 使用较小的值mgrid:
In [11]: np.mgrid[0:2,0:3]
Out[11]:
array([[[0, 0, 0],
[1, 1, 1]],
[[0, 1, 2],
[0, 1, 2]]])
In [12]: np.mgrid[0:2,0:3].T # (matrix) transpose
Out[12]:
array([[[0, 0],
[1, 0]],
[[0, 1],
[1, 1]],
[[0, 2],
[1, 2]]])
In [13]: np.mgrid[0:2,0:3].T.reshape(-1, 2) # reshape to an Nx2 matrix
Out[13]:
array([[0, 0],
[1, 0],
[0, 1],
[1, 1],
[0, 2],
[1, 2]])
Run Code Online (Sandbox Code Playgroud)
然后objp[:,:2] =将 的第 0 列和第 1 列objp设置为此结果。
第二行创建了一个多维网格 grid,对其进行转置,重新整形,使其代表两列并将其插入到 objp 数组的前两列中。
分解:
np.mgrid[0:8,0:6] 创建以下 mgrid:
>> np.mgrid[0:8,0:6]
array([[[0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1],
[2, 2, 2, 2, 2, 2],
[3, 3, 3, 3, 3, 3],
[4, 4, 4, 4, 4, 4],
[5, 5, 5, 5, 5, 5],
[6, 6, 6, 6, 6, 6],
[7, 7, 7, 7, 7, 7]],
[[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5]]])
Run Code Online (Sandbox Code Playgroud)
.T 转置矩阵,然后 .reshape(-1,2) 将其重塑为两个两列数组形状。这两列是替换原始数组中两列的正确形状。
| 归档时间: |
|
| 查看次数: |
10945 次 |
| 最近记录: |