1.原始的每个DCE序列中有多个dicom文件,经过matlab转换把一个序列换成一个3Dmat矩阵 2.现在的需求是把处理过的3Dmat矩阵还原成2Ddicom文件,并且保留原始dicom信息.
原始文件夹有多个DCE序列
每个DCE序列中有多个dicom文件
经过matlab转换把一个序列换成一个3Dmat矩阵
处理过的3Dmat矩阵
Python代码:还原成2Ddicom文件,并且保留原始dicom信息.
#导入需要的包
import numpy as np
import SimpleITK as sitk
import pydicom
import h5py
import os
import nibabel as nib
1.定义3Dmat矩阵转成2Ddicom文件的函数
def mat2dicom(folderPath):
count_study = 0
all_study = os.listdir(folderPath)
for every_study_idx in range(39,len(all_study)):#遍历所有的病历号
count_study +=1
tmp_DCEMR_path = os.path.join(folderPath,all_study[every_study_idx],'DCEMR')
tmp_MRnew_path = os.path.join(folderPath,all_study[every_study_idx],'MRnew')
for every_MRI in os.listdir(tmp_MRnew_path):#每个病历号下面可能有多次MRI
source_DCE_path = os.path.join(tmp_DCEMR_path,every_MRI)
tmp_mat_path = os.path.join(tmp_MRnew_path,every_MRI,'割完皮肤所有序列mat')
txtpath = os.path.join(tmp_MRnew_path,every_MRI,'原图所有序列mat','md.txt')
all_mat_path = os.listdir(tmp_mat_path)
print(tmp_mat_path,'----',len(all_mat_path),'----',txtpath)
for i in range(len(all_mat_path)-1):#读取每个增强序列对应的mat文件(3维矩阵)
final_mat_path = tmp_mat_path+'\DCE0000'+str(i+1)+'_SR.mat'
#print(final_mat_path,os.path.exists(final_mat_path)) #输出每一个mat文件的路径,及判断是否存在
#读取三维矩阵 mat文件
mat = h5py.File(final_mat_path,'r')#读mat文件
#print(mat.keys(),mat.values())#可以用keys方法查看cell的名字,可以用values方法查看各个cell的信息
# 可以用shape查看维度信息
#print(mat['Breast_region'].shape) #(11, 320, 320)
# 注意,这里看到的shape信息与你在matlab打开的不同 # 这里的矩阵是matlab打开时矩阵的转置 # 所以,我们需要将它转置回来
mat_t = np.transpose(mat['Breast_region'])
print(type(mat_t),mat_t.shape,mat_t.shape[2]) #<class 'numpy.ndarray'> (320, 320, 11) 11
#读取txt文件,获得肿瘤起始位置
f = open(txtpath,"r",encoding="utf-8")
les = f.readline().split(",")
begin , end , middle= int(les[0]),int(les[1]),int(les[2])
print(begin ,end,middle,type(begin),end-begin+1)
#定义存储dicom的路径
every_DCE_path = source_DCE_path + '\DCE0000'+str(i+1)
print(every_DCE_path)
#mat转成dicom后灭个序列存成的文件夹
save_dir = every_DCE_path.replace("Summary_Classification2","Cut_Skin").replace("DCEMR","MR")
print(save_dir)
if not os.path.exists(save_dir):
os.makedirs(save_dir)
for i in range(mat_t.shape[2]):
out = mat_t[:,:,i].astype('int16')#把数据转为无符号整型
InstanceNumber = begin+i
if InstanceNumber <10:
InstanceNumber = '0000'+str(InstanceNumber)
elif InstanceNumber <100:
InstanceNumber = '000'+str(InstanceNumber)
elif InstanceNumber <200:
InstanceNumber = '00'+str(InstanceNumber)
else:
print('Warning!!',InstanceNumber,"dicom数量大于200")
dicom_save_file_path = save_dir+'/'+InstanceNumber+'.dcm'#to do i range
if os.path.exists(dicom_save_file_path):
continue
sitk.WriteImage(sitk.GetImageFromArray(out),dicom_save_file_path)
######再读文件,修改dicom的tag信息使这些图片成为一个序列
read_dicom_path = every_DCE_path+'\\'+InstanceNumber+'.dcm' #每个dcm文件的具体路径
dcm = pydicom.read_file(read_dicom_path)
ds = pydicom.read_file(dicom_save_file_path)
#print(dcm)
print(dicom_save_file_path)
ds = modify_dicom(ds,dcm)
#保存
ds.save_as(dicom_save_file_path)
2.定义给dicom的tag赋值的函数
def modify_dicom(ds,dcm):
#PatientInfo
ds.PatientID = dcm.PatientID
ds.PatientName = dcm.PatientName
ds.PatientBirthDate = dcm.PatientBirthDate
ds.PatientSex = dcm.PatientSex
ds.PatientAge = dcm.PatientAge
ds.PatientWeight = dcm.PatientWeight
try:
ds.MagneticFieldStrength = dcm.MagneticFieldStrength
ds.Manufacturer = dcm.Manufacturer
#ds.InstitutionName = dcm.InstitutionName
except TypeError:
print("Error:没有InstitutionName、Manufacturer、InstitutionName tag")
#studyInfo
ds.StudyDate =dcm.StudyDate
ds.StudyTime = dcm.StudyTime
ds.StudyDescription = dcm.StudyDescription
ds.StudyInstanceUID = dcm.StudyInstanceUID
ds.StudyID = ds.StudyID
# seriesInfo
ds.SeriesInstanceUID = dcm.SeriesInstanceUID #修改Series Instance UID
ds.SeriesDescription = dcm.SeriesDescription
ds.Modality = dcm.Modality
ds.SeriesNumber = dcm.SeriesNumber
ds.InstanceNumber = dcm.InstanceNumber
ds.SeriesDate = dcm.SeriesDate
ds.SeriesTime = dcm.SeriesTime
ds.SliceThickness = dcm.SliceThickness
ds.SliceLocation = dcm.SliceLocation
ds.FrameOfReferenceUID = dcm.FrameOfReferenceUID
ds[0X0020, 0X0052].value = dcm[0X0020, 0X0052].value
#ImageInfo
ds.SOPClassUID = dcm.SOPClassUID
ds.SOPInstanceUID = dcm.SOPInstanceUID
#ds.ReferencedSOPInstanceUID =dcm.ReferencedSOPInstanceUID
#ds.ReferencedSOPClassUID =dcm.ReferencedSOPClassUID
return ds
3.定义路径,调用函数执行
if __name__=="__main__":
folderPath2 =r"G:\cut_SkinData_Copy\expectDCE_otherData2"
otherSeries_mat2dicom(folderPath2)
探讨:程序的整体思路是
1.先用h5py读入处理后的3Dmat矩阵
2.读入处理3Dmat矩阵时的txt文件,里面存放的是肿瘤起始位置,终止位置,最大径位置
3.根据begin , end , middle读取原始影像的dicom的tag信息
4.定义dicom存储路径,把每一维度的mat矩阵的数值信息存入dicom文件
5.把原始dicom的tag信息赋值给存入的dicom文件
这种做法程序执行效率比较低,而且需要对每一个tag信息进行赋值
但是目前还没找到更好的方法,哪位大佬有更好的方法,欢迎一起探讨学习
特别说明:本文为原创文章,参考或转发本文需注明本文链接,有问题请联系:nick.yu.jd@qq.com