Selectively setting parts of image transperent for vtkImageActor in 3D scene

Hi All,

I am looking for some pointers. Please see the included illustration for my problem statement.

I have an image that is rendered as a vtkImageActor in a 3D scene. The image data [0-255] is inside a know geometry and surounded by 0 value pixels. As you’d imaging when being rendered, the image is rendered as a rectangle (WxH). Id like to only render the image pixels that are inside the geometry rather the entire rectangular image. How do I go about it? I tried a couple of approches (albeit quick and dirty) and didnt have much luck.

Any ideas and approaches to try?

Thanks!

Are you grabbing the image from the render? If so, you could set the background to be green, for instance, and set all of the completely green pixels to transparent. The “right way” to do this will depend on how you are rendering the scene.

Is your image a billboard that always faces the user? vtkLogoWidget, vtkLogoRepresentation might help.

Is it some flat surface that needs to have the image on it? You might be able to set the image as a texture on the surface.

Yes, vtkActor with a vtkTexture is 100% the way to go. You can use vtkTextureMapToPlane to generate texture coords for any flat polydata.

@rexthor my image comes in from a buffer via vtkImageImport, so the data/texture is pre-render. It is also not a billboard and has a 3D pose that needs to be rendered in a 3D scene. I will retry the texture mapping method as you and @dgobbi suggested and report back. This was also my intuition but I didnt have too much success in the limited attempts I made. I might have follow up questions.

Here is an example from the VTK Examples site:
https://examples.vtk.org/site/Cxx/Visualization/ClipArt/

The above is in C++, here is an example in Python:

import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkRenderingCore import (
     vtkActor,
     vtkPolyDataMapper,
     vtkRenderer,
     vtkRenderWindow,
     vtkRenderWindowInteractor,
     vtkTexture)
from vtkmodules.vtkInteractionStyle import vtkInteractorStyleTrackballCamera
from vtkmodules.vtkIOImage import vtkPNGReader
from vtkmodules.vtkFiltersSources import vtkPlaneSource
from vtkmodules.vtkFiltersTexture import vtkTextureMapToPlane

r = vtkPNGReader()
r.SetFileName("vtk_logo.png")
tx = vtkTexture()
tx.InterpolateOn()
tx.SetInputConnection(r.GetOutputPort())

ps = vtkPlaneSource()
ps.SetResolution(1, 1)
tm = vtkTextureMapToPlane()
tm.SetInputConnection(ps.GetOutputPort())

mapper = vtkPolyDataMapper()
mapper.SetInputConnection(tm.GetOutputPort())
actor = vtkActor()
actor.SetMapper(mapper)
actor.SetTexture(tx)

ren = vtkRenderer()
ren_win = vtkRenderWindow()
ren_win.SetWindowName('Sprite')
ren_win.SetSize(600, 600)
ren_win.AddRenderer(ren)
iren = vtkRenderWindowInteractor()
iren.SetRenderWindow(ren_win)
style = vtkInteractorStyleTrackballCamera()
iren.SetInteractorStyle(style)

ren.AddActor(actor)
ren.ResetCamera()

ren_win.Render()
iren.Start()