1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| file_name = ["portalvein", "venoussystem", "venacava"] def read_dicom(path): lstFileDCM = [] for dirName, subdirList, fileList in os.walk(path): n = len(fileList) for i in range(n): filename = fileList[i]
idx = filename.split("_")[-1] filename = filename.replace(str(idx), str(i))
lstFileDCM.append(os.path.join(dirName, filename))
RefDs = pydicom.read_file(lstFileDCM[0]) ConstPixelDims = (int(RefDs.Rows), int(RefDs.Columns), len(lstFileDCM)) print("ConstPixelDims:", ConstPixelDims)
ConstPixelSpacing = (float(RefDs.PixelSpacing[0]), float(RefDs.PixelSpacing[1]), float(RefDs.SliceThickness))
Origin = RefDs.ImagePositionPatient
ArrayDicom = np.zeros(ConstPixelDims, dtype=RefDs.pixel_array.dtype)
i = 0 for filenameDCM in lstFileDCM: ds = pydicom.read_file(filenameDCM) ArrayDicom[:, :, lstFileDCM.index(filenameDCM)] = ds.pixel_array i += 1
ArrayDicom = np.transpose(ArrayDicom, (2, 0, 1)) return ArrayDicom,ConstPixelSpacing,Origin
|