ROS Melodic & OpenCV - CompressedImage not publishing

asked 2018-08-04 18:27:13 -0500

rileywiley gravatar image

I am new to ROS. I am playing with integrating OpenCV and ROS. The code I am using is inspired from the ROS tutorial on converting ROS messages to CV::mat and back.

I am subscribing to the topic /raspicam_node/image/compressed published by the raspicam node. I have no problem getting the topic, converting it to CV:mat, and modifying it with OpenCV. Line 32 opens a window and shows me the modified CV:mat image.

My issue comes at Line 36. I can see the topic using rostopic list but when I echo it nothing comes up. Same if I use rqt_image_view.

Any thoughts on what I am missing? Thank you!

#!/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)
edit retag flag offensive close merge delete