英文:
Reshaping (M, N, O) numpy array to point cloud
问题
我有一个3D的numpy数组(2000 x 2000 x 300),由300个二进制图像组成(像素值为0或1)。我想获得一个维度为P x 3的numpy数组,其中P是具有值1的像素的数量。
我想要这样做是为了能够处理每个长度为P的3个向量,其中包含像素的坐标(x、y、z),并能够绘制点云。
显然,我最初考虑使用3个for循环并将结果附加到列表中,但那将非常耗时。
英文:
I have a 3D numpy array (2000 x 2000 x 300) composed of 300 binary images (pixel values are either 0 or 1). I would like to get a numpy array of dimension P x 3, where P is however many pixels have value 1.
I want to do this to be able to handle each of the 3 vectors of length P holds the coordinates of our pixels (x, y, z) and be able to plot a point cloud.
Obviously I initially thought about using 3 for loops and appending to lists but that would take extremely long.
答案1
得分: 3
这是关于 np.argwhere
的目的:
import numpy as np
array = np.array(
[[[1,0,1],[0,0,1],[1,1,0]],
[[0,0,1],[0,0,0],[1,1,0]],
[[0,1,0],[0,0,0],[0,1,1]]]
)
args = np.argwhere(array)
print(args)
输出:
[[0 0 0]
[0 0 2]
[0 1 2]
[0 2 0]
[0 2 1]
[1 0 2]
[1 2 0]
[1 2 1]
[2 0 1]
[2 2 1]
[2 2 2]]
英文:
This is the purpose for np.argwhere
:
import numpy as np
array = np.array(
[[[1,0,1],[0,0,1],[1,1,0]],
[[0,0,1],[0,0,0],[1,1,0]],
[[0,1,0],[0,0,0],[0,1,1]]]
)
args = np.argwhere(array)
print(args)
Output:
[[0 0 0]
[0 0 2]
[0 1 2]
[0 2 0]
[0 2 1]
[1 0 2]
[1 2 0]
[1 2 1]
[2 0 1]
[2 2 1]
[2 2 2]]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论