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

Subscribed topic doesn't call callback function

asked 2019-01-16 07:34:47 -0500

ROStutorialLearner gravatar image

updated 2019-01-16 07:35:50 -0500

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: image description

However, the node is publishing images because in rqt_image_view_ImageView I have a constant updating image:

image description

What am I doing wrong?

edit retag flag offensive close merge delete

Comments

1

Since this is a sensor_msgs/Image topic, why are you not using image_transport? Compressed images are not raw images, so it's possible that without image_transport (which would decompress it for you), there is no valid image data to display.

gvdhoorn gravatar image gvdhoorn  ( 2019-01-16 08:10:09 -0500 )edit

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?

ROStutorialLearner gravatar image ROStutorialLearner  ( 2019-01-16 08:30:59 -0500 )edit
1

I would suggest to check the following tutorials:

gvdhoorn gravatar image gvdhoorn  ( 2019-01-16 08:38:45 -0500 )edit

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...

ROStutorialLearner gravatar image ROStutorialLearner  ( 2019-01-16 08:47:50 -0500 )edit
2

can you please stop deleting questions that have received comments that actually helped you?

gvdhoorn gravatar image gvdhoorn  ( 2019-01-16 10:55:42 -0500 )edit

I've deleted them because they do not solve the issues and thus are not actual solutions?

ROStutorialLearner gravatar image ROStutorialLearner  ( 2019-01-16 11:23:43 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2019-09-27 20:20:20 -0500

404RobotNotFound gravatar image

You are trying to subscribe to a compressed image topic with a callback that subscribes to a raw image.

Either change the topic name to "/camera/image" and use the raw image if it is available, or make your node use image_transport and make it subscribe to "/camera/image" but pass in the image_transport:=compressed parameter when launching.

Follow this tutorial to write the image_transport code: https://wiki.ros.org/image_transport/...

Then run the node like this: rosrun <pkg> image_listener image_transport:=compressed.

Side note: rqt_image_view knows how to handle the compressed image transport when you subscribe to a compressed image, that is why it is working when yours is not.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2019-01-16 07:34:47 -0500

Seen: 551 times

Last updated: Sep 27 '19