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

Image transfer issue: cv_bridge

asked 2013-03-06 03:33:09 -0600

Median gravatar image

updated 2014-01-28 17:15:33 -0600

ngrennan gravatar image

Hey everyone.

I'm a complete newbie in ROS and am attempting to create a simple publishing/subscribing image platform. Some ROS tutorials regarding this issue use a deprecated protocol (CvBridge) so I'm stuck.

Here is what I have so far:

Image server (adapted from image_transfer tutorial (can't post links)):

int main(int argc, char** argv)
{
  ros::init(argc, argv, "image_publisher");


  ros::NodeHandle nh;
  image_transport::ImageTransport it(nh);

  cv_bridge::CvImagePtr cv_ptr(new cv_bridge::CvImage);

  cv_ptr->image = cv::imread("socio_32.jpg",CV_LOAD_IMAGE_COLOR);

  image_transport::Publisher pub = it.advertise("/topic_imgtransport", 1);


  sensor_msgs::ImagePtr msg = cv_ptr->toImageMsg();


  ros::Rate loop_rate(5);


  while (nh.ok()) 
  {
    pub.publish(msg);
    ros::spinOnce();
    loop_rate.sleep();
  }


}

And the Image Client (taken from UsingCvBridgeToConvertBetweenROSImagesAndOpenCVImages):

class ImageConverter
{
  ros::NodeHandle nh_;
  image_transport::ImageTransport it_;
  image_transport::Subscriber image_sub_;
  image_transport::Publisher image_pub_;

public:
  ImageConverter()
    : it_(nh_)
  {
    image_pub_ = it_.advertise("out", 1);
    image_sub_ = it_.subscribe("/topic_imgtransport", 1, &ImageConverter::imageCb, this);

    cv::namedWindow(WINDOW);
  }

  ~ImageConverter()
  {
    cv::destroyWindow(WINDOW);
  }

  void imageCb(const sensor_msgs::ImageConstPtr& msg)
  {
    cout<<"Hello callback"<<endl;
    cv_bridge::CvImagePtr cv_ptr;
    try
    {
      cv_ptr = cv_bridge::toCvCopy(msg, enc::BGR8);
    }
    catch (cv_bridge::Exception& e)
    {
      ROS_ERROR("cv_bridge exception: %s", e.what());
      return;
    }

    if (cv_ptr->image.rows > 60 && cv_ptr->image.cols > 60)
      cv::circle(cv_ptr->image, cv::Point(50, 50), 10, CV_RGB(255,0,0));

    cv::imshow(WINDOW, cv_ptr->image);
    cv::waitKey(3);

    image_pub_.publish(cv_ptr->toImageMsg());
  }
};

int main(int argc, char** argv)
{
  ros::init(argc, argv, "image_converter");
  ImageConverter ic;
  ros::spin();
  return 0;
}

This doesn't work. Here is the output when I run the client and then the server:

Hello callback

[ERROR] [1362578421.357583807]: cv_bridge exception: Unrecognized image encoding []

In the cv_ptr = cv_bridge::toCvCopy(msg, enc::BGR8); line I tried several encoding types, namely "bgr8", "mono8" (as the documentation actually states) and the exception remains the same.

I can visualize the image in the server side, and rostopic echos the correct data about the image.

So obviously the problem is in the way I am receiving the image.

Any thoughts?

edit retag flag offensive close merge delete

Comments

The problem is that it seems like the message you are sending does not fill the encoding field. Therefore, `cv_bridge` cannot know how to interpret the image. Can you run `rostopic echo /topic_imgtransport --noarr -n 1` and post the result in your question?

Thomas gravatar image Thomas  ( 2013-09-30 14:51:58 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2015-03-24 06:55:32 -0600

Wolf gravatar image

In the code on your image publisher side you need to fill the encoding. After

 cv_ptr->image = cv::imread("socio_32.jpg",CV_LOAD_IMAGE_COLOR);

add a line like:

cv_ptr->encoding = "bgr8";

Note that opencv's highgui (e. g. imread, imwrite) functions by default assume BGR8 color space.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2013-03-06 03:33:09 -0600

Seen: 5,863 times

Last updated: Mar 24 '15