# How to apply mask when using rescale filter

**URL:** https://discourse.itk.org/t/how-to-apply-mask-when-using-rescale-filter/5295
**Category:** Beginner Questions
**Created:** [August 26, 2022, 6:24am UTC](https://discourse.itk.org/t/how-to-apply-mask-when-using-rescale-filter/5295 "2022-08-26T06:24:17Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![willi3by](https://discourse.itk.org/letter_avatar_proxy/v4/letter/w/a698b9/32.png) [@willi3by](https://discourse.itk.org/u/willi3by)
#### Post date: [August 26, 2022, 6:24am UTC](https://discourse.itk.org/t/how-to-apply-mask-when-using-rescale-filter/5295/1 "2022-08-26T06:24:17Z")

</div>

Hi, I am implementing a modified version of the filtering approach in this tutorial: [Segment Blood Vessels With Multi-Scale Hessian-Based Measure — v5.3.0](https://examples.itk.org/src/nonunit/review/segmentbloodvesselswithmultiscalehessianbasedmeasure/documentation)

However, my images contain tissue for which I do not want to apply the filter (i.e., GM and CSF). I already have a mask of the tissue that I do want to apply the filter to (WM). Is there a way to apply a mask when filtering so it is only calculated with the voxels inside the mask?

Thanks for your time!

---

<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 26, 2022, 7:53pm UTC](https://discourse.itk.org/t/how-to-apply-mask-when-using-rescale-filter/5295/2 "2022-08-26T19:53:08Z")

</div>

I don’t think that either `HessianToObjectnessMeasureImageFilter` or `MultiScaleHessianBasedMeasureImageFilter` accept a mask. Changing their code to accept a mask should not be hard, though.

---

<div class="post-metadata">

### Author: ![willi3by](https://discourse.itk.org/letter_avatar_proxy/v4/letter/w/a698b9/32.png) [@willi3by](https://discourse.itk.org/u/willi3by)
#### Post date: [August 29, 2022, 11:18am UTC](https://discourse.itk.org/t/how-to-apply-mask-when-using-rescale-filter/5295/3 "2022-08-29T11:18:34Z")

</div>

Thanks for the info! I am currently using the Python wrapper for ITK as I have no experience with C++ code. I am happy to give this a try though. Is there any template code you could point me to that implements masking already?

---

<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 29, 2022, 1:09pm UTC](https://discourse.itk.org/t/how-to-apply-mask-when-using-rescale-filter/5295/4 "2022-08-29T13:09:41Z")

</div>

> [@dzenanz](#):
>
> Changing their code to accept a mask

I meant changing C++ library code to suit your needs.

You can accomplish the same thing by masking the results image, at the cost of the computation being slower than absolutely necessary.

---

<div class="post-metadata">

### Author: ![willi3by](https://discourse.itk.org/letter_avatar_proxy/v4/letter/w/a698b9/32.png) [@willi3by](https://discourse.itk.org/u/willi3by)
#### Post date: [August 29, 2022, 1:52pm UTC](https://discourse.itk.org/t/how-to-apply-mask-when-using-rescale-filter/5295/5 "2022-08-29T13:52:44Z")

</div>

Oh I see, I think I am misunderstanding the implementation of the mask. Since the filters I am trying to use are based on relative intensities surrounding the voxels of interest, my results are being overwhelmed by the zeros in the image. I want to calculation to only be performed within the mask, not just mask the results of the overall calculation (hopefully that makes sense). To explain further, I am using these filters to detect “vesselness”, but since the lateral ventricles are “vessel-like” according to the filter, the focus of the resulting mask is in the ventricles and the areas of actual interest in the white matter are de-emphasized or ignored.

The only other solution I could think of is a patch-based method but I would like a more elegant solution if possible.

Thanks again

---

<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 29, 2022, 1:58pm UTC](https://discourse.itk.org/t/how-to-apply-mask-when-using-rescale-filter/5295/6 "2022-08-29T13:58:15Z")

</div>

I don’t think there is code for what you want. Masking, then computing vesselness, seems to be closest to what you want, by using existing code.
