Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to subscribe to two image topics

Hello,

I am trying to write a node that subscribes to image_color_rect and image_depth_rect topics provided by my Kinect v2's driver node. I have written the following sample:

void kinectdepthCallback(const sensor_msgs::ImageContPtr& msg2){

    cv_bridge::CvImagePtr cv_ptr2;
    try{
        cv_ptr2 = cv_bridge::toCvCopy(msg2, sensor_msgs::image_encodings::BGR8); 
    }
    catch (cv_bridge::Exception& e){
        ROS_ERROR("Could not convert from '%s' to 'bgr8'.", e.what());
    return;
    }
    cv::imshow("Depth Image",cv_ptr2->image);
}


void kinectCallback(const sensor_msgs::ImageConstPtr& msg){
  cv_bridge::CvImagePtr cv_ptr;
    try{
        cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8); 
    }
    catch (cv_bridge::Exception& e){
        ROS_ERROR("Could not convert from '%s' to 'bgr8'.", e.what());
    return;
    }
    (some code here)
}


int main(int argc, char** argv)
{
  ROS_INFO("Initializing node");
  ros::init(argc, argv, "image_converter");
  ros::NodeHandle kinect_node;
  image_transport::ImageTransport it(kinect_node);
  image_transport::ImageTransport it2(kinect_node);
  image_transport::Subscriber sub;
  sub = it.subscribe("/kinect2/qhd/image_color_rect",1,kinectCallback);
  sub = it2.subscribe("/kinect2/qhd/image_depth_rect",1,kinectdepthCallback);

 (some other code here)

  return 0;
}

My code works without errors if I don't subscribe for the depth image. I am trying to do these 2 subscriptions, but I get an error like the following:

error: invalid initialization of reference of type ‘const int&’ from expression of type ‘const boost::shared_ptr<const sensor_msgs::Image_<std::allocator<void> > >’
           BOOST_FUNCTION_RETURN(f(BOOST_FUNCTION_ARGS));

What probably is the reason for that? Aren't my subscriptions correct?

How to subscribe to two image topics

Hello,

I am trying to write a node that subscribes to image_color_rect and image_depth_rect topics provided by my Kinect v2's driver node. I have written the following sample:

void kinectdepthCallback(const sensor_msgs::ImageContPtr& msg2){

    cv_bridge::CvImagePtr cv_ptr2;
    try{
        cv_ptr2 = cv_bridge::toCvCopy(msg2, sensor_msgs::image_encodings::BGR8); 
    }
    catch (cv_bridge::Exception& e){
        ROS_ERROR("Could not convert from '%s' to 'bgr8'.", e.what());
    return;
    }
    cv::imshow("Depth Image",cv_ptr2->image);
}


void kinectCallback(const sensor_msgs::ImageConstPtr& msg){
  cv_bridge::CvImagePtr cv_ptr;
    try{
        cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8); 
    }
    catch (cv_bridge::Exception& e){
        ROS_ERROR("Could not convert from '%s' to 'bgr8'.", e.what());
    return;
    }
    (some code here)
}


int main(int argc, char** argv)
{
  ROS_INFO("Initializing node");
  ros::init(argc, argv, "image_converter");
  ros::NodeHandle kinect_node;
  image_transport::ImageTransport it(kinect_node);
  image_transport::ImageTransport //image_transport::ImageTransport it2(kinect_node);
  image_transport::Subscriber sub;
  sub = it.subscribe("/kinect2/qhd/image_color_rect",1,kinectCallback);
  sub = it2.subscribe("/kinect2/qhd/image_depth_rect",1,kinectdepthCallback);
it.subscribe("/kinect2/qhd/image_depth_rect",1,kinectdepthCallback);

 (some other code here)

  return 0;
}

My code works without errors if I don't subscribe for the depth image. I am trying to do these 2 subscriptions, but I get an error like the following:

error: invalid initialization of reference of type ‘const int&’ from expression of type ‘const boost::shared_ptr<const sensor_msgs::Image_<std::allocator<void> > >’
           BOOST_FUNCTION_RETURN(f(BOOST_FUNCTION_ARGS));

What probably is the reason for that? Aren't my subscriptions correct?

How to subscribe to two image topics

Hello,

I am trying to write a node that subscribes to image_color_rect and image_depth_rect topics provided by my Kinect v2's driver node. I have written the following sample:

void kinectdepthCallback(const sensor_msgs::ImageContPtr& msg2){

    cv_bridge::CvImagePtr cv_ptr2;
    try{
        cv_ptr2 = cv_bridge::toCvCopy(msg2, sensor_msgs::image_encodings::BGR8); 
    }
    catch (cv_bridge::Exception& e){
        ROS_ERROR("Could not convert from '%s' to 'bgr8'.", e.what());
    return;
    }
    cv::imshow("Depth Image",cv_ptr2->image);
}


void kinectCallback(const sensor_msgs::ImageConstPtr& msg){
  cv_bridge::CvImagePtr cv_ptr;
    try{
        cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8); 
    }
    catch (cv_bridge::Exception& e){
        ROS_ERROR("Could not convert from '%s' to 'bgr8'.", e.what());
    return;
    }
    (some code here)
}


int main(int argc, char** argv)
{
  ROS_INFO("Initializing node");
  ros::init(argc, argv, "image_converter");
  ros::NodeHandle kinect_node;
  image_transport::ImageTransport it(kinect_node);
  //image_transport::ImageTransport it2(kinect_node);
  image_transport::Subscriber sub;
  sub = it.subscribe("/kinect2/qhd/image_color_rect",1,kinectCallback);
  sub = it.subscribe("/kinect2/qhd/image_depth_rect",1,kinectdepthCallback);

 (some other code here)

  return 0;
}

My code works without errors if I don't subscribe for the depth image. I am trying to do these 2 subscriptions, but I get an error like the following:

error: invalid initialization of reference of type ‘const int&’ from expression of type ‘const boost::shared_ptr<const sensor_msgs::Image_<std::allocator<void> > >’
           BOOST_FUNCTION_RETURN(f(BOOST_FUNCTION_ARGS));

What probably is the reason for that? Aren't my subscriptions correct?

Also what kind of encoding should I have for the depth image?

Thanks for answering in advance,

Chris