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