1. 程式人生 > >vtk中的圖片傳給opencv的mat操作

vtk中的圖片傳給opencv的mat操作

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】