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

How to get image from custom server using service?

asked 2017-08-21 12:38:59 -0600

the_notorious_kid gravatar image

updated 2017-08-21 17:40:35 -0600

Hello People,

I am trying to pass image from server to a client. The code complies when I use cv_bridge::toCvCopy, but it says core dumped when I actually call the image from the server. So I am currently trying to do the same thing using cv_bridge::toCvShare, but it throws following error -

error: no matching function for call to ‘toCvShare(robot_vision::request_imageResponse_<std::allocator<void> >::_img_type&, const char [5])’ cv::imshow("view", cv_bridge::toCvShare(srv.response.img, "bgr8")->image);

Inorder to check my server please check my previous question (I have updated the code with working one). My code for client is below -

#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"
#include <cstdlib>
#include <iostream>

namespace enc = sensor_msgs::image_encodings;

cv_bridge::CvImagePtr cv_ptr;

int main(int argc,char **argv)
{
    ros::init(argc,argv,"image_client_node");
    ros::NodeHandle nh;
    ros::ServiceClient client = nh.serviceClient<robot_vision::request_image>("image_server");
    robot_vision::request_image srv;
    srv.request.request_msg = true;
    if(client.call(srv))
    {
        ROS_INFO("Service initiated");
        try
        {
           //cv_ptr = cv_bridge::toCvCopy(srv.response.img, sensor_msgs::image_encodings::BGR8);
           cv::imshow("view", cv_bridge::toCvShare(srv.response.img, "bgr8")->image);
           cv::waitKey(30);
        }
        catch (cv_bridge::Exception& e)
        {
           ROS_ERROR("cv_bridge exception: %s", e.what());
           //return;
        }
    }
    else
    {
        ROS_ERROR("Failed to call service image_server");
        return 1;
    }
    ros::spinOnce();
    return 0;
}
edit retag flag offensive close merge delete

Comments

Please use the Preformat Text (101010 ) button to make your code readable. http://wiki.ros.org/Support#Do

jayess gravatar image jayess  ( 2017-08-21 13:03:25 -0600 )edit

Will do it, Thanks @jayess

the_notorious_kid gravatar image the_notorious_kid  ( 2017-08-21 13:33:54 -0600 )edit

You mention a previous question but I don't see a link to one...

jayess gravatar image jayess  ( 2017-08-21 17:37:08 -0600 )edit

Done @jayess

the_notorious_kid gravatar image the_notorious_kid  ( 2017-08-21 17:41:01 -0600 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2017-08-21 18:44:54 -0600

jayess gravatar image

updated 2017-08-21 19:00:28 -0600

According to this answer on Stack Overflow your function call is incorrect.

Here's what the accepted answer says

A shared_ptr to an object owning the sensor_msgs::Image and the other

A shared_ptr to a sensor_msgs::Image message You are providing neither one nor the other.

In your case, the first option should work, which expects the image as first parameter and the shared_ptr to the object owning the image (which is msg) as second parameter:

cv_bridge::toCvShare(msg->color, msg, "bgr8")

In short, try changing

cv::imshow("view", cv_bridge::toCvShare(srv.response.img, "bgr8")->image);

to

cv::imshow("view", cv_bridge::toCvShare(srv.response.img.color, srv.response, "bgr8"));

(Change srv.response.img.color and srv.response to whatever the equivalent is for your service message, since I don't know exactly how yours is formatted.)

edit flag offensive delete link more

Comments

@ jayees I don't have any msg variable in my client code. The link you shared has a image callback function, i.e. it's a subscriber node using customized message to get image data and msg is boost shared pointer in callback (which I don't have). I am passing the image using service.

the_notorious_kid gravatar image the_notorious_kid  ( 2017-08-21 18:58:47 -0600 )edit

I updated the answer to remove the msg variable. I think that how the data is being passed (services vs topics) is irrelevant. The main point is how you're calling the cv_bridge::toCvShare function is incorrect.

jayess gravatar image jayess  ( 2017-08-21 19:02:45 -0600 )edit

I tried with what you said and it throws following error -

error: base operand of ‘->’ has non-pointer type ‘robot_vision::request_imageResponse_<std::allocator<void> >::_img_type {aka sensor_msgs::Image_<std::allocator<void> >}’
the_notorious_kid gravatar image the_notorious_kid  ( 2017-08-22 14:26:14 -0600 )edit

cont....

error: base operand of ‘->’ has non-pointer type ‘robot_vision::request_imageResponse_<std::allocator<void> >::_img_type {aka sensor_msgs::Image_<std::allocator<void> >}’
            cv::imshow("view", cv_bridge::toCvShare(srv.response.img->color,srv.response,"bgr8"));
the_notorious_kid gravatar image the_notorious_kid  ( 2017-08-22 14:26:48 -0600 )edit

Well, it looks like you didn't do exactly what I wrote. It looks like you used ->color instead of .color.

jayess gravatar image jayess  ( 2017-08-22 14:32:33 -0600 )edit

this is how .color went -

error: ‘robot_vision::request_imageResponse_<std::allocator<void> >::_img_type’ has no member named ‘color’
                        cv::imshow("view", cv_bridge::toCvShare(srv.response.img.color,srv.response,"bgr8"))
the_notorious_kid gravatar image the_notorious_kid  ( 2017-08-22 15:10:00 -0600 )edit

I managed to solve the core problem by using cv_bridge::toCvCopy. @jayees Thanks for the help though.

the_notorious_kid gravatar image the_notorious_kid  ( 2017-08-22 17:11:34 -0600 )edit

Great. Post it as answer and you can accept it.

jayess gravatar image jayess  ( 2017-08-22 17:13:08 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-08-21 12:38:59 -0600

Seen: 1,574 times

Last updated: Aug 21 '17