Ask Your Question

How to pass an image (subscribed from a image publishing topic) using service?

asked 2017-08-14 18:39:52 -0600

the_notorious_kid gravatar image

updated 2017-08-15 10:49:41 -0600

Hello People, I am trying to get image from a camera fitted on a robot and pass them using services. I will use a client to grab images from the server and later process it according to the need. It would be great if some can give me a sample template code to follow to achieve the same. But I have been receiving the following error -

error: ‘const struct sensor_msgs::Image_<std::allocator<void> >’ has no member named ‘toImageMsg’
   res.img = image_msg->toImageMsg;
/home/dark_knight/ros_prac/catkin_ws_baxter2.1/src/robot_vision/src/vision_node.cpp: In function ‘void imageCallback(const ImageConstPtr&)’:
/home/dark_knight/ros_prac/catkin_ws_baxter2.1/src/robot_vision/src/vision_node.cpp:37:67: error: no matching function for call to ‘cv_bridge::CvImage::CvImage(std_msgs::Header, const char [5], const ImageConstPtr&)’
     image_msg = cv_bridge::CvImage(std_msgs::Header(), "bgr8", msg).toImageMsg();

Below is my code trying to do the same, I have been trying various things -

#include <ros/ros.h>
#include <image_transport/image_transport.h>
#include <opencv2/highgui/highgui.hpp>
#include <cv_bridge/cv_bridge.h>
#include <sensor_msgs/image_encodings.h>
#include "sensor_msgs/Image.h"
#include <opencv2/imgproc/imgproc.hpp>
#include "robot_vision/request_image.h"

ros::ServiceServer service;
image_transport::Subscriber sub;

sensor_msgs::ImageConstPtr image_msg;

bool get_image(robot_vision::request_image::Request  &req, robot_vision::request_image::Response &res)
    if(req.request_message == true)
        ROS_INFO("sending back response");
        res.img = *image_msg;
        ROS_INFO("No request received");
    return true;
void imageCallback(const sensor_msgs::ImageConstPtr& msg)
    image_msg = msg;
  catch (cv_bridge::Exception& e)
    ROS_ERROR("Image data was not received");

int main(int argc, char **argv)
  ros::init(argc, argv, "vision_node");
  ros::NodeHandle nh;
  image_transport::ImageTransport it(nh);
  sub = it.subscribe("/cameras/head_camera/image", 1, imageCallback);
  service = nh.advertiseService("image_server", get_image);

The above coded has been edited to work.

edit retag flag offensive close merge delete



I'm trying to do something very similar to what you did here, but instead of camera I'm using sonar.

Could you please post here\send me the .srv file and the client code that you used ?

Thanks :)

Paz gravatar image Paz  ( 2019-04-02 06:43:51 -0600 )edit

1 Answer

Sort by » oldest newest most voted

answered 2017-08-14 19:19:41 -0600

lucasw gravatar image

updated 2017-08-14 19:35:59 -0600

toImageMsg is a function, so if image_msg were a cv image it needs parenthesis.

res.img = image_msg->toImageMsg();

Except image_msg is already a sensor_msgs::Image, so doesn't need a conversion:

res.img = image_msg;

(or res.img = *image_msg?)

And likewise no conversion is necessary in the callback, just store the incoming message in the same type:

image_msg = msg;
edit flag offensive delete link more


Thanks again @lucasw. It worked.

the_notorious_kid gravatar image the_notorious_kid  ( 2017-08-14 22:49:27 -0600 )edit

Hello @lucasw. I still have a query it works using res.img = *image_msg, but that means I am passing the data stored at the address in image_msg, however image_msg itself has the image data.

Also, what does sensor_msgs::ImageConstPtr image_msg mean, is that the image_msg is a pointer variable?

the_notorious_kid gravatar image the_notorious_kid  ( 2017-08-17 18:14:14 -0600 )edit

It's actually a boost smart pointer, #q212857 has a few more details.

lucasw gravatar image lucasw  ( 2017-08-19 08:35:23 -0600 )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

1 follower


Asked: 2017-08-14 18:39:52 -0600

Seen: 941 times

Last updated: Aug 15 '17