[vtkusers] Convert OpenCV Mat to vtkImageData
Ronald van Duren
ronaldvanduren at hotmail.com
Wed Apr 13 06:54:56 EDT 2016
Dear VTK Community,
I'm trying to run several computer vision algorithms on a serie of video frames. But because these algorithms result in some very strange output I figured there might be something wrong with my video frames. So to check if the frames are correct I want to visualize them with VTK. VTK doesn't support the Mat type so I convert the Mat(the video frames) to vtkImageData, but vtkImageData is just shown as a white rectangle.
I get the video frames by converting infrared frames to an openCV Mat type like this.
openni::Grayscale16Pixel* imgBuf = (openni::Grayscale16Pixel*)irFrame.getData();
int w = irFrame.getWidth();
int h = irFrame.getHeight();
cv::Mat frameOne(h, w, CV_16U, imgBuf);
frameOne.convertTo(frameOne, CV_8U);
I then try to convert the Mat to vtkImageData like this.
vtkSmartPointer<vtkImageData> convertMatToVtkImageData(const Mat &source)
{
vtkSmartPointer<vtkImageData> output = vtkSmartPointer<vtkImageData>::New();
vtkSmartPointer<vtkInformation> outInfo = vtkSmartPointer<vtkInformation>::New();
output->SetDimensions(source.cols, source.rows, 1);
output->AllocateScalars(VTK_UNSIGNED_CHAR, source.channels());
unsigned char* dptr = reinterpret_cast<unsigned char*>(output->GetScalarPointer());
size_t elem_step = output->GetIncrements()[1]/sizeof(unsigned char);
for (int y = 0; y < source.rows; ++y)
{
unsigned char* drow = dptr + elem_step * y;
const unsigned char *srow = source.ptr<unsigned char>(y);
for (int x = 0; x < source.cols; ++x)
drow[x] = *srow++;
}
output->Modified();
return output;
}
Is there something wrong with my code or am I trying to do something that is not possible?
Some extra info:
* I use Android and do most of the programming in C++ (NDK)
* I make use of the Structure Sensor's IR sensor to get the video frames
* I cannot use the vtkImageImporter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20160413/8f80bc65/attachment.html>
More information about the vtkusers
mailing list