# Run length matrix using mask

**URL:** https://discourse.itk.org/t/run-length-matrix-using-mask/1340
**Category:** Algorithms
**Created:** [October 8, 2018, 5:14pm UTC](https://discourse.itk.org/t/run-length-matrix-using-mask/1340 "2018-10-08T17:14:57Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![pfreire](https://discourse.itk.org/user_avatar/discourse.itk.org/pfreire/32/513_2.png) [@pfreire](https://discourse.itk.org/u/pfreire)
#### Post date: [October 8, 2018, 5:14pm UTC](https://discourse.itk.org/t/run-length-matrix-using-mask/1340/1 "2018-10-08T17:14:57Z")

</div>

Hello, everyone.

I’m using the itkScalarImageToRunLengthMatrixFilter to extract texture features from brain images. I know that this particular class has the method SetMaskImage that can be used to constrain the region we want to extract features from. However, when I try to use it I always get blank images as a result. I’ve tried using masks with foreground values equal to 1 or equal to itk::NumericTraits\< T \>::max(), where T is the pixel type. All to no avail. When I don’t use a mask image, I get the expected outputs, but it’s really time-consuming.

Can someone please point me in the right direction? Using a mask to constrain the region to be processed would dramatically decrease the amount of time required to get the results I’m interested in.

Thank you all 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: [October 8, 2018, 6:12pm UTC](https://discourse.itk.org/t/run-length-matrix-using-mask/1340/2 "2018-10-08T18:12:30Z")

</div>

The mask condition seems to be checked here:

> <https://github.com/InsightSoftwareConsortium/ITK/blob/d72d28f5a1f405f740fe7f296d0f308793263ab2/Modules/Numerics/Statistics/include/itkScalarImageToRunLengthMatrixFilter.hxx#L195-L197>

I would recommend running your program under debugging, stepping into `GetMaskImage()` and inspecting relevant variables. That should give you a clue about what is wrong.

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [October 8, 2018, 6:22pm UTC](https://discourse.itk.org/t/run-length-matrix-using-mask/1340/3 "2018-10-08T18:22:14Z")

</div>

@pfreire you may be interested in the _ITKTextureFeatures_ module:

> **[InsightSoftwareConsortium/ITKTextureFeatures](https://github.com/InsightSoftwareConsortium/ITKTextureFeatures)**
>
> Fast, Texture Feature Maps from N-Dimensional Images - InsightSoftwareConsortium/ITKTextureFeatures

This supports a mask and can generate texture maps.

---

<div class="post-metadata">

### Author: ![pfreire](https://discourse.itk.org/user_avatar/discourse.itk.org/pfreire/32/513_2.png) [@pfreire](https://discourse.itk.org/u/pfreire)
#### Post date: [October 9, 2018, 1:27pm UTC](https://discourse.itk.org/t/run-length-matrix-using-mask/1340/4 "2018-10-09T13:27:13Z")

</div>

Thank you, @matt.mccormick. I’ve looked into it and I was able to run the RLM example, but I was still not able to get texture maps using masks (only using the whole image).

For instance, the images I’m using can be downloaded from here: [https://drive.google.com/file/d/139zkEEMwIfaAsEolBivsas19u9dj7nvs/view?usp=sharing](https://drive.google.com/file/d/139zkEEMwIfaAsEolBivsas19u9dj7nvs/view?usp=sharing)

This is rather frustrating, for it seems that I’m missing something very basic.

Thank you 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: [October 9, 2018, 1:37pm UTC](https://discourse.itk.org/t/run-length-matrix-using-mask/1340/5 "2018-10-09T13:37:12Z")

</div>

Can you also share source code of the minimum working example which shows the problem?

---

<div class="post-metadata">

### Author: ![pfreire](https://discourse.itk.org/user_avatar/discourse.itk.org/pfreire/32/513_2.png) [@pfreire](https://discourse.itk.org/u/pfreire)
#### Post date: [October 9, 2018, 1:39pm UTC](https://discourse.itk.org/t/run-length-matrix-using-mask/1340/6 "2018-10-09T13:39:38Z")

</div>

Certainly, @dzenanz! Here it is: [https://drive.google.com/file/d/1dOORBsD7DuVBxWDJc6KLUib9zyqwSdcg/view?usp=sharing](https://drive.google.com/file/d/1dOORBsD7DuVBxWDJc6KLUib9zyqwSdcg/view?usp=sharing)

Thank you very much.

---

<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: [October 9, 2018, 4:37pm UTC](https://discourse.itk.org/t/run-length-matrix-using-mask/1340/7 "2018-10-09T16:37:45Z")

</div>

Mystery solved!

```auto
const InputImageType *mask_img = bip::utils::ReadImage< InputImageType >(argv[2]);

```

should be replaced by

```auto
InputImageType::ConstPointer mask_img = bip::utils::ReadImage< InputImageType >(argv[2]);

```

An ordinary pointer does not keep ownership of ITK’s reference counted classes such as `Image`, so the return value of the `ReadImage` function immediately goes out of scope, leaving `mask_img` a dangling pointer. It is pure luck that the program does not crash.

---

<div class="post-metadata">

### Author: ![pfreire](https://discourse.itk.org/user_avatar/discourse.itk.org/pfreire/32/513_2.png) [@pfreire](https://discourse.itk.org/u/pfreire)
#### Post date: [October 9, 2018, 4:59pm UTC](https://discourse.itk.org/t/run-length-matrix-using-mask/1340/8 "2018-10-09T16:59:06Z")

</div>

Perfect! Thank you so much, @dzenanz! Awesome help and support. Cheers!
