我是 3D 图像处理新手。我想知道如何用python查看dicom系列。我尝试使用 matplotlib 和 VTK。在 matplot 中,我无法像使用 volViewer 在 matlab 中查看那样查看体积。关于 VTK,我无法导入 VTKRAyCASt 来观看 3D。我使用的版本是8.2.0.
我正在使用 scipy.ndimages 进行处理
请为我的体积 dicom 文件推荐一些资源
您可以尝试ipyvolume https://github.com/maartenbreddels/ipyvolume进行交互式绘图,我发现它非常有用。 另外,您可以使用 matplotlib 通过使用行进立方体来绘制它们来获取表面网格,但速度相当慢:
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import numpy as np
from skimage import measure
def plot_3d(image, threshold=-300):
p = image.transpose(2,1,0)
verts, faces, normals, values = measure.marching_cubes_lewiner(p, threshold)
fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(111, projection='3d')
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_xlim(0, p.shape[0])
ax.set_ylim(0, p.shape[1])
ax.set_zlim(0, p.shape[2])
plt.show()
-300 HU 的阈值适合可视化胸部 CT 扫描,但如果您要使用 MRI(检查强度值分布)或二进制体积(阈值 =0),请更改它。
有一些可视化的例子:
使用 vtkplotter 你应该能够轻松做到这一点:
from vtkplotter import *
volume = load(mydicomdir) #returns a vtkVolume object
show(volume, bg='white')
安装:
pip install vtkplotter
此代码可以帮助您使用 VTK 库从 DICOM 数据创建 3D 模型!
import sys
import os
import itk
import vtk
# Define input and output directories directly as variables
dicom_directory = "Patient_Data"
output_image = "knee_3D.nrrd"
series_name = None # Series name is set to None by default
print("Start: Reading and writing images from the DICOM directory")
# Default to current directory
dirName = dicom_directory if dicom_directory else "."
print(f"Using DICOM directory: {dirName}")
PixelType = itk.ctype("signed short")
Dimension = 3
ImageType = itk.Image[PixelType, Dimension]
# Use GDCMSeriesFileNames to get the DICOM filenames
namesGenerator = itk.GDCMSeriesFileNames.New()
namesGenerator.SetUseSeriesDetails(True)
namesGenerator.AddSeriesRestriction("0008|0021")
namesGenerator.SetGlobalWarningDisplay(False)
namesGenerator.SetDirectory(dirName)
print("Searching for DICOM files...")
seriesUID = namesGenerator.GetSeriesUIDs()
if len(seriesUID) < 1:
print("No DICOMs in: " + dirName)
sys.exit(1)
print("The directory: " + dirName)
print("Contains the following DICOM Series: ")
for uid in seriesUID:
print(uid)
seriesFound = False
for uid in seriesUID:
seriesIdentifier = uid
if series_name:
seriesIdentifier = series_name
seriesFound = True
print("Reading: " + seriesIdentifier)
fileNames = namesGenerator.GetFileNames(seriesIdentifier)
# Read the DICOM series
print(f"Reading series: {seriesIdentifier}...")
reader = itk.ImageSeriesReader[ImageType].New()
dicomIO = itk.GDCMImageIO.New()
reader.SetImageIO(dicomIO)
reader.SetFileNames(fileNames)
reader.ForceOrthogonalDirectionOff()
# Write the 3D image
print("Writing the 3D image...")
writer = itk.ImageFileWriter[ImageType].New()
outFileName = os.path.join(dirName, seriesIdentifier + ".nrrd")
if output_image:
outFileName = output_image
writer.SetFileName(outFileName)
writer.UseCompressionOn()
writer.SetInput(reader.GetOutput())
print("Writing: " + outFileName)
writer.Update()
print("3D image successfully written.")
if seriesFound:
break