简体   繁体   English

在给定大小为 (64,64,64) 的 numpy 数组的情况下显示 3D 图像,其中每个元素代表 Python 中 3d 空间中的强度 (0-1)(灰度)

[英]Display 3D image given a numpy array of size (64,64,64) where each element represents the intensity(0-1)(grayscale) in 3d space in Python

I am trying to do a 3D reconstruction project using GANs and trying to check my output #which is a 64,64,64 numpy array.我正在尝试使用 GAN 进行 3D 重建项目并尝试检查我的输出 #which 是一个 64,64,64 numpy 数组。 However I cant find a way to display the numpy array.但是我找不到显示 numpy 数组的方法。

Tried something like试过类似的东西

array = np.random.rand(64, 64,64)


plt.imshow(array)
plt.show()

Output输出

Invalid shape (10, 10, 10) for image data

3d plots are done with '3d' projection. 3d 图是用“3d”投影完成的。 You have several choices, but none, I think are perfect for such example.您有多种选择,但我认为没有一种是完美的示例。

You could plot 3d scatter plot.您可以绘制 3d 散点图。 Or voxels.或体素。

But in all cases, you have a very pragmatic problem: you can't see if there are things between your eye and an object但在所有情况下,你都会遇到一个非常实际的问题:你看不到你的眼睛和物体之间是否有东西

import matplotlib.pyplot as plt
import numpy as np

N=20
array = np.random.rand(N, N, N)

x=np.linspace(0,N-1,N)
y=np.linspace(0,N-1,N)
z=np.linspace(0,N-1,N)
gridx, gridy, gridz=np.meshgrid(x,y,z)
ax = plt.figure().add_subplot(projection='3d')
ax.scatter(gridx, gridy, gridz, c=array)
plt.show()

在此处输入图像描述

Note that with N=20 it is quite messy, so 64 is impossible (plus very heavy for even a good computer)请注意,N=20 时它非常混乱,因此 64 是不可能的(加上即使是一台好的计算机也非常重)

Also random voxels would hardly make any visual sense anyway.无论如何,随机体素也几乎没有任何视觉意义。

Another options is voxels.另一种选择是体素。 But voxels are array of booleans, which contains False for voxels to be drawn, and true for others.但是体素是布尔数组,对于要绘制的体素包含 False,对于其他体素包含 true。 You can add an optional color array您可以添加一个可选的颜色数组

import matplotlib.pyplot as plt
import numpy as np

N=20
array = np.random.rand(N, N, N)

ax = plt.figure().add_subplot(projection='3d')
# Just an array of r,g,b,a colors. With arbitrary choices on my behalf
colors=np.zeros((N,N,N,4))
colors[:,:,:,0] = 1-array
colors[:,:,:,1] = 2-np.clip(array, 0.5, 1)*2
colors[:,:,:,3] = array
ax.voxels(array>0.25, facecolors=colors)
plt.show()

在此处输入图像描述

If I replace pure random with a more simple shape, such as如果我用更简单的形状替换纯随机,例如

import matplotlib.pyplot as plt
import numpy as np

N=20
x=np.linspace(0,N-1,N)
y=np.linspace(0,N-1,N)
z=np.linspace(0,N-1,N)
x, y, z=np.meshgrid(x,y,z)
array=np.sqrt((x-10)**2+(y-10)**2+(z-10)**2)

ax = plt.figure().add_subplot(projection='3d')
# Colors, half red, half yellow, along x-axis
# half opaque, half semi-transparent,m along y-axis
colors=np.zeros((N,N,N,4))
colors[:,:,:,0] = 1
colors[:10,:,:,1] = 1
colors[:,:10,:,3]=1
colors[:,10:,:,3]=0.5
# Sphere
ax.voxels(array<8, facecolors=colors)
plt.show()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM