[vtkusers] Convert OpenCV Mat to vtkImageData
Cory Quammen
cory.quammen at kitware.com
Wed Apr 13 09:58:36 EDT 2016
Hi Ronald,
That looks reasonable. Perhaps the problem is in the image display code.
Maybe you could save the vtkImageData to a .vti file with
vtkXMLImageDataWriter and view it in ParaView to see if the image
looks reasonable. If it does, then the problem lies in the display
code.
Thanks,
Cory
On Wed, Apr 13, 2016 at 6:54 AM, Ronald van Duren
<ronaldvanduren at hotmail.com> wrote:
> 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
>
>
>
> _______________________________________________
> 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:
> http://public.kitware.com/mailman/listinfo/vtkusers
>
--
Cory Quammen
R&D Engineer
Kitware, Inc.
More information about the vtkusers
mailing list