how to output the OpenCV image out of the callback function?
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;
}
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();
.