所以这是我的问题。我正在使用ROS和opencv,尝试使用两个摄像头创建深度图。但是,我写的代码,似乎没有做任何事情,我有点困惑为什么。 (我以前运行的代码具有相同的结构)
#!/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)
答案 0 :(得分:3)
您必须在订阅任何主题之前执行rospy.init。