itk中的数据变大变小
程序员文章站
2022-05-31 09:13:02
...
变变变,图像处理的实质就是对图像的数学变换。本文简单介绍图像的变大变小,大致可以理解为就是图像的插值运算。
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();
思路:保留大的,丢掉小的。变小就是对原始数据的点进行采样,但会损失精度。
上一篇: m阶B树中“阶”的含义