ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

学习记录(四):医学图像配准使用到的代码。

2021-07-09 12:06:25  阅读:360  来源: 互联网

标签:配准 dice nib import 代码 ants 图像 path


1.python以三视图形式显示三维.nii格式医学图像

import  nibabel as nib
from nibabel.viewers import OrthoSlicer3D

file = nib.load("path") #将.nii格式图像读入
OrthoSlicer3D(file.dataobj).show()

2.对图像进行裁剪(.nii格式)

import numpy as np
import  nibabel as nib

file = nib.load("path") #将.nii格式图像读入
vol = file.get_data() #获取图像数据
vol = vol[ : ,  ; , : ] #冒号两边写裁剪的范围,几维就有几个冒号
affine =  file.affine # afiine与变换之前保持同步
newvol = nib.NiftilImage(vol,affine) 
nibsave(newvol,'path')#保存变换后的新图像

3.ANTs对图像进行配准
安装antspy

pip install https://github.com/ANTsX/ANTsPy/release/download/v0.1.4/antspy-0.1.4-cp36-cp36m-linux_X86_64.whl

使用ANTs中的syn配准(如果.nii是float64会报错,float32不会,未知原因。)

import ants

fixed = ants.image_read("path")
moving =  ants.image_read("path")
SyN = ants.registration(fixed = fixed, moving = moving, typr_of_transform = 'SyN')
result = ants.apply_transforms(fixed = fixed, moving = moving, transformlist =            
         SyN['fwdtransforms'])
ants.image_write(result ,'path')

4.计算两图Dice

import  numpy as np
import nibabel as nib

def DSC(pred,target)
    smooth = 1e-5
    m1 = pred.flatten()
    m2 = target.flatten()
    intersection = (m1*m2).sum()
    return (2.*intersection+smooth)/(m1.sum()+m2.sum() + smooth)

def compute_label_dice(gt,pred)
    cls_lst = [1,...,35]
    dice_list = []
    for cls in cls_lst
        dice = DSC(gt == cls , pred == cls)
        dice_lst.append(dice)
    return np.mean(dice_lst)

file1 = nib.load("path") 
file2 = nib.load("path") 
vol1 = file1.get_data()
vol2 = file2.get_data()
dice = compute_label_dice(vol1.vol2)

标签:配准,dice,nib,import,代码,ants,图像,path
来源: https://blog.csdn.net/qq_41624868/article/details/118597257

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有