Filter used after LabelIntensityStatisticsImageFilter

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 …

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

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

2 Likes

You can also use the ChangeLabelImageFilter 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.

3 Likes

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.

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.

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.

2 Likes

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 :slight_smile: , that is why I am written my previous post.

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.

1 Like

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

1 Like

Thank you again Dave :slight_smile:

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)

???

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.

1 Like