Subscribed topic doesn't call callback function
Hi, I'm trying to get an image from the topic "/camera/image/compressed", however my attempts at doing this are not successful.
int main(int argc, char **argv)
{
ros::init(argc, argv, "image_listener");
ros::NodeHandle nh;
cv::namedWindow("view");
cv::startWindowThread();
ros::Subscriber sub = nh.subscribe("/camera/image/compressed", 1, imageCallback);
ros::spin();
cv::destroyWindow("view");
return 0;
}
void imageCallback(const sensor_msgs::ImageConstPtr& msg)
{
std::cout << "Hello?" << std::endl;
try
{
Mat img = cv::imdecode(msg->data, 1);
std::cout << img.size << " | " << img.cols << " | " << img.rows << std::endl;
cv::Mat image = cv::imdecode(cv::Mat(msg->data),1);//convert compressed image data to cv::Mat
cv::imshow("view", image);
cv::waitKey(10);
}
catch (cv_bridge::Exception& e)
{
ROS_ERROR("Could not convert to image!");
}
}
When I look at the console after running the code the console remains empty not a single "Hello?" is seen, this leads me to believe the callback function is never called.
Also this window pops up but stays empty:
However, the node is publishing images because in rqt_image_view_ImageView I have a constant updating image:
What am I doing wrong?
Since this is a
sensor_msgs/Image
topic, why are you not usingimage_transport
? Compressed images are not raw images, so it's possible that withoutimage_transport
(which would decompress it for you), there is no valid image data to display.Aha, now it tells me to: Try subscribing to the base topic 'camera/image' instead with parameter ~image_transport set to 'compressed'
How do I set a parameter?
I would suggest to check the following tutorials:
Also: Running the Simple Image Publisher and Subscriber with Different Transports.
I have followed those tutorials, but they don't tell me how I would set the parameter in C++ it only deals with command line codes? I've also looked at: https://wiki.ros.org/roscpp_tutorials... and when I use that I get another error...
can you please stop deleting questions that have received comments that actually helped you?
I've deleted them because they do not solve the issues and thus are not actual solutions?