温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python加载dicom图片怎么实现

发布时间:2020-08-24 17:31:25 来源:亿速云 阅读:186 作者:Leah 栏目:编程语言

这篇文章运用简单易懂的例子给大家介绍python加载dicom图片怎么实现,代码非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

用python加载dicom图片的方法:使用pydicom、CV2、numpy、matplotlib等库即可。pydicom库是专门用来处理dicom图像的python专用库。

python加载dicom图片怎么实现

python读取DICOM图像,需要以下几个库:pydicom、CV2、numpy、matplotlib。pydicom是专门处理dicom图像的python专用包,numpy高效处理科学计算的包,依据数据绘图的库。

安装需要的库

pip install matplotlib
pip install opencv-python
pip install pydicom    
pip install 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加载dicom图片怎么实现就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI