[vtkusers] From vtkImageData to Iplimage (OpenCV)

Ignacio Avellino ignacioavellino at gmail.com
Wed Sep 12 10:18:23 EDT 2012


Hey all!

Solved, here comes the code:

....
vtkImageData* image = windowToImageFilter->GetOutput();
// Check number of components.
const int numComponents =  image->GetNumberOfScalarComponents(); // 3

// Construct the OpenCv Mat
int dims[3];
image->GetDimensions(dims);
cv::Mat openCVImage(dims[0], dims[1], CV_8UC4, image->GetScalarPointer());
// Unsigned int, 4 channels

cvtColor(fingerOpenCVImage, fingerOpenCVImage, CV_BGRA2GRAY);

cv::flip(fingerOpenCVImage,fingerOpenCVImage, 0);

// Flip because of different origins between vtk and OpenCV
cv::flip(openCVImage,openCVImage, 0);

...

NOTE: I think vtk is giving me the image in BGRA, so I perform a conversion
from BGRA to GRAY. Doing it to RGB did not give me the expected results, but
since I just need a black and white image I did not did deeper.
If you need to keep colors, just figure out if vtk is storing the image in
RGBA, BGRA or how and transform into RGB.

Hop it helps :)

Cheers!



--
View this message in context: http://vtk.1045678.n5.nabble.com/From-vtkImageData-to-Iplimage-OpenCV-tp5716020p5716031.html
Sent from the VTK - Users mailing list archive at Nabble.com.



More information about the vtkusers mailing list