【代码】将dcm和nii.gz文件转为h5文件

该博客主要介绍了如何处理医学影像数据,包括读取DICOM格式的图像和NII格式的标签,然后将它们转换并存储为H5文件。使用了pydicom、nibabel等库进行文件读取,通过遍历文件夹结构,将多个图像和对应的标签整合到单个H5文件中,方便后续的深度学习模型训练。
import pydicom
import matplotlib.pyplot as plt
import numpy as np
import os
import time
import nibabel as nib
import h5py

def show_image(image1):
    plt.figure(figsize=(10, 5))

    plt.subplot(1, 1, 1)
    plt.axis('off')
    plt.imshow(image1)

    plt.show()

path = '/root/data/lak/hhh/data/pancreas'

# 读取dcm文件
def read_image(path):
    images = np.zeros((500, 512, 512))
    images_path_list = os.listdir(path)
    for id, i in enumerate(images_path_list):
        image_path = os.path.join(path, i)
        # print(image_path)
        image = pydicom.read_file(image_path).pixel_array
        images[id] = image
    return images

# 读取nii文件
def read_label(path):
    # print(path)
    labels = np.zeros((500, 512, 512))
    labelsobj = nib.load(path).dataobj
    for j in range(labelsobj.shape[2]):
        label = labelsobj[:, :, j]
        label = label.transpose()
        labels[j] = label
    return labels

if __name__ == '__main__':

    start_all_data_time = time.time()
 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kunsir_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值