Ask Your Question
4

pcl::PointCloud to cv::Mat -> optimal way?

asked 2012-07-22 22:43:39 -0600

Flamenco gravatar image

Hi everybody!

I would like to transform my point cloud 3D coordinates:

pcl::PointCloud<pcl::PointXYZ> point_cloud;

to an OpenCV matrix:

cv::Mat point_cloud_matrix;

I could define a 3 channel float matrix with one row per point in the point cloud, like this:

cv::Mat point_cloud_matrix(point_cloud.points.size(), 1, CV_32FC3);

and then manually copy each point on the point cloud to the matrix. But, it feels like a little bit brute force approach.

Can anyone suggest a more elegant way to do it? My ultimate goal is to apply OpenCV's k-means algorithm to extract clusters of points.

Thanks in advance!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
4

answered 2012-07-23 16:09:31 -0600

Vincent Rabaud gravatar image

if pcl::PointXYZ is just a struct with 3 float, just do a reinterpret_cast of the pointer to your data to a void* and then use one of the cv::Mat constructors using that.

The memory mapping between the pcl vector<pcl::pointxyz> is the same as storing 3 columns and your vector size rows.

edit flag offensive delete link more

Comments

1

Hi can anyone please explain this with example? I am bit new to C++ and OpenCV.

amitshri gravatar imageamitshri ( 2013-04-06 12:26:56 -0600 )edit

The struct is not only 3 floats.

waschbaer gravatar imagewaschbaer ( 2017-06-14 07:18:20 -0600 )edit

Question Tools

Stats

Asked: 2012-07-22 22:43:39 -0600

Seen: 3,932 times

Last updated: Jul 23 '12