How to pass an image (subscribed from a image publishing topic) using service?
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;
}
else
{
ROS_INFO("No request received");
}
ros::spinOnce();
ros::Duration(0.2).sleep();
return true;
}
void imageCallback(const sensor_msgs::ImageConstPtr& msg)
{
try
{
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;
//cv::namedWindow("view");
//cv::startWindowThread();
image_transport::ImageTransport it(nh);
sub = it.subscribe("/cameras/head_camera/image", 1, imageCallback);
service = nh.advertiseService("image_server", get_image);
ros::spin();
//cv::destroyWindow("view");
}
The above coded has been edited to work.
Hello,
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 :)
Hi Im trying also same with IMU. Can you please post the final code for client and server and the .srv file?