Code does nothing when i run it (ROS, opencv: Depth map using two cameras)

asked 2016-06-08 10:02:46 -0500

laurent gravatar image

So here's my problem. I'm using ROS and opencv, trying to create a depth map using two cameras. However, the code that i wrote, does not seem to do anything and im a little confused about why

#!/usr/bin/env python
from __future__ import print_function
import roslib
roslib.load_manifest('test_cam')
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 message_filters

class image_converter:

  def __init__(self):

    self.bridge = CvBridge()
    self.image_sub_1 = message_filters.Subscriber("/cameras/left_hand_camera/image",Image)
    self.image_sub_2 = message_filters.Subscriber("/cameras/head_camera/image",Image)
    self.ts = message_filters.TimeSynchronizer([self.image_sub_1, self.image_sub_2], 10)
    self.ts.registerCallback(self.callback)

  def callback(self,Image):
    try:
      cv_image_1 = self.bridge.imgmsg_to_cv2(Image, "bgr8")
      cv_image_2 = self.bridge.imgmsg_to_cv2(Image, "bgr8")

    except CvBridgeError as e:
      print(e)

    stereo = cv2.StereoBM_create(numDisparities=16, blockSize=15)
    disparity = stereo.compute(cv_image_1,cv_image_2)
    plt.imshow(disparity,'gray')
    plt.show()
    plt.waitKey(1)

def main(args):
  ic = image_converter()

  rospy.init_node('image_converter', anonymous=True)
  try:
    rospy.spin()
  except KeyboardInterrupt:
    print("Shutting down")
  cv2.destroyAllWindows()

if __name__ == '__main__':
    main(sys.argv)
edit retag flag offensive close merge delete

Comments

Hey, I'm not clear when you say your code does nothing. Can you please provide a sample output when you execute the code?

janindu gravatar image janindu  ( 2016-06-09 20:52:53 -0500 )edit

The code would execute nothing, but still run. But this happened because the timestamps of my cameras were different. I used an approximate function to fix that, but thanks anyway :)

laurent gravatar image laurent  ( 2016-06-10 08:41:25 -0500 )edit