# Apply Region Growing Segmentation Filter Multiple Times

**URL:** <https://discourse.itk.org/t/apply-region-growing-segmentation-filter-multiple-times/4747>\
**Category:** Algorithms\
**Created:** [January 21, 2022, 9:13pm UTC](https://discourse.itk.org/t/apply-region-growing-segmentation-filter-multiple-times/4747 "2022-01-21T21:13:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oddone-Scatena](https://discourse.itk.org/user_avatar/discourse.itk.org/oddone-scatena/32/2651_2.png) [@Oddone-Scatena](https://discourse.itk.org/u/Oddone-Scatena)\
**Post date:** [January 21, 2022, 9:13pm UTC](https://discourse.itk.org/t/apply-region-growing-segmentation-filter-multiple-times/4747/1 "2022-01-21T21:13:24Z")

</div>

Hello,

I am currently sementing the trachea with ConnectedThresholdImageFilter. Several authors segment the trachea using explosion controlled region growing (eg. “Automatic lung segmentation from thoracic computed tomography scans using a hybrid approach with error detection”). That is, the apply the growing algorithm multiple times changing the threshold and when they detect an “growth explosion” (usually two times the number of segmented pixels), they set the “optimal” threshold.

I was wondering how could I apply the ConnectedThresholdImageFilter multiple times. The problem is that the setUpper and setLower thresholds are const and I can’t change them.

Thanks,

Rafael

---

<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:** [January 22, 2022, 12:03am UTC](https://discourse.itk.org/t/apply-region-growing-segmentation-filter-multiple-times/4747/2 "2022-01-22T00:03:20Z")

</div>

You can invoke SetUpper and SetLower multiple times. Pseudocode:

```cpp
auto filter = ConnectedThresholdImageFilter::New();
for i:
  filter->SetUpper(100+i*10);
  filter->Update();
  iResult = filter->GetOutput();
  // count voxels, whatever
  if (condition)
    break; // exit the for loop
// now compute optimal upper and lower threshold
filter->SetUpper(optimal);
filter->Update();
optimalResult = filter->GetOutput();

```

---

<div class="post-metadata">

**Author:** ![Oddone-Scatena](https://discourse.itk.org/user_avatar/discourse.itk.org/oddone-scatena/32/2651_2.png) [@Oddone-Scatena](https://discourse.itk.org/u/Oddone-Scatena)\
**Post date:** [January 27, 2022, 2:28am UTC](https://discourse.itk.org/t/apply-region-growing-segmentation-filter-multiple-times/4747/3 "2022-01-27T02:28:10Z")

</div>

I used filter-\>getUpper() and the values of the thresholds do indeed change. But the segmented image is always the same.

---

<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:** [January 27, 2022, 1:22pm UTC](https://discourse.itk.org/t/apply-region-growing-segmentation-filter-multiple-times/4747/4 "2022-01-27T13:22:36Z")

</div>

What happens if you move the creation of the filter inside the for loop?

```auto
for i:
  auto filter = ConnectedThresholdImageFilter::New();
  filter->SetUpper(100+i*10);
  filter->Update();
  iResult = filter->GetOutput();
  // count voxels, whatever
  if (condition)
    break; // exit the for loop
// now compute optimal upper and lower threshold
auto filter = ConnectedThresholdImageFilter::New();
filter->SetUpper(optimal);
filter->Update();
optimalResult = filter->GetOutput();
```

---

<div class="post-metadata">

**Author:** ![Oddone-Scatena](https://discourse.itk.org/user_avatar/discourse.itk.org/oddone-scatena/32/2651_2.png) [@Oddone-Scatena](https://discourse.itk.org/u/Oddone-Scatena)\
**Post date:** [January 29, 2022, 8:59pm UTC](https://discourse.itk.org/t/apply-region-growing-segmentation-filter-multiple-times/4747/5 "2022-01-29T20:59:22Z")

</div>

![IMG_20220129_172207871](https://discourse.itk.org/uploads/default/original/2X/4/4f0b4a8d3550627753bc5771dbeb42330b86e67b.jpeg)  
 ![IMG_20220129_161813898](https://discourse.itk.org/uploads/default/original/2X/f/fb2f748a2300e9078a2b082c35ecc38620374ed5.jpeg)

---

<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:** [January 31, 2022, 4:36pm UTC](https://discourse.itk.org/t/apply-region-growing-segmentation-filter-multiple-times/4747/6 "2022-01-31T16:36:04Z")

</div>

These photos don’t tell me whether the code worked, nor whether you are satisfied.

---

<div class="post-metadata">

**Author:** ![Oddone-Scatena](https://discourse.itk.org/user_avatar/discourse.itk.org/oddone-scatena/32/2651_2.png) [@Oddone-Scatena](https://discourse.itk.org/u/Oddone-Scatena)\
**Post date:** [January 31, 2022, 6:11pm UTC](https://discourse.itk.org/t/apply-region-growing-segmentation-filter-multiple-times/4747/7 "2022-01-31T18:11:27Z")

</div>

> [@dzenanz](#):
>
> These photos don’t tell me whether the code worked, nor whether you are satisfied

it worked

---

<div class="post-metadata">

**Author:** ![Oddone-Scatena](https://discourse.itk.org/user_avatar/discourse.itk.org/oddone-scatena/32/2651_2.png) [@Oddone-Scatena](https://discourse.itk.org/u/Oddone-Scatena)\
**Post date:** [January 31, 2022, 6:11pm UTC](https://discourse.itk.org/t/apply-region-growing-segmentation-filter-multiple-times/4747/8 "2022-01-31T18:11:36Z")

</div>

thanks!
