# Registering with only one slice of 3d image

**URL:** <https://discourse.itk.org/t/registering-with-only-one-slice-of-3d-image/7028>\
**Category:** Algorithms\
**Tags:** simpleitk, registration\
**Created:** [July 9, 2024, 11:58am UTC](https://discourse.itk.org/t/registering-with-only-one-slice-of-3d-image/7028 "2024-07-09T11:58:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cadaei](https://discourse.itk.org/user_avatar/discourse.itk.org/cadaei/32/4282_2.png) [@cadaei](https://discourse.itk.org/u/cadaei)\
**Post date:** [July 9, 2024, 11:58am UTC](https://discourse.itk.org/t/registering-with-only-one-slice-of-3d-image/7028/1 "2024-07-09T11:58:10Z")

</div>

Hi,  
I want to register 2 dicom images which are of the same examination, but with a slightly different FOV. Each image stack consists of three slices, but their distance is too large, so each slice has to be registered separately.  
The issue mostly is that the registration doesn’t like there’s only 2 dimensions, the error message is

Using C# I tried

> var registration = new ImageRegistrationMethod();  
> registration.SetMetricAsMattesMutualInformation(50);  
> registration.SetInterpolator(InterpolatorEnum.sitkLinear);  
> registration.SetOptimizerAsGradientDescent(1.0, 100);  
> registration.SetOptimizerScalesFromPhysicalShift();

> var initialTransform = SimpleITK.CenteredTransformInitializer(  
> imageFixed,  
> imageMoving,  
> new AffineTransform(preCI.GetDimension()),  
> //new Euler3DTransform(),  
> CenteredTransformInitializerFilter.OperationModeType.GEOMETRY  
> );

> registration.SetInitialTransform(initialTransform);  
> var finalTransform = registration.Execute(imageFixed, imageMoving);

The last line give the exception

> itk::ERROR: RecursiveGaussianImageFilter(00000128B0DDD040): The number of pixels along direction 2 is less than 4. This filter requires a minimum of four pixels along the dimension to be processed.’

Which sounds reasonable since there’s only one slice. Is there a way to restrict the registration to the given image plane?

---

<div class="post-metadata">

**Author:** ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)\
**Post date:** [July 9, 2024, 1:08pm UTC](https://discourse.itk.org/t/registering-with-only-one-slice-of-3d-image/7028/2 "2024-07-09T13:08:09Z")

</div>

Hello @cadaei,

According to the textual description you are attempting to perform 2D/2D registration. The error message is indicating that the image is actually 3D. Note that an image of size [512, 512, 1] is a 3D image while the same image content with a size of [512, 512] is a 2D image.

If you are reading a 3D slice as a single image its size will be `[sx, sy, 1]`. You can extract the 2D equivalent with the [ExtractImageFilter](https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1ExtractImageFilter.html), or configure this during the image reading. Something along the lines of:

```auto
imageFileReader = SimpleITK.ImageFileReader();
imageFileReader.SetFileName(fixedImageFileName);
imageFileReader.ReadImageInformation();
int[] extractSize = new int[] { imageFileReader.GetWidth(), imageFileReader.GetHeight(), 0 };
imageFileReader.SetExtractSize(extractSize);
fixedImage2D = imageFileReader.Execute();

```

---

<div class="post-metadata">

**Author:** ![cadaei](https://discourse.itk.org/user_avatar/discourse.itk.org/cadaei/32/4282_2.png) [@cadaei](https://discourse.itk.org/u/cadaei)\
**Post date:** [July 9, 2024, 3:52pm UTC](https://discourse.itk.org/t/registering-with-only-one-slice-of-3d-image/7028/3 "2024-07-09T15:52:12Z")

</div>

Hi Zivy,  
thank you for your answer! When trying your code I realized I have a very outdated version of the sitk library, apparently the one on the nuget packet manager for Visual Studio is not maintained anymore. Now I’m using the latest version from github.

Your suggestion works, I could process the images.  
Now I’m trying to save the resampled image back to a dicom file for further processing in a different app (which previously complained the images are not aligned).

When saving the image as dicom, there’s the error

> ITK ERROR: GDCMImageIO(000001D6A1242500): Invalid direction cosines, non-orthogonal or unit length.

Did the extraction of the layer remove all the topological info from the image, so the dicom file writer complains these are missing? Can I keep that information, or do I have to reinsert the resampled image in the initial dicom image somehow?

---

<div class="post-metadata">

**Author:** ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)\
**Post date:** [July 9, 2024, 4:57pm UTC](https://discourse.itk.org/t/registering-with-only-one-slice-of-3d-image/7028/4 "2024-07-09T16:57:23Z")

</div>

Hello @cadaei,

If you used the code which extracted the 2D image during reading, then the metadata information is partially lost.

Instead, read the image as a 3D image use the `ExtractImageFilter` to get the 2D version and after resampling you can re-construct it as a 3D image by calling the [JoinSeriesImageFilter](https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1JoinSeriesImageFilter.html) with the image as input. Then copy the meta-data information from the original image using the [CopyInformation](https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1Image.html#afa8a4757400c414e809d1767ee616bd0) method.

---

<div class="post-metadata">

**Author:** ![cadaei](https://discourse.itk.org/user_avatar/discourse.itk.org/cadaei/32/4282_2.png) [@cadaei](https://discourse.itk.org/u/cadaei)\
**Post date:** [July 15, 2024, 9:30am UTC](https://discourse.itk.org/t/registering-with-only-one-slice-of-3d-image/7028/5 "2024-07-15T09:30:14Z")

</div>

Thank you very much again, that solved it.
