# Overlay model with original image in slicer

**URL:** <https://discourse.slicer.org/t/overlay-model-with-original-image-in-slicer/33305>\
**Category:** Support\
**Tags:** vtk, python\
**Created:** [December 8, 2023, 9:02pm UTC](https://discourse.slicer.org/t/overlay-model-with-original-image-in-slicer/33305 "2023-12-08T21:02:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![G\_Tom](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/g_tom/32/68257_2.png) [@G\_Tom](https://discourse.slicer.org/u/G_Tom)\
**Post date:** [December 8, 2023, 9:02pm UTC](https://discourse.slicer.org/t/overlay-model-with-original-image-in-slicer/33305/1 "2023-12-08T21:02:26Z")

</div>

Hi, I have the following MWE code in slicer. I want to show the 3D grid of the image along with the image. I use the vtkExtractEdges and pass that to a vtkTubeFilter.  
When I visualize the result, the extracted grid is much smaller than the original image? What gives ? Please see image below.

Thanks

```auto
import os

nodeName = "MyNewVolume"
imageSize = [4, 6, 4]
voxelType=vtk.VTK_UNSIGNED_CHAR
imageOrigin = [0.0, 0.0, 0.0]
imageSpacing = [13.0, 13.0, 13.0]
imageDirections = [[1,0,0], [0,1,0], [0,0,1]]
fillVoxelValue = 500

# Create an empty image volume, filled with fillVoxelValue
imageData = vtk.vtkImageData()
imageData.SetDimensions(imageSize)
imageData.AllocateScalars(voxelType, 1)
imageData.GetPointData().GetScalars().Fill(fillVoxelValue)

extractEdges = vtk.vtkExtractEdges()
extractEdges.SetInputData(imageData) 

# Tube the edges
tubes = vtk.vtkTubeFilter()
tubes.SetInputConnection(extractEdges.GetOutputPort())
tubes.SetRadius(0.008)
tubes.SetNumberOfSides(32)

model = slicer.modules.models.logic().AddModel(tubes.GetOutputPort())

# Create volume node
volumeNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLScalarVolumeNode", nodeName)
volumeNode.SetOrigin(imageOrigin)
volumeNode.SetSpacing(imageSpacing)
volumeNode.SetIJKToRASDirections(imageDirections)
volumeNode.SetAndObserveImageData(imageData)
volumeNode.CreateDefaultDisplayNodes()
volumeNode.CreateDefaultStorageNode()

```

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/3/5/356dd9288804d522e1be01f22d625991a79eff7d.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:** [December 8, 2023, 9:45pm UTC](https://discourse.slicer.org/t/overlay-model-with-original-image-in-slicer/33305/2 "2023-12-08T21:45:08Z")

</div>

Looks like you need to apply the spacing to expand the result of the tube filter. For example, you could get the IJKToRAS matrix from the volumeNode and use it for a linear transform you could apply to the model node.

---

<div class="post-metadata">

**Author:** ![G\_Tom](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/g_tom/32/68257_2.png) [@G\_Tom](https://discourse.slicer.org/u/G_Tom)\
**Post date:** [December 13, 2023, 5:46pm UTC](https://discourse.slicer.org/t/overlay-model-with-original-image-in-slicer/33305/3 "2023-12-13T17:46:08Z")

</div>

HI Pieper,  
thanks for your answer. I just wondered why the scaling is not applied automatically. Is my VTK code having an issue ?

Thanks

GT

---

<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:** [December 13, 2023, 10:11pm UTC](https://discourse.slicer.org/t/overlay-model-with-original-image-in-slicer/33305/4 "2023-12-13T22:11:44Z")

</div>

Yes, you need to take the image spacing into account when to map your tube from local space to world (patient) space. The edges are being extracted from the image data which doesn’t include the ijkToRAS information which is only added at the MRML level.
