itk中的配准整理

文章目录

    • Perform 2D Translation Registration With Mean Squares
      • 效果:
      • 源码:
    • 多模态互信息配准 Perform Multi Modality Registration With Viola Wells Mutual Information
      • 效果图
      • 源码:
    • Register Image to Another Using Landmarks 通过标记点配准图像
      • 效果图
      • 源码

Perform 2D Translation Registration With Mean Squares

原文地址: https://examples.itk.org/src/registration/common/perform2dtranslationregistrationwithmeansquares/documentation

效果:

在这里插入图片描述

源码:

import os
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cmimport itk# Perform 2D Translation Registration With Mean Squares
baseDir = r'D:\learn\itk\itk5.3.0\ITK-5.3.0\Examples\Data'
fixed_input_image = baseDir + r"\BrainProtonDensitySlice.png"
moving_input_image = baseDir + r"\BrainProtonDensitySliceShifted13x17y.png"PixelType = itk.ctype("float")
fixedImage = itk.imread(fixed_input_image, PixelType)
movingImage = itk.imread(moving_input_image, PixelType)Dimension = fixedImage.GetImageDimension()
FixedImageType = itk.Image[PixelType, Dimension]
MovingImageType = itk.Image[PixelType, Dimension]TransformType = itk.TranslationTransform[itk.D, Dimension]
initialTransform = TransformType.New()optimizer = itk.RegularStepGradientDescentOptimizerv4.New(LearningRate=4,MinimumStepLength=0.001,RelaxationFactor=0.5,NumberOfIterations=200,
)metric = itk.MeanSquaresImageToImageMetricv4[FixedImageType, MovingImageType].New()registration = itk.ImageRegistrationMethodv4.New(FixedImage=fixedImage,MovingImage=movingImage,Metric=metric,Optimizer=optimizer,InitialTransform=initialTransform,
)movingInitialTransform = TransformType.New()
initialParameters = movingInitialTransform.GetParameters()
initialParameters[0] = 0
initialParameters[1] = 0
movingInitialTransform.SetParameters(initialParameters)
registration.SetMovingInitialTransform(movingInitialTransform)identityTransform = TransformType.New()
identityTransform.SetIdentity()
registration.SetFixedInitialTransform(identityTransform)registration.SetNumberOfLevels(1)
registration.SetSmoothingSigmasPerLevel([0])
registration.SetShrinkFactorsPerLevel([1])
registration.Update()transform = registration.GetTransform()
finalParameters = transform.GetParameters()
translationAlongX = finalParameters.GetElement(0)
translationAlongY = finalParameters.GetElement(1)
numberOfIterations = optimizer.GetCurrentIteration()
bestValue = optimizer.GetValue()print("Result = ")
print(" Translation X = " + str(translationAlongX))
print(" Translation Y = " + str(translationAlongY))
print(" Iterations    = " + str(numberOfIterations))
print(" Metric value  = " + str(bestValue))CompositeTransformType = itk.CompositeTransform[itk.D, Dimension]
outputCompositeTransform = CompositeTransformType.New()
outputCompositeTransform.AddTransform(movingInitialTransform)
outputCompositeTransform.AddTransform(registration.GetModifiableTransform())resampler = itk.ResampleImageFilter.New(Input=movingImage,Transform=outputCompositeTransform,UseReferenceImage=True,ReferenceImage=fixedImage,
)
resampler.SetDefaultPixelValue(100)OutputPixelType = itk.ctype("unsigned char")
OutputImageType = itk.Image[OutputPixelType, Dimension]
caster = itk.CastImageFilter[FixedImageType, OutputImageType].New(Input=resampler)
output_image_arr = itk.GetArrayFromImage(caster)  # 配准后的结果difference = itk.SubtractImageFilter.New(Input1=fixedImage, Input2=resampler)
intensityRescaler = itk.RescaleIntensityImageFilter[FixedImageType, OutputImageType
].New(Input=difference,OutputMinimum=itk.NumericTraits[OutputPixelType].min(),OutputMaximum=itk.NumericTraits[OutputPixelType].max(),
)
resampler.SetDefaultPixelValue(1)
intensityRescaler.Update()
difference_image_after_arr = itk.GetArrayFromImage(intensityRescaler.GetOutput())  # 配准后与原图像的差别resampler.SetTransform(identityTransform)
intensityRescaler.Update()
difference_image_before_arr = itk.GetArrayFromImage(intensityRescaler.GetOutput())  # 配准前与原图像的差别# output_image  difference_image_after  difference_image_before
plt.subplot(231), plt.imshow(itk.GetArrayFromImage(fixedImage), 'gray'), plt.title('fixedImage')
plt.subplot(232), plt.imshow(itk.GetArrayFromImage(movingImage), 'gray'), plt.title('movingImage')
plt.subplot(233), plt.imshow(output_image_arr, 'gray'), plt.title('output_image')
plt.subplot(234), plt.imshow(difference_image_after_arr, 'gray'), plt.title('difference_image_after')
plt.subplot(235), plt.imshow(difference_image_before_arr, 'gray'), plt.title('difference_image_before')
# plt.subplot(236), plt.imshow(itk.GetArrayFromImage(moving_smoothed_image), 'gray'), plt.title('moving_smoothed_image')
plt.show()

多模态互信息配准 Perform Multi Modality Registration With Viola Wells Mutual Information

原文地址: https://examples.itk.org/src/registration/common/performmultimodalityregistrationwithmutualinformation/documentation

效果图

在这里插入图片描述

源码:

import os
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cmimport itkdim = 2
ImageType = itk.Image[itk.F, dim]
FixedImageType = ImageType
MovingImageType = ImageTypebaseDir = r'D:\learn\itk\itk5.3.0\ITK-5.3.0\Examples\Data'fixed_img_path = baseDir + r"\BrainT1SliceBorder20.png"
moving_img_path = baseDir + r"\BrainProtonDensitySliceShifted13x17y.png"fixed_img = itk.imread(fixed_img_path, itk.F)
moving_img = itk.imread(moving_img_path, itk.F)fixed_normalized_image = itk.normalize_image_filter(fixed_img)
fixed_smoothed_image = itk.discrete_gaussian_image_filter(fixed_normalized_image, variance=2.0)moving_normalized_image = itk.normalize_image_filter(moving_img)
moving_smoothed_image = itk.discrete_gaussian_image_filter(moving_normalized_image, variance=2.0)TransformType = itk.TranslationTransform[itk.D, dim]
OptimizerType = itk.GradientDescentOptimizer
MetricType = itk.MutualInformationImageToImageMetric[ImageType, ImageType]
RegistrationType = itk.ImageRegistrationMethod[ImageType, ImageType]
InterpolatorType = itk.LinearInterpolateImageFunction[ImageType, itk.D]
n_iterations = 200
transform = TransformType.New()
metric = MetricType.New()
optimizer = OptimizerType.New()
registrar = RegistrationType.New()
interpolator = InterpolatorType.New()registrar.SetFixedImage(fixed_smoothed_image)
registrar.SetMovingImage(moving_smoothed_image)
registrar.SetOptimizer(optimizer)
registrar.SetTransform(transform)
registrar.SetInterpolator(interpolator)
registrar.SetMetric(metric)
registrar.SetFixedImageRegion(fixed_img.GetBufferedRegion())
registrar.SetInitialTransformParameters(transform.GetParameters())
numberOfSamples = int(fixed_img.GetBufferedRegion().GetNumberOfPixels() * 0.01)
print('numberOfSamples:', numberOfSamples)
metric.SetNumberOfSpatialSamples(numberOfSamples)
metric.SetFixedImageStandardDeviation(0.4)
metric.SetMovingImageStandardDeviation(0.4)
metric.ReinitializeSeed(121212)optimizer.SetLearningRate(15.0)
optimizer.SetNumberOfIterations(n_iterations)
optimizer.MaximizeOn()def log_iteration():print('Iteration:', optimizer.GetCurrentIteration())print('Value:', optimizer.GetValue())print('CurrentPosition:', list(optimizer.GetCurrentPosition()))optimizer.AddObserver(itk.IterationEvent(), log_iteration)
registrar.Update()
print('-'*50)
print(f"Its: {optimizer.GetCurrentIteration()}")
print(f"Final Value: {optimizer.GetValue()}")
print(f"Final Position: {list(registrar.GetLastTransformParameters())}")ResampleFilterType = itk.ResampleImageFilter[MovingImageType, FixedImageType]
resample = ResampleFilterType.New(Transform=transform,Input=moving_img,Size=fixed_img.GetLargestPossibleRegion().GetSize(),OutputOrigin=fixed_img.GetOrigin(),OutputSpacing=fixed_img.GetSpacing(),OutputDirection=fixed_img.GetDirection(),DefaultPixelValue=100,
)
resample.Update()
output_image_arr = itk.GetArrayFromImage(resample.GetOutput())CheckerBoardFilterType = itk.CheckerBoardImageFilter[FixedImageType]
checker = CheckerBoardFilterType.New()
checker.SetInput1(fixed_img)
checker.SetInput2(resample.GetOutput())
checker.Update()
difference_image_after_arr = itk.GetArrayFromImage(checker.GetOutput())identityTransform = TransformType.New()
identityTransform.SetIdentity()
resample.SetTransform(identityTransform)
checker.Update()
difference_image_before_arr = itk.GetArrayFromImage(checker.GetOutput())# output_image  difference_image_after  difference_image_before
plt.subplot(231), plt.imshow(itk.GetArrayFromImage(fixed_img), 'gray'), plt.title('fixed_img')
plt.subplot(232), plt.imshow(itk.GetArrayFromImage(moving_img), 'gray'), plt.title('moving_img')
plt.subplot(233), plt.imshow(output_image_arr, 'gray'), plt.title('output_image')
plt.subplot(234), plt.imshow(difference_image_after_arr, 'gray'), plt.title('difference_image_after')
plt.subplot(235), plt.imshow(difference_image_before_arr, 'gray'), plt.title('difference_image_before')
# plt.subplot(236), plt.imshow(itk.GetArrayFromImage(moving_smoothed_image), 'gray'), plt.title('moving_smoothed_image')
plt.show()

Register Image to Another Using Landmarks 通过标记点配准图像

原文地址:https://examples.itk.org/src/registration/common/registerimagetoanotherusinglandmarks/documentation

效果图

在这里插入图片描述

源码

import os
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cmimport itk# Perform 2D Translation Registration With Mean Squares
baseDir = r'D:\learn\itk\itk5.3.0\ITK-5.3.0\Examples\Data'
fixed_input_image = baseDir + r"\BrainProtonDensitySlice.png"
moving_input_image = baseDir + r"\BrainProtonDensitySliceShifted13x17y.png"PixelType = itk.ctype("float")
fixedImage = itk.imread(fixed_input_image, PixelType)
movingImage = itk.imread(moving_input_image, PixelType)Dimension = fixedImage.GetImageDimension()
ImageType = itk.Image[PixelType, Dimension]LandmarkPointType = itk.Point[itk.D, Dimension]
LandmarkContainerType = itk.vector[LandmarkPointType]fixed_landmarks = LandmarkContainerType()
moving_landmarks = LandmarkContainerType()
fixed_point = LandmarkPointType()
moving_point = LandmarkPointType()
fixed_point[0] = 66
fixed_point[1] = 79
moving_point[0] = 99
moving_point[1] = 116
fixed_landmarks.push_back(fixed_point)
moving_landmarks.push_back(moving_point)
fixed_point[0] = 115
fixed_point[1] = 81
moving_point[0] = 148
moving_point[1] = 117
fixed_landmarks.push_back(fixed_point)
moving_landmarks.push_back(moving_point)
fixed_point[0] = 75
fixed_point[1] = 139
moving_point[0] = 108
moving_point[1] = 176
fixed_landmarks.push_back(fixed_point)
moving_landmarks.push_back(moving_point)TransformInitializerType = itk.LandmarkBasedTransformInitializer[itk.Transform[itk.D, Dimension, Dimension]
]
transform_initializer = TransformInitializerType.New()
transform_initializer.SetFixedLandmarks(fixed_landmarks)
transform_initializer.SetMovingLandmarks(moving_landmarks)transform = itk.Rigid2DTransform[itk.D].New()
transform_initializer.SetTransform(transform)
transform_initializer.InitializeTransform()output = itk.resample_image_filter(movingImage,transform=transform,use_reference_image=True,reference_image=fixedImage,default_pixel_value=50,
)
print('transform:', transform)output_image_arr = itk.GetArrayFromImage(output)  # 配准后的结果OutputPixelType = itk.ctype("unsigned char")
OutputImageType = itk.Image[OutputPixelType, Dimension]difference = itk.SubtractImageFilter.New(Input1=fixedImage, Input2=output)
intensityRescaler = itk.RescaleIntensityImageFilter[ImageType, OutputImageType
].New(Input=difference,OutputMinimum=itk.NumericTraits[OutputPixelType].min(),OutputMaximum=itk.NumericTraits[OutputPixelType].max(),
)
intensityRescaler.Update()
difference_image_after_arr = itk.GetArrayFromImage(intensityRescaler.GetOutput())  # 配准后与原图像的差别# output_image  difference_image_after  difference_image_before
plt.subplot(221), plt.imshow(itk.GetArrayFromImage(fixedImage), 'gray'), plt.title('fixedImage')
plt.subplot(222), plt.imshow(itk.GetArrayFromImage(movingImage), 'gray'), plt.title('movingImage')
plt.subplot(223), plt.imshow(output_image_arr, 'gray'), plt.title('output_image')
plt.subplot(224), plt.imshow(difference_image_after_arr, 'gray'), plt.title('difference_image_after')
plt.show()

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/293406.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

探索Qt 6.3:了解基本知识点和新特性

学习目标: 理解Qt6.3的基本概念和框架:解释Qt是什么,它的核心思想和设计原则。学会安装和配置Qt6.3开发环境:提供详细的步骤,让读者能够顺利安装和配置Qt6.3的开发环境。掌握Qt6.3的基本编程技巧:介绍Qt6.…

[python]python实现对jenkins 的任务触发

目录 关键词平台说明背景一、安装 python-jenkins 库二、code三、运行 Python 脚本四、注意事项 关键词 python、excel、DBC、jenkins 平台说明 项目Valuepython版本3.6 背景 用python实现对jenkins 的任务触发。 一、安装 python-jenkins 库 pip install python-jenkin…

react生命周期详解,代码示例(新生命周期,与旧生命周期对比)

旧生命周期:https://blog.csdn.net/kkkys_kkk/article/details/135130549?spm1001.2014.3001.5501 目录 React 生命周期中常见的坑 为什么要移除 “will” 相关生命周期方法呢? Fiber是什么 新生命周期图示 新增生命周期与功能变化 完整生命周期…

批量获取虾皮shopee商品详情信息的方法分享(API免费测试)

在当今的电子商务环境中,数据是至关重要的。对于电商平台的商家和开发者来说,获取商品详情信息是他们日常工作的关键部分。虾皮Shopee作为东南亚最大的电商平台,其商品信息对于商家和开发者来说具有极高的价值。本文将分享如何通过API批量获取…

【Java】spring

一、spring spring是一个很大的生态圈,里面有很多技术。 其中最基础的是spring framework,主要的技术 是springboot以及springcloud。 1、spring framework spring framework是spring生态圈中最基础的项目,是其他项目的基础。 1.1、核心…

蓝桥杯:日期问题

目录 引言一、日期问题1.题目描述2.代码实现3.测试 二、回文日期1.题目描述2.代码实现3.测试 引言 关于这个蓝桥杯的日期问题,其实有一个明确的思路就感觉很简单,这个思路就是不用依照日期的顺序去把每一天走完,而是根据一个数加一&#xff…

亿发零售云引领新零售时代:智能收银系统助力连锁门店多业态发展

近来,越来越多零售从业者认识到,线上和线下全渠道整合将成为国内消费市场的主要趋势。在这个趋势中,线下门店仍然被视为品牌发展的重要价值来源。 在连锁门店中,收银系统被认为是运营管理的关键工具,能够为品牌门店提供…

Kylin基础知识点解析与应用探索

目录 学习目标: 学习内容: 学习时间: 学习产出: Kylin简介 什么是Kylin Kylin的历史和发展 Kylin在大数据领域的地位和作用 Kylin架构 Kylin的组成部分和模块 Kylin的工作原理和流程 Kylin与其他大数据组件的关系和集成 Kylin功能…

C语言操作符详解+运算符优先级表格

目录 前言 一、操作符是什么? 二、操作符的分类 三、算术操作符 四、逻辑操作符 五、比较操作符 六、位操作符 七、赋值操作符 八、其他操作符 九、运算符优先级表格 总结 前言 在编写程序时,最常用到的就是操作符,本文将详细的介绍…

JNI学习(二)

静态注册 接着上篇博客学习 JNI函数 JNIEXPORT void JNICALL Java_com_example_jnidemo_TextDemo_setText(JNIEnv *env, jobject this, jstring string){ __android_log_print(ANDROID_LOG_ERROR, "test", "invoke set from C\n");char* str (char*)(*e…

在Linux系统中安装MySQL数据库

目录 一、MySQL简介 二、MySQL安装步骤 1、下载MySQL的YUM仓库文件 2、安装MySQL源 3、解决密钥异常问题 4、安装MySQL服务器 5、开启MySQL服务 6、查看MySQL服务器中root用户的初始密码 7、使用初始密码登录MySQL服务器 8、修改root用户登录MySQL服务器的密码 三、…

【圣诞】极安云科赠书活动第①期:CTF实战:从入门到提升

【圣诞】极安云科赠书活动第①期:CTF实战:从入门到提升 9787111724834 9787121376955 9787302556275 ISBN编号:9787111724834 书名:CTF实战:从入门到提升 定:99.00元 开本:184mm260&#xff…