You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python将多帧DICOM体数据可视化并获取Z-buffer信息

Great questions! Let's break them down one by one with practical code examples and explanations:

1. Visualizing 3D DICOM Volume as a Single Image

To turn your 160-slice DICOM volume into a single meaningful image, you’ll use volume projection techniques or multi-planar reformats—these are standard ways to condense 3D data into a 2D view. Here’s how to implement it:

Step 1: Load and sort all DICOM slices

First, load all files and sort them correctly (filenames don’t always match the actual slice order in the volume). We’ll use the InstanceNumber DICOM tag to ensure proper sequencing:

import pydicom as dicom
import os
import numpy as np
import matplotlib.pyplot as plt

# Path to your DICOM folder
dicom_dir = "./myfiles"

# Load all valid DICOM files
slices = []
for filename in os.listdir(dicom_dir):
    if filename.endswith(".dcm"):
        filepath = os.path.join(dicom_dir, filename)
        dataset = dicom.dcmread(filepath)
        slices.append(dataset)

# Sort slices by InstanceNumber (critical for correct volume alignment)
slices.sort(key=lambda x: x.InstanceNumber)

# Convert slices to a 3D numpy array (shape: [number of slices, height, width])
volume = np.stack([s.pixel_array for s in slices])

Step 2: Generate a single projection image

Choose a method based on what you want to highlight:

Option A: Maximum Intensity Projection (MIP)

MIP takes the highest pixel value across all slices for each (x,y) position—ideal for highlighting dense structures like bones or contrast-filled vessels:

# Compute MIP along the slice (z) axis
mip_image = np.max(volume, axis=0)

# Visualize
plt.imshow(mip_image, cmap=plt.cm.bone)
plt.title("Maximum Intensity Projection (MIP)")
plt.axis("off")
plt.show()

Option B: Average Intensity Projection

This averages pixel values across slices, great for soft tissue visualization:

avg_image = np.mean(volume, axis=0)
plt.imshow(avg_image, cmap=plt.cm.bone)
plt.title("Average Intensity Projection")
plt.axis("off")
plt.show()

Option C: Multi-Planar Reformat (MPR)

Extract a coronal or sagittal slice (instead of the default axial slices) to view the volume from a different angle:

# Coronal slice (middle of the height axis)
coronal_slice = volume[:, volume.shape[1]//2, :]
plt.imshow(coronal_slice, cmap=plt.cm.bone)
plt.title("Coronal MPR Slice")
plt.axis("off")
plt.show()

# Sagittal slice (middle of the width axis)
sagittal_slice = volume[:, :, volume.shape[2]//2]
plt.imshow(sagittal_slice, cmap=plt.cm.bone)
plt.title("Sagittal MPR Slice")
plt.axis("off")
plt.show()
2. Getting Z-Buffer (Depth) Information

DICOM volumes don’t have a built-in Z-buffer, but you can derive depth data using spatial metadata stored in each slice. Here’s how:

Key DICOM Metadata for Depth

Each slice contains the ImagePositionPatient tag, which defines the 3D coordinates (x, y, z) of the slice’s upper-left corner in the patient’s coordinate system. The third value in this tuple is the slice’s Z-axis depth.

Extract Depth Data

# Extract z-position for each slice
z_positions = np.array([s.ImagePositionPatient[2] for s in slices])

# Create a 3D depth volume that matches your pixel volume's shape
depth_volume = np.zeros_like(volume, dtype=np.float32)
for i, z in enumerate(z_positions):
    depth_volume[i, :, :] = z

Simulate Z-Buffer Behavior

If you want to map depth to your projection (e.g., track which slice contributes to each pixel in a MIP), you can pair intensity data with depth values:

# Get the index of the slice with the maximum intensity at each (x,y) position
max_intensity_slice_indices = np.argmax(volume, axis=0)
# Map those indices to actual z-positions
depth_map = z_positions[max_intensity_slice_indices]

# Visualize the depth map
plt.imshow(depth_map, cmap=plt.cm.viridis)
plt.title("Depth Map of MIP Peak Voxels")
plt.colorbar(label="Z Position (mm)")
plt.axis("off")
plt.show()

Notes

  • Ensure your DICOM files have valid ImagePositionPatient tags (most clinical scans do, but some test/research files may not).
  • Check the ImageOrientationPatient tag if you need to confirm the direction of the Z-axis (it defines the scan’s orientation in 3D space).

内容的提问来源于stack exchange,提问作者Anjan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:42:51