gpt4 book ai didi

opencv - 当我运行它时代码什么都不做(ROS,opencv)

转载 作者:太空宇宙 更新时间:2023-11-03 22:10:50 26 4
gpt4 key购买 nike

所以这是我的问题。我正在使用 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)

最佳答案

您必须在订阅任何主题之前执行 rospy.init。

关于opencv - 当我运行它时代码什么都不做(ROS,opencv),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37705965/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com