[Python] Project PNG image data as texture on plane in vtk

Discussão em 'Python' iniciado por Stack, Setembro 24, 2024.

  1. Stack

    Stack Membro Participativo

    I have been trying to visualize in Python an image as a partially translucent texture on a plane in VTK.

    My approach is to use vtkThresholdTextureCoords() to set the transparency based on the image values. However, I am struggling to apply the threshold. The error message states that there are no scalar data to texture threshold.

    Thank you for any help!

    Here is the code snippet:

    def main():
    #fileName = get_program_parameters()
    fileName = 'ice.png'


    readerFactory = vtkImageReader2Factory()
    textureFile = readerFactory.CreateImageReader2(fileName)
    textureFile.SetFileName(fileName)
    textureFile.Update()


    texture = vtkTexture()
    texture.SetInputConnection(textureFile.GetOutputPort())
    texture.InterpolateOn()


    plane = vtkPlaneSource()
    plane.SetCenter(0.0,0.0,0.0)
    plane.SetNormal(0.0,1.0,0.0)

    texturePlane = vtk.vtkTextureMapToPlane()
    texturePlane.SetInputConnection(plane.GetOutputPort())



    thresh = vtkThresholdTextureCoords()
    thresh.SetInputConnection(texturePlane.GetOutputPort())
    thresh.ThresholdByUpper(0.1)




    planeMapper = vtkPolyDataMapper()
    planeMapper.SetInputConnection(thresh.GetOutputPort())
    #planeMapper.SetInputConnection(plane.GetOutputPort()) # This does work
    planeMapper.SetScalarRange(textureFile.GetOutput().GetScalarRange())

    planeActor = vtkActor()
    planeActor.SetMapper(planeMapper)
    planeActor.SetTexture(texture)




    # Create the RenderWindow, Renderer and Interactor.
    renderer = vtkRenderer()
    renWin = vtkRenderWindow()
    renWin.AddRenderer(renderer)
    iren = vtkRenderWindowInteractor()
    iren.SetRenderWindow(renWin)

    # Add the actors to the renderer, set the background and size.
    renderer.AddActor(planeActor)
    colors = vtkNamedColors()
    renderer.SetBackground(colors.GetColor3d('DarkSlateGray'))

    renWin.SetSize(640, 480)
    renWin.SetWindowName('TexturePlane')

    # render the image
    renWin.Render()

    renderer.ResetCamera()
    renderer.GetActiveCamera().Elevation(-30)
    renderer.GetActiveCamera().Roll(-20)
    renderer.ResetCameraClippingRange()
    renWin.Render()
    iren.Start()




    return

    Continue reading...

Compartilhe esta Página