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

how to output the OpenCV image out of the callback function?

asked 2021-10-15 04:35:46 -0600

luzhenzheng1990 gravatar image

updated 2021-10-15 05:00:49 -0600

Hi, I am subscribing a videostream of compressed image and i knew how to convert it into OpenCV image and was able to show it already. But i want to output(like return value) the OpenCV image from the callback function.How should I do it?


void imageCallback_L(const sensor_msgs::CompressedImageConstPtr&msg)
     {
            try
            {
            cv::imshow("imgCallBack",cv::imdecode(cv::Mat(msg->data),1));
            cv::waitKey(1);
            ROS_INFO("receving fisheye_left_image");
            }
            catch(cv_bridge::Exception&e)
            {
            ROS_ERROR("cv_bridge exception: %s",e.what());
            }   
    }


int main(int argc, char** argv)
{
    ros::init(argc,argv,"CompressedImage");
    ros::NodeHandle nh;
    ros::Subscriber image_sub;
    std::string image_topic="/sensors/camera/fisheye_left/image_raw/compressed";
    image_sub=nh.subscribe(image_topic,10,imageCallback_L);

    while (ros::ok())
    {
        ros::spinOnce();
    }

    return 0;
}
edit retag flag offensive close merge delete

Comments

If you check your cpu usage, you will find that this code is using 100% of the cpu. You need to limit how many times per second that while loop executes, and you do that by adding a short sleep inside your while loop. Or simply replace the entire loop with a ros::spin();.

Mike Scheutzow gravatar image Mike Scheutzow  ( 2021-10-15 06:30:53 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-10-15 05:22:58 -0600

Delb gravatar image

The method cv::imdecode returns a cv::Mat object so you just need to store the output in a cv::Mat object that is not in the callback's scope :

cv::Mat img;
void imageCallback_L(const sensor_msgs::CompressedImageConstPtr&msg)
{
    try
    {
        img = cv::imdecode(cv::Mat(msg->data),1)
        cv::imshow("imgCallBack",img);
        cv::waitKey(1);
        ROS_INFO("receving fisheye_left_image");
    }
    catch(cv_bridge::Exception&e)
    {
        ROS_ERROR("cv_bridge exception: %s",e.what());
    }   
}
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2021-10-15 04:31:05 -0600

Seen: 607 times

Last updated: Oct 15 '21