Ask Your Question
0

Reg: Subscribing to Depth and rgb image at the same time

asked 2012-10-18 23:50:59 -0500

this post is marked as community wiki

This post is a wiki. Anyone with karma >75 is welcome to improve it.

Hi,

I am new in ROS. I wanted to get both the depth and rgb image of an object at the same time and use it in the callback function. if I use it as mentioned below. It works for me.

void imageCallback(const sensor_msgs::ImageConstPtr& kinect_image)

image_transport::Subscriber sub = it.subscribe("camera/rgb/image_color", 1, imageCallback); or image_transport::Subscriber sub = it.subscribe("camera/depth/image", 1, imageCallback);

I dont know how can I get both the rgb and depth image in the callback funtion. The below way doesn't work.

void imageCallback(const sensor_msgs::ImageConstPtr& kinect_image,const sensor_msgs::ImageConstPtr& depth_image)

image_transport::Subscriber sub = it.subscribe("camera/rgb/image_color","camera/depth/image", 1, imageCallback);

I know, may be the question sounds very naive. But, I would really be grateful if someone can guide me.

edit retag flag offensive close merge delete

3 Answers

Sort by » oldest newest most voted
5

answered 2012-10-19 00:18:34 -0500

Lorenz gravatar image

The simplest solution is to create two different image callbacks as methods of a class and store the images in member variables when they are received.

A better solution is to use a synchronizer provided by the message_filters package. Have a look at the TimeSynchronizer example.

edit flag offensive delete link more

Comments

If anyone reads this...Do we need to sync if the images are from the same device, say a kinect?

aguadopd gravatar imageaguadopd ( 2016-03-08 16:49:02 -0500 )edit

Well, we needed indeed. After synchronising, offeset became lower. ROS Indigo Node for saving color and depth images from a Kinect

aguadopd gravatar imageaguadopd ( 2016-05-09 18:40:32 -0500 )edit
2

answered 2016-05-09 18:42:28 -0500

aguadopd gravatar image

Sample code. This is a ROS Indigo node for saving depth and color images from a Kinect - originally in a Turtlebot. Source, usage and more

/* Este es un nodo de ROS Indigo para guardar imagenes de /camera/depth_registered/image_rect_raw
 * y de /camera/rgb/color/image_rect_raw de un Turtlebot1 para luego procesarlas con otro programa. 
 * Las de profundidad se guardan como unsigned int de 16 bits 
 * y 1 canal, las de color se guardan como unsigned int de 8 bits en 3 canales.
 * Se utiliza un suscriptor sincronizado para guardar el par de imagenes que estén más
 * cercanas en el tiempo.
 * LAS IMAGENES SE GUARDAN ADONDE SE EJECUTE EL NODO.
 * ---------------------------------------------------------
 * Creado por Fabricio Emder y Pablo Aguado en el 2016 */


/* This is a ROS Indigo node for saving Turtlebot images from the /camera/depth_registered/image_rect_raw
 * and /camera/rgb/color/image_rect_raw topics, for further processing outside of the program. 
 * Depth images are saved as 1 channel 16 bit unsigned int PNGs (CV_16UC1), and 
 * RGB images are saved as 3 channel 8 bit unsigned int PNGs (CV_8UC3).
 * A synchronized subscriber is used, for saving the pair of images that are most closer in time.
 * THE IMAGES ARE SAVED WHEREVER THE NODE IS RUN.
 * Created by Fabricio Emder and Pablo Aguado - 2016
 * */

#include <ros/ros.h>
#include <cv_bridge/cv_bridge.h>    // OpenCV
#include <sensor_msgs/Image.h>
#include <sensor_msgs/image_encodings.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

#include <message_filters/subscriber.h>
#include <message_filters/synchronizer.h>


/* La política de sincronización exacta NO FUNCIONA para los topics seleccionados, ya 
 * que la Kinect o sus drivers no los están publicando con la misma marca temporal.
 * Ver más en http://wiki.ros.org/message_filters 
 * 
 * Exact sychronization policy IS NOT WORKING for the topics used, because the kinect
 * is not publishing them with the same timestamp. 
 * See more in http://wiki.ros.org/message_filters  */

//#define EXACT
#define APPROXIMATE


#ifdef EXACT
#include <message_filters/sync_policies/exact_time.h>
#endif
#ifdef APPROXIMATE
#include <message_filters/sync_policies/approximate_time.h>
#endif



using namespace std;
//using namespace sensor_msgs;
using namespace message_filters;


// Contador para la numeración de los archivos.
// Counter for filenames.
unsigned int cnt = 1;



// Handler / callback
void callback( const sensor_msgs::ImageConstPtr& msg_rgb , const sensor_msgs::ImageConstPtr& msg_depth )
{
        //ROS_INFO_STREAM("Adentro del callback\n");
      cv_bridge::CvImagePtr img_ptr_rgb;
        cv_bridge::CvImagePtr img_ptr_depth;
    try{
        img_ptr_depth = cv_bridge::toCvCopy(*msg_depth, sensor_msgs::image_encodings::TYPE_16UC1);
    }
    catch (cv_bridge::Exception& e)
    {
        ROS_ERROR("cv_bridge exception:  %s", e.what());
        return;
    }
    try{
        img_ptr_rgb = cv_bridge::toCvCopy(*msg_rgb, sensor_msgs::image_encodings::TYPE_8UC3);
    }
    catch (cv_bridge::Exception& e)
    {
        ROS_ERROR("cv_bridge exception:  %s", e.what());
        return;
    }

    cv::Mat& mat_depth = img_ptr_depth->image;
        cv::Mat& mat_rgb = img_ptr_rgb->image;

        char file_rgb[100];
        char file_depth[100];

        sprintf( file_rgb, "%04d_rgb.png", cnt );
        sprintf( file_depth, "%04d_depth.png", cnt );

        vector<int> png_parameters;
        png_parameters.push_back( CV_IMWRITE_PNG_COMPRESSION );
        /* We save with no compression for faster processing.
         * Guardamos PNG sin compresión para menor retardo. */
        png_parameters.push_back( 9 ); 

        cv::imwrite( file_rgb , mat_rgb, png_parameters );
        cv::imwrite( file_depth, mat_depth, png_parameters );

        ROS_INFO_STREAM(cnt << "\n");
        ROS_INFO_STREAM("Imágenes guardadas - "
                        "Images saved\n");

        cnt++;

}





int main(int argc, char** argv)
{
    // Initialize the ROS system and become a node.
  ros::init(argc, argv, "guardar_imagenes ...
(more)
edit flag offensive delete link more

Comments

1

I don't get it why such a good answer did not get upvotes.

Maverobot gravatar imageMaverobot ( 2018-04-27 03:12:59 -0500 )edit
0

answered 2012-10-19 01:41:26 -0500

this post is marked as community wiki

This post is a wiki. Anyone with karma >75 is welcome to improve it.

Thanks a lot for your response.

edit flag offensive delete link more

Comments

Please do not create answers for discussions, comments or subsequent questions. This is not a forum. Instead, please either use the comment functionality or edit your original post.

Lorenz gravatar imageLorenz ( 2012-10-19 02:15:30 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2012-10-18 23:50:59 -0500

Seen: 2,520 times

Last updated: May 09 '16