# Remove small component

**URL:** https://discourse.itk.org/t/remove-small-component/2858
**Category:** Beginner Questions
**Tags:** simpleitk
**Created:** [March 20, 2020, 12:29pm UTC](https://discourse.itk.org/t/remove-small-component/2858 "2020-03-20T12:29:32Z")
**Posts on this page:** 9
**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: [March 20, 2020, 12:29pm UTC](https://discourse.itk.org/t/remove-small-component/2858/1 "2020-03-20T12:29:32Z")

</div>

I intend to remove from sitk::Image the smallest component from it (in the first stage). For that, I wrote:

```
BOOL CMyDoc::Segement(sitk::Image& img)
{
		sitk::Image imgTemp(img);
		int64_t nMaxLabel = 0, nMaxSize = 0;
		sitk::Image cc = sitk::SLIC(imgTemp);
		sitk::LabelIntensityStatisticsImageFilter statistics;
		statistics.Execute(cc, imgTemp);
		std::vector<int64_t>::iterator it;
		std::vector<int64_t> labels = statistics.GetLabels();
		for (it = labels.begin(); it != labels.end(); ++it)
		{
			if (nMaxLabel < statistics.GetPhysicalSize(*it))
			{
				nMaxLabel = *it;
				nMaxSize = statistics.GetPhysicalSize(*it);
			}
		}

		CArray<long, long> arr;
		for (it = labels.begin(); it != labels.end(); ++it)
		{
			if (statistics.GetPhysicalSize(*it) > nMaxSize * 0.5)
				arr.Add(*it);
		}

```

My question is, how can use it **arr** object to remove the points from the resulting image ? What kind of filter should I use ?

I have read about this filter:[https://itk.org/SimpleITKDoxygen/html/classitk\_1\_1simple\_1\_1MaskNegatedImageFilter.html](https://itk.org/SimpleITKDoxygen/html/classitk_1_1simple_1_1MaskNegatedImageFilter.html), but I don’t know how to use it in this case … I mean, what mask image should I provide ? [https://itk.org/SimpleITKDoxygen/html/classitk\_1\_1simple\_1\_1MaskNegatedImageFilter.html#ad5c71726224210803c9233e5ed8473d0](https://itk.org/SimpleITKDoxygen/html/classitk_1_1simple_1_1MaskNegatedImageFilter.html#ad5c71726224210803c9233e5ed8473d0)

Might be a trivial question, but for me is not. Thank you for your patience and your time.

Flaviu.

---

<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: [March 20, 2020, 3:15pm UTC](https://discourse.itk.org/t/remove-small-component/2858/2 "2020-03-20T15:15:53Z")

</div>

[Relabel filter](https://itk.org/Doxygen/html/classitk_1_1RelabelComponentImageFilter.html) reorders the labels and removes the small ones. Would that work for you?

---

<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: [March 20, 2020, 3:56pm UTC](https://discourse.itk.org/t/remove-small-component/2858/3 "2020-03-20T15:56:27Z")

</div>

Good point, I will try it.

I have tried myself something:

```
	std::vector<unsigned int> size = imgTemp.GetSize();
	int64_t* p = imgTemp.GetBufferAsInt64();
	unsigned int nCount = size[0] * size[1] * size[2];
	for (unsigned int i = 0; i < nCount; ++i)
	{
		TRACE("%d===%I64d\n", i, p[i]);
	}

```

but this is no fit with **statistics.GetLabels()** method:

`sitk::ERROR: The image is of type: 16-bit signed integer but the GetBuffer access method requires type: 64-bit signed integer!`

… hmm … what could I plugin the data ?

---

<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: [March 21, 2020, 7:56pm UTC](https://discourse.itk.org/t/remove-small-component/2858/4 "2020-03-21T19:56:01Z")

</div>

@dzenanz, seem to work sitk::RelabelComponent filter … 🙂 the resulting image are not very sharp, but it is a step forward.

---

<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: [March 26, 2020, 11:43am UTC](https://discourse.itk.org/t/remove-small-component/2858/5 "2020-03-26T11:43:56Z")

</div>

I have used sitk:: **RelabelComponent** with pretty well results. But I noticed that if I _denoise_ the image before applying this filter I got an error. Here is the code:

```
	sitk::Image imgTemp(imgOrg);
	sitk::Image curvature = sitk::CurvatureFlow(imgTemp, 0.125, 5);
	imgOuput = sitk::RelabelComponent(curvature, 200);

```

and the error is: _sitk::ERROR: Pixel type: 64-bit float is not supported in 3D byclass itk::simple::RelabelComponentImageFilter_

If I didn’t apply **sitk::CurvatureFlow** I have no problem … how can I overcome this error ?

Thanks much.  
Flaviu.

---

<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: [March 26, 2020, 12:49pm UTC](https://discourse.itk.org/t/remove-small-component/2858/6 "2020-03-26T12:49:52Z")

</div>

@blowekamp @zivy?

---

<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: [March 26, 2020, 12:59pm UTC](https://discourse.itk.org/t/remove-small-component/2858/7 "2020-03-26T12:59:31Z")

</div>

It makes no sense to run diffusion on a labeled image and then “relabel” it. I can’t help here.

---

<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: [March 26, 2020, 1:14pm UTC](https://discourse.itk.org/t/remove-small-component/2858/9 "2020-03-26T13:14:48Z")

</div>

“_run diffusion on a labeled image_” **imgOrg** is not labeled image. Here is the functional code:

`imgOut = sitk::CurvatureFlow(imgOrg, 0.022, 1);`

And here is the code with the error:

```
sitk::Image curvature = sitk::CurvatureFlow(imgOrg, 0.022, 1);
imgOut = sitk::RelabelComponent(curvature, 200);

```

The fact is that another filter is run without any problem:

```
sitk::Image curvature = sitk::CurvatureFlow(imgOrg, 0.022, 1);
imgOut = sitk::ConnectedThreshold(curvature, list, 1400, 2600);
```

---

<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: [March 26, 2020, 3:43pm UTC](https://discourse.itk.org/t/remove-small-component/2858/10 "2020-03-26T15:43:34Z")

</div>

I guess I have found a workaround for **CurvatureFlow / RelabelComponent** issue, and I am listing here for anyone who meet the same problem:

```
sitk::Image curvature = sitk::CurvatureFlow(imgOrg, 0.022, 1);
sitk::Image imgCast = sitk::Cast(curvature, imgOrg.GetPixelID());
imgOut = sitk::RelabelComponent(imgCast, 200);

```

Please correct me if I did something wrong.

Flaviu.
