docker OpenCV在ROS Melodic上不显示VideoCapture

dced5bon  于 2023-03-29  发布在  Docker
关注(0)|答案(1)|浏览(92)

我试图做一个节点来捕获视频从网络摄像头并发布它,但首先我试图显示图像与命令“cv2.imshow”,但这不起作用.错误是这样的:

Traceback (most recent call last):   File "/ws/src/test/scripts/testing-camera.py", line 29, in <module>     cv2.imshow("Image",img) TypeError: mat is not a numpy array, neither a scalar

我在Docker上使用ROS Melodic,使用ubuntu 22.04。我是在ROS上使用OpenCV的新手,所以如果你能推荐一些教程,我会很感激。Python版本是2.7.17
这是我的代码:

#!/usr/bin/env python

import rospy
import cv2
import numpy as np
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError

video = cv2.VideoCapture(0)
img = []
class Camera:
    def __init__(self):
        self.publisher = rospy.Publisher('camera_img', Image, queue_size = 10)
        self.status, img = video.read()
        self.bridge = CvBridge()
        print(self.status)

    def publish_image(self):
        image = self.bridge.cv2_to_imgmsg(img,encoding="passthrough")
        self.publisher.publish(image)
        print("Se publico la imagen")

if __name__=='__main__':
    try:
        rospy.init_node('camera_node')
        rate = rospy.Rate(10)

        cam = Camera()
        cv2.imshow("Image",img)

        while not rospy.is_shutdown():
            cam.publish_image()
            rate.sleep()
    
    except rospy.ROSInterruptException:
        pass

我试着改变

img = 0

img = []

及以后

img = np.array([])

但这不管用

8cdiaqws

8cdiaqws1#

本教程很好地开始使用ros & opencv(它是针对ros2的,但我认为它使概念清晰):https://automaticaddison.com/getting-started-with-opencv-in-ros-2-foxy-fitzroy-python/

相关问题