Apply 3D mask to 4D image - itkSliceBySliceImageFilter

Hello,

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

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:

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:

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.

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.

1 Like