# Apply 3D mask to 4D image - itkSliceBySliceImageFilter

**URL:** <https://discourse.itk.org/t/apply-3d-mask-to-4d-image-itkslicebysliceimagefilter/2694>\
**Category:** Algorithms\
**Tags:** mask\
**Created:** [February 9, 2020, 6:57pm UTC](https://discourse.itk.org/t/apply-3d-mask-to-4d-image-itkslicebysliceimagefilter/2694 "2020-02-09T18:57:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![javierjuan](https://discourse.itk.org/letter_avatar_proxy/v4/letter/j/b5a626/32.png) [@javierjuan](https://discourse.itk.org/u/javierjuan)\
**Post date:** [February 9, 2020, 6:57pm UTC](https://discourse.itk.org/t/apply-3d-mask-to-4d-image-itkslicebysliceimagefilter/2694/1 "2020-02-09T18:57:03Z")

</div>

Hello,

I am trying to mask a 4D image with a 3D mask with the itkSliceBySliceImageFilter. My piece of code is:

```auto
using SliceBySliceImageFilterType = itk::SliceBySliceImageFilter<ImageType, ImageType>;
typename SliceBySliceImageFilterType::Pointer sliceBySliceImageFilter = SliceBySliceImageFilterType::New();
sliceBySliceImageFilter->SetInput(image);
using MaskImageFilterType = itk::MaskImageFilter<typename SliceBySliceImageFilterType::InternalInputImageType, MaskType>;
typename MaskImageFilterType::Pointer maskFilter = MaskImageFilterType::New();
maskFilter->SetMaskImage(mask);
sliceBySliceImageFilter->SetFilter(maskFilter);
sliceBySliceImageFilter->Update();
typename ImageType::Pointer output = sliceBySliceImageFilter->GetOutput();

```

where `ImageType = itk::Image<float, 4>` and `MaskType = itk::Image<unsigned char, 3>`.

When I perform the `sliceBySliceImageFilter->Update();` I get the following error:

```nohighlight
itk::ExceptionObject (0x55e954bec040)
Location: "unknown" 
File: /usr/local/include/ITK-5.0/itkImageToImageFilter.hxx
Line: 230
Description: itk::ERROR: MaskImageFilter(0x55e954be8020): Inputs do not occupy the same physical space!
InputImage Direction: 1.0000000e+00 0.0000000e+00 0.0000000e+00
0.0000000e+00 1.0000000e+00 0.0000000e+00
0.0000000e+00 0.0000000e+00 1.0000000e+00
, InputImage_1 Direction: 9.7443149e-01 -2.0041182e-01 -1.0157935e-01
-1.9376737e-01 -9.7842928e-01 7.1626399e-02
1.1374299e-01 5.0112252e-02 9.9224558e-01
	Tolerance: 1.0000000e-06

```

I have printed the spacing, origin and direction of the image and mask and both are the same:

```nohighlight
Image
Spacing: 1.718800 1.718800 5.000001 2.000000
Origin: -86.511650 103.042786 -11.219935
Mask
Spacing: 1.718800 1.718800 5.000001 0.000000
Origin: -86.511650 103.042786 -11.219935

```

What is happening? It seems that when the SliceBySliceImageFilter extracts the single 3D volumes from my 4D image, it not preserves the image meta-information. Is it correct? How can I solve it? Is there some way to do what I want rather than using the SliceBySliceImageFilter?

Thank you very much.  
Javier.

---

<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:** [February 10, 2020, 3:35pm UTC](https://discourse.itk.org/t/apply-3d-mask-to-4d-image-itkslicebysliceimagefilter/2694/2 "2020-02-10T15:35:14Z")

</div>

Spacing and origin are not the issue. As the error message states, the images do not have the same direction (orientation in space). One has identity direction (oriented parallel to coordinate axes), while the other has an orientation which is close to identity, but not quite the same.

If you think that voxel grids align, then you could copy the information from your main image to your mask: `mask->SetDirection(image->GetDirection());` Otherwise you might need to resample your mask to your main image.
