Python 能否将 3D numpy 数组转换为 3D 图像?
Can a 3D numpy array be converted into a 3D image in Python?
我在 3D 阵列中制作了以下虚拟“房间”,并希望将其可视化。我找不到这样做的方法,请协助。这个想法是将阵列的“3D 图像”视为不同值具有不同颜色或灰度强度的图,以便您可以在“房间”内看到“患者”和“检测器”:
import numpy as np
# Diff. values in the Room define objects: 0 = walls, 1 = inside room, 2 = patient's tissue, 3 = bone, 4 = x-ray detector
Room = np.array([[[0.0 for i in range(0,101,1)] for j in range(0,101,1)] for k in range(0,101,1)]) #The entire room with walls
for i in range(1,100,1):
for j in range(1,100,1):
for k in range(1,100,1):
Room[i,j,k] +=1 # The room not counting the walls
for i in range(30,70,1):
for j in range(30,70,1):
for k in range(30,70,1):
Room[i,j,k] +=1 #The patient's body (tissue)
for i in range(50,55,1):
for j in range(50,55,1):
for k in range(50,55,1):
Room[i,j,k] +=1 #The patient's bone #1
for i in range(58,63,1):
for j in range(58,63,1):
for k in range(58,63,1):
Room[i,j,k] +=1 #The patient's bone #2
for i in range(88,92,1):
for j in range(10,90,1):
for k in range(10,90,1):
Room[i,j,k] +=1 # X-ray Detector
您可以借助 matplotlib 和 numpy 创建 3 维网格。这是此类情节的示例。您只想将 X、Y 和 Z 值作为列表输入
import numpy as np
import matplotlib.pyplot as plt
# Create figure and add axis
fig = plt.figure(figsize=(8,6))
ax = plt.subplot(111, projection='3d')
# Remove gray panes and axis grid
ax.xaxis.pane.fill = False
ax.xaxis.pane.set_edgecolor('white')
ax.yaxis.pane.fill = False
ax.yaxis.pane.set_edgecolor('white')
ax.zaxis.pane.fill = False
ax.zaxis.pane.set_edgecolor('white')
ax.grid(False)
# Remove z-axis
ax.w_zaxis.line.set_lw(0.)
ax.set_zticks([])
# Create meshgrid
X, Y = np.meshgrid(np.linspace(0, 2, len(afm_data)), np.linspace(0, 2, len(afm_data)))
# Plot surface
plot = ax.plot_surface(X=X, Y=Y, Z=Z, cmap='YlGnBu_r', vmin=0, vmax=200)
Towards Data Science 也有一篇关于此主题的文章:https://towardsdatascience.com/visualizing-three-dimensional-data-heatmaps-contours-and-3d-plots-with-python-bd718d1b42b4
只是 re-adding 这是因为第一个答案在我有机会回复之前被删除了:
您是否在寻找体积渲染方法?
有范围广泛的 Python 库具有可用的功能。
这是一个 example link.
对于您的具体情况,您可以使用例如plotly,我刚刚根据他们的教程为您的数据编写了一个快速脚本:
import numpy as np
import plotly.graph_objects as go
# Generate nicely looking random 3D-field
np.random.seed(0)
l = 5
X, Y, Z = np.mgrid[:l, :l, :l]
vol = np.zeros((l, l, l))
#pts = (l * np.random.rand(3, 15)).astype(np.int)
# Diff. values in the Room define objects: 0 = walls, 1 = inside room, 2 = patient's tissue, 3 = bone, 4 = x-ray detector
Room = np.array([[[0.0 for i in range(0,102,1)] for j in range(0,102,1)] for k in range(0,102,1)]) #The entire room with walls
for i in range(1,100,1):
for j in range(1,100,1):
for k in range(1,100,1):
Room[i,j,k] +=1 # The room not counting the walls
for i in range(30,70,1):
for j in range(30,70,1):
for k in range(30,70,1):
Room[i,j,k] +=1 #The patient's body (tissue)
for i in range(50,55,1):
for j in range(50,55,1):
for k in range(50,55,1):
Room[i,j,k] +=1 #The patient's bone #1
for i in range(58,63,1):
for j in range(58,63,1):
for k in range(58,63,1):
Room[i,j,k] +=1 #The patient's bone #2
for i in range(88,92,1):
for j in range(10,90,1):
for k in range(10,90,1):
Room[i,j,k] +=1 # X-ray Detector
pts = Room.reshape(3,353736).astype(np.int)
print( tuple(indices for indices in pts) )
vol[tuple(indices for indices in pts)] = 1
from scipy import ndimage
vol = ndimage.gaussian_filter(vol, 0.5)
vol /= vol.max()
fig = go.Figure(data=go.Volume(
x=X.flatten(), y=Y.flatten(), z=Z.flatten(),
value=vol.flatten(),
isomin=0.2,
isomax=0.7,
opacity=0.1,
surface_count=25,
))
fig.update_layout(scene_xaxis_showticklabels=False,
scene_yaxis_showticklabels=False,
scene_zaxis_showticklabels=False)
fig.show()
输出如下。不知道这是不是你想要的。
我在 3D 阵列中制作了以下虚拟“房间”,并希望将其可视化。我找不到这样做的方法,请协助。这个想法是将阵列的“3D 图像”视为不同值具有不同颜色或灰度强度的图,以便您可以在“房间”内看到“患者”和“检测器”:
import numpy as np
# Diff. values in the Room define objects: 0 = walls, 1 = inside room, 2 = patient's tissue, 3 = bone, 4 = x-ray detector
Room = np.array([[[0.0 for i in range(0,101,1)] for j in range(0,101,1)] for k in range(0,101,1)]) #The entire room with walls
for i in range(1,100,1):
for j in range(1,100,1):
for k in range(1,100,1):
Room[i,j,k] +=1 # The room not counting the walls
for i in range(30,70,1):
for j in range(30,70,1):
for k in range(30,70,1):
Room[i,j,k] +=1 #The patient's body (tissue)
for i in range(50,55,1):
for j in range(50,55,1):
for k in range(50,55,1):
Room[i,j,k] +=1 #The patient's bone #1
for i in range(58,63,1):
for j in range(58,63,1):
for k in range(58,63,1):
Room[i,j,k] +=1 #The patient's bone #2
for i in range(88,92,1):
for j in range(10,90,1):
for k in range(10,90,1):
Room[i,j,k] +=1 # X-ray Detector
您可以借助 matplotlib 和 numpy 创建 3 维网格。这是此类情节的示例。您只想将 X、Y 和 Z 值作为列表输入
import numpy as np
import matplotlib.pyplot as plt
# Create figure and add axis
fig = plt.figure(figsize=(8,6))
ax = plt.subplot(111, projection='3d')
# Remove gray panes and axis grid
ax.xaxis.pane.fill = False
ax.xaxis.pane.set_edgecolor('white')
ax.yaxis.pane.fill = False
ax.yaxis.pane.set_edgecolor('white')
ax.zaxis.pane.fill = False
ax.zaxis.pane.set_edgecolor('white')
ax.grid(False)
# Remove z-axis
ax.w_zaxis.line.set_lw(0.)
ax.set_zticks([])
# Create meshgrid
X, Y = np.meshgrid(np.linspace(0, 2, len(afm_data)), np.linspace(0, 2, len(afm_data)))
# Plot surface
plot = ax.plot_surface(X=X, Y=Y, Z=Z, cmap='YlGnBu_r', vmin=0, vmax=200)
Towards Data Science 也有一篇关于此主题的文章:https://towardsdatascience.com/visualizing-three-dimensional-data-heatmaps-contours-and-3d-plots-with-python-bd718d1b42b4
只是 re-adding 这是因为第一个答案在我有机会回复之前被删除了:
您是否在寻找体积渲染方法? 有范围广泛的 Python 库具有可用的功能。 这是一个 example link.
对于您的具体情况,您可以使用例如plotly,我刚刚根据他们的教程为您的数据编写了一个快速脚本:
import numpy as np
import plotly.graph_objects as go
# Generate nicely looking random 3D-field
np.random.seed(0)
l = 5
X, Y, Z = np.mgrid[:l, :l, :l]
vol = np.zeros((l, l, l))
#pts = (l * np.random.rand(3, 15)).astype(np.int)
# Diff. values in the Room define objects: 0 = walls, 1 = inside room, 2 = patient's tissue, 3 = bone, 4 = x-ray detector
Room = np.array([[[0.0 for i in range(0,102,1)] for j in range(0,102,1)] for k in range(0,102,1)]) #The entire room with walls
for i in range(1,100,1):
for j in range(1,100,1):
for k in range(1,100,1):
Room[i,j,k] +=1 # The room not counting the walls
for i in range(30,70,1):
for j in range(30,70,1):
for k in range(30,70,1):
Room[i,j,k] +=1 #The patient's body (tissue)
for i in range(50,55,1):
for j in range(50,55,1):
for k in range(50,55,1):
Room[i,j,k] +=1 #The patient's bone #1
for i in range(58,63,1):
for j in range(58,63,1):
for k in range(58,63,1):
Room[i,j,k] +=1 #The patient's bone #2
for i in range(88,92,1):
for j in range(10,90,1):
for k in range(10,90,1):
Room[i,j,k] +=1 # X-ray Detector
pts = Room.reshape(3,353736).astype(np.int)
print( tuple(indices for indices in pts) )
vol[tuple(indices for indices in pts)] = 1
from scipy import ndimage
vol = ndimage.gaussian_filter(vol, 0.5)
vol /= vol.max()
fig = go.Figure(data=go.Volume(
x=X.flatten(), y=Y.flatten(), z=Z.flatten(),
value=vol.flatten(),
isomin=0.2,
isomax=0.7,
opacity=0.1,
surface_count=25,
))
fig.update_layout(scene_xaxis_showticklabels=False,
scene_yaxis_showticklabels=False,
scene_zaxis_showticklabels=False)
fig.show()
输出如下。不知道这是不是你想要的。