# Load Dicom Image and a mesh

**URL:** <https://discourse.slicer.org/t/load-dicom-image-and-a-mesh/4468>\
**Category:** Development\
**Tags:** dicom\
**Created:** [October 19, 2018, 3:56pm UTC](https://discourse.slicer.org/t/load-dicom-image-and-a-mesh/4468 "2018-10-19T15:56:03Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abdelkhalek](https://avatars.discourse-cdn.com/v4/letter/a/a6a055/32.png) [@Abdelkhalek](https://discourse.slicer.org/u/Abdelkhalek)\
**Post date:** [October 19, 2018, 3:56pm UTC](https://discourse.slicer.org/t/load-dicom-image-and-a-mesh/4468/1 "2018-10-19T15:56:03Z")

</div>

Hi, how can I create a .py script in order to create a button for loading a .mha, .hdr files and a button for .stl and .vtk files, please?

---

<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:** [October 20, 2018, 1:58am UTC](https://discourse.slicer.org/t/load-dicom-image-and-a-mesh/4468/2 "2018-10-20T01:58:35Z")

</div>

Start by looking through the programming tutorials:

[https://www.slicer.org/wiki/Documentation/4.10/Training#Tutorials\_for\_software\_developers](https://www.slicer.org/wiki/Documentation/4.10/Training#Tutorials_for_software_developers)

The script repository also has lots of examples to learn from:

[https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository)

---

<div class="post-metadata">

**Author:** ![Abdelkhalek](https://avatars.discourse-cdn.com/v4/letter/a/a6a055/32.png) [@Abdelkhalek](https://discourse.slicer.org/u/Abdelkhalek)\
**Post date:** [October 20, 2018, 10:06am UTC](https://discourse.slicer.org/t/load-dicom-image-and-a-mesh/4468/3 "2018-10-20T10:06:33Z")

</div>

Thank you for your answer. I checked both references but I did not find how can I import an .stl and .vtk files using a button and Python programming language. Could you please send me an exemple of the code?

---

<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:** [October 20, 2018, 1:30pm UTC](https://discourse.slicer.org/t/load-dicom-image-and-a-mesh/4468/4 "2018-10-20T13:30:21Z")

</div>

I don’t think there’s an existing example that does exactly that - but it doesn’t sound like a very difficult task if you learn how to do it. The best is to start with examples that do something similar and then experiment.

Why do you need this program? What do you intend to do with it?

---

<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:** [October 21, 2018, 2:46am UTC](https://discourse.slicer.org/t/load-dicom-image-and-a-mesh/4468/5 "2018-10-21T02:46:01Z")

</div>

You can load a model from stl or vtk file using [`slicer.util.loadModel()`](https://slicer.readthedocs.io/en/latest/developer_guide/slicer.html#module-slicer.util). You may also use `slicer.util.openAddModelDialog()` to show a model file selector dialog and load the selected file:

```
button = qt.QPushButton("Load!")
button.connect("clicked()", slicer.util.openAddModelDialog)
button.show()
```

---

<div class="post-metadata">

**Author:** ![Abdelkhalek](https://avatars.discourse-cdn.com/v4/letter/a/a6a055/32.png) [@Abdelkhalek](https://discourse.slicer.org/u/Abdelkhalek)\
**Post date:** [October 22, 2018, 8:52am UTC](https://discourse.slicer.org/t/load-dicom-image-and-a-mesh/4468/6 "2018-10-22T08:52:48Z")

</div>

Thank you Andras for your help,  
After using the code which you suggested :

```auto
# button for importing mesh files
    button = qt.QPushButton("Load Mesh")
    self.formFrame.layout().addWidget(button)
    button.connect("clicked()", slicer.util.openAddModelDialog)
    button.show()

```

I have got the mesh in the 3D Widget and out of the original image (dicom). The mesh is not visuliazed in 2D widgets.  
Could I have any solution to get the mesh inside of the original image for the 4 slicer widgets?  
Thank you in advance.

 ![mesh](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/9/f/9f4680d581f1e6bb1bad8dd44ac8fc30ae706658.png)

---

<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:** [October 22, 2018, 7:19pm UTC](https://discourse.slicer.org/t/load-dicom-image-and-a-mesh/4468/7 "2018-10-22T19:19:28Z")

</div>

> [@Abdelkhalek](#):
>
> Could I have any solution to get the mesh inside of the original image for the 4 slicer widgets?

Yes. Enable slice intersections in the model node’s [display node](http://apidocs.slicer.org/master/classvtkMRMLDisplayNode.html#aa73e1a73218ddeb1cd40bfbae4e17e58) - see SetSliceIntersectionVisibility, SetSliceIntersectionOpacity, SetSliceIntersectionThickness.

Mulitple display modes are available: intersection and various projections - see details [here](http://apidocs.slicer.org/master/classvtkMRMLModelDisplayNode.html#a7bf7b91a4a4db25759bed9965d8c40e8).

---

<div class="post-metadata">

**Author:** ![Abdelkhalek](https://avatars.discourse-cdn.com/v4/letter/a/a6a055/32.png) [@Abdelkhalek](https://discourse.slicer.org/u/Abdelkhalek)\
**Post date:** [October 25, 2018, 8:25am UTC](https://discourse.slicer.org/t/load-dicom-image-and-a-mesh/4468/8 "2018-10-25T08:25:36Z")

</div>

Thank you Andras.  
For now I have two issues:  
1- The mesh needs a linear transformation in order to be in the perfect location (correct and incorrect example is attached).  
2- How can I use the slicer fill feature from the Segmentations module as a python code in order to transform the region into a contour?

Thank you in advance ![correct](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/c/a/ca5e9f7416bda89c246eaceea6b90264e6482cad.png)

---

<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:** [October 25, 2018, 2:02pm UTC](https://discourse.slicer.org/t/load-dicom-image-and-a-mesh/4468/9 "2018-10-25T14:02:54Z")

</div>

At a first glance that looks like an RAS/LPS issue - see this for background: [https://www.slicer.org/wiki/Coordinate\_systems](https://www.slicer.org/wiki/Coordinate_systems)

Adding a transform diag(-1,-1,1,1) would fix that.

---

<div class="post-metadata">

**Author:** ![Abdelkhalek](https://avatars.discourse-cdn.com/v4/letter/a/a6a055/32.png) [@Abdelkhalek](https://discourse.slicer.org/u/Abdelkhalek)\
**Post date:** [October 25, 2018, 6:17pm UTC](https://discourse.slicer.org/t/load-dicom-image-and-a-mesh/4468/10 "2018-10-25T18:17:07Z")

</div>

Thank you Steve. However, I could not have an access to the node in order to apply the example of the linear transformation below:

```
# Linear Transformation
transformNode=slicer.util.getNode('LinearTransform_3')
referenceVolumeNode=slicer.util.getNode('MRHead')
slicer.modules.transforms.logic().CreateDisplacementVolumeFromTransform(transformNode, referenceVolumeNode, True)

```

My actual script looks like:

```
button1 = qt.QPushButton("Load Segmentation")
self.formFrame1.layout().addWidget(button1)
button1.connect("clicked()", slicer.util.openAddSegmentationDialog)
button1.show()

```

Could you please assist me in this matter?  
Thank you 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:** [October 27, 2018, 5:24pm UTC](https://discourse.slicer.org/t/load-dicom-image-and-a-mesh/4468/11 "2018-10-27T17:24:45Z")

</div>

Yes, you’ll need to get the model node, maybe by using a node combo box.

Then you can add a linear transform, like the last block of code in this example:

[https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Add\_a\_texture\_mapped\_plane\_to\_the\_scene\_as\_a\_model](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Add_a_texture_mapped_plane_to_the_scene_as_a_model)

Except that you need to use the diag(-1,-1, 1,1). Probably you want to do it manually with the GUI and then map that onto the script.

---

<div class="post-metadata">

**Author:** ![Abdelkhalek](https://avatars.discourse-cdn.com/v4/letter/a/a6a055/32.png) [@Abdelkhalek](https://discourse.slicer.org/u/Abdelkhalek)\
**Post date:** [October 27, 2018, 8:28pm UTC](https://discourse.slicer.org/t/load-dicom-image-and-a-mesh/4468/12 "2018-10-27T20:28:37Z")

</div>

Thank you Steve. I solved it finally 🙂
