【ITK库学习】使用itk库进行图像滤波ImageFilter:几何变换:翻转、重采样

1、itkFlipImageFilter 图像翻转滤波器

该类的主要功能是使输入数据在用户指定的轴上进行翻转。

翻转轴通过函数SetFlipAxes(array) 设置,其中输入是FixArray<bool,ImageDimension>。 图像在 array[i] 为 true 的轴上翻转。

就网格坐标而言,图像在输入图像的最大可能区域内翻转,因此,输出图像的 LargestPossibleRegion与输入图像相同。

就几何坐标而言,输出原点是图像相对于坐标轴翻转的。

常用的成员函数

  • Set/GetFlipAxes():设置/获取要翻转的轴, 图像沿着array[i]为true的轴翻转,默认为 false
  • Set/GetFlipAboutOrigin():设置/获取计算的输出原点,如果FlipAboutOrigin为“On”,则翻转将围绕轴的原点发生,如为“false”则翻转将围绕轴的中心发生,默认为“On”
  • FlipAboutOriginOn/Off():同上

示例代码

#include "itkImage.h"
#include "itkFlipImageFilter.h"

typedef itk::Image<short, 3> ShortImageType;

bool flipImageFilter(ShortImageType* image, ShortImageType* outImage)
{
	typename FlipFilterType::FlipAxesArrayType flipArray;
	flipArray[0] = true;      //沿x轴翻转
	flipArray[1] = false;
	flipArray[2] = false;

    typedef itk::FlipImageFilter<ShortImageType>  FlipFilterType;
	typename FlipFilterType::Pointer flipFilter = FlipFilterType::New();
	flipFilter->SetInput(image);
	flipFilter->SetFlipAxes(flipArray);
	flipFilter->SetFlipAboutOrigin(false);   //On:围绕圆点进行翻转,false:围绕轴中心进行翻转,默认为“On”
	//flipFilter->FlipAboutOriginOn();       //与SetFlipAboutOrigin功能一样
	//flipFilter->FlipAboutOriginOff();
	try
	{
		flipFilter->Update();
	}
	catch (itk::ExceptionObject& ex)
	{
		//读取过程发生错误
		std::cerr << "Error: " << ex << std::endl;
		return false;
	}

	outImage = flipFilter->GetOutput();
	return true;
}

2、itkResampleImageFilter 重采样图像滤波器

该类通过坐标变换对图像进行重新采样,并通过插值函数对图像进行插值

该类是根据输入和输出图像的类型进行模板化的,

请注意,插值器函数的选择可能很重要,该函数通过**SetInterpolator()**进行设置。 默认为 LinearInterpolateImageFunction<InputImageType, TInterpolatorPrecisionType>,这对于普通医学图像来说是合理的。 然而,一些合成图像的像素是从有限的指定集合中提取的, 比如一个掩模,它指示将大脑分割成少量的组织类型,对于这样的图像,一般不在不同的像素值之间进行插值,NearestNeighborInterpolateImageFunction<InputImageType, TCoordRep> 会是更好的选择。

如果样本是从图像域外部获取的,则默认行为是使用默认像素值。 如果需要不同的行为,可以使用 SetExtrapolator() 设置外推器函数

应设置输出图像的输出信息(间距、大小和方向),该信息具有单位间距、零原点和同一方向的正常默认值,输出信息可以从参考图像获取;如果提供了参考图像并且UseReferenceImage 为“On”,则将使用参考图像的间距、原点和方向。重采样是在空间坐标中执行,而不是像素/网格坐标。

由于此过滤器生成的图像与其输入的大小不同,因此它需要重写ProcessObject中定义的多个方法,以便正确管理管道执行模型。 特别是,此过滤器重写 ProcessObject::GenerateInputRequestedRegion() 和 ProcessObject::GenerateOutputInformation()。

此过滤器亦可实现多线程过滤器,使用DynamicThreadedGenerateData() 方法来实现。

常用的成员函数

  • Set/GetInterpolator():设置/获取插值器函数,默认值为 LinearInterpolateImageFunction,其他选项:NearestNeighborInterpolateImageFunction(对于二进制蒙版和其他具有少量可能像素值的图像很有用)和 BSplineInterpolateImageFunction(提供更高阶的插值)
  • SetExtrapolator():设置/获取外推器函数,默认值为 DefaultPixelValue,其他选项:NearestNeighborExtrapolateImageFunction
  • SetDefaultPixelValue():当设置/获取变换后的像素位于图像外部的像素值,默认为 0
  • GetOutputStartIndex():获取输出最大可能区域的起始索引
  • Set/GetSize():设置/获取输出图像的大小
  • Set/GetOutputDirection():设置/获取输出方向余弦矩阵
  • Set/GetOutputOrigin():设置/获取输出图像的原点
  • Set/GetOutputSpacing():设置/获取输出图像的像素间距
  • SetOutputParametersFromImage():根据此图像设置输出参数的辅助方法
  • Set/GetTransformInput():设置/获取用于重采样的坐标变换,注意,这必须在物理坐标中,并且它是输出到输入的变换,默认情况下,过滤器使用身份转换,如果不想使用默认的Identity转换,则在尝试运行过滤器之前,必须在此处提供不同的转换
  • UseReferenceImageOn/Off():打开/关闭是否应使用指定的参考图像来定义输出信息
  • Set/GetUseReferenceImage():同上true:On,false:Off
  • Set/GetReferenceImage():设置用于定义输出信息的参考图像,默认情况下,输出信息通过 SetOutputSpacing、SetOutputOrigin 和 SetOutputDirection 或 SetOutputParametersFromImage 方法指定,此方法可用于指定要从中复制像素信息的图像,必须设置UseReferenceImageOn才能使用参考图像

坐标变换
恒等变换:输出图像点(x,y,x)的像素值 = 输入图像点(x,y,z)的像素值,点坐标均为同一个空间坐标。

非恒等:当输入输出图像的原点Orign和间距Space相同时,从输出空间到输入空间的点映射,(-30,-50,-10)的变换,指输出图像点(x,y,x)的像素值=输入图像点(x-30,y-50,z-10)的像素值。

恒等变换计算公式

输出图像参数:
原点 orignOut = {orignOut0,orignOut1,orignOut2}
间距 spaceOut = {spaceOut0,spaceOut1,spaceOut2}
尺寸大小 size= {sizeOut0,sizeOut1,sizeOut2}

输出图像参数:
原点 orignIn = {orignIn0,orignIn1,orignIn2}
间距 spaceIn={spaceIn0,space1In,spaceIn2}
尺寸大小sizeIn ={sizeIn0,sizeIn1,sizeIn2}

求像素值:
输出图像点I[3] = {X, Y, Z}的像素将于空间坐标点 P 相关联

P 的坐标值:P[3] = {X*spaceOut0+orignOut0, Y*spaceOut1+orignOut1, Z*spaceOut2+orignOut2}

P 点在输入图像对应的点 Q 坐标值为:Q[3] = {(P[0]-orignIn0)/spaceIn0, (P[1]-orignIn1)/spaceIn1, (P[1]-orignIn1)/spaceIn1}

如果Q不是整数坐标,那么输出图像点 I 的像素值 = 输入图像中围绕非整数标记 Q 插入值来计算的。

示例代码

#include "itkImage.h"
#include "itkAffineTransform.h"
#include "itkNearestNeighborInterpolateImageFunction.h"
#include "itkResampleImageFilter.h"

typedef itk::Image<short, 3> ShortImageType;
typedef itk::Image<float, 3> FloatImageType;

bool resampleImageFilter(ShortImageType* image, FloatImageType* outImage)
{
	double space[3] = { 1,1,1 };
	double origin[3] = { 0,0,0 };
	ShortImageType::SizeType size = { 300,300,300 };

	typedef itk::ResampleImageFilter<ShortImageType, FloatImageType>  ResampleFilterType;
	typename ResampleFilterType::Pointer resampleFilter = ResampleFilterType::New();
	resampleFilter->SetInput(image);

	//使用用来表示空间坐标类型和图像维度来定义变换类型,默认的设置变换参数来表示恒等变换
	typedef itk::AffineTransform<double, 3> TransformType;
	typename TransformType::Pointer transform = TransformType::New();
	//typename TransformType::OutputVectorType translation;
	//translation[0] = -30;
	//translation[1] = -50;
	//translation[2] = -10;
	//transform->Translate(translation);
	transform->SetIdentity();
	resampleFilter->SetTransform(transform);    

	//使用用来表示空间坐标类型和图像类型来定义校对机类型
	typedef itk::NearestNeighborInterpolateImageFunction<ShortImageType, double> NNInterpolateType;
	typename NNInterpolateType::Pointer interpolate = NNInterpolateType::New();
	resampleFilter->SetInterpolator(interpolate);

    //输出参数设置
	resampleFilter->SetDefaultPixelValue(0);
	resampleFilter->SetOutputSpacing(space);
	resampleFilter->SetOutputOrigin(origin);
	resampleFilter->SetSize(size);
	try
	{
		resampleFilter->Update();
	}
	catch (itk::ExceptionObject& ex)
	{
		//读取过程发生错误
		std::cerr << "Error: " << ex << std::endl;
		return false;
	}

	outImage = resampleFilter->GetOutput();
	return true;
}

旋转图像

旋转是围绕物理坐标的原点而不是图像原点也不是图像中心执行的。

旋转步骤:
(1)将图像原点移动到坐标系的原点,可以通过使用与图像原点相反的值来完成变换;
(2)设置旋转角度,可以用itk::AffineTransform实现,如果变换的当前更新需要预构成或后构成当前转换内容,那么就需要第二个bool变量指定,false:旋转需要应用在当前变换内容之后;
(3)将图像原点转变为它原来的位置。

    typedef itk::AffineTransform<double, 3> TransformType;
    typename TransformType::Pointer transform = TransformType::New();
    //第一步
	const double imageCenter[3] = { origin[0] + space[0] * size[0] / 2,origin[1] + space[1] * size[1] / 2,origin[2] + space[2] * size[2] / 2};
	typename TransformType::OutputVectorType translation1;
	translation1[0] = -imageCenter[0];
	translation1[1] = -imageCenter[1];
	translation1[2] = -imageCenter[2];
	transform->Translate(translation1);
	//第二步
	const double rotateAngle = 30;   //旋转角度
	const double degree = atan(1.0) / 45;
	const double angle = rotateAngle * degree;
	transform->Rotate3D(-angle, false);
	//第三步
	typename TransformType::OutputVectorType translation2;
	translation2[0] = imageCenter[0];
	translation2[1] = imageCenter[1];
	translation2[2] = imageCenter[2];
	transform->Translate(translation2,false);
    resampleFilter->SetTransform(transform);    
  • 27
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值