# thresholding a mask

**URL:** <https://discourse.itk.org/t/thresholding-a-mask/1270>\
**Category:** Beginner Questions\
**Created:** [September 11, 2018, 4:32pm UTC](https://discourse.itk.org/t/thresholding-a-mask/1270 "2018-09-11T16:32:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tia](https://discourse.itk.org/letter_avatar_proxy/v4/letter/t/ac91a4/32.png) [@tia](https://discourse.itk.org/u/tia)\
**Post date:** [September 11, 2018, 4:32pm UTC](https://discourse.itk.org/t/thresholding-a-mask/1270/1 "2018-09-11T16:32:57Z")

</div>

Hello!

I have a mask corresponding to an image patch and I would like to further modify the mask to exclude from the mask all voxels that correspond to image voxels outside a range of intensities. What is the best way to accomplish this?

Thanks!  
Tia

---

<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:** [September 11, 2018, 5:08pm UTC](https://discourse.itk.org/t/thresholding-a-mask/1270/2 "2018-09-11T17:08:25Z")

</div>

You can use iterators to go through the input image and mask, and set appropriate values to pixels in the output image. An [example](https://itk.org/ITKExamples/src/Core/Common/FilterAndParallelizeImageRegion/Documentation.html) which uses iterators.

If you don’t want to do that, you can threshold your input again, then do some operations on the two masks (multiply or subtract) to get the desired output. An [example](https://itk.org/Wiki/ITK/Examples/ImageProcessing/MaskImageFilter) which uses a mask. A [list](https://itk.org/Doxygen/html/group__ITKImageIntensity.html) of intensity-based filters.

---

<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:** [September 11, 2018, 5:52pm UTC](https://discourse.itk.org/t/thresholding-a-mask/1270/3 "2018-09-11T17:52:21Z")

</div>

There are not a shortage of ways to do this in ITK.

A new option with ITKv5 is to use the [BinaryGeneratorImageFilter](https://itk.org/Doxygen/html/classitk_1_1BinaryGeneratorImageFilter.html) to us a white a little lambda function to place run inside a filter:

> [@New Functor Filter with lambda support](https://discourse.itk.org/t/new-functor-filter-with-lambda-support/899):
>
> This is in regards to the following topic: [http://review.source.kitware.com/#/c/23352/](http://review.source.kitware.com/#/c/23352/) This patch introduces the UnaryGeneratorImageFilter BinaryGeneratorImageFilters, as replacements for the UnaryFunctorImageFilter and the BinaryFunctorImageFilters. The two big advantages are: Support for C++ lambda’s Removed class’s template parameter for functor, which enables reuse of a parent class. These new classes enable the setting for the functor with a “SetFunctor” method, where the type of functo…

I haven’t compiled this code, but it should be something like this:

```auto
using FilterType = itk::BinaryGeneratorImageFilter<InputImageType, MaskImageType OutputImageType>;
auto filter = FilterType::New();
filter->SetFunctor([](const InputPixelType &p1, const MaskPixelType &mask)->OutputPixelType
 { return mask!=1 || p1 > threshold; });

```
