gpt4 book ai didi

numpy - Python - numpy mgrid 和 reshape

转载 作者:行者123 更新时间:2023-12-01 09:16:44 28 4
gpt4 key购买 nike

有人可以向我解释这段代码的第二行是做什么的吗?

objp = np.zeros((48,3), np.float32)
objp[:,:2] = np.mgrid[0:8,0:6].T.reshape(-1,2)

有人可以向我解释代码的 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]])

然后 objp[:,:2] =设置 objp 的第 0 和第 1 列到这个结果。

关于numpy - Python - numpy mgrid 和 reshape ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42308270/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com