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


Posted in Python onJanuary 24, 2019

目标:利用python读取dicom文件,并进行处理生成info.txt和raw文件

实现:通过pydicom读取dicom文件

代码:

import numpy
import pydicom
import 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 file
RefDs = 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.txt
info = ConstPixelDims + ConstPixelSpacing
f = 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 sorting
location = []
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 files
for 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 raw
ds = 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文件的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python发送邮件示例(支持中文邮件标题)
Feb 16 Python
在Heroku云平台上部署Python的Django框架的教程
Apr 20 Python
Python简单检测文本类型的2种方法【基于文件头及cchardet库】
Sep 18 Python
深入理解Python中range和xrange的区别
Nov 26 Python
python存储16bit和32bit图像的实例
Dec 05 Python
Python 正则表达式匹配字符串中的http链接方法
Dec 25 Python
python交换两个变量的值方法
Jan 12 Python
Python读取Pickle文件信息并计算与当前时间间隔的方法分析
Jan 30 Python
对PyQt5基本窗口控件 QMainWindow的使用详解
Jun 19 Python
django组合搜索实现过程详解(附代码)
Aug 06 Python
Python使用pymysql模块操作mysql增删改查实例分析
Dec 19 Python
Python opencv缺陷检测的实现及问题解决
Apr 24 Python
Python可视化mhd格式和raw格式的医学图像并保存的方法
Jan 24 #Python
python Selenium实现付费音乐批量下载的实现方法
Jan 24 #Python
在python下读取并展示raw格式的图片实例
Jan 24 #Python
Python字典的核心底层原理讲解
Jan 24 #Python
使用PIL(Python-Imaging)反转图像的颜色方法
Jan 24 #Python
Python3实现取图片中特定的像素替换指定的颜色示例
Jan 24 #Python
python 实现图片旋转 上下左右 180度旋转的示例
Jan 24 #Python
You might like
php出现内存位置访问无效错误问题解决方法
2014/08/16 PHP
PHP序列化/对象注入漏洞分析
2016/04/18 PHP
PHP判断JSON对象是否存在的方法(推荐)
2016/07/06 PHP
PHP7 其他修改
2021/03/09 PHP
被jQuery折腾得半死,揭秘为何jQuery为何在IE/Firefox下均无法使用
2010/01/22 Javascript
判断访客终端类型集锦
2015/06/05 Javascript
基于dropdown.js实现的两款美观大气的二级导航菜单
2015/09/02 Javascript
使用Bootstrap打造特色进度条效果
2017/05/02 Javascript
three.js 入门案例详解
2018/01/23 Javascript
老生常谈JS中的继承及实现代码
2018/07/06 Javascript
H5+C3+JS实现双人对战五子棋游戏(UI篇)
2020/05/28 Javascript
js嵌套的数组扁平化:将多维数组变成一维数组以及push()与concat()区别的讲解
2019/01/19 Javascript
layer关闭弹出窗口触发表单提交问题的处理方法
2019/09/25 Javascript
分析Python编程时利用wxPython来支持多线程的方法
2015/04/07 Python
Python实现Pig Latin小游戏实例代码
2018/02/02 Python
python多进程实现文件下载传输功能
2018/07/28 Python
python数据结构之线性表的顺序存储结构
2018/09/28 Python
python对视频画框标记后保存的方法
2018/12/07 Python
Python从函数参数类型引出元组实例分析
2019/05/28 Python
Python如何合并多个字典或映射
2020/07/24 Python
HTML5 CSS3实现一个精美VCD包装盒个性幻灯片案例
2014/06/16 HTML / CSS
Java的for语句中break, continue和return的区别
2013/12/19 面试题
汽车技术服务与营销专业在籍生自荐信
2013/09/28 职场文书
房地产销售计划书
2014/01/10 职场文书
水利水电专业自荐信
2014/07/08 职场文书
庆祝教师节演讲稿
2014/09/03 职场文书
大学生学习计划书
2014/09/15 职场文书
校园安全广播稿范文
2014/09/25 职场文书
房屋买卖委托书格式范本格式
2014/10/13 职场文书
党员考试作弊检讨书1000字
2015/02/16 职场文书
2015年学校综合治理工作总结
2015/07/20 职场文书
基于Python绘制子图及子图刻度的变换等的问题
2021/05/23 Python
redis 存储对象的方法对比分析
2021/08/02 Redis
纯html+css实现打字效果
2021/08/02 HTML / CSS
Python中第三方库Faker的使用详解
2022/04/02 Python
MySQL数据库查询进阶之多表查询详解
2022/04/08 MySQL