# Filter used after LabelIntensityStatisticsImageFilter

**URL:** https://discourse.itk.org/t/filter-used-after-labelintensitystatisticsimagefilter/3108
**Category:** Uncategorized
**Created:** [May 22, 2020, 2:39pm UTC](https://discourse.itk.org/t/filter-used-after-labelintensitystatisticsimagefilter/3108 "2020-05-22T14:39:04Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.itk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.itk.org/u/flaviu2)
#### Post date: [May 22, 2020, 2:39pm UTC](https://discourse.itk.org/t/filter-used-after-labelintensitystatisticsimagefilter/3108/1 "2020-05-22T14:39:05Z")

</div>

I have used **sitk::LabelIntensityStatisticsImageFilter** like this:

```
		sitk::Image imgTemp(img);
    .... another filters
		sitk::Image cc = sitk::SLIC(imgTemp);
		sitk::LabelIntensityStatisticsImageFilter stat;
		stat.Execute(cc, imgTemp);
		std::vector<int64_t>::iterator it;
		std::vector<int64_t> labels = stat.GetLabels();
		for (it = labels.begin(); it != labels.end(); ++it)
		{
			double size = stat.GetPhysicalSize(*it);
			if (size >= dValue1 &&
				size <= dValue2)
			{
				// what should I collect here to let in the output image only values between dValue1 and dValue2 ?
			}
		}

```

in order to know what components are bigger or less than some predefined values (dValue1 and dValue2).

How can I do in order to let as ouput image the values between (predefined) **dValue1** and **dValue2**? What _SimpleITK_ filter should I use ?

I have to mention that I am working in C++ only. I said that because I noticed that Python has extra methods …

---

<div class="post-metadata">

### Author: ![dchen](https://discourse.itk.org/user_avatar/discourse.itk.org/dchen/32/34_2.png) [@dchen](https://discourse.itk.org/u/dchen)
#### Post date: [May 22, 2020, 3:12pm UTC](https://discourse.itk.org/t/filter-used-after-labelintensitystatisticsimagefilter/3108/2 "2020-05-22T15:12:11Z")

</div>

To remove all labels with a size smaller than dValue1, you can use the RelabelComponentImageFilter. Use the SetMinimumObjectSize method with dValue.

[https://simpleitk.org/doxygen/latest/html/classitk\_1\_1simple\_1\_1RelabelComponentImageFilter.html](https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1RelabelComponentImageFilter.html)

As far as the maximum size, I think you need to do some sort of masking. For labels of size larger than dValue2, mask them out, i.e. change them to 0. You can use the MaskNegatedImageFilter for that:

[https://simpleitk.org/doxygen/latest/html/classitk\_1\_1simple\_1\_1MaskNegatedImageFilter.html](https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1MaskNegatedImageFilter.html)

---

<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: [May 22, 2020, 3:37pm UTC](https://discourse.itk.org/t/filter-used-after-labelintensitystatisticsimagefilter/3108/3 "2020-05-22T15:37:30Z")

</div>

You can also use the [ChangeLabelImageFilter](https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1ChangeLabelImageFilter.html) to map input label ids to output label ids. For the case of wanting to remove certain labels the ChangeMap member would contain entries which map labels to be removed to zero.

---

<div class="post-metadata">

### Author: ![dchen](https://discourse.itk.org/user_avatar/discourse.itk.org/dchen/32/34_2.png) [@dchen](https://discourse.itk.org/u/dchen)
#### Post date: [May 22, 2020, 3:46pm UTC](https://discourse.itk.org/t/filter-used-after-labelintensitystatisticsimagefilter/3108/4 "2020-05-22T15:46:12Z")

</div>

Yeah, I figured there was a better way of doing it, but my brain was not quite firing on all cylinders. And of course @blowekamp knows the filters (and SimpleITK in general) much better than I do.

---

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.itk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.itk.org/u/flaviu2)
#### Post date: [May 28, 2020, 12:58pm UTC](https://discourse.itk.org/t/filter-used-after-labelintensitystatisticsimagefilter/3108/5 "2020-05-28T12:58:04Z")

</div>

> [@dchen](#):
>
> As far as the maximum size, I think you need to do some sort of masking. For labels of size larger than dValue2, mask them out, i.e. change them to 0.

Dave, can you elaborate a little bit here, please ? Can you write me a little piece of pseudo code for “sort of masking” ? Are you talked about `sitk::Mask` filter ? If you did, what should I put as **maskImage**? And for **maskingValue**? Sorry for (maybe) so basically questions, I would appreciate your answer.

Regards,  
Flaviu.

---

<div class="post-metadata">

### Author: ![dchen](https://discourse.itk.org/user_avatar/discourse.itk.org/dchen/32/34_2.png) [@dchen](https://discourse.itk.org/u/dchen)
#### Post date: [May 28, 2020, 2:27pm UTC](https://discourse.itk.org/t/filter-used-after-labelintensitystatisticsimagefilter/3108/6 "2020-05-28T14:27:39Z")

</div>

Actually, using @blowekamp’s suggestion of the ChangeLabelImageFilter, the process of changing labels is trivial. Here’s some completely untested C++ code I’m writing off the top of my head:

```
std::map<double,double> change_map;
for (it = labels.begin(); it != labels.end(); ++it)
	{
		double size = stat.GetPhysicalSize(*it);
		if (size < dValue1 ||
			size > dValue2)
		{
			change_map.insert((double)(*it), 0);
		}
	}
sitk::Image result = sitk::ChangeLabel(cc, change_map);

```

I make no guarantees that this works or even compiles, since I’m more of a Python person these days.

---

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.itk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.itk.org/u/flaviu2)
#### Post date: [May 28, 2020, 2:39pm UTC](https://discourse.itk.org/t/filter-used-after-labelintensitystatisticsimagefilter/3108/7 "2020-05-28T14:39:23Z")

</div>

Thank you David. I have tried solution proposed by @blowekamp, I was meaning of yours, with masking, in order to learn as much as I can 🙂 , that is why I am written my previous post.

---

<div class="post-metadata">

### Author: ![dchen](https://discourse.itk.org/user_avatar/discourse.itk.org/dchen/32/34_2.png) [@dchen](https://discourse.itk.org/u/dchen)
#### Post date: [May 28, 2020, 2:50pm UTC](https://discourse.itk.org/t/filter-used-after-labelintensitystatisticsimagefilter/3108/8 "2020-05-28T14:50:40Z")

</div>

Ok, here was my masking idea, in Python, because everything is so much easier in Python.

```
img = sitk.ReadImage("your_label_img.nii")

# make a binary mask image for just value 42
mask_img = (img == 42)

# make an inverse of the mask
inverse_mask = 1-mask_img

# apply inverse_mask so that 42 goes to 0
new_img = img * inverse_mask

```

You could achieve similar results in C++, but you’d have to use a bunch of math image filters. Like I said, everything is just so much easier in Python.

---

<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: [May 28, 2020, 3:04pm UTC](https://discourse.itk.org/t/filter-used-after-labelintensitystatisticsimagefilter/3108/9 "2020-05-28T15:04:59Z")

</div>

If you include `sitkImageOperators.h`, then you will have the overloaded Image operator in C++.

---

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.itk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.itk.org/u/flaviu2)
#### Post date: [May 28, 2020, 3:05pm UTC](https://discourse.itk.org/t/filter-used-after-labelintensitystatisticsimagefilter/3108/10 "2020-05-28T15:05:24Z")

</div>

Thank you again Dave 🙂

For line:

```
# make an inverse of the mask
inverse_mask = 1-mask_img

```

I have found

`sitk::MaskNegated`

For line

```
# apply inverse_mask so that 42 goes to 0
new_img = img * inverse_mask

```

I have found

`sitk::InvertIntensity`

The only line which I don’t know how to translate in C++ is

`mask_img = (img == 42)`

???

---

<div class="post-metadata">

### Author: ![dchen](https://discourse.itk.org/user_avatar/discourse.itk.org/dchen/32/34_2.png) [@dchen](https://discourse.itk.org/u/dchen)
#### Post date: [May 28, 2020, 4:19pm UTC](https://discourse.itk.org/t/filter-used-after-labelintensitystatisticsimagefilter/3108/11 "2020-05-28T16:19:45Z")

</div>

```
mask_img = (img == 42)

```

For that you can use the sitk::Equal function, although, presumably with C++ ImageOperators just using that line directly ought to work.
