ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Is there a way to convert LaserScan messages to Image message?

asked 2016-02-20 20:59:20 -0500

San gravatar image

Hello I am trying to convert laser scan data into images so that I can find "Lines". Following are the snippets from my code:

pcl::PointCloud<pcl::PointXYZRGB> cloudp;
sensor_msgs::Image image;


pcl::toROSMsg (cloudp, image); //convert the cloud

image_pub_.publish (image); //publish our cloud image

When I run it, "Failed to find match for field 'rgb' " is the error I get. When I use rviz, all I see is an Image which is full of black color. My laser gives is 2D data (i.e, z in point cloud is 0) and I need a 2D image so that I can apply CV algorithms on the image.

Thank you.

edit retag flag offensive close merge delete


Hello, were you able to get the image? I also am working on a similar issue. I am trying to find simple 2d shapes from laser scan data. I tried converting the point cloud to image. I also faced the same 'no rgb' issue. I converted the PointXYZ point cloud to PointXYZRGB type.

srik11 gravatar image srik11  ( 2016-05-10 21:20:25 -0500 )edit

I found that the default rgb value was 0 which is black. I tried manually setting a rgb field.But i get an image with horizontal line with varying width. Wanted to know if you made any progress.

srik11 gravatar image srik11  ( 2016-05-10 21:22:12 -0500 )edit

Hi srik11, I posted the answer, have a look.

San gravatar image San  ( 2016-05-11 13:39:12 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2016-05-11 13:38:20 -0500

San gravatar image

Hello srik11 and all, If all you need is to convert laser scan msgs into an image and apply some cv algorithms, you don't even need PCL in between. This is how I did hallway detection from the laser data: (Note: This is not the only way. It works and does the job)

If you realized, the laser output is already in polar form i.e., (r, theta) [distance of object in a particular direction]. All you need to do is take an empty image (by which I mean an image with all white pixels) and using (r, theta) calculate (x, y) which is (r cos theta, r sin theta). Round of (x, y) and in the image, change the corresponding (x, y) pixel from white to black. Repeat this for all the (r, theta) pair from the laser scan. Please also note that you might want to shift the origin (laser origin is at the center and the image origin should always be on the top left corner - atleast for openCV image format). formulas for origin shift: X = x - h, Y = y - k.

You might want to scale the image down to a reasonable size for you to process it especially if your laser scanner range is high.

I hope this helps. Happy coding!

edit flag offensive delete link more


Hey San, so I'm trying your approach and I think this could be the answer to this question. There's a tiny problem. The data you get from the sensor (r) is float. and the pixels (x,y) should be integers right? I'm a beginner in OpenCV and trying to find an answer to this quesiton.

Aabed Solayman gravatar image Aabed Solayman  ( 2020-04-16 13:51:15 -0500 )edit

Question Tools



Asked: 2016-02-20 20:59:20 -0500

Seen: 2,446 times

Last updated: May 11 '16