Subscriber C++ to camera_info

asked 2016-12-11 15:38:41 -0600

fabbro gravatar image

Hi everyone,

I followed the tutorial on the subscriber to the Image subscriber here: http://wiki.ros.org/image_transport/T...

It worked but now I would like to add a subscriber to the camera_info topic.

I tried to understand how to do it but I didn't succeed. Basically I found someone who is doing it through the message_filters::Subscriber but I didn't understand how to do it.

Could you help me please?

The code that I have is the following right now:

#include <ros/ros.h>
#include <image_transport/image_transport.h>
#include <opencv2/highgui/highgui.hpp>
#include <cv_bridge/cv_bridge.h>
#include <sensor_msgs/CameraInfo.h>

void imageCallback(const sensor_msgs::ImageConstPtr& msg)
{
    try
    {
        cv::imshow("view", cv_bridge::toCvShare(msg, "bgr8")->image);
        cv::waitKey(30);
    }
    catch (cv_bridge::Exception& e)
    {
        ROS_ERROR("Could not convert from '%s' to 'bgr8'.", msg->encoding.c_str());
    }
}

int main(int argc, char **argv)
{
    ros::init(argc, argv, "image_listener");
    ros::NodeHandle nh;
    cv::namedWindow("view");
    cv::startWindowThread();
    image_transport::ImageTransport it(nh);
    image_transport::Subscriber sub = it.subscribe("camera/image_raw", 1, imageCallback);

    ros::Subscriber<sensor_msgs::CameraInfo> sub_cameraInfo(); // this is wrong
    message_filters::Subscriber<sensor_msgs::CameraInfo> sub_cameraInfo ; // I don't know how to finish this!
    ros::spin();
    cv::destroyWindow("view");
}

Thanks a lot for your help.

edit retag flag offensive close merge delete

Comments

Did you check the ROS wiki? It actually has exactly what you are looking for as an example.

mgruhler gravatar imagemgruhler ( 2016-12-12 01:52:51 -0600 )edit

Hi, thanks for the reply. What is the difference between:

image_transport::ImageTransport it(nh);

image_transport::Subscriber sub = it.subscribe("camera/image_raw", 1, imageCallback);

and

message_filters::Subscriber<Image> image_sub(nh, "image", 1);

Thanks

fabbro gravatar imagefabbro ( 2016-12-12 03:40:51 -0600 )edit

Ok I understood that:

Example (C++)

message_filters::Subscriber<std_msgs::UInt32> sub(nh, "my_topic", 1);

sub.registerCallback(myCallback);

is the equivalent of:

ros::Subscriber sub = nh.subscribe("my_topic", 1, myCallback);
fabbro gravatar imagefabbro ( 2016-12-12 04:05:51 -0600 )edit