lambda
lambda

Reputation: 85

Code does nothing when i run it (ROS, opencv)

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. (previous codes that i've been running had the same structure)

#!/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)

Upvotes: 1

Views: 255

Answers (1)

You must execute rospy.init before subscribing to any topic.

Upvotes: 3

Related Questions