# Mask (ROI) as NumPy array

**URL:** https://discourse.itk.org/t/mask-roi-as-numpy-array/2933
**Category:** Beginner Questions
**Created:** [April 12, 2020, 12:41am UTC](https://discourse.itk.org/t/mask-roi-as-numpy-array/2933 "2020-04-12T00:41:47Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![3omarkamal](https://discourse.itk.org/letter_avatar_proxy/v4/letter/3/c77e96/32.png) [@3omarkamal](https://discourse.itk.org/u/3omarkamal)
#### Post date: [April 12, 2020, 12:41am UTC](https://discourse.itk.org/t/mask-roi-as-numpy-array/2933/1 "2020-04-12T00:41:47Z")

</div>

I have a 3D nii image file. I have drawn a 3D label (ROI) in ITK snap. Now I want to analyze this ROI beyond the limited options in the labelIntensityStatistics. For example, I want to calculate the 10th, 20th, … percentiles. One idea is to convert this ROI into numpy array with the real values from the original image but I can’t find a solution.

I tried:  
import SimpleITK as sitk  
import numpy as np

ADC600 = sitk.ReadImage( ‘/maps/ADC600.nii’)  
ROI = sitk.ReadImage( ‘/ROI2.nii’)  
shape = sitk.LabelShapeStatisticsImageFilter()  
shape.Execute(ROI)  
bounds = shape.GetBoundingBox(1)

new\_im = sitk.RegionOfInterest(ADC600,bounds)  
new\_im = sitk.GetArrayFromImage(newim)  
mean = np.mean(new\_im)

But the value is totally off

Any help?  
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: [April 12, 2020, 12:56am UTC](https://discourse.itk.org/t/mask-roi-as-numpy-array/2933/2 "2020-04-12T00:56:52Z")

</div>

The Regions in SimpleITK are defined here: [https://simpleitk.readthedocs.io/en/master/conventions.html#image-regions-as-index-and-size](https://simpleitk.readthedocs.io/en/master/conventions.html#image-regions-as-index-and-size)

The RegionOfInterest filter is expecting a region, so try GetRegion. Also consider using the LabelStatisticsImageFilter over the LabelShapeStatisticsImageFilter for this purpose.

Also inspect the “bounds”, and the resulting new\_im from your cropping operations to give hints on wha the problems are.

---

<div class="post-metadata">

### Author: ![3omarkamal](https://discourse.itk.org/letter_avatar_proxy/v4/letter/3/c77e96/32.png) [@3omarkamal](https://discourse.itk.org/u/3omarkamal)
#### Post date: [April 12, 2020, 8:08pm UTC](https://discourse.itk.org/t/mask-roi-as-numpy-array/2933/3 "2020-04-12T20:08:38Z")

</div>

I made the changes but still the same result.

Is there a way to get the individual pixel values from an ROI? then I can do the first order statistics.

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.itk.org/user_avatar/discourse.itk.org/lassoan/32/27_2.png) [@lassoan](https://discourse.itk.org/u/lassoan)
#### Post date: [April 13, 2020, 2:31am UTC](https://discourse.itk.org/t/mask-roi-as-numpy-array/2933/4 "2020-04-13T02:31:37Z")

</div>

Using ITK regions and iterators seem like an overkill for getting intensity statistics. Getting n-th percentile of grayscale voxel values corresponding to a specific label value in the mask volume is a one-liner in Python (assuming both are stored as a numpy array of the same size, corresponding to the same image geometry).

For example, for grayscale image `image` you can get 10th percentile voxel values of a structure labelled as 18 in the corresponding `mask` volume by calling:

```
np.percentile(image[mask==18], 10)
```

---

<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: [April 13, 2020, 12:42pm UTC](https://discourse.itk.org/t/mask-roi-as-numpy-array/2933/5 "2020-04-13T12:42:53Z")

</div>

This appears to be a cross posting, with the OP [accepting the answer on GitHub](https://github.com/SimpleITK/SimpleITK/issues/1059).

---

<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: [April 13, 2020, 3:14pm UTC](https://discourse.itk.org/t/mask-roi-as-numpy-array/2933/6 "2020-04-13T15:14:34Z")

</div>

The proposed solution, which uses a numpy mask, or `numpy.extract` have the additional memory and computational time on the order of the number of pixels.

One future possibility is to add a method to `sitk::LabelIntensityStatisticsImageFilter::GetIntensities` which returns an array of the values for a given label. For cases where there are a large number of small labels this would be significantly more computationally efficient.

---

<div class="post-metadata">

### Author: ![3omarkamal](https://discourse.itk.org/letter_avatar_proxy/v4/letter/3/c77e96/32.png) [@3omarkamal](https://discourse.itk.org/u/3omarkamal)
#### Post date: [April 13, 2020, 9:11pm UTC](https://discourse.itk.org/t/mask-roi-as-numpy-array/2933/7 "2020-04-13T21:11:04Z")

</div>

Thanks all.

Yes I have found numpy.extract option to be useful. I didn’t get time to update the forum here.

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.itk.org/user_avatar/discourse.itk.org/lassoan/32/27_2.png) [@lassoan](https://discourse.itk.org/u/lassoan)
#### Post date: [April 16, 2020, 1:41pm UTC](https://discourse.itk.org/t/mask-roi-as-numpy-array/2933/8 "2020-04-16T13:41:03Z")

</div>

> [@blowekamp](#):
>
> The proposed solution, which uses a numpy mask, or `numpy.extract` have the additional memory and computational time on the order of the number of pixels.

For reference, for a 512^3 16-bit volume with a fairly large mask, numpy provides result in a fraction of a second (around 200msec). Interestingly, `np.extract` is about 50% slower than direct array indexing `image[condition]`.

So, using numpy for image masking is indeed much slower than ITK but still good enough for many use cases.

---

<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: [April 16, 2020, 1:47pm UTC](https://discourse.itk.org/t/mask-roi-as-numpy-array/2933/9 "2020-04-16T13:47:44Z")

</div>

> [@lassoan](#):
>
> So, using numpy for image masking is indeed much slower than ITK but still good enough for many use cases.

Yes certainly.

To clarify my comment, if you need to do this operations for many _different_ labels for example over a set of organs or a set of cells it is computationally beneficial to use the ITK infrastructure which works with multiple labels.
