国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python 讀取dicom文件,生成info.txt和raw文件的方法

2020-02-16 00:51:56
字體:
來源:轉載
供稿:網友

目標:利用python讀取dicom文件,并進行處理生成info.txt和raw文件

實現:通過pydicom讀取dicom文件

代碼:

import numpyimport pydicomimport os# dicom文件所在的文件夾目錄PathDicom = '/home/lk/testdata/1.3.6.1.4.1.9328.50.1.42697596859477567872763647333745089432/'# 篩選出文件夾目錄下所有的dicom文件lstFilesDCM = []for dirName, subdirList, fileList in os.walk(PathDicom):  for filename in fileList:    if '.dcm' in filename.lower():      lstFilesDCM.append(os.path.join(dirName, filename))# Get ref fileRefDs = pydicom.read_file(lstFilesDCM[0])# Load dimensions based on the number of rows, columns, and slices (along the Z axis)ConstPixelDims = (int(RefDs.Rows), int(RefDs.Columns), len(lstFilesDCM))# Load spacing values (in mm)ConstPixelSpacing = (float(RefDs.PixelSpacing[0]), float(RefDs.PixelSpacing[1]), float(RefDs.SliceThickness))# save info.txtinfo = ConstPixelDims + ConstPixelSpacingf = open('/home/lk/testdata/1.3.6.1.4.1.9328.50.1.42697596859477567872763647333745089432/info.txt', 'w')for n in info:  f.write(str(n)+' ')f.close()# According to location sortinglocation = []for i in range(len(lstFilesDCM)):  ds = pydicom.read_file(lstFilesDCM[i])  location.append(ds.SliceLocation)location.sort()# The array is sized based on 'ConstPixelDims'ArrayDicom = numpy.zeros((len(lstFilesDCM), RefDs.Rows, RefDs.Columns), dtype=RefDs.pixel_array.dtype)# loop through all the DICOM filesfor filenameDCM in lstFilesDCM:  # read the file  ds = pydicom.read_file(filenameDCM)  # store the raw image data  ArrayDicom[location.index(ds.SliceLocation), :, :] = ds.pixel_array# save rawds = ArrayDicom.tostring()f = open('/home/lk/testdata/1.3.6.1.4.1.9328.50.1.42697596859477567872763647333745089432/1.raw', 'wb')f.write(ds)f.close()

代碼編寫過程遇到的問題及解決方法:

Problem one: pydicom版本問題。

pydicom1.x中讀取dicom文件調用pydicom.read_file(filename);

pydicom0.9中讀取dicom文件調用dicom.read_file(filename);

Problem two:python中IO操作

(1) f = open(filename, mode)

其中filename為文件的路徑, mode為操作標識符:‘r' 表示讀, ‘w'表示寫,‘a'表示既可讀又可寫,‘b'表示二進制文件。

(2) f.write(value)

其中參數value必須是字符串類型的。

當然還有一些其他的問題,在這里就不細說了,多入坑才能學的多,切不可煩躁,代碼就是要多敲才能得心應手,共勉。

以上這篇python 讀取dicom文件,生成info.txt和raw文件的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林站長站。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 绥棱县| 彭泽县| 七台河市| 成都市| 双峰县| 永胜县| 揭阳市| 怀宁县| 绍兴市| 榕江县| 许昌市| 佳木斯市| 若羌县| 漳平市| 宁都县| 宾川县| 弥勒县| 开阳县| 马边| 大港区| 佛学| 综艺| 长宁县| 共和县| 普定县| 开化县| 延长县| 海阳市| 伊宁县| 乐安县| 临城县| 长岛县| 禄丰县| 且末县| 肥乡县| 宁陕县| 廉江市| 宜黄县| 塔城市| 巴东县| 鄄城县|