# How to display only a specific part of a 3D image defined by a label (using volume raycast mapper)?

**URL:** https://discourse.vtk.org/t/how-to-display-only-a-specific-part-of-a-3d-image-defined-by-a-label-using-volume-raycast-mapper/13287
**Category:** Support
**Created:** [February 8, 2024, 10:44pm UTC](https://discourse.vtk.org/t/how-to-display-only-a-specific-part-of-a-3d-image-defined-by-a-label-using-volume-raycast-mapper/13287 "2024-02-08T22:44:28Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Anton\_Botnov](https://discourse.vtk.org/user_avatar/discourse.vtk.org/anton_botnov/32/7407_2.png) [@Anton\_Botnov](https://discourse.vtk.org/u/Anton_Botnov)
#### Post date: [February 8, 2024, 10:44pm UTC](https://discourse.vtk.org/t/how-to-display-only-a-specific-part-of-a-3d-image-defined-by-a-label-using-volume-raycast-mapper/13287/1 "2024-02-08T22:44:28Z")

</div>

Hi everyone!

I’m using vtkOpenGLGPUVolumeRayCastMapper for 3D image volume rendering. For this purpose, I have two 3D images: one is the original image and the other is a mask image. The mask image contains labels that describe corresponding segments. I set up both of them into an instance of vtkOpenGLGPUVolumeRayCastMapper.

The question is, how can I display only a part of the original image based on a specific label from the mask and hide all other parts of the image? I know that there is a method SetMaskTypeToBinary, and in this case, all voxels with label 0 will be hidden, but I need to show, for example, voxels with label 20 and hide all other voxels.

Thanks!

---

<div class="post-metadata">

### Author: ![rexthor](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rexthor/32/6481_2.png) [@rexthor](https://discourse.vtk.org/u/rexthor)
#### Post date: [February 9, 2024, 1:12pm UTC](https://discourse.vtk.org/t/how-to-display-only-a-specific-part-of-a-3d-image-defined-by-a-label-using-volume-raycast-mapper/13287/2 "2024-02-09T13:12:54Z")

</div>

Perhaps you could use the binary mask, on a scalar generated by [vtkProgrammableAttributeDataFilter](https://vtk.org/doc/nightly/html/classvtkProgrammableAttributeDataFilter.html#details), i.e., make a new array that is zero whenever your label is == some number of your choosing?

I looked around for some more elegant way, but there are a LOT of classes to look through.

Perhaps also [vtkThresholdFilter](https://vtk.org/doc/nightly/html/classvtkThreshold.html#details).You can select cells of input based on one of these conditions: 1) greater than some value, 2) less than some value, or what may interest you is 3) between some values … so if you wanted 20, you could say between 19 and 21?

Also perhaps: [vtkExtractSelection](https://vtk.org/doc/nightly/html/classvtkExtractSelection.html#details) … which uses [vtkSelection](https://vtk.org/doc/nightly/html/classvtkSelection.html#details).

---

<div class="post-metadata">

### Author: ![Anton\_Botnov](https://discourse.vtk.org/user_avatar/discourse.vtk.org/anton_botnov/32/7407_2.png) [@Anton\_Botnov](https://discourse.vtk.org/u/Anton_Botnov)
#### Post date: [February 9, 2024, 1:27pm UTC](https://discourse.vtk.org/t/how-to-display-only-a-specific-part-of-a-3d-image-defined-by-a-label-using-volume-raycast-mapper/13287/3 "2024-02-09T13:27:38Z")

</div>

Hi @rexthor, thank you for your assistance. I believe vtkThresholdFilter isn’t suitable in this scenario because the mask is set directly into vtkOpenGLGPUVolumeRayCastMapper via the function, not through the algorithm port. Here’s the function: void SetMaskInput(vtkImageData\* mask). I’m also considering creating separate 3D images for each label, but this would increase memory consumption.

---

<div class="post-metadata">

### Author: ![rexthor](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rexthor/32/6481_2.png) [@rexthor](https://discourse.vtk.org/u/rexthor)
#### Post date: [February 9, 2024, 1:34pm UTC](https://discourse.vtk.org/t/how-to-display-only-a-specific-part-of-a-3d-image-defined-by-a-label-using-volume-raycast-mapper/13287/4 "2024-02-09T13:34:00Z")

</div>

I never use volume mappers … but would the selection suggestion work?

---

<div class="post-metadata">

### Author: ![Anton\_Botnov](https://discourse.vtk.org/user_avatar/discourse.vtk.org/anton_botnov/32/7407_2.png) [@Anton\_Botnov](https://discourse.vtk.org/u/Anton_Botnov)
#### Post date: [February 9, 2024, 1:42pm UTC](https://discourse.vtk.org/t/how-to-display-only-a-specific-part-of-a-3d-image-defined-by-a-label-using-volume-raycast-mapper/13287/5 "2024-02-09T13:42:51Z")

</div>

I think selection work in the same way if I just create separate image volumes per each label. And it takes time on CPU, I would prefer to do it on fly in GPU. I even looked at the source code, and found out that modifying the shader might have helped, but I’m not sure, also I don’t have access to change shader outside of vtkOpenGLGPUVolumeRayCastMapper

---

<div class="post-metadata">

### Author: ![rexthor](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rexthor/32/6481_2.png) [@rexthor](https://discourse.vtk.org/u/rexthor)
#### Post date: [February 9, 2024, 1:44pm UTC](https://discourse.vtk.org/t/how-to-display-only-a-specific-part-of-a-3d-image-defined-by-a-label-using-volume-raycast-mapper/13287/6 "2024-02-09T13:44:09Z")

</div>

Is there some way to change the opacity of the color map to render transparent all of the stuff you don’t want to see?

---

<div class="post-metadata">

### Author: ![Anton\_Botnov](https://discourse.vtk.org/user_avatar/discourse.vtk.org/anton_botnov/32/7407_2.png) [@Anton\_Botnov](https://discourse.vtk.org/u/Anton_Botnov)
#### Post date: [February 9, 2024, 1:52pm UTC](https://discourse.vtk.org/t/how-to-display-only-a-specific-part-of-a-3d-image-defined-by-a-label-using-volume-raycast-mapper/13287/7 "2024-02-09T13:52:42Z")

</div>

This can be a solution by the way, there is a possibility to setup colors and opactiy per label and also change blending between mask and image. It seems like workaround

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [February 10, 2024, 7:08pm UTC](https://discourse.vtk.org/t/how-to-display-only-a-specific-part-of-a-3d-image-defined-by-a-label-using-volume-raycast-mapper/13287/8 "2024-02-10T19:08:40Z")

</div>

We experimented with basic binary labelmap based masking and the results were really bad. It can only be useful if the boundary of the masked region is already empty and you only need to use a single color.

Instead, we developed a new method in 3D Slicer that works very well. It dilates labels and masks the volume with smooth transition, and generates an RGBA volume as output that VTK can directly render using raycasting.

 ![image](https://discourse.vtk.org/uploads/default/original/2X/d/d3c689ad8ee685112f425c395b4c627abc0db6a3.jpeg)

Full implementation is available in 3D Slicer in “Colorize volume” module (in Sandbox extension) - see [full source code](https://github.com/PerkLab/SlicerSandbox/blob/master/ColorizeVolume/ColorizeVolume.py).

Some more images and discussion is available here:

- [VTK multivolume/cinematic volume rendering - Support - 3D Slicer Community](https://discourse.slicer.org/t/vtk-multivolume-cinematic-volume-rendering/31981)
- [New Colorize Volume module - Success stories - 3D Slicer Community](https://discourse.slicer.org/t/new-colorize-volume-module/32254)

The processing (except the labelmap dilation) is very simple, so it would not be too hard to port to work on GPU, but it is probably not worth the trouble since processing takes only a few seconds on the CPU, volume rendering pipeline is already very complex, and shaders are a bit moving target in VTK now.

---

<div class="post-metadata">

### Author: ![Anton\_Botnov](https://discourse.vtk.org/user_avatar/discourse.vtk.org/anton_botnov/32/7407_2.png) [@Anton\_Botnov](https://discourse.vtk.org/u/Anton_Botnov)
#### Post date: [February 11, 2024, 4:44pm UTC](https://discourse.vtk.org/t/how-to-display-only-a-specific-part-of-a-3d-image-defined-by-a-label-using-volume-raycast-mapper/13287/9 "2024-02-11T16:44:37Z")

</div>

Hi @lassoan, thanks for information. Is there implementation for C++ or it is available only for python?

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [February 11, 2024, 5:12pm UTC](https://discourse.vtk.org/t/how-to-display-only-a-specific-part-of-a-3d-image-defined-by-a-label-using-volume-raycast-mapper/13287/10 "2024-02-11T17:12:41Z")

</div>

C++ implementation is exactly the same as Python. If you don’t want to translate the code yourself then you can probably use any of that AI chatbots.
