Rosanswers logo

Hey all,

I'm working on a code which publishes images coming from a camera (atm a video is used for testing). To publish those Images I would like to use the sensor_msgs. In the following is the code I got so far:

#!/usr/bin/env python

import rospy
from sensor_msgs.msg import Image

import cv2
from cv_bridge import CvBridge, CvBridgeError

rospy.init_node('VideoPublisher', anonymous=True)

VideoRaw = rospy.Publisher('VideoRaw', Image, queue_size=10)
NeedleBorder = rospy.Publisher('NeedleBorder', Image, queue_size=10)

cam = cv2.VideoCapture('output.avi')

while cam.isOpened():
    meta, frame = cam.read()

    frame_gaus = cv2.GaussianBlur(frame, gaussian_blur_ksize, gaussian_blur_sigmaX)

    frame_gray = cv2.cvtColor(frame_gaus, cv2.COLOR_BGR2GRAY)

    frame_edges = cv2.Canny(frame_gray, threshold1, threshold2)

    # I want to publish the Canny Edge Image and the original Image
    msg_frame = CvBridge().cv2_to_imgmsg(frame)
    msg_frame_edges = CvBridge().cv2_to_imgmsg(frame_edges)

    VideoRaw.publish(msg_frame, "RGB8")
    NeedleBorder.publish(msg_frame_edges, "mono8")


So the original image and the image the OpenCV Canny Edge Detector produces should be published.

If I run this code I just get the following output in the console once!: ...252, 255, 255, 252, 255, 255, 252, 255, 255, 252, 255, 255, 252, 255, 255, 252, 255, 255, 252, 255], 'RGB8')

But there isn't even a print command in my code o.O ? So it seems the code prints the original image once in the console and the program closes. If I rostopic echo both topics (in my code named 'VideoRaw' and 'NeedleBorder') I get to see nothing.

I would appreciate any help! Thanks a lot in advance!

Originally posted by Holzroller on ROS Answers with karma: 62 on 2014-12-11

Post score: 0

Original comments

Comment by Cyril Jourdan on 2014-12-11:
If you don't especially need sensor_msgs, I would use image_transport instead to publish images, it works fine.

Comment by Holzroller on 2014-12-11:
If I'm not mistaken, its doesn't support Python yet: Python Usage

image_transport does not yet support Python, though it is on the Roadmap. If you need to interface a Python node with some compressed image transport, try interposing a republish node.

Comment by Cyril Jourdan on 2014-12-11:
Ok, my bad, I use it with C++ so I assumed it was implemented in python...


2 Answers 2


Rosanswers logo

Somehow I feel really stupid now. The exactly same code in another directory works totally fine... ?!?! So there seems nothing wrong with the code ¯_(ツ)_/¯

Originally posted by Holzroller with karma: 62 on 2014-12-11

This answer was ACCEPTED on the original site

Post score: 0


Rosanswers logo

np_arr = np.fromstring(ros_data.data, np.uint8) np_img = np_arr.reshape((480, 640, 3)) imshow(np_img)

Originally posted by Guan Heng with karma: 11 on 2017-03-27

This answer was NOT ACCEPTED on the original site

Post score: 1


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.