知行编程网知行编程网  2022-12-01 23:00 知行编程网 隐藏边栏  16 
文章评分 0 次,平均分 0.0
导语: 本文主要介绍了关于怎样用python加载dicom图片的相关知识,包括python 图片,以及python爬取js加载的数据这些编程知识,希望对大家有参考作用。

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

本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

知行编程网
知行编程网 关注:1    粉丝:1
这个人很懒,什么都没写
扫一扫二维码分享