# I need to extract only the brain portion excluding the skull from the CTA image. How can I do it using simpleITK filters? I tried otsu thresholding filters followed by connected components largest label but haven't got result i expected. Can someone help?

**URL:** <https://discourse.itk.org/t/i-need-to-extract-only-the-brain-portion-excluding-the-skull-from-the-cta-image-how-can-i-do-it-using-simpleitk-filters-i-tried-otsu-thresholding-filters-followed-by-connected-components-largest-label-but-havent-got-result-i-expected-can-someone-help/5935>\
**Category:** Beginner Questions\
**Tags:** simpleitk, registration\
**Created:** [May 11, 2023, 9:52am UTC](https://discourse.itk.org/t/i-need-to-extract-only-the-brain-portion-excluding-the-skull-from-the-cta-image-how-can-i-do-it-using-simpleitk-filters-i-tried-otsu-thresholding-filters-followed-by-connected-components-largest-label-but-havent-got-result-i-expected-can-someone-help/5935 "2023-05-11T09:52:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![arpan56](https://discourse.itk.org/letter_avatar_proxy/v4/letter/a/b2d939/32.png) [@arpan56](https://discourse.itk.org/u/arpan56)\
**Post date:** [May 11, 2023, 9:52am UTC](https://discourse.itk.org/t/i-need-to-extract-only-the-brain-portion-excluding-the-skull-from-the-cta-image-how-can-i-do-it-using-simpleitk-filters-i-tried-otsu-thresholding-filters-followed-by-connected-components-largest-label-but-havent-got-result-i-expected-can-someone-help/5935/1 "2023-05-11T09:52:13Z")

</div>

```auto
otsu_filter = sitk.OtsuThresholdImageFilter()
otsu_filter.SetInsideValue(0)
otsu_filter.SetOutsideValue(1)
binary_image = otsu_filter.Execute(input_image)

cc_filter = sitk.ConnectedComponentImageFilter()
brain_mask = cc_filter.Execute(binary_image)

label_shape_filter = sitk.LabelShapeStatisticsImageFilter()
label_shape_filter.Execute(brain_mask)
labels = label_shape_filter.GetLabels()
sizes = [label_shape_filter.GetPhysicalSize(label) for label in labels]
max_label = labels[sizes.index(max(sizes))]

binary_mask = sitk.BinaryThreshold(brain_mask, max_label, max_label, 1, 0)

brain_image = sitk.Mask(input_image, binary_mask)

```

---

<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:** [May 11, 2023, 12:57pm UTC](https://discourse.itk.org/t/i-need-to-extract-only-the-brain-portion-excluding-the-skull-from-the-cta-image-how-can-i-do-it-using-simpleitk-filters-i-tried-otsu-thresholding-filters-followed-by-connected-components-largest-label-but-havent-got-result-i-expected-can-someone-help/5935/2 "2023-05-11T12:57:59Z")

</div>

Hello @arpan56,

The Otsu filter is appropriate when you have a bi-modal intensity distribution. In your case you roughly have (1) brain (2) skull (3) background, so you could try the [OtsuMultipleThresholdsImageFilter.](https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1OtsuMultipleThresholdsImageFilter.html), setting the number of thresholds to two. Most likely this will not work well, though better than a single threshold which doesn’t match the image content.

The operation you want to perform is known as “skull stripping”. It requires more complex algorithmic flows. See [the recipe](https://lassoan.github.io/SlicerSegmentationRecipes/CTSkullStripping/) from Slicer, or [this paper](https://www.insight-journal.org/browse/publication/859) for a registration based approach, or [this paper](https://www.sciencedirect.com/science/article/abs/pii/S0925231219304734) which describes a deep learning based approach.

---

<div class="post-metadata">

**Author:** ![Stephen\_Aylward](https://discourse.itk.org/user_avatar/discourse.itk.org/stephen_aylward/32/654_2.png) [@Stephen\_Aylward](https://discourse.itk.org/u/Stephen_Aylward)\
**Post date:** [May 11, 2023, 3:49pm UTC](https://discourse.itk.org/t/i-need-to-extract-only-the-brain-portion-excluding-the-skull-from-the-cta-image-how-can-i-do-it-using-simpleitk-filters-i-tried-otsu-thresholding-filters-followed-by-connected-components-largest-label-but-havent-got-result-i-expected-can-someone-help/5935/3 "2023-05-11T15:49:27Z")

</div>

Hi,

If speed isn’t a requirement, there is an example notebook in itkTubeTK that performs CTA skull stripping (not that most skull stripping methods are for MRI/MRA/CT - few work with CTA).

> <https://github.com/InsightSoftwareConsortium/ITKTubeTK/blob/master/examples/CTA-Head/1-SegmentBrainFromCTA%20-%20Jupyter%20Notebook.pdf>

The approach registers 8 pre-segmented head CTA with your data, uses a voting scheme to create an initial inner and outer mask, and then uses region growing and morphological operators to skull strip. It isn’t perfect by any means, but it is typically “good”-ish.

To run the notebook, you’ll need to clone ITKTubeTK from github, pip install girder-client ([https://pypi.org/project/girder-client/](https://pypi.org/project/girder-client/)), and then run the ITKTubeTK/examples/Download-Data.sh script to download the 8 pre-segmented head CTAs.

Note that there is an itk module called itkSkullStripping. You can pip install it, but I had no luck getting it to work with itkv5.3 last week. It probably needs updating.

Stephen

---

<div class="post-metadata">

**Author:** ![Stephen\_Aylward](https://discourse.itk.org/user_avatar/discourse.itk.org/stephen_aylward/32/654_2.png) [@Stephen\_Aylward](https://discourse.itk.org/u/Stephen_Aylward)\
**Post date:** [May 11, 2023, 3:52pm UTC](https://discourse.itk.org/t/i-need-to-extract-only-the-brain-portion-excluding-the-skull-from-the-cta-image-how-can-i-do-it-using-simpleitk-filters-i-tried-otsu-thresholding-filters-followed-by-connected-components-largest-label-but-havent-got-result-i-expected-can-someone-help/5935/4 "2023-05-11T15:52:54Z")

</div>

Correct link: [https://github.com/InsightSoftwareConsortium/ITKTubeTK/blob/master/examples/CTA-Head/1-SegmentBrainFromCTA.ipynb](https://github.com/InsightSoftwareConsortium/ITKTubeTK/blob/master/examples/CTA-Head/1-SegmentBrainFromCTA.ipynb)

---

<div class="post-metadata">

**Author:** ![arpan56](https://discourse.itk.org/letter_avatar_proxy/v4/letter/a/b2d939/32.png) [@arpan56](https://discourse.itk.org/u/arpan56)\
**Post date:** [May 12, 2023, 5:10am UTC](https://discourse.itk.org/t/i-need-to-extract-only-the-brain-portion-excluding-the-skull-from-the-cta-image-how-can-i-do-it-using-simpleitk-filters-i-tried-otsu-thresholding-filters-followed-by-connected-components-largest-label-but-havent-got-result-i-expected-can-someone-help/5935/5 "2023-05-12T05:10:10Z")

</div>

Yes, thank you. I will look into the materials you provided.
