Rosanswers logo

I'm trying to use the following piece of code for acquiring depth as well as the color data from kinect. I would now like to use the depth information to extract some points/pixel in the color image after a keyboard input ('c') a) What is the best way to pass information from callback2 to callback ? b) How do I ensure that the depth data(callback2) is obtained before callback is executed?


import roslib
import sys
import rospy
import cv2
from std_msgs.msg import String
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
import numpy as np
import math;
import cv2;
import matplotlib.pyplot as plt;
import sys;
import caffe;
import socket;

class image_converter:

  def __init__(self):
    cv2.namedWindow("Image window", 1)
    print 'start bridge and subscribe'
    self.bridge = CvBridge()
    self.depth_sub = rospy.Subscriber("/camera/depth_registered/image_raw",Image,self.callback2,queue_size=1)
    self.image_sub = rospy.Subscriber("/camera/rgb/image_color",Image,self.callback,queue_size=1)

  def callback2(self,data):
        depth_image_raw = self.bridge.imgmsg_to_cv2(data, "passthrough")
        depth_image = depth_image_raw.astype(np.uint8)
        cv2.imshow("Depth window", depth_image)
        key = cv2.waitKey(3)
        if (key == 99): # 'c'
            # save depth data as numpy array

    except CvBridgeError, e:
      print e

  def callback(self,data):

      image = self.bridge.imgmsg_to_cv2(data, "bgr8")
    except CvBridgeError, e:
      print e

    cv_image = image[:,:,:];

    if (key == 99): # 'c'
       # Use the depth data and get the corresponding points in the color image

def main(args):
  ic = image_converter()
  rospy.init_node('image_converter', anonymous=True)
      except KeyboardInterrupt:
    print "Shutting down"

if __name__ == '__main__':

Originally posted by pnambiar on ROS Answers with karma: 120 on 2015-08-03

Post score: 0


1 Answer 1


Rosanswers logo

You can use message filters to synchronize the topics. http://wiki.ros.org/message_filters

(I never used that with python, btw.)

Originally posted by cyborg-x1 with karma: 1376 on 2015-08-03

This answer was ACCEPTED on the original site

Post score: 1

Original comments

Comment by pnambiar on 2015-08-04:
Thanks. ApproximateTimeSynchronizer(python) is added in the indigo version

Comment by cyborg-x1 on 2015-08-04:
You're welcome ;-)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.