itk中的数据变大变小

变变变,图像处理的实质就是对图像的数学变换。本文简单介绍图像的变大变小,大致可以理解为就是图像的插值运算。

1.变大
在《itk中的花式数据切割(一)》中提到过,对原始数据切割后,size会变小,在对ROI区域做各种变换后,得到的结果其实还是要和原始数据最对比的,这时就需要resize,即还原图像大小。
typedef itk::ResampleImageFilter<ImageType, ImageType> ImageFilterType;
ImageFilterType::Pointer resample = ImageFilterType::New();
resample->SetInput(input_data);

typedef itk::AffineTransform<double,3> TransformType;
TransformType::Pointer transform = TransformType::New();
resample->SetTransform(transform);

typedef itk::NearestNeighborInterpolateImageFunction<ImageType,double> InterpolatorType;
InterpolatorType::Pointer interpolator = InterpolatorType::New();
resample->SetInterpolator(interpolator);

resample->SetDefaultPixelValue(m_DefautPixel);
resample->SetSize(src_data->GetLargestPossibleRegion().GetSize());
resample->SetOutputSpacing(src_data->GetSpacing());
resample->SetOutputOrigin(src_data->GetOrigin());
resample->SetOutputStartIndex(src_data->GetLargestPossibleRegion().GetIndex());

ImageType::DirectionType direction;
direction.SetIdentity();
resample->SetOutputDirection(direction);
try
{    
	resample->UpdateLargestPossibleRegion();
}
catch( itk::ExceptionObject & exp )
{
	cerr << "Exception caught !" << std::endl;
	cerr << exp << std::endl;
}
output_data = resample->GetOutput();

思路:这里说到的大小实际上包括三部分:大小,原点,像素间距,将这三要素从原始数据src_data中取出来,通过itkResampleImageFilter方法赋给input_data就可以了。
2.变小
在不考虑精度的前提下,可以对原始数据进行采样。
typedef itk::Image<float,3> FImageType;
typedef itk::CastImageFilter<ImageType,FImageType> CastType;
CastType::Pointer castFilter = CastType::New();
castFilter->SetInput(input_data);
castFilter->Update();

ImageType::SizeType outputSize;
ImageType::SpacingType inputSpace = input->GetSpacing();
ImageType::SpacingType outputSpacing;
outputSpacing[0] = input_data->GetSpacing()[0] * (static_cast<double>( factor_a));
outputSpacing[1] = input_data->GetSpacing()[1] * (static_cast<double>( factor_a));
outputSpacing[2] = input_data->GetSpacing()[2] * (static_cast<double>( factor_a));

ImageType::IndexType startIndex = input_data->GetLargestPossibleRegion().GetIndex();
ImageType::IndexType newStart;

for (inti = 0 ; i < 3 ; ++i)
{
	newStart[i] = static_cast<unsigned short>(static_cast<double>(startIndex[i])*inputSpace[i]/outputSpacing[i]);
}
for (inti = 0 ; i < 3 ; ++i)
{
	outputSize[i] = static_cast<short>(static_cast<double>(input->GetLargestPossibleRegion().GetSize()[i])*
		input->GetSpacing()[i]/outputSpacing[i] + 0.5);
}
typedef itk::AffineTransform< double, 3 > TransformType;
typedef itk::ResampleImageFilter<FImageType, ImageType> ResampleImageFilterType;
ResampleImageFilterType::Pointer resample = ResampleImageFilterType::New();
resample->SetInput(castFilter->GetOutput());
resample->SetSize(outputSize);
resample->SetOutputSpacing(outputSpacing);
resample->SetOutputDirection(input_data->GetDirection());
resample->SetOutputOrigin(input_data->GetOrigin());
resample->SetDefaultPixelValue(0);
resample->SetOutputStartIndex(newStart);

TransformType::Pointer idTransf = TransformType::New();
resample->SetTransform(idTransf);

typedef itk::LinearInterpolateImageFunction<FImageType, double > InterpolatorType;
InterpolatorType::Pointer interpolator = InterpolatorType::New();
resample->SetInterpolator( interpolator );

try
{    
	resample->UpdateLargestPossibleRegion();
}
catch( itk::ExceptionObject & exp )
{
	cerr << "Exception caught !" << std::endl;
	cerr << exp << std::endl;
	throw;
}
output_data  = resample->GetOutput();

思路:保留大的,丢掉小的。变小就是对原始数据的点进行采样,但会损失精度。
参考文献:
2.《三维图像编程实验》

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值