[Insight-users] Reuse of filter with different parameters and images
Krzysztof J. Rechowicz
k.rechowicz at gmail.com
Wed Nov 7 22:54:37 EST 2012
Hello,
In the piece of code I am working on I want to reuse ResampleFilter.
The first use downscale the image to low resolution (which is
performed correctly) and then the output is used to be upscaled using
bicubic interpolation. After this operation, the resulting image is
identical as the image that was read in (the original image before
applying any filters) instead of being blurred. Using another instance
of the filter for the second part works well and produces the right
output. How to correctly reuse a filter (in this case ResampleFilter)?
Thank you in advance,
Krzysztof
// Instantiate the transform and specify it should be the id transform.
TransformType::Pointer _pTransform = TransformType::New();
_pTransform->SetIdentity();
// Instantiate the b-spline interpolator and set it as the third order
// for bicubic.
InterpolatorType::Pointer _pInterpolator = InterpolatorType::New();
_pInterpolator->SetSplineOrder(3);
// Instantiate the resampler. Wire in the transform and the interpolator.
ResampleFilterType::Pointer _pResizeFilter = ResampleFilterType::New();
_pResizeFilter->SetTransform(_pTransform);
_pResizeFilter->SetInterpolator(_pInterpolator);
const double vfOutputOrigin[2] = { 0.0, 0.0 };
_pResizeFilter->SetOutputOrigin(vfOutputOrigin);
// Fetch original image size.
const ImageType::RegionType& inputRegion = image->GetLargestPossibleRegion();
const ImageType::SizeType& vnInputSize = inputRegion.GetSize();
unsigned int nOldWidth = vnInputSize[0];
unsigned int nOldHeight = vnInputSize[1];
unsigned int nNewWidth = vnInputSize[0]/scale;
unsigned int nNewHeight = vnInputSize[1]/scale;
// Fetch original image spacing.
const ImageType::SpacingType& vfInputSpacing = image->GetSpacing();
double vfOutputSpacing[2];
vfOutputSpacing[0] = vfInputSpacing[0] * (double) nOldWidth / (double)
nNewWidth;
vfOutputSpacing[1] = vfInputSpacing[1] * (double) nOldHeight /
(double) nNewHeight;
_pResizeFilter->SetOutputSpacing(vfOutputSpacing);
itk::Size<2> vnOutputSize = { {nNewWidth, nNewHeight} };
_pResizeFilter->SetSize(vnOutputSize);
_pResizeFilter->SetInput(image);
_pResizeFilter->Update();
ImageType::Pointer lores = _pResizeFilter->GetOutput();
_pResizeFilter->UpdateLargestPossibleRegion();
const ImageType::RegionType& inputRegion2 = lores->GetLargestPossibleRegion();
const ImageType::SizeType& vnInputSize2 = inputRegion2.GetSize();
nOldWidth = vnInputSize2[0];
nOldHeight = vnInputSize2[1];
//
nNewWidth = vnInputSize2[0]*scale;
nNewHeight = vnInputSize2[1]*scale;
const ImageType::SpacingType& vfInputSpacingLow = lores->GetSpacing();
vfOutputSpacing[0] = vfInputSpacingLow[0] * (double) nOldWidth /
(double) nNewWidth;
vfOutputSpacing[1] = vfInputSpacingLow[1] * (double) nOldHeight /
(double) nNewHeight;
_pResizeFilter->SetTransform(_pTransform);
_pResizeFilter->SetInterpolator(_pInterpolator);
_pResizeFilter->SetOutputOrigin(vfOutputOrigin);
_pResizeFilter->SetOutputSpacing(vfOutputSpacing);
vnOutputSize[0] = nNewWidth;
vnOutputSize[1] = nNewHeight;
_pResizeFilter->SetSize(vnOutputSize);
_pResizeFilter->UpdateLargestPossibleRegion();
_pResizeFilter->SetInput(lores);
_pResizeFilter->Modified();
_pResizeFilter->Update();
More information about the Insight-users
mailing list