Increase the image size by padding with a constant value.
But I would like the output image to have the same size as the input image, i.e. I would like the padding to go “inward” and overwrite some of the content of my image.
Just for information, here is the code I have that creates nice black margins but increase the size of my image by 10 in all dimensions (5+5).
// add black border to prevent leakage
using ConstantPadImageFilterType = itk::ConstantPadImageFilter <Image3d, Image3d>;
ConstantPadImageFilterType::Pointer padFilter = ConstantPadImageFilterType::New();
Image3d::SizeType v_ExtendRegion;
v_ExtendRegion.Fill(5); // 5 pixels wide
padFilter->SetInput(v_CropedImg);
padFilter->SetPadLowerBound(v_ExtendRegion);
padFilter->SetPadUpperBound(v_ExtendRegion);
padFilter->SetConstant(-3000); // very low value (black)
padFilter->Update();
I had to set the cropsize to 5 and not 10 though. Otherwise the resulting image is somehow 10 pixels wider. It seems contre-intuitive when I read the comments from the page you linked.
The code bellow works for me.
// add black border to prevent leakage
// first shrink the image
using CropImageFilterType = itk::CropImageFilter <Image3d, Image3d>;
CropImageFilterType::Pointer cropFilter = CropImageFilterType::New();
cropFilter->SetInput(v_CropedImg);
Image3d::SizeType cropSize;
cropSize.Fill(5);
cropFilter->SetBoundaryCropSize(cropSize);
// then expand with black borders
using ConstantPadImageFilterType = itk::ConstantPadImageFilter <Image3d, Image3d>;
ConstantPadImageFilterType::Pointer padFilter = ConstantPadImageFilterType::New();
Image3d::SizeType v_ExtendRegion;
v_ExtendRegion.Fill(5);
padFilter->SetInput(cropFilter->GetOutput());
padFilter->SetPadLowerBound(v_ExtendRegion);
padFilter->SetPadUpperBound(v_ExtendRegion);
padFilter->SetConstant(-3000);
padFilter->Update();
return padFilter->GetOutput();
Glad to hear it worked and seems easier if crop and pad share the same size, I was confused by the comment in the example.
By the way, instead of using -3000 as the constant you might find useful to use: itk::NumericTraits< Image3d::PixelType >::min() that will put a black pixel for any kind of image. You would need to #include "itkNumericTraits.h" for it to work.
Take into account that itk::NumericTraits::min() is the lowest possible pixel value given the pixel type (double, int, etc), the actual min value found in your image might be other. So your computation of that might be useful !. ITK provides a way to compute min,max, mean, etc of an image in: https://itk.org/Doxygen/html/classitk_1_1StatisticsImageFilter.html
Yeah that’s the filter I used. I needed a low-enough value not to be mistaken as foreground. itk::NumericTraits::min() always qualifies for it in my application and is easier to compute.