[vtkusers] Image from OpenCV as texture
Andreas Pedroni
anpedroni at gmail.com
Sun Oct 28 07:33:10 EDT 2018
Dear list
My final goal is to render a movie (read by the openCV method VideoCapture) on a plane in 3D space (using VTK).
As a first step I tried to map a single RGB-frame (a numpy nd.array (1020, 720,3) ) as texture on a plane (see the code snippet below).
However this does not work. What I don’t get is how to create a 2D color image to map to the texture. Now it is recognized as a 3D texture map and returns the error: "3D texture maps currently are not supported!“.
As I am very new to VTK and do not know anything about C++, I find it difficult to orient myself in VTK through the C++ examples.
So, any hints would be greatly appreciated!
Best
Andreas
# Python 3.6, VTK 8.1.1, OpenCV 3.4.2)
# Create a video capture object to read videos
cap = cv2.VideoCapture(VIDEOPATH)
# Read first frame
success, frame = cap.read()
frame_shape = frame.shape
vtk_array = numpy_support.numpy_to_vtk(frame.transpose(2, 0, 1).ravel(), deep=True, array_type=vtk.VTK_UNSIGNED_CHAR)
# Convert vtkFloatArray to vtkImageData
vtk_image_data = vtk.vtkImageData()
vtk_image_data.SetDimensions(frame.shape)
vtk_image_data.SetSpacing([1,1,1])
vtk_image_data.GetPointData().SetScalars(vtk_array)
vtk_image_data.SetOrigin(0,0,0)
# create the texture
atext = vtk.vtkTexture()
atext.SetInputData(vtk_image_data)
atext.InterpolateOn()
plane = vtk.vtkPlaneSource()
…
More information about the vtkusers
mailing list