功能
- 利用ANTs,将Moving图像配准到Fix图像上
项目:https://antspy.readthedocs.io/en/latest/registration.html
函数库下载
pip install pillow
pip install numpy
Linux安装ants
pip install antspyx
windows安装ants
网址:https://github.com/antsx/antspy/releases 【支持python 3.10及以上,选一个】
下载下来之后复制文件路径:
pip install D:/download/antspyx-0.5.4-cp311-cp311-win_amd64.whl # 记得换成自己文件路径
安装成功:
重点函数ants.registration
这点就是这一行代码
registration = ants.registration(fixed_image, moving_image, type_of_transform='Rigid', use_gpu=True)
参数介绍:
type_of_transform主要分为以下几种类型:
1、Translation:
2、Rigid:刚性-旋转和平移
3、Similarity:相似性转换–也可缩放
4、QuickRigid:刚性(快速可视化修复–旋转和平移)
5、DenseRigid:刚性(度量估计期间采用密集采样)
6、BOLDRigid:刚性(互信息)
7、Affine:仿射变换(平移及线性映射/线性变换)–刚性+缩放
8、AffineFast:快
9、BOLDAffine:与BOLD的结合体
10、TRSAA:前三种类型加仿射变换(俩次)–可能有mask
11、ElasticSyN:对称归一化:仿射和可变性转换(以MI做优化度量和elastic正则化)
12、SyN:对称归一化:仿射和可变性转换(以互信息(MI)做优化度量)
13、SyNRA:对称归一化:刚性、仿射和可变性转换(以互信息做优化度量)
14、SyNOnly:对称归一化:无初始变换,以互信息为优化指标。 假设图像通过初始转换对齐。如果您想运行未屏蔽仿射,然后运行蒙版可变形配准,这可能很有用。
15、SyNCC:SyN–以互相关(CC)作为优化度量
16、SyNabp:SyN的abpBrainExtraction
17、SyNBold:SyN,对BOLD和T1之间的配准进行优化
18、SyNBoldAff:SyN,对BOLD和T1之间的配准进行优化,加仿射
19、SyNAggro:精细配准和变性,时间长
20、TVMSQ:具有均方度量的时变微分同胚(可以保障处理对象之间的结构相似性,防止产生畸变)
注:对给定的两个光滑流形M与N,若f:M→N为双射,且f与f-1均为光滑映射,则称f为微分同胚。该方法配准精度高
21、TVMSQC:具有均方度量的时变微分同胚,用于非常大的变形
代码
import ants
from PIL import Image
import numpy as np
# 用Image加载 JPG 图像,转为灰度图,计算量会小很多
fixed_image = Image.open('fix.jpg').convert('L')
moving_image = Image.open('moving.jpg').convert('L')
# 将 PIL Image 转换为 ANTs Image
fixed_image = ants.from_numpy(np.array(fixed_image))
moving_image = ants.from_numpy(np.array(moving_image))
# 进行图像配准,Rigid时仿射变换
registration = ants.registration(fixed_image, moving_image, type_of_transform='Rigid', use_gpu=True)
# 获取配准后的图像和变换参数
registered_image = registration['warpedmovout']
transformation = registration['fwdtransforms']
# 将 ANTs Image 转换为 PIL Image 并保存
registered_image = Image.fromarray(registered_image.numpy().astype('uint8'), 'L')
rotated_image = registered_image.rotate(-90) #ant处理前会对图片进行旋转,所以保存的时候要再旋转一下
registered_image.save('registered_image.jpg')