重塑(M,N,O)的NumPy数组为点云。

huangapple go评论66阅读模式
英文:

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]]

huangapple
  • 本文由 发表于 2023年7月11日 04:50:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76657252.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定