.IMA文件转换成.png文件

Low Dose CT Challenge Images and Information Available
公开的医学CT数据集
https://aapm.app.box.com/s/eaw4jddb53keg1bptavvvd1sf4x3pe9h

话不多说
直接上代码

import pydicom
import numpy as np
import os
import cv2


# 下面getCtHU()是将.IMA文件转成HU形式
def getCtHU(dicm):
    '''直接传入dicm文件/IMA文件'''
    img = np.array(dicm.pixel_array).astype('int32')
    img[img == -2000.0] = 0
    Hu = np.array(img).astype('float64')
    RescaleIntercept = dicm.RescaleIntercept
    RescaleSlope = dicm.RescaleSlope
    if RescaleSlope != 1:
        Hu = Hu * RescaleSlope
    Hu += RescaleIntercept
    return Hu


# 下面的windowsLevelTransform()是将上面的HU转为numpy形式
def windowsLevelTransform(Hu, window, level):
    img = Hu
    min = level - float(window) * 0.5;
    max = level + float(window) * 0.5;  # 这个是CT窗口设置,相关问题百度或评论。下面调用这个函数时候,我拟定拟定窗口[-160,240]
    img[img < min] = min
    img[img > max] = max
    norm_ = (img - min) / window
    norm_.astype('float32')
    return norm_


root = r'G:\数据集\train\3mm D45\FD_3mm_sharp\full_3mm_sharp\L506\full_3mm_sharp'  # 这是每个病例的路径
savepath = r'C:\Users\\Desktop\t\506'  # 之后保存图片的路径
#更改此处就可以了

# # 下面遍历每个切片

i = 0
for filename in os.listdir(root):
    img = pydicom.read_file(os.path.join(root, filename))  # .IMA文件
    img_hu = getCtHU(img)  # 由.IMA文件转换成HU形式的
    img_np = windowsLevelTransform(Hu=img_hu, window=400, level=40)  # 再由HU形式的转成.numpy形式的
    output_filename = os.path.join(savepath, '506-'+f'{i}.png')  # Use f-string to create unique filenames
    cv2.imwrite(output_filename, img_np * 255)
    i += 1  # Increment i for the next iteration

# ...从0.png开始命名
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值