Python读取DICOM图像完整指南:环境配置与代码实战

苏见山
苏见山
发布时间:2026-10-13| 3人浏览过
原创

本文详解利用Python读取DICOM医学影像的方法。涵盖pydicom、OpenCV、numpy及matplotlib库的安装配置,通过具体代码演示如何解析DICOM文件元数据、处理像素数组、进行阈值分割与形态学操作,并展示最终图像的可视化结果,帮助开发者快速掌握DICOM数据处理流程。

DICOM概述

DICOM 3.0标准图像由各类医学影像设备生成,是放射医疗、心血管成像及放射诊疗诊断设备(如X射线、CT、核磁共振、超声等)中的标准医学影像格式,同时在眼科、牙科等其他医学领域的应用也日益广泛。在现有的数万台医学成像设备中,DICOM是部署最为普遍的医疗信息标准之一,目前全球约有百亿张符合DICOM标准的医学图像投入临床使用。

面对看似复杂的图像文件,读取方法其实多样,但网络上缺乏系统性的操作指南。下文综合相关资料,结合Python 2.7环境,详细讲解如何读取及使用DICOM图像。

读取DICOM图像主要依赖以下Python库:pydicom、CV2(OpenCV)、numpy和matplotlib。其中,pydicom是专门用于处理DICOM图像的Python包,numpy用于高效处理科学计算数据,而matplotlib则是依据数据进行绘图的工具库。

安装依赖库:

pip install matplotlib
pip install opencv-python #opencv的安装,小度上基本都是要下载包,安装包后把包复制到某个文件夹下,
#后来我在https://pypi.python.org/pypi/opencv-python找到这种pip的安装方法,亲测可用
pip install pydicom
pip install numpy

通常情况下,安装pydicom时会自动连带安装numpy。

完成上述库的安装后,即可开始对DICOM文件进行操作。

具体实现代码如下:

#-*-coding:utf-8-*-
import cv2
import numpy
import dicom
from matplotlib import pyplot as plt

dcm = dicom.read_file("AT0001_100225002.DCM")
dcm.image = dcm.pixel_array * dcm.RescaleSlope + dcm.RescaleIntercept

slices = []
slices.append(dcm)
img = slices[ int(len(slices)/2) ].image.copy()
ret,img = cv2.threshold(img, 90,3071, cv2.THRESH_BINARY)
img = numpy.uint8(img)

im2, contours, _ = cv2.findContours(img,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
mask = numpy.zeros(img.shape, numpy.uint8)
for contour in contours:
 cv2.fillPoly(mask, [contour], 255)
img[(mask > 0)] = 255


kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(2,2))
img = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)


img2 = slices[ int(len(slices)/2) ].image.copy()
img2[(img == 0)] = -2000


plt.figure(figsize=(12, 12))
plt.subplot(131)
plt.imshow(slices[int(len(slices) / 2)].image, 'gray')
plt.title('Original')
plt.subplot(132)
plt.imshow(img, 'gray')
plt.title('Mask')
plt.subplot(133)
plt.imshow(img2, 'gray')
plt.title('Result')
plt.show()

DICOM图像文件中包含患者相关信息的字典,我们可以通过dir()函数查看DICOM文件包含的具体信息,并通过字典方式获取所需的特定值。

import dicom
import json
def loadFileInformation(filename):
 information = {}
 ds = dicom.read_file(filename)
 information['PatientID'] = ds.PatientID
 information['PatientName'] = ds.PatientName
 information['PatientBirthDate'] = ds.PatientBirthDate
 information['PatientSex'] = ds.PatientSex
 information['StudyID'] = ds.StudyID
 information['StudyDate'] = ds.StudyDate
 information['StudyTime'] = ds.StudyTime
 information['InstitutionName'] = ds.InstitutionName
 information['Manufacturer'] = ds.Manufacturer
 print dir(ds)
 print type(information)
 return information

a=loadFileInformation('AT0001_100225002.DCM')
print a

总结

以上就是本文的全部内容,希望这些内容能对您的学习或工作有所帮助。如有任何疑问,欢迎留言交流,感谢大家对本站的支持。

相关标签:

免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。

Python 相关产品和应用

全部下载
Selenium

Selenium

4.51.0 · Windows

用于浏览器自动化和 Web 测试的 Python 工具。

scikit-learn

scikit-learn

1.9.1 · Windows

常用 Python 机器学习算法与模型评估工具库。

Black

Black

26.10.1 · Windows

强调一致性的 Python 自动代码格式化工具。

Ruff

Ruff

0.17.1 · macOS

由 Rust 编写的高速 Python 代码检查和格式化工具。

pytest

pytest

9.1.1 · Windows

简洁而强大的 Python 自动化测试框架。

FastAPI

FastAPI

0.146.0 · Windows

高性能 Python API 框架,支持类型提示和自动接口文档。

Flask

Flask

3.1.3 · Windows

轻量灵活的 Python Web 应用框架。

Django

Django

6.1.2 · Windows

成熟的 Python Web 框架,适合构建内容站点和业务系统。

Spyder

Spyder

6.1.7 · Windows

面向科学计算与数据分析的 Python 集成开发环境。

SciPy

SciPy

1.18.1 · Windows

面向科学、工程和技术计算的 Python 算法库。