Ask Your Question

how to convert sensor_msgs::PointCloud2 to cv::Mat

asked 2017-12-20 02:40:00 -0500

YangLei1993 gravatar image

I subscribed the ros message which type is sensor_msgs::PointCloud2, and I want to convert it to cv::Mat.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2017-12-20 05:33:49 -0500

Okay, your question makes sense now you've said

I want to obtain the grid map by converting this ros_msg to mat

This is fairly straightforward but you'll need to define a couple of things first, the cell size of your gridmap needs to be set, using this and the extents of your point-cloud you can work out the size of the cv::Mat you need to create to store the map.

If you create this Mat with all pixels set to empy, then you just need to loop through each point in the cloud and which 'pixel' of the map it falls into then set that pixel to occupied.

It's worth noting that you'll need to record the location of the Mat's origin within the coordinate frame that contains the point-cloud otherwise the position of the whole map will be unknown.

edit flag offensive delete link more


Thank you very much, I ignored the problem of the coordinate frame.

YangLei1993 gravatar image YangLei1993  ( 2017-12-21 06:32:25 -0500 )edit

Glad you got it sorted, if this helped you solve the problem can you accept this answer. Thanks.

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2017-12-29 11:12:52 -0500 )edit

I'm sorry, I am a novice, I now know I need to mark this answer as correct, thank you again!

YangLei1993 gravatar image YangLei1993  ( 2018-01-03 07:16:27 -0500 )edit

No problem, we all start somewhere!

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2018-01-03 10:00:32 -0500 )edit

answered 2017-12-20 03:35:59 -0500

My way is to create the cv::mat which can contain 3 channel (Like RGB - but it will store XYZ) make a loop through width and height and store subscribed data onto it :)

edit flag offensive delete link more


Thank you firstly! But the cloud is unordered, height is 1 and width is the length of the point cloud. I want to obtain the grid map by converting this ros_msg to mat.

YangLei1993 gravatar image YangLei1993  ( 2017-12-20 04:51:12 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2017-12-20 02:40:00 -0500

Seen: 636 times

Last updated: Dec 20 '17