# How can I segment T1W MRI and produce its image mask?

**URL:** <https://discourse.slicer.org/t/how-can-i-segment-t1w-mri-and-produce-its-image-mask/24818>\
**Category:** Support\
**Tags:** segmentation, extensions-manager, dicom\
**Created:** [August 18, 2022, 11:40am UTC](https://discourse.slicer.org/t/how-can-i-segment-t1w-mri-and-produce-its-image-mask/24818 "2022-08-18T11:40:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![das](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/das/32/67119_2.png) [@das](https://discourse.slicer.org/u/das)\
**Post date:** [August 18, 2022, 11:40am UTC](https://discourse.slicer.org/t/how-can-i-segment-t1w-mri-and-produce-its-image-mask/24818/1 "2022-08-18T11:40:48Z")

</div>

I currently have a task at hand which has been challenging for me. I wish to produce a mask image (to be in .nii format) from a T1-weighted image (which is in .nii format and containing a glioma region). Before producing this image, I wish to segment the image into:

1. White matter
2. Grey matter
3. CSF
4. Tumour (if possible, with peritumoral edema region)

I would be very glad to get your recommendations and suggestions on the best way this task could be done with 3D slicer. I would not mind articles and lectures notes.

By the way, is it possible to also extract out only CSF regions and save as xxx.nii?

Many thanks in advance.

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [August 18, 2022, 2:48pm UTC](https://discourse.slicer.org/t/how-can-i-segment-t1w-mri-and-produce-its-image-mask/24818/2 "2022-08-18T14:48:40Z")

</div>

I don’t think there’s an off-the-shelf automated way of doing all of that currently, but if anybody knows of one I hope they will post here.

There are several pieces available and with a bit of manual work you can put together a nice segmentation.

For example the models trained on [BraTS](http://braintumorsegmentation.org/) are getting very good at tumor and edema. E.g. [this one has been made available for Slicer](https://github.com/razeineldin/DeepSeg). Other tools like [SynthSeg](https://github.com/BBillot/SynthSeg) are very good at non-tumor brain segmentation (including CSF). You can manually combine the results of these and fix errors with the Segment Editor and extract CSF masks and other structures.

---

<div class="post-metadata">

**Author:** ![ebrahim](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ebrahim/32/13403_2.png) [@ebrahim](https://discourse.slicer.org/u/ebrahim)\
**Post date:** [August 18, 2022, 4:17pm UTC](https://discourse.slicer.org/t/how-can-i-segment-t1w-mri-and-produce-its-image-mask/24818/3 "2022-08-18T16:17:48Z")

</div>

I don’t know what Slicer offers for this task directly, but here is an approach I have taken before using [antspy](https://github.com/ANTsX/ANTsPy) [prior-based segmentation](https://antspy.readthedocs.io/en/latest/segmentation.html?highlight=prior_based_segmentation#ants.prior_based_segmentation). Below is a sketch of the code to give the idea; I haven’t tested this code directly.

```nohighlight
import ants

# This method of segmentation uses registration to
# an atlas that comes with a known segmentation
# I have used SRI24 in the past: https://www.nitrc.org/projects/sri24/

# Load atlas
atlas_img = ants.image_read(path_to_T1_atlas)
atlas_csf_img = ants.image_read(path_to_csf_segment_of_atlas)
atlas_gm_img = ants.image_read(path_to_gm_segment_of_atlas)
atlas_wm_img = ants.image_read(path_to_wm_segment_of_atlas)

# Load input image
img = ants.image_read(path_to_input_image)

# Run N4 bias correction
img_n4 = ants.n4_bias_field_correction(img)

# Register to atlas
img_reg = ants.registration(atlas_img, img_n4)

# create mask to restrict the segmentation inside this region
priors = [atlas_csf_img, atlas_gm_img, atlas_wm_img]
mask = priors[0].copy()
mask_view = mask.view()
for i in range(1, len(priors)):
    mask_view[priors[i].numpy() > 0] = 1
mask_view[mask_view > 0] = 1

# Do prior-based segmentation
seg = ants.prior_based_segmentation(img_reg, priors, mask)

# Take csf label
seg_img_view = seg['segmentation'].view()
csf_img = seg['segmentation'].copy()
csf_img_view = feature_img.view()
csf_img_view.fill(0)
csf_img_view[seg_img_view == 1] = 1 # 1 is the CSF label

# Write to nifti file
ants.image_write(csf_img, "csf_mask.nii.gz")

```
