环境简介
语言是python,主要介绍可能用到的库
- Scipy
- ITK
- VTK
- Mayavi
- TVTK
- Matplotlib
方法
在尝试重建三维模型的过程中,查询了不同版本的方法,在这里记录一下。
方法一 Poly3DCollection+matplotlib
使用mpl_toolkits 的Poly3DCollection,其中使用的是marching_cubes算法。
使用matplotlib进行显示。
import numpy as np
import pandas as pd
import os
import scipy.ndimage
import matplotlib.pyplot as plt
from skimage import measure, morphology
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
def plot_3d(image, threshold=-300):
# Position the scan upright,
# so the head of the patient would be at the top facing the camera
p = image.transpose(2,1,0)
p = p[:,:,::-1]
verts, faces = measure.marching_cubes(p, threshold)
fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(111, projection='3d')
# Fancy indexing: `verts[faces]` to generate a collection of triangles
mesh = Poly3DCollection(verts[faces], alpha=0.1)
face_color = [0.5, 0.5, 1]
mesh.set_facecolor(face_color)
ax.add_collection3d(mesh)
ax.set_xlabel("x-axis")
ax.set_ylabel("y-axis")
ax.set_zlabel("z-axis")
ax.set_xlim(0, p.shape[0]) # a = 6 (times two for 2nd ellipsoid)
ax.set_ylim(0, p.shape[1]) # b = 10
ax.set_zlim(0, p.shape[2]) # c = 16
plt.show()
优点:轻量化可以嵌入ipython里
缺点:
- 不能旋转视图观察,可以使用poltly进行交互式显示。
- 显示效果差,smooth的效果差。可以自己造轮子对源数据进行插值。然而插值始终不是基于Isosurface的,所以显示效果不会太好。
方法二 VTK+ITK
自己造轮子,基于VTK进行重建显示。
import vtk
def main():
colors = vtk.vtkNamedColors()
fileName = get_program_parameters()
colors.SetColor("SkinColor", [255, 125, 64, 255])
colors.SetColor("BkgColor", [51, 77, 102, 255])
# Create the renderer, the render window, and the interactor. The renderer
# draws into the render win