Ask Your Question
1

convert from sensor_msgs::Image bgr8 to a png file on disk

asked 2015-11-08 07:07:44 -0600

mpatalberta gravatar image

This is the subroutine I need. What is holding me back from converting the rgb image you send me is converting to a png file. This is done by using cv_bridge to convert from ros to the cv space.

The problem is cv is expecting a const sensor_msgs::imageConstPtr msg

I only have a const sensor_msgs::image. We need to use “boost” some way to make a new member. Do you remember the syntax?

void convert_image_BGR82PNG(const sensor_msgs::Image msg) {

//Convert from the ROS image message to a CvImage suitable for working with OpenCV for processing
cv_bridge::CvImagePtr cv_ptr;
try
{
    //Always copy, returning a mutable CvImage
    //OpenCV expects color images to use BGR channel order.
    const sensor_msgs::ImageConstPtr msgIn;
   //  msgIn = msg::ConstPtr<msg>;
   //msgIn = sensor_msgs::ImageConstPtr(msg);
   cv_ptr = cv_bridge::toCvCopy(msgIn,"bgr8");
   cv::imwrite("jnk.pcd",cv_ptr->image);

}
catch (cv_bridge::Exception& e)
{
    //if there is an error during conversion, display it
    ROS_ERROR("tutorialROSOpenCV::main.cpp::cv_bridge exception: %s", e.what());
    return;
}

}

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2015-11-08 07:08:40 -0600

mpatalberta gravatar image

Good or bad the code compiles and links. I will need to check the routine at work: This involved linking in boost and cv_bridge also painful.

void convert_image_BGR82PNG(const sensor_msgs::Image msg) {

//Convert from the ROS image message to a CvImage suitable for working with OpenCV for processing
cv_bridge::CvImagePtr cv_ptr;
try
{
    //Always copy, returning a mutable CvImage
    //OpenCV expects color images to use BGR channel order.

    sensor_msgs::ImagePtr itI = boost::make_shared<sensor_msgs::Image>(msg);


   cv_ptr = cv_bridge::toCvCopy(itI,"bgr8");
   cv::imwrite("jnk.pcd",cv_ptr->image);

}
catch (cv_bridge::Exception& e)
{
    //if there is an error during conversion, display it
    ROS_ERROR("tutorialROSOpenCV::main.cpp::cv_bridge exception: %s", e.what());
    return;
}

}

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

Stats

Asked: 2015-11-08 07:07:44 -0600

Seen: 1,122 times

Last updated: Nov 08 '15