ANTs配准图像CT

import os
import glob
import ants
import numpy as np
import SimpleITK as sitk
import time
# ants图片的读取
start = time.time()
f_img = ants.image_read("/home/user0/Recursive/ANTsPy/ANTsdatas/testImg1.nii.gz")
m_img = ants.image_read("/home/user0/Recursive/ANTsPy/ANTsdatas/testImg2.nii.gz")
f_label = ants.image_read("/home/user0/Recursive/ANTsPy/ANTsdatas/testLabel2.nii.gz")
m_label = ants.image_read("/home/user0/Recursive/ANTsPy/ANTsdatas/testLabel2.nii.gz")

'''
ants.registration()函数的返回值是一个字典:
    warpedmovout: 配准到fixed图像后的moving图像 
    warpedfixout: 配准到moving图像后的fixed图像 
    fwdtransforms: 从moving到fixed的形变场 
    invtransforms: 从fixed到moving的形变场

type_of_transform参数的取值可以为:
    Rigid:刚体
    Affine:仿射配准,即刚体+缩放
    ElasticSyN:仿射配准+可变形配准,以MI为优化准则,以elastic为正则项
    SyN:仿射配准+可变形配准,以MI为优化准则
    SyNCC:仿射配准+可变形配准,以CC为优化准则
'''

# 图像配准
mytx = ants.registration(fixed=f_img, moving=m_img, type_of_transform='SyN')
# 将形变场作用于moving图像,得到配准后的图像,interpolator也可以选择"nearestNeighbor"等
warped_img = ants.apply_transforms(fixed=f_img, moving=m_img, transformlist=mytx['fwdtransforms'],
                                   interpolator="linear")
# 对moving图像对应的label图进行配准
warped_label = ants.apply_transforms(fixed=f_img, moving=m_label, transformlist=mytx['fwdtransforms'],
                                     interpolator="linear")
# 将配准后图像的direction/origin/spacing和原图保持一致
warped_img.set_direction(f_img.direction)
warped_img.set_origin(f_img.origin)
warped_img.set_spacing(f_img.spacing)
warped_label.set_direction(f_img.direction)
warped_label.set_origin(f_img.origin)
warped_label.set_spacing(f_img.spacing)
img_name = "/home/user0/Recursive/ANTsPy/ANTsdatas/result/warped_img.nii"
label_name = "/home/user0/Recursive/ANTsPy/ANTsdatas/result/warped_label.nii"
# 图像的保存
ants.image_write(warped_img, img_name)
ants.image_write(warped_label, label_name)

# 将antsimage转化为numpy数组
warped_img_arr = warped_img.numpy(single_components=False)
# 从numpy数组得到antsimage
img = ants.from_numpy(warped_img_arr, origin=None, spacing=None, direction=None, has_components=False, is_rgb=False)
# 生成图像的雅克比行列式
jac = ants.create_jacobian_determinant_image(domain_image=f_img, tx=mytx["fwdtransforms"][0], do_log=False, geom=False)
ants.image_write(jac, "/home/user0/Recursive/ANTsPy/ANTsdatas/result/jac.nii")
# 生成带网格的moving图像,实测效果不好
m_grid = ants.create_warped_grid(m_img)
m_grid = ants.create_warped_grid(m_grid, grid_directions=(False, False), transform=mytx['fwdtransforms'],
                                 fixed_reference_image=f_img)
ants.image_write(m_grid, "/home/user0/Recursive/ANTsPy/ANTsdatas/result/m_grid.nii")
end = time.time()
print(end-start)
'''
以下为其他不常用的函数:

ANTsTransform.apply_to_image(image, reference=None, interpolation='linear')
ants.read_transform(filename, dimension=2, precision='float')
# transform的格式是".mat"
ants.write_transform(transform, filename)
# field是ANTsImage类型
ants.transform_from_displacement_field(field)
'''
print("End")

  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值