# \[solved\] Padding without extending image size

**URL:** https://discourse.itk.org/t/solved-padding-without-extending-image-size/847
**Category:** Algorithms
**Created:** [April 18, 2018, 12:57pm UTC](https://discourse.itk.org/t/solved-padding-without-extending-image-size/847 "2018-04-18T12:57:10Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![antoine.letouzey](https://discourse.itk.org/user_avatar/discourse.itk.org/antoine.letouzey/32/57_2.png) [@antoine.letouzey](https://discourse.itk.org/u/antoine.letouzey)
#### Post date: [April 18, 2018, 12:57pm UTC](https://discourse.itk.org/t/solved-padding-without-extending-image-size/847/1 "2018-04-18T12:57:10Z")

</div>

Hi,

I’m trying to add an uniform (in color and size) margin to a 3D image and the only filter that always comes up in google is itk::ConstantPadImageFilter. [https://itk.org/Doxygen/html/classitk\_1\_1ConstantPadImageFilter.html](https://itk.org/Doxygen/html/classitk_1_1ConstantPadImageFilter.html)

Documentation clearly states:

> 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();

Any idea on how to proceed from there ?

---

<div class="post-metadata">

### Author: ![phcerdan](https://discourse.itk.org/user_avatar/discourse.itk.org/phcerdan/32/286_2.png) [@phcerdan](https://discourse.itk.org/u/phcerdan)
#### Post date: [April 18, 2018, 1:14pm UTC](https://discourse.itk.org/t/solved-padding-without-extending-image-size/847/2 "2018-04-18T13:14:59Z")

</div>

One approach is to first crop the image, see [example](https://itk.org/Wiki/ITK/Examples/ImageProcessing/CropImageFilter). And then pad the cropped image.

Following the example, I think the cropSize you want is 10 (this will crop 5 from each side).

```cpp
Image3d::SizeType cropSize;
cropSize.Fill(10)

```

And then use your current pad size `v_ExtendRegion.Fill(5)` with the cropped image.

---

<div class="post-metadata">

### Author: ![antoine.letouzey](https://discourse.itk.org/user_avatar/discourse.itk.org/antoine.letouzey/32/57_2.png) [@antoine.letouzey](https://discourse.itk.org/u/antoine.letouzey)
#### Post date: [April 18, 2018, 1:45pm UTC](https://discourse.itk.org/t/solved-padding-without-extending-image-size/847/3 "2018-04-18T13:45:25Z")

</div>

Well, thanks a lot, that did the trick.

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();

---

<div class="post-metadata">

### Author: ![phcerdan](https://discourse.itk.org/user_avatar/discourse.itk.org/phcerdan/32/286_2.png) [@phcerdan](https://discourse.itk.org/u/phcerdan)
#### Post date: [April 18, 2018, 2:01pm UTC](https://discourse.itk.org/t/solved-padding-without-extending-image-size/847/4 "2018-04-18T14:01:55Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![antoine.letouzey](https://discourse.itk.org/user_avatar/discourse.itk.org/antoine.letouzey/32/57_2.png) [@antoine.letouzey](https://discourse.itk.org/u/antoine.letouzey)
#### Post date: [April 18, 2018, 2:03pm UTC](https://discourse.itk.org/t/solved-padding-without-extending-image-size/847/5 "2018-04-18T14:03:43Z")

</div>

thanks for the tip about the min value I added another filter to compute the min value of the image but this is better and faster 🙂

---

<div class="post-metadata">

### Author: ![phcerdan](https://discourse.itk.org/user_avatar/discourse.itk.org/phcerdan/32/286_2.png) [@phcerdan](https://discourse.itk.org/u/phcerdan)
#### Post date: [April 18, 2018, 2:16pm UTC](https://discourse.itk.org/t/solved-padding-without-extending-image-size/847/6 "2018-04-18T14:16:09Z")

</div>

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](https://itk.org/Doxygen/html/classitk_1_1StatisticsImageFilter.html)

---

<div class="post-metadata">

### Author: ![antoine.letouzey](https://discourse.itk.org/user_avatar/discourse.itk.org/antoine.letouzey/32/57_2.png) [@antoine.letouzey](https://discourse.itk.org/u/antoine.letouzey)
#### Post date: [April 18, 2018, 2:18pm UTC](https://discourse.itk.org/t/solved-padding-without-extending-image-size/847/7 "2018-04-18T14:18:14Z")

</div>

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.
