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中super函数的用法
Nov 17 Python
Python实现改变与矩形橡胶的线条的颜色代码示例
Jan 05 Python
儿童学习python的一些小技巧
May 27 Python
在Qt5和PyQt5中设置支持高分辨率屏幕自适应的方法
Jun 18 Python
解决Python正则表达式匹配反斜杠''\''问题
Jul 17 Python
Python 字符串类型列表转换成真正列表类型过程解析
Aug 26 Python
Python日期格式和字符串格式相互转换的方法
Feb 18 Python
python numpy--数组的组合和分割实例
Feb 24 Python
python json 递归打印所有json子节点信息的例子
Feb 27 Python
Python实现LR1文法的完整实例代码
Oct 25 Python
Python字符串对齐、删除字符串不需要的内容以及格式化打印字符
Jan 23 Python
Pytorch DataLoader shuffle验证方式
Jun 02 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中的字符串函数
2006/11/24 PHP
php判断变量类型常用方法
2012/04/24 PHP
PHP CURL CURLOPT参数说明(curl_setopt)
2013/09/30 PHP
php定时计划任务与fsockopen持续进程实例
2014/05/23 PHP
php设置静态内容缓存时间的方法
2014/12/01 PHP
WordPress中用于获取文章作者与分类信息的方法整理
2015/12/17 PHP
Python中使用django form表单验证的方法
2017/01/16 PHP
PHP数组式访问接口ArrayAccess用法分析
2017/12/28 PHP
PHP面向对象程序设计(OOP)之方法重写(override)操作示例
2018/12/21 PHP
gearman中任务的优先级和返回状态实例分析
2020/02/27 PHP
javascript两段代码,两个小技巧
2010/02/04 Javascript
JavaScript isArray()函数判断对象类型的种种方法
2010/10/11 Javascript
js 得到文件后缀(通过正则实现)
2013/07/08 Javascript
JQuery事件e参数的方法preventDefault()取消默认行为
2013/09/26 Javascript
JS将制定内容复制到剪切板示例代码
2014/02/11 Javascript
Nodejs使用mysql模块之获得更新和删除影响的行数的方法
2014/03/18 NodeJs
JS使用for循环遍历Table的所有单元格内容
2014/08/21 Javascript
全面解析JavaScript中“&&”和“||”操作符(总结篇)
2016/07/18 Javascript
详解elementui之el-image-viewer(图片查看器)
2019/08/30 Javascript
[02:26]2016国际邀请赛8月3日开战 中国军团出征西雅图
2016/08/02 DOTA
使用Python脚本将文字转换为图片的实例分享
2015/08/29 Python
Django 根据数据模型models创建数据表的实例
2018/05/27 Python
python实现内存监控系统
2021/03/07 Python
python实现植物大战僵尸游戏实例代码
2019/06/10 Python
超简单的Python HTTP服务
2019/07/22 Python
python 代码运行时间获取方式详解
2020/09/18 Python
Application Cache未缓存文件无法访问无法加载问题
2014/05/31 HTML / CSS
香蕉共和国Banana Republic官网:美国GAP旗下偏贵族风格服饰品牌
2016/11/21 全球购物
苏宁红孩子母婴商城:redbaby
2017/02/12 全球购物
星空联盟C# .net笔试题
2014/12/05 面试题
设计部经理的岗位职责
2013/11/16 职场文书
四年级评语大全
2014/04/21 职场文书
软件售后服务承诺书
2014/05/21 职场文书
2014年教育培训工作总结
2014/12/08 职场文书
大四学生个人总结
2015/02/15 职场文书
2019年干货:自我鉴定
2019/03/25 职场文书