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

Combining the depth and color data

asked 2015-08-03 12:30:41 -0500

pnambiar gravatar image

updated 2015-08-03 12:38:17 -0500

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?

Thanks, -P

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__':
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2015-08-03 13:01:32 -0500

updated 2015-08-03 13:01:45 -0500

You can use message filters to synchronize the topics.

(I never used that with python, btw.)

edit flag offensive delete link more


Thanks. ApproximateTimeSynchronizer(python) is added in the indigo version

pnambiar gravatar image pnambiar  ( 2015-08-04 12:38:01 -0500 )edit

You're welcome ;-)

cyborg-x1 gravatar image cyborg-x1  ( 2015-08-04 14:44:06 -0500 )edit

Question Tools

1 follower


Asked: 2015-08-03 12:30:41 -0500

Seen: 1,787 times

Last updated: Aug 03 '15