[vtkusers] Image from OpenCV as texture
Addison Elliott
addison.elliott at gmail.com
Sun Oct 28 14:30:51 EDT 2018
I came across a similar problem when I was working on a VTK application in
Python. There are few examples of how to do this (if any).
Take a look at this excerpt of code that I use to load in RGB data. In
short, the answer to your problem is to have the data is NxM where N is the
product of the size of the data (i.e. 10x10x10 => N = 1000) and M is the
number of color channels you have (1 for grayscale, 3 for RGB, 4 for RGBA).
# Use VTK support function to convert Numpy to VTK array
# The data is reshaped to one long 2D array where the first dimension is
the data and the second dimension is
# the color channels (1, 3 or 4 typically)
# Note: Fortran memory ordering is required to import the data correctly
# Type is not specified, default is to use type of Numpy array
vtkArray = numpy_support.numpy_to_vtk(self.data.reshape((-1,
self.channels), order='F'), deep=False)
# Create image, set parameters and import data
self.vtkImage = vtkImageData()
# For vtkImage, dimensions, spacing and origin is assumed to be 3D. VTK
images cannot be larger than 3D and if
# they are less than 3D, the remaining dimensions should be set to default
values. The function padRightMinimum
# adds default values to make the list 3D.
self.vtkImage.SetDimensions(padRightMinimum(self.size, 3, 1))
self.vtkImage.SetSpacing(padRightMinimum(self.spacing, 3, 1))
self.vtkImage.SetOrigin(padRightMinimum(self.origin, 3, 0))
# Import the data (vtkArray) into the vtkImage
self.vtkImage.GetPointData().SetScalars(vtkArray)
On Sun, Oct 28, 2018 at 6:33 AM Andreas Pedroni <anpedroni at gmail.com> wrote:
>
> 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()
> …
>
>
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at:
http://www.vtk.org/Wiki/VTK_FAQ
>
> Search the list archives at: http://markmail.org/search/?q=vtkusers
>
> Follow this link to subscribe/unsubscribe:
> https://public.kitware.com/mailman/listinfo/vtkusers
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://public.kitware.com/pipermail/vtkusers/attachments/20181028/43681f96/attachment.html>
More information about the vtkusers
mailing list