ROS Melodic和OpenCV-CompressedImage未发布

时间:2018-08-04 19:11:42

标签: opencv raspberry-pi3 opencv3.0 ros

我是ROS的新手。我正在集成OpenCV和ROS。我使用的代码是从ROS教程启发而来的,该教程将ROS消息转换为CV :: mat并返回。

我正在订阅raspicam节点发布的主题/raspicam_node/image/compressed。我可以轻松获取主题,将其转换为CV:mat,并使用OpenCV对其进行修改。第32行打开一个窗口,向我显示修改后的CV:mat图像。

我的问题出现在第36行。我可以使用rostopic list看到该主题,但是当我echo时却什么也没有。如果使用rqt_image_view,则相同。

对我所缺少的东西有什么想法吗?谢谢!

#!/usr/bin/env python

from __future__ import print_function

import roslib
roslib.load_manifest('comp_vision')
import sys
import rospy
import cv2
from std_msgs.msg import String
from sensor_msgs.msg import Image, CompressedImage
from cv_bridge import CvBridge, CvBridgeError


class ImageConverter:

    def __init__(self):
        self.image_pub = rospy.Publisher("modified_image", CompressedImage, queue_size=10)
        self.brige = CvBridge()
        self.image_sub = rospy.Subscriber("/raspicam_node/image/compressed", CompressedImage, self.callback)

    def callback(self,data):
        try:
            cv_image = self.brige.compressed_imgmsg_to_cv2(data, "passthrough")
        except CvBridgeError as e:
            print(e)

        (rows, cols, channels) = cv_image.shape
        if cols > 60 and rows > 60:
            cv2.circle(cv_image, (50,50), 10, 255)

        cv2.imshow("Image Window", cv_image)
        cv2.waitKey(3)

        try:
            self.image_pub.publish(self.brige.cv2_to_compressed_imgmsg(cv_image))
        except CvBridgeError as e:
            print(e)


def main(args):
    ic = ImageConverter()
    rospy.init_node("image_converter", anonymous=True)
    try:
        rospy.spin()
    except KeyboardInterrupt:
        print("shutting down")
    cv2.destroyAllWindows()


if __name__ == '__main__':
    main(sys.argv)

1 个答案:

答案 0 :(得分:0)

如果您选中rostopic echo /modified_image,则可以看到您的主题已成功发布。

rqt_image_view将显示它,如果您调用已发布的主题modified_image/compressed

来自compressed_image_transport wiki

  

将其发布为image_raw / compressed形式的主题。然后,任何使用image_transport的ROS节点都可以通过压缩的传输来订阅image_raw,就像在发布者端使用了image_transport