Python教程

[python]-SimpleITK模块-医学影像标注nii.gz文件的读取与保存

本文主要是介绍[python]-SimpleITK模块-医学影像标注nii.gz文件的读取与保存,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

SimpleITK模块以多种语言为 ITK 提供简化的接口,支持Python、R、Java、C#、Lua、Ruby、TCL 和 C++ 中的开源多维图像分析,由 Insight Toolkit 社区为生物医学科学及其他领域开发。
官方文档链接:https://simpleitk.org/#

在实际使用中,医学影像标注nii.gz文件的读取与保存用它比较方便。

nii.gz文件的读取

import SimpleITK as sitk
import skimage.io as io

def read_img(path):
    img = sitk.ReadImage(path)
    data = sitk.GetArrayFromImage(img) 
    # print(data.shape)
    return data

nii.gz文件保存为标注json文件

import os
import numpy as np
import cv2
import SimpleITK as sitk 
import json

def read_img(path): # 读取nii.gz文件
    img = sitk.ReadImage(path)
    data = sitk.GetArrayFromImage(img)
    # print(data.shape)
    return data

def save_json(save_path, dict_name): # 存为json文件
    jsonData = json.dumps(dict_name)
    with open(save_path,'w') as jsonf:
        jsonf.write(jsonData)

def copy_ (back, img, size): # 标注之外的地方作为背景类
    # print(type(img), img.shape)
    for i in range(size[0]):
        for j in range(size[1]):
            if img[i][j] > 0:
                back[i][j] = 0
    return back

def resize_ann(img, size): # 将图像转换到需要的尺寸
    path = 'mid_path.jpg' # 中间文件,先保存再以灰度图模式读取
    cv2.imwrite(path, img)
    # print(path,size)
    original_img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
    # print(original_img.shape)
    resize_img = cv2.resize(original_img, size, cv2.INTER_NEAREST)
    # print(resize_img.shape)
    # cv.imwrite('save_path.jpg', resize_img)
    return resize_img


def save_ann(path1, path2):

    # 例如有两种标注,分别存在path1和path2,且含有多层
    img_arr1 = read_img(path1)
    img_arr2 = read_img(path2)

    x=0 
    img_arr_size = img_arr1.shape
    # print(img_arr.shape)    

    # 读取niigz
    for i in range(img_arr1.shape[0]):

        img_need1 = img_arr1[i,:,:] 
        img_need2 = img_arr2[i,:,:] 

        # 检查是否需要转置
        # img_need = img_need.T   
        # print(img_need)
        # 检查标注值
        # print(np.max(img_need))

        # 统一尺寸大小为(512,512)
        resize_size = (512,512)
        if np.max(img_need1) == 0:
            im_0 = np.zeros((512,512), dtype="uint8")
        else:
            # 如果尺寸不一需要转换
            im_0 = resize_ann(img_need1, resize_size)
            im_0[im_0>1]=1

        if np.max(img_need2) == 0:
            im_1 = np.zeros((512,512), dtype="uint8")
        else:
            im_1 = resize_ann(img_need2, resize_size)
            im_1[im_1>1]=1

        back = np.ones((512,512), dtype="uint8")
        back = copy_(back, im_0, resize_size)
        back = copy_(back, im_1, resize_size)
            
        im_0 = im_0.tolist()
        im_1 = im_1.tolist()
        back = back.tolist()

        imglist = []
        for x in [im_0, im_1, back]:
            imglist.append(x)

        if not os.path.exists(path_ann):    #  如果不存在就创建文件夹
            os.mkdir(path_ann)

        path_ann = 'save_path' + 'save_name'

        save_json(path_ann, imglist)
这篇关于[python]-SimpleITK模块-医学影像标注nii.gz文件的读取与保存的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!