# Segmentation and modeling of MRI image

**URL:** <https://discourse.slicer.org/t/segmentation-and-modeling-of-mri-image/6238>\
**Category:** Development\
**Created:** [March 21, 2019, 2:18pm UTC](https://discourse.slicer.org/t/segmentation-and-modeling-of-mri-image/6238 "2019-03-21T14:18:53Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZiyunLiang](https://avatars.discourse-cdn.com/v4/letter/z/5daacb/32.png) [@ZiyunLiang](https://discourse.slicer.org/u/ZiyunLiang)\
**Post date:** [March 21, 2019, 2:18pm UTC](https://discourse.slicer.org/t/segmentation-and-modeling-of-mri-image/6238/1 "2019-03-21T14:18:53Z")

</div>

Hi,

I want to implement the segmentation of bladder from pelvic floor MRI images and then implement 3D reconstruction of the organ in a python scripted module.  
I currently created a model which can predict by each pixel of the MRI image. The model returns a (227,227,30) matrix of value 0 and 1. (the MRI image has 30 images with 227\*227 pixels). In the matrix, 1 indicates this pixel is a part of bladder and 0 is not. This model tells the position of the segmented organ in the image.  
For the 3D Modeling part, I think the _Show 3D_ and _Fill between slices_ of the Segment Editor module will do it. But how can I segment the organ by the matrix predicted by my model? Does anyone have any idea how to implement this?

Thanks for your time!  
Andrea

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [March 21, 2019, 2:26pm UTC](https://discourse.slicer.org/t/segmentation-and-modeling-of-mri-image/6238/2 "2019-03-21T14:26:51Z")

</div>

The data you call model we call binary labelmap. Please load is to Slicer as a labelmap (choose labelmap in the options of the load window, or convert it to labelmap after loading in the Volumes module), then import it as a segmentation (right-click labelmap in Data module and choose import labelmap to segmentation).

---

<div class="post-metadata">

**Author:** ![ZiyunLiang](https://avatars.discourse-cdn.com/v4/letter/z/5daacb/32.png) [@ZiyunLiang](https://discourse.slicer.org/u/ZiyunLiang)\
**Post date:** [March 21, 2019, 2:49pm UTC](https://discourse.slicer.org/t/segmentation-and-modeling-of-mri-image/6238/3 "2019-03-21T14:49:03Z")

</div>

Hi, thanks so much for your help.  
Your idea seems practical but I’m sorry I didn’t completely understand. I load MRI images in the format .gz to Slicer, and the model is a .h5 file which is made of a convolutional neural network. My model can predict the MRI images to a (227,227,30) matrix. What should I load to Slicer? I’m new to Slicer so sorry for lack of knowledge.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [March 21, 2019, 3:06pm UTC](https://discourse.slicer.org/t/segmentation-and-modeling-of-mri-image/6238/4 "2019-03-21T15:06:25Z")

</div>

Save the “model” (we call it labelmap volume) as a .nrrd or .mha file and you will be able to load it as segmentation or labelmap volume.

---

<div class="post-metadata">

**Author:** ![ZiyunLiang](https://avatars.discourse-cdn.com/v4/letter/z/5daacb/32.png) [@ZiyunLiang](https://discourse.slicer.org/u/ZiyunLiang)\
**Post date:** [March 21, 2019, 3:27pm UTC](https://discourse.slicer.org/t/segmentation-and-modeling-of-mri-image/6238/5 "2019-03-21T15:27:29Z")

</div>

Hi, thanks for answering.  
I’m a little confused. The “model” is a deep convolutional neural network, how can I load it as volume?

---

<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:** [March 21, 2019, 3:58pm UTC](https://discourse.slicer.org/t/segmentation-and-modeling-of-mri-image/6238/6 "2019-03-21T15:58:12Z")

</div>

@ZiyunLiang, what you call the “matrix” (output of your neural network “model”) is what we call a labelmap, so if you load that into Slicer, you can make it into a what Slicer calls a “model” which is a triangulated surface. 😀

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [March 21, 2019, 4:14pm UTC](https://discourse.slicer.org/t/segmentation-and-modeling-of-mri-image/6238/7 "2019-03-21T16:14:44Z")

</div>

That’s right. matrix = labelmap, not model as I said.

You cannot import your model to Slicer, but if it’s python or C++ or Matlab, then you can integrate it within a new module.

---

<div class="post-metadata">

**Author:** ![ZiyunLiang](https://avatars.discourse-cdn.com/v4/letter/z/5daacb/32.png) [@ZiyunLiang](https://discourse.slicer.org/u/ZiyunLiang)\
**Post date:** [March 22, 2019, 9:01am UTC](https://discourse.slicer.org/t/segmentation-and-modeling-of-mri-image/6238/8 "2019-03-22T09:01:07Z")

</div>

Hi, really appreciate your help and explanation, I have figured out a way to implement this now. However, I still got a problem when I’m trying to write it in my scripted python module.  
When I’m trying to convert my volume to labelmap by using `Volumes` module, `Volume information` section, and `Convert to label map`  
I used the code

> volumeNode=slicer.util.getNode(‘vtkMRMLScalarVolumeNode1’)  
> labelmap=slicer.mrmlScene.AddNewNodeByClass('vtkMRMLLabelMapVolumeNode’)  
> slicer.modules.Volumes.logic().CreateLabelVolumeFromVolume(vtkMRMLScene,label map,volumeNode)

But there is something wrong.

> AttributeError: ‘module’ object has no attribute ‘Volumes’

I don’t have much experience in writing python code in 3d Slicer so please help me out. What is the correct way of realizing this?

Thanks for your time!

---

<div class="post-metadata">

**Author:** ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)\
**Post date:** [March 22, 2019, 9:22am UTC](https://discourse.slicer.org/t/segmentation-and-modeling-of-mri-image/6238/9 "2019-03-22T09:22:00Z")

</div>

You have the case wrong which is causing the attribute error.

```python
slicer.modules.Volumes.logic().CreateLabelVolumeFromVolume(vtkMRMLScene,label map,volumeNode)

```

should be

```python
slicer.modules.volumes.logic().CreateLabelVolumeFromVolume(vtkMRMLScene,label map,volumeNode)

```

A tip is also to press {TAB} after any period to see the available options in an auto-complete type of way.

---

<div class="post-metadata">

**Author:** ![ZiyunLiang](https://avatars.discourse-cdn.com/v4/letter/z/5daacb/32.png) [@ZiyunLiang](https://discourse.slicer.org/u/ZiyunLiang)\
**Post date:** [March 22, 2019, 9:26am UTC](https://discourse.slicer.org/t/segmentation-and-modeling-of-mri-image/6238/10 "2019-03-22T09:26:29Z")

</div>

Hi, thanks for the quick response and the tip, I’ll keep that in mind. I tried this but I think I’m still implementing this function wrong. What should the vtkMRMLScene be?

> NameError: name ‘vtkMRMLScene’ is not defined

---

<div class="post-metadata">

**Author:** ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)\
**Post date:** [March 22, 2019, 9:36am UTC](https://discourse.slicer.org/t/segmentation-and-modeling-of-mri-image/6238/11 "2019-03-22T09:36:51Z")

</div>

Yes any variable needs to be defined as well. In this case, vtkMRMLScene wasn’t defined. We can instead use “slicer.mrmlScene” which is defined.

```python
slicer.modules.volumes.logic().CreateLabelVolumeFromVolume(slicer.mrmlScene, labelmap, volumeNode)

```

---

<div class="post-metadata">

**Author:** ![ZiyunLiang](https://avatars.discourse-cdn.com/v4/letter/z/5daacb/32.png) [@ZiyunLiang](https://discourse.slicer.org/u/ZiyunLiang)\
**Post date:** [March 22, 2019, 9:40am UTC](https://discourse.slicer.org/t/segmentation-and-modeling-of-mri-image/6238/12 "2019-03-22T09:40:23Z")

</div>

Hi James,  
Thanks so much for your help. It’s working now.😀

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [March 22, 2019, 1:29pm UTC](https://discourse.slicer.org/t/segmentation-and-modeling-of-mri-image/6238/13 "2019-03-22T13:29:14Z")

</div>

Instead of this approach, I’d load the volume as labelmap to begin with:

`slicer.util.loadLabelVolume('d:/path/to/Voulme.nrrd',{})`

---

<div class="post-metadata">

**Author:** ![ZiyunLiang](https://avatars.discourse-cdn.com/v4/letter/z/5daacb/32.png) [@ZiyunLiang](https://discourse.slicer.org/u/ZiyunLiang)\
**Post date:** [March 22, 2019, 1:37pm UTC](https://discourse.slicer.org/t/segmentation-and-modeling-of-mri-image/6238/14 "2019-03-22T13:37:38Z")

</div>

This is indeed an easier way to do this. Thanks:smiley:
