-
-
Save rethink-imcmahon/77a1a4d5506258f3dc1f to your computer and use it in GitHub Desktop.
#! /usr/bin/python | |
# Copyright (c) 2015, Rethink Robotics, Inc. | |
# Using this CvBridge Tutorial for converting | |
# ROS images to OpenCV2 images | |
# http://wiki.ros.org/cv_bridge/Tutorials/ConvertingBetweenROSImagesAndOpenCVImagesPython | |
# Using this OpenCV2 tutorial for saving Images: | |
# http://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_gui/py_image_display/py_image_display.html | |
# rospy for the subscriber | |
import rospy | |
# ROS Image message | |
from sensor_msgs.msg import Image | |
# ROS Image message -> OpenCV2 image converter | |
from cv_bridge import CvBridge, CvBridgeError | |
# OpenCV2 for saving an image | |
import cv2 | |
# Instantiate CvBridge | |
bridge = CvBridge() | |
def image_callback(msg): | |
print("Received an image!") | |
try: | |
# Convert your ROS Image message to OpenCV2 | |
cv2_img = bridge.imgmsg_to_cv2(msg, "bgr8") | |
except CvBridgeError, e: | |
print(e) | |
else: | |
# Save your OpenCV2 image as a jpeg | |
cv2.imwrite('camera_image.jpeg', cv2_img) | |
def main(): | |
rospy.init_node('image_listener') | |
# Define your image topic | |
image_topic = "/cameras/left_hand_camera/image" | |
# Set up your subscriber and define its callback | |
rospy.Subscriber(image_topic, Image, image_callback) | |
# Spin until ctrl + c | |
rospy.spin() | |
if __name__ == '__main__': | |
main() |
I got an error.
I run this program but It doesn't work
only printed
segmentation fault
I got an error.
I run this program but It doesn't work
only printed
segmentation fault
The error segmentation fault could appear if you mix Python 2 and Python 3. ROS normally is installed under Python 2. But you could also install as example rospy for Python 3. Maybe this is not the point. If you install ROS OpenCV will be installed for ROS under Python 2 with a version 3.x. If you have separate installed OpenCV the segmentation fault could also appear. It could appear if you use OpenCV 4.x an Python do not know which OpenCV Version it should import. Another solution could be that OpenCV is separately installed for Python 3 if you use Python 2 or the other way around.
Hi, Is there a way to republish converted jpeg to a certain topic? Using this program as a real-time converter?
Thanks.
In ROS you can send a jpeg image as jpeg image. So you have to imread a picture, create an array or matrix and have to send it as sensor_msgs/Image with a publisher. On another node you can subscribe it like shown in this code.
Hello, I am working on a project having a realsense camera in a gazebo robot model and would like to manipulate it with rospy or roscpp. (The camera should identify the color in the gazebo environment ). Thanks
Hello, I'm sorry, I'm still new using ROS. I wanted to ask where does the file saved?
Hi, Is there a way to republish converted jpeg to a certain topic? Using this program as a real-time converter? Thanks.
should be, you could publish the saved image
Hello, I'm sorry, I'm still new using ROS. I wanted to ask where does the file saved?
Image location in your "Catkin_make" folder that you uing "Catkin_make" command
I need to play back the rosbag at a slower speed to capture all the frames, or some of them will be lost. Is there a more elegant solution?
I need to play back the rosbag at a slower speed to capture all the frames, or some of them will be lost. Is there a more elegant solution?
I think on the ros Wiki, there a Parameter where you can adjust the playback speed.
I used this code to simulate a pipeline where my robot detects text in a cluttered environment and reads it. I needed images every 1 second so I added a simple
rospy.sleep(1)
after line 27.