Home Ask Login Register

Developers Planet

Your answer is one click away!

linzhang.robot February 2016

memcpy vtkPoints using GetVoidPointer method

I have a opencv 3-channel Mat which each pixel contains a 3D point. Now I want to copy this Mat (points) to VTK points and then polydata to visualize it. There is an example here showing this but I want to copy the cv::Mat to vtkPoints directly using memcpy.

I did:

// Create colored point clodu in opencv format
cv::Mat cld_cv = cv::Mat::zeros(1, 3, CV_32FC3);
int num_points = cld_cv.cols * cld_cv.rows;

// Fill cld_cv code here
// .....

vtkSmartPointer<vtkPoints> points =
::memcpy(points->GetVoidPointer(0), cld_cv.data, sizeof(float) * num_points * 3);

vtkSmartPointer<vtkPolyData> pointsPolydata =


// Normal VTK pipeline as shown in the example

However I can only see 1 point which seems to be at origin. Please point out where I have done wrong.


mirni February 2016

What version of OpenCV are you using? I don't see any cv::Mat::data in the currents docs.

Also, there is no guarantee that vtkPoints or cv::Mat are stored in a contiguous block of memory. Looking at the VTK 6.2 source, the data inside vtkPoints is stored as a FloatArray by default, which has contiguous storage, so you should be ok there. OpenCV provides a method to check. Try

if (cld_cv.isContinuous()) 
   ::memcpy(points->GetVoidPointer(0), cld_cv.ptr<float>(), sizeof(float) * num_points * 3);
else  {
    std::cerr << "cv Mat not contiguous" << std::endl; 
    // other, perhaps not as fast, method of copy

See the OpenCV docs for isContinous for details.

Post Status

Asked in February 2016
Viewed 3,046 times
Voted 6
Answered 1 times


Leave an answer

Quote of the day: live life