# Wired ResampleImageFilter results after downsampling a 3D image and then upsampling it to the original size

**URL:** <https://discourse.itk.org/t/wired-resampleimagefilter-results-after-downsampling-a-3d-image-and-then-upsampling-it-to-the-original-size/5193>\
**Category:** Algorithms\
**Created:** [July 18, 2022, 2:58pm UTC](https://discourse.itk.org/t/wired-resampleimagefilter-results-after-downsampling-a-3d-image-and-then-upsampling-it-to-the-original-size/5193 "2022-07-18T14:58:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ruoyan](https://discourse.itk.org/letter_avatar_proxy/v4/letter/r/7feea3/32.png) [@Ruoyan](https://discourse.itk.org/u/Ruoyan)\
**Post date:** [July 18, 2022, 2:58pm UTC](https://discourse.itk.org/t/wired-resampleimagefilter-results-after-downsampling-a-3d-image-and-then-upsampling-it-to-the-original-size/5193/1 "2022-07-18T14:58:55Z")

</div>

Hi,  
I’m trying to downsample an image to `50*50*50` and then upsample it back to the original size. After I processed this with two ResampleImageFilter, the new upsampled image has `2*z_size*y_size ` black/empty pixels at the end of x-direction.  
Original look:

 ![Screenshot from 2022-07-18 16-22-59](https://discourse.itk.org/uploads/default/original/2X/4/4e292b5c71052968629dbb1acfe411e41eb250e5.png)  
Downsampled look:  
 ![Screenshot from 2022-07-18 16-24-04](https://discourse.itk.org/uploads/default/original/2X/4/47a5360696def3d98622d8e35a89f21712798545.png)  
Upsampled look:  
 ![Screenshot from 2022-07-18 16-29-53](https://discourse.itk.org/uploads/default/original/2X/5/5a489e3b7d3fde0e56e4c2758d7d7b32a5e62851.png)

Any idea what could lead to this problem? Thanks a lot!

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [July 18, 2022, 3:07pm UTC](https://discourse.itk.org/t/wired-resampleimagefilter-results-after-downsampling-a-3d-image-and-then-upsampling-it-to-the-original-size/5193/2 "2022-07-18T15:07:07Z")

</div>

You might be assuming that pixel coordinates are in the corner. They are at the [center of the pixel](https://itk.org/ITKSoftwareGuide/html/Book1/ITKSoftwareGuide-Book1ch4.html#x45-54001r1). You might need to adjust your math to account for this.

---

<div class="post-metadata">

**Author:** ![Ruoyan](https://discourse.itk.org/letter_avatar_proxy/v4/letter/r/7feea3/32.png) [@Ruoyan](https://discourse.itk.org/u/Ruoyan)\
**Post date:** [July 18, 2022, 3:36pm UTC](https://discourse.itk.org/t/wired-resampleimagefilter-results-after-downsampling-a-3d-image-and-then-upsampling-it-to-the-original-size/5193/3 "2022-07-18T15:36:54Z")

</div>

Thanks for the quick reply. In my case, the origin of these 3 images are the same. If I understand correctly, for the upsampled image it should have the same size and physical coordinates as the original image. Am I correct?

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [July 18, 2022, 3:49pm UTC](https://discourse.itk.org/t/wired-resampleimagefilter-results-after-downsampling-a-3d-image-and-then-upsampling-it-to-the-original-size/5193/4 "2022-07-18T15:49:55Z")

</div>

What is the original resolution? Can you share your code?

---

<div class="post-metadata">

**Author:** ![Ruoyan](https://discourse.itk.org/letter_avatar_proxy/v4/letter/r/7feea3/32.png) [@Ruoyan](https://discourse.itk.org/u/Ruoyan)\
**Post date:** [July 18, 2022, 5:28pm UTC](https://discourse.itk.org/t/wired-resampleimagefilter-results-after-downsampling-a-3d-image-and-then-upsampling-it-to-the-original-size/5193/5 "2022-07-18T17:28:43Z")

</div>

Hi, here is the code

```auto
// GET ORIGINAL IMAGE INFO
    auto inputImage = imageAReader->GetOutput();
    auto outputOrigin = inputImage->GetOrigin();
    auto outputDirection= inputImage->GetDirection();
    auto inputSizePixels = inputImage->GetLargestPossibleRegion().GetSize();//[279,212,89]
    auto inputSpacing = inputImage->GetSpacing();
    double inputSize[3] = {inputSpacing[0] * inputSizePixels[0],
                           inputSpacing[1] * inputSizePixels[1],
                           inputSpacing[2] * inputSizePixels[2]};

    auto _pTransform = T_Transform::New();
    _pTransform->SetIdentity();
    typename InterpolatorType::Pointer interpolator = BSplineInterpolatorType::New();

    // DOWNSAMPLING
    auto imageResampleFilterD = ResampleImageFilterType::New();
    typename ImageType::SizeType outputSizeD = {static_cast<SizeValueType>(50),static_cast<SizeValueType>(50),static_cast<SizeValueType>(50)};
    typename ImageType::SpacingType outputSpacingD;
    outputSpacingD[0] = inputSize[0] / static_cast<float>(50);
    outputSpacingD[1] = inputSize[1] / static_cast<float>(50);
    outputSpacingD[2] = inputSize[2] / static_cast<float>(50);

    imageResampleFilterD->SetInput(imageAReader->GetOutput());
    imageResampleFilterD->SetOutputOrigin(outputOrigin);
    imageResampleFilterD->SetOutputSpacing(outputSpacingD);
    imageResampleFilterD->SetOutputDirection(outputDirection);
    imageResampleFilterD->SetInterpolator(interpolator);
    imageResampleFilterD->SetSize(outputSizeD);
    imageResampleFilterD->SetTransform(_pTransform);
    imageResampleFilterD->Update();

    // UPSAMPLING
    auto imageResampleFilterU = ResampleImageFilterType::New();

    imageResampleFilterU->SetInput(imageResampleFilterD->GetOutput());
    imageResampleFilterU->SetOutputOrigin(outputOrigin);
    imageResampleFilterU->SetOutputSpacing(inputSpacing);
    imageResampleFilterU->SetOutputDirection(outputDirection);
    imageResampleFilterU->SetInterpolator(interpolator);
    imageResampleFilterU->SetSize(inputSizePixels);
    imageResampleFilterU->SetTransform(_pTransform);
    imageResampleFilterU->Update();

```

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [July 18, 2022, 6:57pm UTC](https://discourse.itk.org/t/wired-resampleimagefilter-results-after-downsampling-a-3d-image-and-then-upsampling-it-to-the-original-size/5193/6 "2022-07-18T18:57:55Z")

</div>

Consider 1D case of 8 pixels:

 ![2022-07-18 14.41.27](https://discourse.itk.org/uploads/default/original/2X/a/ad480783fab2dc030c5d725b24a0bff0fa0c2028.jpeg)  
First row is the original image, filled with 7s.  
Second row is down-sampled to 2 pixels.  
Third row is expansion back to 8 pixels.

You need to adjust the origin to keep the same image extent. This becomes obvious when you go back to your original image grid.

---

<div class="post-metadata">

**Author:** ![Ruoyan](https://discourse.itk.org/letter_avatar_proxy/v4/letter/r/7feea3/32.png) [@Ruoyan](https://discourse.itk.org/u/Ruoyan)\
**Post date:** [July 18, 2022, 9:33pm UTC](https://discourse.itk.org/t/wired-resampleimagefilter-results-after-downsampling-a-3d-image-and-then-upsampling-it-to-the-original-size/5193/7 "2022-07-18T21:33:47Z")

</div>

Thanks a lot! This explains why in our case we lost some image data. I’ll try to adjust the origin for each resample processes. Thanks again for your help🙌
