Ask Your Question

how to separate channels of a topic of the type sensor_msgs/Image

asked 2015-02-09 03:17:57 -0500

z.bagheri63 gravatar image

Hi guys,

I'm new to ROS and struggling with understanding it so I would really appreciate your help. I have a node which publishes /ladybug_image with type sensor_msgs/Image but I need to extract the green channel of this topic. I looked at the ros documentation about sensor_msgs/Image. To me it sounds the uint8[] data contains the image data but I don't get how can I refer to each channel explicitly. Could you please help me how can I do that?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2015-02-09 07:59:57 -0500

Robot gravatar image

updated 2015-02-09 08:36:43 -0500


the uint8[] data is the raw image data. inorder to have individual channel you need to know the image data is structured or unstructured. it means the it is RGB[pix1] RGB[pix2]....... or R[pix1] R[pix2].........G[pix1] G[pix2].........B[pix1] B[pix2].......... once u know which type it is using a incremental loop (for loop) you can extract whatever you want. i can give you one eg i used for reconstruction of image in the subscriber node.

for(int i=(w-1);i>=0;i--)
    for(int j=(h-1);j>=0;j--)
        *(image(i,j)) = *(&img->data[count]);
        *(image(i,j)+1) = *(&img->data[count]);
        *(image(i,j)+2) = *(&img->data[count]);

here img is sensor_msgs::Image. please tell me if its not clear.

edit flag offensive delete link more

answered 2015-02-09 08:35:15 -0500

You could use cv_bridge to convert the incoming sensor_msgs/Image into an OpenCV datatype. This allows you to use OpenCV functions to perform operations such as accessing channels. As an additional note, you can look at the string contained in the encoding field of the sensor_msgs/Image to determine what format the image is expressed in.

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

1 follower


Asked: 2015-02-09 03:17:57 -0500

Seen: 302 times

Last updated: Feb 09 '15