# Using an image as a mask

**URL:** <https://discourse.itk.org/t/using-an-image-as-a-mask/3921>\
**Category:** Engineering\
**Tags:** simpleitk, python\
**Created:** [March 2, 2021, 11:06pm UTC](https://discourse.itk.org/t/using-an-image-as-a-mask/3921 "2021-03-02T23:06:31Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Diego\_C](https://discourse.itk.org/user_avatar/discourse.itk.org/diego_c/32/279_2.png) [@Diego\_C](https://discourse.itk.org/u/Diego_C)\
**Post date:** [March 2, 2021, 11:06pm UTC](https://discourse.itk.org/t/using-an-image-as-a-mask/3921/1 "2021-03-02T23:06:31Z")

</div>

Hi!  
I want to retrieve the original intensities of `image` but only if the voxel is 1 in the `foreground` mask

My code looks like this:

```
# compute foreground image
# -------------------------------------
bg_data = sitk.GetArrayFromImage(background)
fg_data = np.zeros(bg_data.shape)
fg_data[np.where(bg_data == 0)] = 1 # opposite to background
foreground = sitk.GetImageFromArray(fg_data)

# apply mask
#---------------------------------------
sitk.Mask(image, foreground, maskingValue=1, outsideValue=0)

```

I am getting this error:

```
RuntimeError: Exception thrown in SimpleITK Mask: Users/runner/work/1/sitk/Code/Common/include/sitkDualMemberFunctionFactory.hxx:178:sitk::ERROR: Pixel type: 16-bit signed integer is not supported in 3D byN3itk6simple15MaskImageFilterE

```

What should I do?

---

<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:** [March 3, 2021, 1:02am UTC](https://discourse.itk.org/t/using-an-image-as-a-mask/3921/2 "2021-03-03T01:02:03Z")

</div>

Hello @Diego_C ,

The code is a bit complicated, not sure what you are trying to achieve with the back and forth via numpy. Below is a simple example which creates a binary mask via thresholding and zeros out all the voxels that are in the background.

```auto
import SimpleITK as sitk

image = sitk.ReadImage('training_001_ct.mha')
foreground_mask = image>0
foreground_image = image*sitk.Cast(foreground_mask, image.GetPixelID())

```

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [March 3, 2021, 2:15pm UTC](https://discourse.itk.org/t/using-an-image-as-a-mask/3921/3 "2021-03-03T14:15:15Z")

</div>

> [@Diego\_C](#):
>
> ```auto
> sitk.Mask(image, foreground, maskingValue=1, outsideValue=0)
> 
> ```
> 
> I am getting this error:
> 
> ```auto
> RuntimeError: Exception thrown in SimpleITK Mask: Users/runner/work/1/sitk/Code/Common/include/sitkDualMemberFunctionFactory.hxx:178:sitk::ERROR: Pixel type: 16-bit signed integer is not supported in 3D byN3itk6simple15MaskImageFilterE
> 
> ```
> 
> What should I do?

When you get this type of error you need to inspect the image inputs to the filter. Look at the image dimensions, pixel types, meta-data etc.

The `Mask` filter support most sensible combinations for it’s image arguments:

```auto
In [1]: import SimpleITK as sitk

In [2]: img = sitk.Image([16]*3,sitk.sitkFloat32)

In [3]: mask = sitk.Image([16]*3, sitk.sitkInt16)

In [4]: sitk.Mask(img, mask)
Out[4]: <SimpleITK.SimpleITK.Image; proxy of <Swig Object of type 'std::vector< itk::simple::Image >::value_type *' at 0x7fab28ba8d50> >

```

Unlike when the multiplication operator which requires the images to be the same type ( thus why the `Cast` was needed.

For you case I suspect that there is a mismatch in number of dimensions in the images after converting to and fro NumPy. Unfortunately, since your code is not self contained I can not reproduce it. If you initialized the `background` and `image` with something like `background = sitk.Image([32]*2, sitk.sitkFloat32)` the could would be self contained.

---

<div class="post-metadata">

**Author:** ![Diego\_C](https://discourse.itk.org/user_avatar/discourse.itk.org/diego_c/32/279_2.png) [@Diego\_C](https://discourse.itk.org/u/Diego_C)\
**Post date:** [March 5, 2021, 4:44am UTC](https://discourse.itk.org/t/using-an-image-as-a-mask/3921/4 "2021-03-05T04:44:29Z")

</div>

Hi @blowekamp

No mismatch in number of dimensions between input and foreground (filter inputs).

I managed to fix the problem like this:

```
sitk.Mask(image, sitk.Cast(foreground,sitk.sitkInt8), maskingValue=1, outsideValue=0)

```

So it looks like I needed to specify the pixel type of the numpy array I was using to build the foreground mask.

**Masking Behaviour**

I am a bit confused though about the mask masking _in_ or masking _out_ behaviour?

My foreground mask is 1 for the head and 0 for the background, so I was expecting as a result of using the masking filter to see only the “head” voxels using:

```
  sitk.Mask(image, sitk.Cast(foreground,sitk.sitkInt8), maskingValue=1, outsideValue=0)

```

but this is what I obtained:

 ![ROI_tissue](https://discourse.itk.org/uploads/default/original/2X/7/7d85877b812fdd02dabf704dfa141cdb705a91f4.png)

In contrast, if I change the instruction to:

```
sitk.Mask(image, sitk.Cast(foreground,sitk.sitkInt8), maskingValue=0, outsideValue=0)

```

Then I obtain the expected result:

 ![ROI_tissue_2](https://discourse.itk.org/uploads/default/original/2X/7/7b86afa5da4efd3c033cc60d4b322e91d3f0c573.png)

So I thought that the `maskingValue` parameter referred to the intensity value in the mask that the filter would use to select voxels _in_ from the original input. But I am seeing this is not the case.

What is `maskingValue`?

Thanks,

Diego

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [March 5, 2021, 1:04pm UTC](https://discourse.itk.org/t/using-an-image-as-a-mask/3921/5 "2021-03-05T13:04:05Z")

</div>

From the [sitk::MaskImageFilter](https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1MaskImageFilter.html) doxygen page:

```auto
if pixel_from_mask_image != masking_value
 pixel_output_image = pixel_input_image
else
 pixel_output_image = outside_value

```

The current brief documentation for the filter is: “Mask an image with a mask.” Suggestions for a brief line to better describe this behavior are welcome.

---

<div class="post-metadata">

**Author:** ![bdzyubak](https://discourse.itk.org/user_avatar/discourse.itk.org/bdzyubak/32/3707_2.png) [@bdzyubak](https://discourse.itk.org/u/bdzyubak)\
**Post date:** [October 3, 2023, 6:20pm UTC](https://discourse.itk.org/t/using-an-image-as-a-mask/3921/6 "2023-10-03T18:20:57Z")

</div>

This is the behavior I expect, but not one I get.  
label\_np = np.zeros([5, 5, 5])  
label\_np[4, 0, 0] = 120  
image = sitk.GetImageFromArray(label\_np)

```
    masked_image = sitk.Mask(image, image >0, maskingValue=1)

    get_max(masked_image)
    >> 0

```

I would expect this to be 1 with the foreground mask being set to the maskingValue. Am I misreading, and what is the canonical way to achieve that? Thanks.

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [October 3, 2023, 6:40pm UTC](https://discourse.itk.org/t/using-an-image-as-a-mask/3921/7 "2023-10-03T18:40:11Z")

</div>

The logic for the MaskImageFilter is:

```auto
if pixel_from_mask_image != masking_value:
 pixel_output_image = pixel_input_image
else:
 pixel_output_image = outside_value

```

So the default maskValue of `0` should work. The default behavior is for the non-zero locations to preserve or keep those values in the input image.

---

<div class="post-metadata">

**Author:** ![bdzyubak](https://discourse.itk.org/user_avatar/discourse.itk.org/bdzyubak/32/3707_2.png) [@bdzyubak](https://discourse.itk.org/u/bdzyubak)\
**Post date:** [October 4, 2023, 5:29pm UTC](https://discourse.itk.org/t/using-an-image-as-a-mask/3921/8 "2023-10-04T17:29:27Z")

</div>

I guess the definition is what it is… which is different from the textbook definition of image masking

> **[What is image masking?](https://www.topazlabs.com/learn/what-is-image-masking)**
>
> Image masking is an extremely useful technique to edit your images in a non-destructive way. Here are a few types of image masking techniques and use cases to inspire your editing workflow.

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [October 4, 2023, 11:31pm UTC](https://discourse.itk.org/t/using-an-image-as-a-mask/3921/9 "2023-10-04T23:31:51Z")

</div>

Newly added in SimpleITK is support for images as masked with the **setitem** method. So you find it more explicit to do something like:  
`image[image<=0] = 0`

I am not clear what your initial expectations were, but the above syntax should make it more clean what you are trying to do.
