# Render time in Slicer vs in python code

**URL:** <https://discourse.vtk.org/t/render-time-in-slicer-vs-in-python-code/5639>\
**Category:** Development\
**Created:** [April 26, 2021, 4:13am UTC](https://discourse.vtk.org/t/render-time-in-slicer-vs-in-python-code/5639 "2021-04-26T04:13:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tzodge](https://discourse.vtk.org/user_avatar/discourse.vtk.org/tzodge/32/2917_2.png) [@tzodge](https://discourse.vtk.org/u/tzodge)\
**Post date:** [April 26, 2021, 4:13am UTC](https://discourse.vtk.org/t/render-time-in-slicer-vs-in-python-code/5639/1 "2021-04-26T04:13:25Z")

</div>

I am trying to generate DRR of a CT using python code. I am also using Slicer to get an idea of the approximate idea of the transfer function.

I observered that just ther rendering part of my code (only the line renWin.Render()) takes about 4 seconds while the Slicer rendering seems almost instantaneous.

Am I missing something over here?  
Please let me know any additional information that you need. I am not exactly sure what all is required to debug this.

Thanks,  
Tejas

[temp.py](https://discourse.vtk.org/uploads/short-url/A40BtUuRgsyQ8g7Q3hbBbdjpyeK.py) (2.7 KB)

---

<div class="post-metadata">

**Author:** ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)\
**Post date:** [April 26, 2021, 7:34pm UTC](https://discourse.vtk.org/t/render-time-in-slicer-vs-in-python-code/5639/2 "2021-04-26T19:34:57Z")

</div>

It could be the data type of the image data. Some DICOM readers convert CT data to `float`, which might slow down the GPU. I’m fairly certain that Slicer would use `short` or `ushort` for DICOM CT. If the data is `float`, then check the range to see if you can safely cast to `ushort`.

---

<div class="post-metadata">

**Author:** ![tzodge](https://discourse.vtk.org/user_avatar/discourse.vtk.org/tzodge/32/2917_2.png) [@tzodge](https://discourse.vtk.org/u/tzodge)\
**Post date:** [April 26, 2021, 9:13pm UTC](https://discourse.vtk.org/t/render-time-in-slicer-vs-in-python-code/5639/3 "2021-04-26T21:13:59Z")

</div>

Damn David! You rock! That worked like a charm! My rendering time went to 0.8 secs from 4.1 secs just by changing double to short.

Thanks a lot 🙂

---

<div class="post-metadata">

**Author:** ![estan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/estan/32/460_2.png) [@estan](https://discourse.vtk.org/u/estan)\
**Post date:** [May 8, 2021, 9:08am UTC](https://discourse.vtk.org/t/render-time-in-slicer-vs-in-python-code/5639/4 "2021-05-08T09:08:35Z")

</div>

Wow, I did not know that this could make such a difference. I would be interested in what your rendering pipeline looks like @tzodge. Are you using vtkGPUVolumeRayCastMapper?

The reason I’m asking is that we are doing rendering of float data in our application, but hearing this I’m wondering whether we should possibly convert to ushort prior to rendering (our data range would permit it).

---

<div class="post-metadata">

**Author:** ![tzodge](https://discourse.vtk.org/user_avatar/discourse.vtk.org/tzodge/32/2917_2.png) [@tzodge](https://discourse.vtk.org/u/tzodge)\
**Post date:** [May 8, 2021, 10:06am UTC](https://discourse.vtk.org/t/render-time-in-slicer-vs-in-python-code/5639/5 "2021-05-08T10:06:12Z")

</div>

Hey Elvis I won’t be able to share the entire codebase as it’s a funded project. But I can share the specific code snippet.

I am doing some processing in numpy and then converting the voccels back into vtkImageData. This is how I do it :). Hope it helps.

```
def imageData_from_voccel_numpy(imageData, voccel_np):
    depthArray = numpy_to_vtk(voccel_np.flatten(order='F'), deep=True, array_type=vtk.VTK_SHORT)
    imageData.GetPointData().SetScalars(depthArray)
    return imageData  

```

Also, I am using vtkSmartVolumeMapper. I found that for my case, changing volume mapper did not significanlty affect the renderring time.

---

<div class="post-metadata">

**Author:** ![estan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/estan/32/460_2.png) [@estan](https://discourse.vtk.org/u/estan)\
**Post date:** [May 8, 2021, 10:17am UTC](https://discourse.vtk.org/t/render-time-in-slicer-vs-in-python-code/5639/6 "2021-05-08T10:17:34Z")

</div>

Thanks @tzodge, if you’re using vtkSmartVolumeMapper that probably means it’ll use a vtkGPUVolumeRayCastMapper under the hood if a GPU is available.

I think I’ll do some experiments with converting to short in our application.
