dicom文件的处理——pydicom库的使用

2024-03-01 10:04
文章标签 使用 处理 dicom pydicom

本文主要是介绍dicom文件的处理——pydicom库的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1. 计算两个切片的质量(dicom格式)

import cv2
from pydicom import dcmread
import numpy as np
from skimage.metrics import structural_similarity as compare_ssim
from skimage.metrics import peak_signal_noise_ratio as compare_psnr
from skimage.metrics import mean_squared_error as compare_mse# 读取第一个DICOM文件
d1 = dcmread('PA1/SE0/IM0.IMA')
image_data1 = d1.pixel_array# 读取第二个DICOM文件
d2 = dcmread('PA1/SE1/IM0.IMA')
image_data2 = d2.pixel_array# 将图像转换为8位无符号整型格式
image_data1 = image_data1.astype(np.uint16)
image_data2 = image_data2.astype(np.uint16)# 计算SSIM值
ssim_value = compare_ssim(image_data1, image_data2)
psnr_value = compare_psnr(image_data1,image_data2)
mse_value = compare_mse(image_data1,image_data2)
print(ssim_value, psnr_value, mse_value)

2. 对dicom文件进行重采样

之前的size大小不一,因此需要重采样到同一个size


import pydicom
import numpy as npimport pydicom
from PIL import Image
import os
from matplotlib import pyplot as plt
import lpips
from matplotlib import pyplotdef get_filelist(folder_path):file_names = os.listdir(folder_path)return file_namesdef resample_dicom(ds, out_file, PA, idx):image_data = ds.pixel_array# 设置目标分辨率(这里以2x2作为示例)target_resolution = (512, 512)# 创建PIL图像对象image = Image.fromarray(image_data).resize(target_resolution)# 转换为numpy数组resampled_data = np.asanyarray(image)ds.PixelRepresentation = 1if ds.BitsAllocated == 16:resampled_data = resampled_data.astype(np.uint16, casting="safe")elif ds.BitsAllocated == 8:resampled_data = resampled_data.astype(np.int8, casting="safe")else:raise Exception("unknow Bits Allocated value in dicom header")# 设置新的像素数组ds.PixelData = resampled_data.tobytes()ds.Rows, ds.Columns = resampled_data.shape# 保存为新的DICOM文件# ds.save_as("output_file.dcm")ds.save_as("CTA-GAN1/"+ "PA"+str(PA+1)+"/"+out_file+"/IM"+str(idx)+".IMA")for PA in range(10):SE0_path = "CTA-GAN/PA" + str(PA + 1) + "/SE0"SE1_path = "CTA-GAN/PA" + str(PA + 1) + "/SE1"files_name = get_filelist(SE0_path)for idx in range(len(files_name)):ds1 = pydicom.dcmread(SE0_path+"/IM"+str(idx)+".IMA",  force=True)  # 读取头文件ds2 = pydicom.dcmread(SE1_path+"/IM"+str(idx)+".IMA",  force=True)  # 读取头文件resample_dicom(ds1,"SE0", PA ,idx)resample_dicom(ds2, "SE1", PA, idx)

3. 创建训练、测试的txt,用于加载数据集

import osos.remove('train.txt')
os.remove('val.txt')
os.remove('test.txt')f1 = open("train.txt", "w")  # 564
f2 = open("val.txt", "w")  # 187
f3 = open("test.txt", "w")  # 188def get_filelist(folder_path):file_names = os.listdir(folder_path)return file_namesdef write_txt(path, txt):files_name = get_filelist(path)for i in range(len(files_name)):# aa = "data/" + path + "/" + files_name[i]aa = "data/" + path + "/" + "IM" + str(i) + ".IMA"txt.writelines(aa + "\n")for i in range(8):SE0_path = "CTA-GAN/PA" + str(i + 1) + "/SE0"SE1_path = "CTA-GAN/PA" + str(i + 1) + "/SE1"write_txt(SE0_path, f1)write_txt(SE1_path, f1)SE0_path = "CTA-GAN/PA9/SE0"
SE1_path = "CTA-GAN/PA9/SE1"write_txt(SE0_path, f2)
write_txt(SE1_path, f2)SE0_path = "CTA-GAN/PA10/SE0"
SE1_path = "CTA-GAN/PA10/SE1"write_txt(SE0_path, f3)
write_txt(SE1_path, f3)f1.close()
f1.close()
f2.close()

4. 对dicom进行rename

import osdef count_files(folder):file_count = len([name for name in os.listdir(folder) if os.path.isfile(os.path.join(folder, name))])return file_countdef get_filelist(folder_path):file_names = []for file in os.listdir(folder_path):if not os.path.isdir(os.path.join(folder_path, file)):file_names.append(file)return file_namesdef rename_file(old_file, idx, file_path):# 定义原始文件路径和新文件名# old_file = "path/to/original.txt"old_file = os.path.join(file_path, old_file)new_name = os.path.join(file_path, "IM"+str(idx)+".IMA")try:# 调用rename()函数来重命名文件os.rename(old_file, new_name)print("文件已成功重命名为", new_name)except FileNotFoundError:print("未找到指定的文件")except Exception as e:print("发生错误:", str(e))for i in range(10):SE0_path = "PA"+str(i+1)+"/SE0"SE1_path = "PA"+str(i+1)+"/SE1"num_file = count_files(SE0_path)files_name = get_filelist(SE0_path)for i in range(num_file):rename_file(files_name[i], i, SE0_path)files_name = get_filelist(SE1_path)for i in range(num_file):rename_file(files_name[i], i, SE1_path)

5. 删除文件夹下面的所有文件(只删除文件,不删除文件夹)

#删除文件夹下面的所有文件(只删除文件,不删除文件夹)
import os
import shutildef del_file(path_data):for i in os.listdir(path_data) :# os.listdir(path_data)#返回一个列表,里面是当前目录下面的所有东西的相对路径file_data = path_data + "\\" + i#当前文件夹的下面的所有东西的绝对路径if os.path.isfile(file_data) == True:#os.path.isfile判断是否为文件,如果是文件,就删除.如果是文件夹.递归给del_file.os.remove(file_data)else:del_file(file_data)
path_data = r"CTA-GAN1"
del_file(path_data)

这篇关于dicom文件的处理——pydicom库的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/762066

相关文章

MySQL 日期时间格式化函数 DATE_FORMAT() 的使用示例详解

《MySQL日期时间格式化函数DATE_FORMAT()的使用示例详解》`DATE_FORMAT()`是MySQL中用于格式化日期时间的函数,本文详细介绍了其语法、格式化字符串的含义以及常见日期... 目录一、DATE_FORMAT()语法二、格式化字符串详解三、常见日期时间格式组合四、业务场景五、总结一、

Python中配置文件的全面解析与使用

《Python中配置文件的全面解析与使用》在Python开发中,配置文件扮演着举足轻重的角色,它们允许开发者在不修改代码的情况下调整应用程序的行为,下面我们就来看看常见Python配置文件格式的使用吧... 目录一、INI配置文件二、YAML配置文件三、jsON配置文件四、TOML配置文件五、XML配置文件

Go使用pprof进行CPU,内存和阻塞情况分析

《Go使用pprof进行CPU,内存和阻塞情况分析》Go语言提供了强大的pprof工具,用于分析CPU、内存、Goroutine阻塞等性能问题,帮助开发者优化程序,提高运行效率,下面我们就来深入了解下... 目录1. pprof 介绍2. 快速上手:启用 pprof3. CPU Profiling:分析 C

MySQL InnoDB引擎ibdata文件损坏/删除后使用frm和ibd文件恢复数据

《MySQLInnoDB引擎ibdata文件损坏/删除后使用frm和ibd文件恢复数据》mysql的ibdata文件被误删、被恶意修改,没有从库和备份数据的情况下的数据恢复,不能保证数据库所有表数据... 参考:mysql Innodb表空间卸载、迁移、装载的使用方法注意!此方法只适用于innodb_fi

Gin框架中的GET和POST表单处理的实现

《Gin框架中的GET和POST表单处理的实现》Gin框架提供了简单而强大的机制来处理GET和POST表单提交的数据,通过c.Query、c.PostForm、c.Bind和c.Request.For... 目录一、GET表单处理二、POST表单处理1. 使用c.PostForm获取表单字段:2. 绑定到结

mysql8.0无备份通过idb文件恢复数据的方法、idb文件修复和tablespace id不一致处理

《mysql8.0无备份通过idb文件恢复数据的方法、idb文件修复和tablespaceid不一致处理》文章描述了公司服务器断电后数据库故障的过程,作者通过查看错误日志、重新初始化数据目录、恢复备... 周末突然接到一位一年多没联系的妹妹打来电话,“刘哥,快来救救我”,我脑海瞬间冒出妙瓦底,电信火苲马扁.

Python中conda虚拟环境创建及使用小结

《Python中conda虚拟环境创建及使用小结》本文主要介绍了Python中conda虚拟环境创建及使用小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们... 目录0.前言1.Miniconda安装2.conda本地基本操作3.创建conda虚拟环境4.激活c

Spring中@Lazy注解的使用技巧与实例解析

《Spring中@Lazy注解的使用技巧与实例解析》@Lazy注解在Spring框架中用于延迟Bean的初始化,优化应用启动性能,它不仅适用于@Bean和@Component,还可以用于注入点,通过将... 目录一、@Lazy注解的作用(一)延迟Bean的初始化(二)与@Autowired结合使用二、实例解

SpringBoot使用Jasypt对YML文件配置内容加密的方法(数据库密码加密)

《SpringBoot使用Jasypt对YML文件配置内容加密的方法(数据库密码加密)》本文介绍了如何在SpringBoot项目中使用Jasypt对application.yml文件中的敏感信息(如数... 目录SpringBoot使用Jasypt对YML文件配置内容进行加密(例:数据库密码加密)前言一、J

Spring Boot 中正确地在异步线程中使用 HttpServletRequest的方法

《SpringBoot中正确地在异步线程中使用HttpServletRequest的方法》文章讨论了在SpringBoot中如何在异步线程中正确使用HttpServletRequest的问题,... 目录前言一、问题的来源:为什么异步线程中无法访问 HttpServletRequest?1. 请求上下文与线