基本是看的这篇博客,稍微增删了一些东西
使用 pydicom.dcmread() 函数进行单张影像的读取,返回一个pydicom.dataset.FileDataset对象。
import os
import pydicom
# 调用本地的 dicom file
folder_path = r"D:\Data"
file_name = "PA1_0001.dcm"
file_path = os.path.join(folder_path,file_name)
ds = pydicom.dcmread(file_path)
print(ds.PatientID,ds.StudyDate,ds.Modality)
ds.get(0x00100020) # 这里得到的是PatientID
from matplotlib import pyplot
pyplot.imshow(ds.pixel_array,cmap=pyplot.cm.bone)
pyplot.show()
颜色是有区别的.导致这种差别的原因是pyplot函数使用的cm也就是"color map" 是简单的"bone" 并不能满足医学图像的要求。
这里ds.pixel_array很可能会报错,是本次读取的DICOM图像和一般的DICOM图像数据存储方式不同,而导致的pydicom无法按常规方式进行读取。
file = sitk.ReadImage(filepath)
data = sitk.GetArrayFromImage(file)
img = sitk.ReadImage(file_path)
print(type(img)) # <class 'SimpleITK.SimpleITK.Image'>
file_reader = sitk.ImageFileReader()
file_reader.SetFileName(file_path) #这里只显示了必需的,还有很多可以设置的参数
data = file_reader.Execute()
# 使用这种方法读取Dicom的Tag Value
for key in file_reader.GetMetaDataKeys():
print(key,file_reader.GetMetaData(key))
series_reader = sitk.ImageSeriesReader()
fileNames = series_reader.GetGDCMSeriesFileNames(folder_name)
series_reader.SetFileNames(fileNames)
images = series_reader.Execute()
data_32 = sitk.Cast(data,sitk.sitkFloat32)
data_edge_1 = sitk.CannyEdgeDetection(data_32,5,30,[5]*3,[0.8]*3)
Canny = sitk.CannyEdgeDetectionImageFilter()
Canny.SetLowerThreshold(5)
Canny.SetUpperThreshold(30)
Canny.SetVariance([5]*3)
Canny.SetMaximumError([0.5]*3)
data_edge_2 = Canny.Execute(data_32)
可视化的方法非常简单 只需要一条指令:
sitk.Show()
new_name = "new_MR_2.dcm"
sitk.WriteImage(img,os.path.join(folder_name,new_name))
file_writer = sitk.ImageFileWriter()
file_writer.SetFileName(os.path.join(folder_name,new_name))
file_writer.SetImageIO(imageio="GDCMImageIO")
file_writer.Execute(img)
使用这两种方法进行写入的时候,会发现,即便什么也没有做,但得到的新Dicom文件要小于原始的Dicom文件.这是因为新的Dicom文件中没有Private Creator信息
import pydicom
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import SimpleITK as sitk
import os
import scipy.misc
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
folder_path = r"C:\Users\Lenovo\Desktop\1"
file_name = "1.dcm"
file_path = os.path.join(folder_path, file_name) #合成文件路径
ds = pydicom.dcmread(file_path) #读取dicom文件
def getInformation():
data = []
name = ds.get("PatientName")
data.append(name)
ID = ds.get("PatientID")
data.append(ID)
age = ds.get("PatientAge")
data.append(age)
center = ds.get("WindowCenter")
data.append(center)
width = ds.get("WindowWidth")
data.append(width)
patientposition = ds.get(0x00200032)
patientposition = [patientposition[0], patientposition[1]]
data.append(patientposition)
return data
def imgshow(ds):
plt.imshow(ds.pixel_array, cmap=plt.cm.bone)
plt.show()
def getimgsArray(path):
file = sitk.ReadImage(path)
data = sitk.GetArrayFromImage(file)
return data
def getalltag(path): #提取所有tag
file_reader = sitk.ImageFileReader()
file_reader.SetFileName(path)
data = file_reader.Execute()
for key in file_reader.GetMetaDataKeys():
print(key, file_reader.GetMetaData(key))
def paddingcheck(data):
data_32 = sitk.Cast(data, sitk.sitkFloat32)
data_edge_1 = sitk.CannyEdgeDetection(data_32, 5, 30, [5] * 3, [0.8] * 3)
return data_edge_1