# Extract image using threshold

**URL:** <https://discourse.itk.org/t/extract-image-using-threshold/2757>\
**Category:** Algorithms\
**Created:** [February 23, 2020, 3:01pm UTC](https://discourse.itk.org/t/extract-image-using-threshold/2757 "2020-02-23T15:01:43Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![osemmler](https://discourse.itk.org/user_avatar/discourse.itk.org/osemmler/32/1252_2.png) [@osemmler](https://discourse.itk.org/u/osemmler)\
**Post date:** [February 23, 2020, 3:01pm UTC](https://discourse.itk.org/t/extract-image-using-threshold/2757/1 "2020-02-23T15:01:43Z")

</div>

Hello, can you advise me the most efficient combination of filters to extract region of image based on threshold value? (ie reduce the image by margins that are below the defined threshold value)

---

<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:** [February 24, 2020, 2:21pm UTC](https://discourse.itk.org/t/extract-image-using-threshold/2757/2 "2020-02-24T14:21:35Z")

</div>

In SimpleITK you could convert your threshold into a label image, and then run the LabelShapeStatisticsImageFilter. One of the methods is GetBoundingBox, which you can use to extract the image region. Here’s the documentation for the filter:

[https://itk.org/SimpleITKDoxygen/html/classitk\_1\_1simple\_1\_1LabelShapeStatisticsImageFilter.html](https://itk.org/SimpleITKDoxygen/html/classitk_1_1simple_1_1LabelShapeStatisticsImageFilter.html)

In ITK, the equivalent is the LabelGeometryImageFilter, although I haven’t used that myself.

[https://itk.org/Doxygen/html/classitk\_1\_1LabelGeometryImageFilter.html](https://itk.org/Doxygen/html/classitk_1_1LabelGeometryImageFilter.html)

---

<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:** [February 25, 2020, 2:48pm UTC](https://discourse.itk.org/t/extract-image-using-threshold/2757/3 "2020-02-25T14:48:24Z")

</div>

What you might need is [`BinaryThresholdImageFilter`](https://itk.org/Doxygen/html/classitk_1_1BinaryThresholdImageFilter.html). Then followed by iterator pass to determine bounds of your object, or use [LabelGeometryImageFilter](https://itk.org/Doxygen/html/classitk_1_1LabelGeometryImageFilter.html) if you prefer use of existing filters over efficient code. Finally use [`RegionOfInterestImageFilter`](https://itk.org/Doxygen/html/classitk_1_1RegionOfInterestImageFilter.html) to get the image reduced to only the extracted object.

---

<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 8, 2020, 2:43pm UTC](https://discourse.itk.org/t/extract-image-using-threshold/2757/4 "2020-03-08T14:43:55Z")

</div>

Hello Dave. Based on your words, I have tried:

```
BOOL CMyDoc::Segement5(sitk::Image& img)
{
	sitk::Image imgTemp(img);
	sitk::LabelShapeStatisticsImageFilter statistic;
	statistic.Execute(imgTemp);
	statistic.SetBackgroundValue(200);
	std::vector<unsigned int> box = statistic.GetBoundingBox(20);
	sitk::ConfidenceConnectedImageFilter confidence;
	confidence.SetSeed(box);
	img = confidence.Execute(imgTemp);

```

Now, **img** object I have transferred forward into **VTK** and I visualized as **vtkVolume** (color), but I see only a diffuse cube. Can you lead me into a bone segmentation ? I need to remove a bone from a _VTK volume_, or, at least to color it.

Of course, I have tried with other **SetBackgroundValue** values, none of them had worked.

---

<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 9, 2020, 12:41pm UTC](https://discourse.itk.org/t/extract-image-using-threshold/2757/5 "2020-03-09T12:41:25Z")

</div>

Of course, I have tried every algorithm from here: [http://insightsoftwareconsortium.github.io/SimpleITK-Notebooks/Python\_html/300\_Segmentation\_Overview.html](http://insightsoftwareconsortium.github.io/SimpleITK-Notebooks/Python_html/300_Segmentation_Overview.html)

I guess I need a good values for this task …

---

<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:** [March 9, 2020, 1:03pm UTC](https://discourse.itk.org/t/extract-image-using-threshold/2757/6 "2020-03-09T13:03:42Z")

</div>

SetBackgroundValue has to be called before the Execute. Execute is when the stats are actually computed. After that, SetBackground won’t have any effect unless you call Execute again.

---

<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 10, 2020, 6:05pm UTC](https://discourse.itk.org/t/extract-image-using-threshold/2757/7 "2020-03-10T18:05:50Z")

</div>

David, you had right 🙂

```
BOOL CMyDoc::Segement5(sitk::Image& img)
{
	sitk::Image imgTemp(img);
	sitk::LabelShapeStatisticsImageFilter statistic;
	statistic.SetBackgroundValue(200);
	statistic.Execute(imgTemp);
	std::vector<unsigned int> box = statistic.GetBoundingBox(20);
	sitk::ConfidenceConnectedImageFilter confidence;
	confidence.SetSeed(box);
	img = confidence.Execute(imgTemp);

```

But after I corrected the order of code, I got no segmentation organ … of course, due to statistic.SetBackgroundValue(200); and / or statistic.GetBoundingBox(20);

Where can I find proper values for bone segmentation ?

---

<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:** [March 10, 2020, 6:19pm UTC](https://discourse.itk.org/t/extract-image-using-threshold/2757/8 "2020-03-10T18:19:45Z")

</div>

Hello @flaviu2,  
If you are working with CT there are [known Hounsfield unit values for bone](https://en.wikipedia.org/wiki/Hounsfield_scale). If you are working with another modality the value may not be consistent (MR will vary across machines), so you will have to compute threshold values for each image.

---

<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 10, 2020, 6:41pm UTC](https://discourse.itk.org/t/extract-image-using-threshold/2757/9 "2020-03-10T18:41:05Z")

</div>

Thank Ziv. I have a CT modality, and I am struggle to find a SimpleITK filter to achieve that. And I’ve found sitk::ConfidenceConnected, but I don’t know where to put those HU values for bones … look, I wrote this:

```
std::vector<std::vector<unsigned int>> list;
InitializeList2(list);

    sitk::Image img;
	sitk::Image imgTemp(img);
	img = sitk::ConfidenceConnected(imgTemp, list, 5, 2.5, 2, 200);

```

and the list is:

```
void CMyDoc::InitializeList2(std::vector<std::vector<unsigned int>>& list)
{
	std::vector<unsigned int> sub;
	sub.push_back(1000);
	sub.push_back(1200);
	sub.push_back(1900);
	list.push_back(sub);
}

```

but I got bone segmentation … only a diffuse noising cube … obviously, I didn’t write something right, or, more probably, I didn’t understand something right 🙂

---

<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:** [March 10, 2020, 6:58pm UTC](https://discourse.itk.org/t/extract-image-using-threshold/2757/10 "2020-03-10T18:58:42Z")

</div>

Ok. Well, segmentation based on thresholds is a very basic approach and will rarely work, sometimes with luck it will. ConfidenceConnected does not allow for explicit thresholds, [ConnectedThreshold](https://itk.org/SimpleITKDoxygen/html/classitk_1_1simple_1_1ConnectedThresholdImageFilter.html) does.

If you don’t mind reading Python code, [this jupyter notebook](https://github.com/InsightSoftwareConsortium/SimpleITK-Notebooks/blob/master/Python/30_Segmentation_Region_Growing.ipynb) illustrates how to use several region growing filters including the two mentioned above.

---

<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 10, 2020, 8:45pm UTC](https://discourse.itk.org/t/extract-image-using-threshold/2757/11 "2020-03-10T20:45:25Z")

</div>

With every discussion from here, some things become clearly ! Many many thanks !  
So, in order to use my clicks points, I should not use ConfidenceConnected filter because does not allow explicit thresholds. For this approach, I need to use [ConnectedThreshold](https://itk.org/SimpleITKDoxygen/html/classitk_1_1simple_1_1ConnectedThresholdImageFilter.html) filter.

Now I am gonna figure out how to use vtkCellPicker point inside ConnectedThreshold filter … 🙄

Yaniv, thank you a lot for your words, if you have more, don’t hesitate to provide me 🙂

Flaviu.
