Publish Image msg
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")
time.sleep(0.1)
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!
If you don't especially need sensor_msgs, I would use image_transport instead to publish images, it works fine.
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.
Ok, my bad, I use it with C++ so I assumed it was implemented in python...