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_8UC3, image->GetScalarPointer()); // Unsigned int, 3 channels
cvtColor(openCVImage, openCVImage, CV_BGRA2GRAY);
// Flip because of different origins between vtk and OpenCV
cv::flip(openCVImage,openCVImage, 0);
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.
网上搜了一下,用vtk得到的图像传给opencv使用,资料里说明,vtk的图像可能是RGBA的4通道,所以用转换一下成灰度图像。并且两个库得到图像的坐标也不一样,所以要对图片进行翻转操作。
再来对vtk图像进行了解,vtkChangeImageInformation可以作为管线中的一个filter来修改图像信息。利用这个filter可以修改图像的原点,像素间隔以及范围起点(extent),另外还可以对图像平移缩放等操作。【2】
参考资料:
【1】
http://vtk.1045678.n5.nabble.com/From-vtkImageData-to-Iplimage-OpenCV-td5716020.html
【2】http://blog.youkuaiyun.com/shenziheng1/article/details/54646862