# How to use ITKMontage to stitch segmentation label images?

**URL:** https://discourse.itk.org/t/how-to-use-itkmontage-to-stitch-segmentation-label-images/6611
**Category:** Beginner Questions
**Tags:** itk, python
**Created:** [May 2, 2024, 10:37am UTC](https://discourse.itk.org/t/how-to-use-itkmontage-to-stitch-segmentation-label-images/6611 "2024-05-02T10:37:02Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Mazen\_Soufi](https://discourse.itk.org/user_avatar/discourse.itk.org/mazen_soufi/32/4018_2.png) [@Mazen\_Soufi](https://discourse.itk.org/u/Mazen_Soufi)
#### Post date: [May 2, 2024, 10:37am UTC](https://discourse.itk.org/t/how-to-use-itkmontage-to-stitch-segmentation-label-images/6611/1 "2024-05-02T10:37:02Z")

</div>

Hi,

I’m trying to use ITKMontage’s TileMergeImageFilter to stitch segmentation label images (uint8) using the transformations obtained from grayscale images.

The file below shows at its header that the default interpolator is LinearInterpolateImageFunction.  
[itkTileMergeImageFilter.h](https://github.com/InsightSoftwareConsortium/ITKMontage/blob/c5f9214ab8993dec0ba2fe0a95eb614cd6558bfa/include/itkTileMergeImageFilter.h)  
When using the default linear interpolator, the intermediate values between different classes is mistakenly interpolated to other label values.  
 ![image](https://discourse.itk.org/uploads/default/original/2X/c/cade2813447db94edd95e92c43b3d523eca77794.png)

Is it possible to set it to NearestNeighborInterpolateImageFunction without rebuilding itk from source?

Below is a code with the issued error when trying to set the interpolator at the constructor:  
Code snippet:

```auto
label = itk.imread(filename, itk.US) # read as grayscale
label.SetOrigin(origin)
labels.append(label)
...
    resampleF = itk.TileMergeImageFilter[type(labels[0]),\
                itk.NearestNeighborInterpolateImageFunction[type(labels[0])].New(labels[0])].New()
    resampleF.SetMontageSize(stage_tiles.GetAxisSizes())
    for t in range(stage_tiles.LinearSize()):
        resampleF.SetInputTile(t, labels[t])
        index = stage_tiles.LinearIndexToNDIndex(t)
        resampleF.SetTileTransform(index, montage.GetOutputTransform(index))
    resampleF.Update()
    itk.imwrite(resampleF.GetOutput(), os.path.join(tgt, f'stitched_{k}_label.mhd'))

```

Error log:

```auto
> itk.NearestNeighborInterpolateImageFunction[type(labels[0])].New(labels[0])].New()
  File "py\lib\site-packages\itk\support\template_class.py", line 529, in __getitem__
    raise itk.TemplateTypeError(self, key)
itk.support.extras.TemplateTypeError: itk.NearestNeighborInterpolateImageFunction is not wrapped for input type `itk.Image[itk.UC,3]`.

To limit the size of the package, only a limited number of
types are available in ITK Python. To print the supported
types, run the following command in your python environment:

    itk.NearestNeighborInterpolateImageFunction.GetTypes()

Possible solutions:
* If you are an application user:
** Convert your input image into a supported format (see below).
** Contact developer to report the issue.
* If you are an application developer, force input images to be
loaded in a supported pixel type.

    e.g.: instance = itk.NearestNeighborInterpolateImageFunction[itk.Image[itk.SS,2], itk.D].New(my_input)

* (Advanced) If you are an application developer, build ITK Python yourself and
turned to `ON` the corresponding CMake option to wrap the pixel type or image
dimension you need. When configuring ITK with CMake, you can set
`ITK_WRAP_${type}` (replace ${type} with appropriate pixel type such as
`double`). If you need to support images with 4 or 5 dimensions, you can add
these dimensions to the list of dimensions in the CMake variable
`ITK_WRAP_IMAGE_DIMS`.

Supported input types:

itk.Image[itk.SS,2]
itk.Image[itk.UC,2]
itk.Image[itk.US,2]
itk.Image[itk.F,2]
itk.Image[itk.D,2]
itk.Image[itk.SS,3]
itk.Image[itk.UC,3]
itk.Image[itk.US,3]
itk.Image[itk.F,3]
itk.Image[itk.D,3]
itk.Image[itk.SS,4]
itk.Image[itk.UC,4]
itk.Image[itk.US,4]
itk.Image[itk.F,4]
itk.Image[itk.D,4]

```

Thankful for your help in advance,

---

<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: [May 2, 2024, 4:05pm UTC](https://discourse.itk.org/t/how-to-use-itkmontage-to-stitch-segmentation-label-images/6611/2 "2024-05-02T16:05:17Z")

</div>

Try replacing

```py
itk.NearestNeighborInterpolateImageFunction[type(labels[0])]

```

by:

```auto
itk.NearestNeighborInterpolateImageFunction[type(labels[0]), itk.D]

```

---

<div class="post-metadata">

### Author: ![Mazen\_Soufi](https://discourse.itk.org/user_avatar/discourse.itk.org/mazen_soufi/32/4018_2.png) [@Mazen\_Soufi](https://discourse.itk.org/u/Mazen_Soufi)
#### Post date: [May 2, 2024, 10:08pm UTC](https://discourse.itk.org/t/how-to-use-itkmontage-to-stitch-segmentation-label-images/6611/3 "2024-05-02T22:08:41Z")

</div>

@dzenanz thanks a lot for reply.

I replaced it as:

```auto
   resampleF = itk.TileMergeImageFilter[type(labels[0]),\
                itk.NearestNeighborInterpolateImageFunction[type(labels[0]), itk.D].New(labels[0])].New()

```

But it gave the error:

```auto
  File "\py\lib\site-packages\itk\support\template_class.py", line 529, in __getitem__
    raise itk.TemplateTypeError(self, key)
itk.support.extras.TemplateTypeError: itk.TileMergeImageFilter is not wrapped for input type `itk.Image[itk.US,3], itk.NearestNeighborInterpolateImageFunction[itk.Image[itk.US,3],itk.D]`.

To limit the size of the package, only a limited number of
types are available in ITK Python. To print the supported
types, run the following command in your python environment:

    itk.TileMergeImageFilter.GetTypes()

Possible solutions:
* If you are an application user:
** Convert your input image into a supported format (see below).
** Contact developer to report the issue.
* If you are an application developer, force input images to be
loaded in a supported pixel type.

    e.g.: instance = itk.TileMergeImageFilter[itk.Image[itk.SS,2]].New(my_input)

* (Advanced) If you are an application developer, build ITK Python yourself and
turned to `ON` the corresponding CMake option to wrap the pixel type or image
dimension you need. When configuring ITK with CMake, you can set
`ITK_WRAP_${type}` (replace ${type} with appropriate pixel type such as
`double`). If you need to support images with 4 or 5 dimensions, you can add
these dimensions to the list of dimensions in the CMake variable
`ITK_WRAP_IMAGE_DIMS`.

Supported input types:

itk.Image[itk.SS,2]
itk.Image[itk.UC,2]
itk.Image[itk.US,2]
itk.Image[itk.F,2]
itk.Image[itk.D,2]
itk.Image[itk.RGBPixel[itk.UC],2]
itk.Image[itk.RGBAPixel[itk.UC],2]
itk.Image[itk.SS,3]
itk.Image[itk.UC,3]
itk.Image[itk.US,3]
itk.Image[itk.F,3]
itk.Image[itk.D,3]
itk.Image[itk.RGBPixel[itk.UC],3]
itk.Image[itk.RGBAPixel[itk.UC],3]
itk.Image[itk.SS,4]
itk.Image[itk.UC,4]
itk.Image[itk.US,4]
itk.Image[itk.F,4]
itk.Image[itk.D,4]
itk.Image[itk.RGBPixel[itk.UC],4]
itk.Image[itk.RGBAPixel[itk.UC],4]

```

Any ideas to solve it?

Thanks again for yur help,

---

<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: [May 3, 2024, 1:22pm UTC](https://discourse.itk.org/t/how-to-use-itkmontage-to-stitch-segmentation-label-images/6611/4 "2024-05-03T13:22:47Z")

</div>

Looking at the [wrapping specification](https://github.com/InsightSoftwareConsortium/ITKMontage/blob/master/wrapping/itkTileMergeImageFilter.wrap), only the default (linear) interpolator is wrapped. Can you add scalar+nearest instantiations there, and propose a PR?

---

<div class="post-metadata">

### Author: ![Mazen\_Soufi](https://discourse.itk.org/user_avatar/discourse.itk.org/mazen_soufi/32/4018_2.png) [@Mazen\_Soufi](https://discourse.itk.org/u/Mazen_Soufi)
#### Post date: [May 7, 2024, 9:31am UTC](https://discourse.itk.org/t/how-to-use-itkmontage-to-stitch-segmentation-label-images/6611/5 "2024-05-07T09:31:19Z")

</div>

Thanks for the proposal. Sure, I will be glad to contribute, but sorry I’m new to ITK development.  
Could you please explain in more detail which lines I need to add/modify?

---

<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: [May 7, 2024, 12:49pm UTC](https://discourse.itk.org/t/how-to-use-itkmontage-to-stitch-segmentation-label-images/6611/6 "2024-05-07T12:49:28Z")

</div>

It would be something similar to [this](https://github.com/InsightSoftwareConsortium/ITKMontage/pull/227). I don’t have time to fix/test this. Feel free to use as a starting point for your own experiments.

---

<div class="post-metadata">

### Author: ![Mazen\_Soufi](https://discourse.itk.org/user_avatar/discourse.itk.org/mazen_soufi/32/4018_2.png) [@Mazen\_Soufi](https://discourse.itk.org/u/Mazen_Soufi)
#### Post date: [May 7, 2024, 2:18pm UTC](https://discourse.itk.org/t/how-to-use-itkmontage-to-stitch-segmentation-label-images/6611/7 "2024-05-07T14:18:43Z")

</div>

Thanks a lot! Will try to use it to fix the issue.

---

<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: [May 7, 2024, 2:23pm UTC](https://discourse.itk.org/t/how-to-use-itkmontage-to-stitch-segmentation-label-images/6611/8 "2024-05-07T14:23:42Z")

</div>

If you make any progress, do share it via a PR.

---

<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: [August 15, 2024, 3:31pm UTC](https://discourse.itk.org/t/how-to-use-itkmontage-to-stitch-segmentation-label-images/6611/9 "2024-08-15T15:31:30Z")

</div>

I updated [PR 227](https://github.com/InsightSoftwareConsortium/ITKMontage/pull/227) and it now compiles locally, and passes tests:

```log
100% tests passed, 0 tests failed out of 68

Label Time Summary:
Montage = 289.73 sec*proc (68 tests)

```
