#!/usr/bin/env python
# -*- coding:utf-8 -*-
import os
import sys
sys.path.append('./')
sys.path.append('../')
import numpy as np
import pydicom
import nibabel
from pathlib import Path
#获取单个文件夹下所有dicom数据的头文件
#返回两个变量
#filelist存储所有数据文件的地址
#value_dict存储关键字
def GetHdrValueDcitByFolder(folderPath, SysFiles, tag_dict, type_dict, NoUseSequence):
illegalSuffix = [
'nii', 'gz', 'jpg', 'png'
]
filelist = []
value_dict = {}
for root, dirs, files in os.walk(folderPath):
files = Except_system_file(files, SysFiles)
for file in files:
# print(root, file)
strd = file.split('.')
if strd[-1] not in illegalSuffix:
if Path( os.path.join( root, file ) ).stat().st_size > 50 * 1024:
hdr = pydicom.dcmread(os.path.join(root, file), stop_before_pixels = True)
SeD = hdr.get(tag_dict["Series Description"]).value
if SeD not in NoUseSequence:
filelist.append(os.path.join(root, file))
for k, v in tag_dict.items():
if hdr.get(tag_dict[k]) is not None:
tvalue = hdr.get(tag_dict[k]).value
ttype = type_dict[hdr.get(tag_dict[k]).VR]
if len(value_dict.keys()) != len(tag_dict.keys()):
value_dict[k] = [np.array(tvalue).astype(ttype)]
else:
value_dict[k].append(np.array(tvalue).astype(ttype))
else:
if k == "Slice Location":
if hdr.get(tag_dict["Image Position Patient"]) is not None:
ImagePositionPatient = hdr.get(tag_dict["Image Position Patient"]).value
ttype = type_dict[hdr.get(tag_dict["Image Position Patient"]).VR]
ImagePositionPatient = np.array(ImagePositionPatient).astype(ttype)
ImageOrientationPatient = hdr.get(tag_dict["Image Orientation Patient"]).value
ttype = type_dict[hdr.get(tag_dict["Image Orientation Patient"]).VR]
ImageOrientationPatient = np.array(ImageOrientationPatient).astype(ttype)
ImageOrientationPatientF = ImageOrientationPatient[: 3]
ImageOrientationPatientS = ImageOrientationPatient[3:]
LocationF = np.argmax(np.abs(ImageOrientationPatientF))
LocationS = np.argmax(np.abs(ImageOrientationPatientS))
LocationT = np.delete(np.array([0, 1, 2], dtype = np.int16), [LocationF, LocationS])
tvalue = ImagePositionPatient[LocationT][0]
if len(value_dict.keys()) != len(tag_dict.keys()):
value_dict[k] = [tvalue]
else:
value_dict[k].append(tvalue)
else:
tvalue = -1
if len(value_dict.keys()) != len(tag_dict.keys()):
value_dict[k] = [tvalue]
else:
value_dict[k].append(tvalue)
elif k == "Image Position Patient":
tvalue = [-1, -1, -1]
if len(value_dict.keys()) != len(tag_dict.keys()):
value_dict[k] = [tvalue]
else:
value_dict[k].append(tvalue)
elif k == 'Image Orientation Patient':
tvalue = [1, 0, 0, 0, 1, 0]
if len(value_dict.keys()) != len(tag_dict.keys()):
value_dict[k] = [tvalue]
else:
value_dict[k].append(tvalue)
else:
tvalue = 1
if len(value_dict.keys()) != len(tag_dict.keys()):
value_dict[k] = [tvalue]
else:
value_dict[k].append(tvalue)
return filelist, value_dict
def normalNii(hdr, aff, data):
ori_shape = data.shape
locationF, locationS, locationT = np.argmax( np.abs(aff[:, 0]) ), np.argmax( np.abs(aff[:, 1]) ), np.argmax( np.abs(aff[:, 2]) )
flipF, flipS, flipT = -1 if aff[locationF, 0] < 0 else 1, -1 if aff[locationS, 1] < 0 else 1, -1 if aff[locationT, 2] < 0 else 1
data = FlipByIndicator(data, [flipF, flipS, flipT])
affIndicator = [ locationF, locationS, locationT ]
flipIndicator = [ flipF, flipS, flipT ]
PixelSpacing = [ np.abs(aff[locationF, 0]), np.abs(aff[locationS, 1]) ]
SlThickness = np.abs(aff[locationT, 2])
dim = len(data.shape)
if dim == 4:
if locationT == 0:
data = np.transpose(data, [2, 0, 1, 3])
flipIndicator = np.array(flipIndicator)[ [2, 0, 1] ]
elif locationT == 1:
data = np.transpose(data, [0, 2, 1, 3])
flipIndicator = np.array(flipIndicator)[ [0, 2, 1] ]
else:
data = np.transpose(data, [0, 1, 2, 3])
flipIndicator = np.array(flipIndicator)[ [0, 1, 2] ]
else:
if locationT == 0:
data = np.transpose(data, [2, 0, 1])
flipIndicator = np.array(flipIndicator)[ [2, 0, 1] ]
elif locationT == 1:
data = np.transpose(data, [0, 2, 1])
flipIndicator = np.array(flipIndicator)[ [0, 2, 1] ]
else:
data = np.transpose(data, [0, 1, 2])
flipIndicator = np.array(flipIndicator)[ [0, 1, 2] ]
if flipT > 0:
StartLocation = aff[locationT, 3]
else:
StartLocation = aff[locationT, 3] - SlThickness * ori_shape[2]
ImagePositionPatient = aff[:, 3][: -1]
hdr, aff = GenHdrAffine(
data, ori_shape, affIndicator, [ flipF, flipS, flipT ], StartLocation,
dim, PixelSpacing, ImagePositionPatient, SlThickness,
0, 0, 0, np.max(data), np.min(data)
)
return hdr, aff, data
def normalNiiFile(folderPath, savePath):
for root, dirs, files in os.walk(folderPath):
for file in files:
if file.split('.')[-1] == 'nii' or file.split('.')[-1] == 'gz':
hdr, aff, data = load_nii( os.path.join(root, file) )
hdr, aff, data = normalNii(hdr, aff, data)
tpath = replaceIllegalChar( os.path.join(savePath, 'mask'), file )
tdir = os.path.split(tpath)[0]
Make_Dir(tdir)
save_nii( hdr, aff, data, tpath )
#对头文件进行排序
#排序规则为
#按不同的series description分类数据
#同类的数据先按照slice location进行数据从小到大排序
#slice location相同的情况下,按Instance Number从小到大排序
#函数返回数据字
沉默着爆发
- 粉丝: 1403
- 资源: 3
最新资源
- 图书管理系统( Spring+Spring MVC+JdbcTemplate)
- Java毕设项目-基于SSM框架的药房管理系统源码+数据库脚本.zip
- 上市公司研究报告20010101-20240929研究报告标题报告人员关联个股证券关联行业名称 数据来源:基于上市公司公告、年报等相关数据整理计算 数据范围:沪深京上市公司A股,包括主板、中小企业板
- 三相LCL型并网逆变器在dq旋转坐标系下,采用逆变器机侧电感电流反馈有源阻尼+网侧电流反馈控制策略,给出控制参数设计及Simulink仿真模型搭建,参数设计稳定,并网波形质量良好 三相LCL型并网逆
- 计算机体系结构论文格式
- 2-BPC(中国码)电波表对时模拟软件
- Java毕设项目-基于SSM框架的药房管理系统源码+数据库脚本(高分毕设)
- 基于CNN的快速VVC帧间编码方法及其应用与性能提升研究
- 网络安全-渗透攻防知识点面试题整合
- 基于梯度方向的VVC帧内编码中CU划分早终止算法研究与实现
- java图书管理系统(JSP+Servlet)
- 毕业设计基于单片机的室内有害气体检测系统源码+论文(高分毕设)
- 毕业设计 springBoot人力资源管理系统+毕业论文+前后端源代码
- 基于单片机的室内有害气体检测系统源码+论文(高分毕设)
- java图书管理系统-技术栈:JSP+Servlet+Tomcat9.0+IDEA+Mysql
- RBP神经网络PID自适应控制模型(送配套资料) Matlab仿真模型 与传统pid控制器相比,省去pid参数调节 附赠详解资料,包思路讲解,代码分析
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈