今天继续研究数据切割,也是对之前四篇切割内容的补充,内容不多,step by step。
itk中有一个类叫做itkCropImageFilter,是itkExtractImageFilter的派生方法。
先上代码,看一下基本用法:
ImageType::SizeType extractSize = {{8, 12}};
extractSize[0] = 1;
extractSize[1] = 1;
typedef itk::CropImageFilter <ImageType, ImageType> CropImageFilterType;
CropImageFilterType::Pointer cropFilter = CropImageFilterType::New();
cropFilter->SetInput(input_data);
cropFilter->SetBoundaryCropSize(extractSize); //和下面两句重复
//cropFilter->SetUpperBoundaryCropSize(extractSize);
//cropFilter->SetLowerBoundaryCropSize(extractSize);
cropFilter->UpdateLargestPossibleRegion();
cropFilter->Update();
接下来咱们一起看源码是怎么写的,这个类超简单,就一个函数,但我看了还是诧异,+_+:
template <class TInputImage, class TOutputImage>// itk的模版
void
CropImageFilter<TInputImage, TOutputImage>
::GenerateOutputInformation()//itk的数据生成都用的这个函数名
{
const TInputImage * inputPtr = this->GetInput();
if( !inputPtr )//防爆安全代码
{
return;
}
// Compute the new region size.
OutputImageRegionType croppedRegion;
SizeType sz;
OutputImageIndexType idx;
InputImageSizeType input_sz =
inputPtr->GetLargestPossibleRegion().GetSize();
InputImageIndexType input_idx =
inputPtr->GetLargestPossibleRegion().GetIndex();
idx = input_idx + m_LowerBoundaryCropSize;
sz = input_sz - (m_UpperBoundaryCropSize + m_LowerBoundaryCropSize);
croppedRegion.SetSize(sz);
croppedRegion.SetIndex(idx);
// Set extraction region in the superclass.
this->SetExtractionRegion(croppedRegion);//父类的函数生效
//
Superclass::GenerateOutputInformation();
}
看下面两行!@_@,我有点晕了,亲测后,原来是按照给定的size对原始图像做对称切割!
idx = input_idx + m_LowerBoundaryCropSize;
sz = input_sz - (m_UpperBoundaryCropSize + m_LowerBoundaryCropSize);
测试结果:(貌似这是第一个与文章内容相关的图)
这个方法,挺好用,但怎么用呢?貌似也没什么情况用得到啊~
别急,别急,一件一件来~
---“勤学似春起之苗,不见其增,日有所长。” 与大家共勉。
参考文献:1. https://itk.org/Wiki/ITK/Examples/ImageProcessing/CropImageFilter