无法使用ROS CvBridge和OpenCV发布Usb相机图像

时间:2017-02-26 22:03:32

标签: python opencv camera usb ros

我正在尝试使用usb_camera,ros_bridge发布opencv图像。主题已发布但我看不到任何图像或错误。我可以看到usb_camera image

class image_converter:
  def __init__(self):
    self.image_pub = rospy.Publisher("/image_converter/image_topic_2",Image, queue_size=10)
    self.bridge = CvBridge()
    self.image_sub =     rospy.Subscriber("/webcam/image_raw",Image,self.callback)

  def callback(self,data):
    try:
      cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8")
    except CvBridgeError as e:
      print(e)
    grey = cv2.cv_image(cv_image,(7,7))
    edges =cv2.canny(grey,15.0,30.0)

    try:
    self.image_pub.publish(self.bridge.cv2_to_imgmsg(edges, "bgr8"))
    except CvBridgeError as e:
      print(e)

def main(args):
  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)

2 个答案:

答案 0 :(得分:0)

在您的main功能中,您应首先致电rospy.init_node,然后再执行任何与ROS相关的内容,例如创建发布商/订阅者。

答案 1 :(得分:0)

不确定,我缺乏疯狂的python技能,但是你只是调用类的“构造函数”而不将其赋值给任何东西,这是不是意味着它会有一个refcount为0并自动被垃圾收集?