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

如何用pydicom显示不含CT轮廓的图像及单独显示轮廓?

解决pydicom分别显示原始CT图像和单独轮廓的问题

看起来你现在的代码应该是不小心把轮廓叠加到原始图像上了,咱们分两步来实现你要的两个需求:

一、显示不含轮廓的原始CT图像

首先,咱们只需要读取CT的像素数据,直接渲染就行,别加任何轮廓绘制的代码。这里要注意CT图像需要调整窗宽窗位才能正确显示(不然会一片黑或者白),我给你整理了完整的代码片段:

import matplotlib.pyplot as plt
import pydicom

def show_raw_ct_image(dicom_path):
    # 读取目标CT DICOM文件
    ds = pydicom.dcmread(dicom_path)
    # 获取原始像素数组
    pixel_array = ds.pixel_array
    
    # 读取DICOM自带的窗宽窗位参数,也可以手动指定
    window_center = ds.WindowCenter
    window_width = ds.WindowWidth
    # 计算显示的像素值范围
    lower_bound = window_center - window_width / 2
    upper_bound = window_center + window_width / 2
    
    # 裁剪超出范围的像素值,避免显示异常
    processed_img = pixel_array.copy()
    processed_img[processed_img < lower_bound] = lower_bound
    processed_img[processed_img > upper_bound] = upper_bound
    # 归一化到0-255范围,适配灰度显示
    processed_img = (processed_img - lower_bound) / (upper_bound - lower_bound) * 255
    
    # 绘制并显示图像
    plt.figure(figsize=(8,8))
    plt.imshow(processed_img, cmap='gray')
    plt.axis('off')  # 隐藏坐标轴
    plt.title("Raw CT Image (No Contours)")
    plt.show()

# 替换为你的CT文件路径
show_raw_ct_image("./Dicoms/your_ct_file.dcm")

这段代码只会读取并渲染原始CT像素数据,完全不会包含任何轮廓元素。如果你的CT是序列文件(多帧DICOM),可以循环读取每个文件并依次显示。

二、单独显示CT轮廓(无原始图像)

CT轮廓一般存储在**RTSTRUCT(结构集)**类型的DICOM文件中,咱们需要读取这个文件提取轮廓坐标,然后在空白画布上绘制出来。代码如下:

import matplotlib.pyplot as plt
import pydicom

def show_ct_contours_only(rtstruct_path):
    # 读取RTSTRUCT文件
    rt_ds = pydicom.dcmread(rtstruct_path)
    
    # 创建空白画布,尺寸和对应CT图像一致(这里假设是512x512,可根据实际调整)
    plt.figure(figsize=(8,8))
    ax = plt.gca()
    ax.set_xlim(0, 512)
    ax.set_ylim(512, 0)  # DICOM坐标以左上角为原点,反转y轴适配常规图像显示逻辑
    plt.axis('off')
    plt.title("CT Contours Only")
    
    # 遍历所有ROI轮廓并绘制
    for roi in rt_ds.ROIContourSequence:
        for contour in roi.ContourSequence:
            # ContourData格式为[x1,y1,z1, x2,y2,z2,...],提取同一层的x、y坐标
            contour_data = contour.ContourData
            x_coords = contour_data[0::3]
            y_coords = contour_data[1::3]
            # 绘制轮廓线
            ax.plot(x_coords, y_coords, color='red', linewidth=2)
    
    plt.show()

# 替换为你的RTSTRUCT文件路径
show_ct_contours_only("./Dicoms/your_rtstruct_file.dcm")

额外注意事项:

  • 要区分CT图像文件和RTSTRUCT文件:CT文件是包含像素数据的序列文件,RTSTRUCT是单独存储轮廓标注的DICOM文件。
  • 如果你的轮廓是直接标记在CT像素数据里(比如人工标注后修改了像素值的图像),那需要通过阈值提取轮廓像素,再在空白画布上绘制这些像素点。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:57:14