def reg(fix_path,fix_label_path,move_path,move_label_path,type='SyN'):
    #读取数据,格式为: ants.core.ants_image.ANTsImage
    fix_img = ants.image_read(fix_path)
    fix_label_img = ants.image_read(fix_label_path)
    move_img = ants.image_read(move_path)
    move_label_img = ants.image_read(move_label_path)	


    g1 = ants.iMath_grad( fix_img )
    g2 = ants.iMath_grad( move_img )


    demonsMetric = ['demons', g1, g2, 1, 1]
    ccMetric = ['CC', fix_img, move_img, 2, 4 ]
    metrics = list( )
    metrics.append( demonsMetric )

    #配准
    # outs = ants.registration(fix_img,move_img,type_of_transforme = 'Affine')
    # outs = ants.registration( fix_img, move_img, 'ElasticSyN',  multivariate_extras = metrics )  
    outs = ants.registration( fix_img, move_img, type)

    #获取配准后的数据,并保存
    reg_img = outs['warpedmovout']  
    save_path = './warp_image.nii.gz'
    ants.image_write(reg_img,save_path)

    #获取move到fix的转换矩阵;将其应用到 move_label上;插值方式选取 最近邻插值; 这个时候也对应的将label变换到 配准后的move图像上
    reg_label_img = ants.apply_transforms(fix_img ,move_label_img,transformlist= outs['fwdtransforms'],interpolator = 'nearestNeighbor')  
    save_label_path = './warp_label.nii.gz'
    ants.image_write(reg_label_img,save_label_path)
    return dice_compute(reg_label_img.numpy().astype(np.int32),fix_label_img.numpy().astype(np.int32),indexes=[5])
def reg(fix_path, fix_label_path, move_path, move_label_path, type='SyN'):
    #读取数据,格式为: ants.core.ants_image.ANTsImage
    fix_img = ants.image_read(fix_path)
    fix_label_img = ants.image_read(fix_label_path)
    move_img = ants.image_read(move_path)
    move_label_img = ants.image_read(move_label_path)

    g1 = ants.iMath_grad(fix_img)
    g2 = ants.iMath_grad(move_img)

    demonsMetric = ['demons', g1, g2, 1, 1]
    ccMetric = ['CC', fix_img, move_img, 2, 4]
    metrics = list()
    metrics.append(demonsMetric)

    #配准
    # outs = ants.registration(fix_img,move_img,type_of_transforme = 'Affine')
    # outs = ants.registration( fix_img, move_img, 'ElasticSyN',  multivariate_extras = metrics )
    # outs = ants.registration( fix_img, move_img, type,verbose=True)

    outs = ants.registration(fix_img,
                             move_img,
                             type,
                             reg_iterations=(10, 10, 20))
    outs = ants.registration(fix_img,
                             move_img,
                             type,
                             syn_metric='mattes',
                             syn_sampling=5,
                             reg_iterations=(10, 10, 20))
    # outs = ants.registration( fix_img, move_img, type,syn_metric='demons',reg_iterations=(40, 20, 10))

    #获取配准后的数据,并保存

    # ants.image_write(outs['warpedmovout']  ,'./warp_image.nii.gz')
    if len(outs['fwdtransforms']) != 2:
        return [0]
    #获取move到fix的转换矩阵;将其应用到 move_label上;插值方式选取 最近邻插值; 这个时候也对应的将label变换到 配准后的move图像上
    reg_label_img = ants.apply_transforms(fix_img,
                                          move_label_img,
                                          transformlist=outs['fwdtransforms'],
                                          interpolator='nearestNeighbor')

    # ants.image_write(reg_label_img,'./warp_label.nii.gz')
    # ants.image_write(move_img,'./mv_img.nii.gz')
    # ants.image_write(fix_img,'./fix_img.nii.gz')
    # ants.image_write(fix_label_img,'./fix_label.nii.gz')
    return dice_compute(reg_label_img.numpy().astype(np.int32),
                        fix_label_img.numpy().astype(np.int32),
                        indexes=[5])
import ants
#https://blog.csdn.net/weixin_43718675/article/details/102606717#2_antspy_92
#获取数据路径
fix_path = 'mr_train_1001_image.nii.gz'   
move_path = 'ct_train_1013_image.nii.gz'
move_label_path = 'ct_train_1013_label.nii.gz'

#读取数据,格式为: ants.core.ants_image.ANTsImage
fix_img = ants.image_read(fix_path)
move_img = ants.image_read(move_path)
move_label_img = ants.image_read(move_label_path)	


g1 = ants.iMath_grad( fix_img )
g2 = ants.iMath_grad( move_img )


demonsMetric = ['demons', g1, g2, 1, 1]
ccMetric = ['CC', fix_img, move_img, 2, 4 ]
metrics = list( )
metrics.append( demonsMetric )

#配准
# outs = ants.registration(fix_img,move_img,type_of_transforme = 'Affine')
outs = ants.registration( fix_img, move_img, 'ElasticSyN',  multivariate_extras = metrics )  
# outs = ants.registration( fix_img, move_img, 'SyNCC')

#获取配准后的数据,并保存
reg_img = outs['warpedmovout']  
save_path = './warp_image.nii.gz'
ants.image_write(reg_img,save_path)
示例#4
0
# -*- coding:utf-8 -*-
import ants
img1 = ants.image_read(
    '../mmwhs/CT_test/205_5/ct-image_crop_man_reg_resize/ct_train_1013_image.nii.gz'
)
img2 = ants.image_read(
    '../mmwhs/CT_test/205_5/ct-image_crop_man_reg_resize/ct_train_1014_image.nii.gz'
)
print(img2)
print(img1)
g1 = ants.iMath_grad(img1)
g2 = ants.iMath_grad(img2)

reg1 = ants.registration(img1, img2, 'SyNOnly')
demonsMetric = ['demons', g1, g2, 1, 1]
ccMetric = ['CC', img1, img2, 2, 4]
metrics = list()
metrics.append(demonsMetric)
print("start to registration...........")
reg2 = ants.registration(img1, img2, 'SyNOnly', multivariate_extras=metrics)
metrics.append(ccMetric)
reg3 = ants.registration(img1, img2, 'SyNOnly', multivariate_extras=metrics)

print(ants.image_mutual_information(img1, img2))
print(ants.image_mutual_information(img1, reg1['warpedmovout']))
print(ants.image_mutual_information(img1, reg2['warpedmovout']))
print(ants.image_mutual_information(img1, reg3['warpedmovout']))