# How to create an image slice and add observer

**URL:** https://discourse.slicer.org/t/how-to-create-an-image-slice-and-add-observer/30745
**Category:** Support
**Tags:** python
**Created:** [July 23, 2023, 10:35am UTC](https://discourse.slicer.org/t/how-to-create-an-image-slice-and-add-observer/30745 "2023-07-23T10:35:22Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![slicer365](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/slicer365/32/67549_2.png) [@slicer365](https://discourse.slicer.org/u/slicer365)
#### Post date: [July 23, 2023, 10:35am UTC](https://discourse.slicer.org/t/how-to-create-an-image-slice-and-add-observer/30745/1 "2023-07-23T10:35:22Z")

</div>

This is a volume slice created by other module, and I can display different slices by dragging the position.  
This is not a plane, but a volume node with only one sheet

so that I can transfer the sigle volume to other programs in real time through IGT.

I would like to ask if there are other ways or script to create this kind of slice, and I can specify the length and width of the slice

[https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/7/4/744d4a847e7ace550fc79b981ccbe06f7039065e.mp4](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/7/4/744d4a847e7ace550fc79b981ccbe06f7039065e.mp4)

---

<div class="post-metadata">

### Author: ![ungi](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ungi/32/78573_2.png) [@ungi](https://discourse.slicer.org/u/ungi)
#### Post date: [July 24, 2023, 2:26am UTC](https://discourse.slicer.org/t/how-to-create-an-image-slice-and-add-observer/30745/2 "2023-07-24T02:26:04Z")

</div>

Hi, maybe this script helps: [Volume reslice with tracking · GitHub](https://gist.github.com/ungi/80442ac03b5ea9ed6ae680a800837ece)  
You need to install the SlicerIGT extension for the Volume Reslice Driver module to use this script.  
It uses a transform node (SliceToRas). Every time that transform changes, it updates the 2D images that is resliced from the 3D volume.

---

<div class="post-metadata">

### Author: ![slicer365](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/slicer365/32/67549_2.png) [@slicer365](https://discourse.slicer.org/u/slicer365)
#### Post date: [July 24, 2023, 10:56am UTC](https://discourse.slicer.org/t/how-to-create-an-image-slice-and-add-observer/30745/3 "2023-07-24T10:56:10Z")

</div>

Yes,this is what I want ,thank you very much,Mr Ungi ！

---

<div class="post-metadata">

### Author: ![derekcbr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/derekcbr/32/18793_2.png) [@derekcbr](https://discourse.slicer.org/u/derekcbr)
#### Post date: [November 22, 2023, 1:24pm UTC](https://discourse.slicer.org/t/how-to-create-an-image-slice-and-add-observer/30745/4 "2023-11-22T13:24:30Z")

</div>

Hi ungi,  
Great script so far. Is it possible to control three slices red, green and yellow together in the meantime by using the same matrix?  
sliceToRasTransform = vtk.vtkTransform()  
sliceToRasTransform.SetMatrix(my\_matrix)  
Thanks a lot!

---

<div class="post-metadata">

### Author: ![ungi](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ungi/32/78573_2.png) [@ungi](https://discourse.slicer.org/u/ungi)
#### Post date: [November 22, 2023, 1:33pm UTC](https://discourse.slicer.org/t/how-to-create-an-image-slice-and-add-observer/30745/5 "2023-11-22T13:33:33Z")

</div>

If you duplicate the code under “Show and follow output image in red slice view” and replace the word “red” with “green”, then the green slice will do the same as the red. And you may repeat this with the yellow slice too. But I’m not sure I understand what you want to achieve. Why would you show the same slice three times? They are duplicates in the 2D views, and they overlap and hide each other in the 3D view.

---

<div class="post-metadata">

### Author: ![derekcbr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/derekcbr/32/18793_2.png) [@derekcbr](https://discourse.slicer.org/u/derekcbr)
#### Post date: [November 22, 2023, 1:40pm UTC](https://discourse.slicer.org/t/how-to-create-an-image-slice-and-add-observer/30745/6 "2023-11-22T13:40:37Z")

</div>

Thank you for the quick answer. If I pass the same matrix to sliceToRasTransform for green, red and yellow slice.  
sliceToRasNode = slicer.mrmlScene.GetFirstNodeByName(“SliceToRas\_%s” % strcolor)  
outputNode = slicer.mrmlScene.GetFirstNodeByName(“OutputVolume\_%s” % strcolor)  
They will generate the same image? Or different image slice?  
Thanks a lot!

---

<div class="post-metadata">

### Author: ![ungi](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ungi/32/78573_2.png) [@ungi](https://discourse.slicer.org/u/ungi)
#### Post date: [November 22, 2023, 2:54pm UTC](https://discourse.slicer.org/t/how-to-create-an-image-slice-and-add-observer/30745/7 "2023-11-22T14:54:20Z")

</div>

The same transform matrix will generate the same image. If you want the same image in all views, then you don’t need to change the code related to reslice, because you can reuse the same image in all the views. Just duplicate lines 87-93 and change “red” to “green”.

---

<div class="post-metadata">

### Author: ![derekcbr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/derekcbr/32/18793_2.png) [@derekcbr](https://discourse.slicer.org/u/derekcbr)
#### Post date: [November 23, 2023, 1:05am UTC](https://discourse.slicer.org/t/how-to-create-an-image-slice-and-add-observer/30745/8 "2023-11-23T01:05:47Z")

</div>

Hi Ungi,  
Thanks a lot for the help! Will try again.

---

<div class="post-metadata">

### Author: ![derekcbr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/derekcbr/32/18793_2.png) [@derekcbr](https://discourse.slicer.org/u/derekcbr)
#### Post date: [November 25, 2023, 12:19am UTC](https://discourse.slicer.org/t/how-to-create-an-image-slice-and-add-observer/30745/9 "2023-11-25T00:19:47Z")

</div>

Hi Tamas,  
I found that by using your script can achieve the same functions by SetSliceToRASByNTP. One question is that in your script it does not use SetSliceToRASByNTP to position the slice. Is there any difference between your script and uusing SetSliceToRASByNTP? Thanks a lot!

---

<div class="post-metadata">

### Author: ![ungi](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ungi/32/78573_2.png) [@ungi](https://discourse.slicer.org/u/ungi)
#### Post date: [November 25, 2023, 12:42am UTC](https://discourse.slicer.org/t/how-to-create-an-image-slice-and-add-observer/30745/10 "2023-11-25T00:42:13Z")

</div>

It should be possible to achieve the same function using SetSliceToRASByNTP too. I just use Volume Reslice Driver for moving the slice, because that is more common in IGT applications. It has a user interface so we don’t always need to code.

---

<div class="post-metadata">

### Author: ![derekcbr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/derekcbr/32/18793_2.png) [@derekcbr](https://discourse.slicer.org/u/derekcbr)
#### Post date: [November 25, 2023, 12:54am UTC](https://discourse.slicer.org/t/how-to-create-an-image-slice-and-add-observer/30745/11 "2023-11-25T00:54:44Z")

</div>

Hi Tamas,  
Ok that is good to know. Also after running the script, one of the slice edge point is on the center of wireframe box. Is it possible to overlap the center with the wireframe box? Thanks a lot!

---

<div class="post-metadata">

### Author: ![ungi](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ungi/32/78573_2.png) [@ungi](https://discourse.slicer.org/u/ungi)
#### Post date: [November 25, 2023, 12:58am UTC](https://discourse.slicer.org/t/how-to-create-an-image-slice-and-add-observer/30745/12 "2023-11-25T00:58:39Z")

</div>

Do you mean the yellow coordinate system model? I don’t see a wireframe box after running the script. If you mean the yellow model, then you can add a transform node in the transform hierarchy tree (Data module) between the model and the SliceToRAS transform to move the model relative to the slice.

---

<div class="post-metadata">

### Author: ![derekcbr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/derekcbr/32/18793_2.png) [@derekcbr](https://discourse.slicer.org/u/derekcbr)
#### Post date: [November 25, 2023, 1:10am UTC](https://discourse.slicer.org/t/how-to-create-an-image-slice-and-add-observer/30745/13 "2023-11-25T01:10:48Z")

</div>

Hi Tamas,  
Thank you for the clarification. That is what I want.

---

<div class="post-metadata">

### Author: ![derekcbr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/derekcbr/32/18793_2.png) [@derekcbr](https://discourse.slicer.org/u/derekcbr)
#### Post date: [November 29, 2023, 6:23am UTC](https://discourse.slicer.org/t/how-to-create-an-image-slice-and-add-observer/30745/14 "2023-11-29T06:23:16Z")

</div>

Hi Tamas,  
Another question is when I rotate the red, green and yellow slice, is it possible to rotate by the image slice’s center point? Now it seems that it is rotated by the points on the edges. Thanks.

---

<div class="post-metadata">

### Author: ![ungi](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ungi/32/78573_2.png) [@ungi](https://discourse.slicer.org/u/ungi)
#### Post date: [November 29, 2023, 1:54pm UTC](https://discourse.slicer.org/t/how-to-create-an-image-slice-and-add-observer/30745/15 "2023-11-29T13:54:30Z")

</div>

Last time I checked, this was not directly possible. See comment near the beginning of the script. But of course you may create additional transforms in the scene and set up the transform hierarchy to achieve the same effect. Similar to how the yellow model can be translated to the center of the slice.

---

<div class="post-metadata">

### Author: ![derekcbr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/derekcbr/32/18793_2.png) [@derekcbr](https://discourse.slicer.org/u/derekcbr)
#### Post date: [November 30, 2023, 4:56am UTC](https://discourse.slicer.org/t/how-to-create-an-image-slice-and-add-observer/30745/16 "2023-11-30T04:56:53Z")

</div>

Thanks a lot for help!
