Skip to content

Instantly share code, notes, and snippets.

@awesomebytes
Created April 18, 2020 04:08
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awesomebytes/df2d4fe59774ee9d3f0b576fb5f0374f to your computer and use it in GitHub Desktop.
Save awesomebytes/df2d4fe59774ee9d3f0b576fb5f0374f to your computer and use it in GitHub Desktop.
Node to convert a ROS Image topic publisher from RGB to grayscale (rgb or bgr to mono8) using opencv and cv_bridge
#!/usr/bin/env python
import sys
import rospy
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
import cv2
"""
Node to transform an input Image topic into
an output grayscale Image topic.
Author: Sammy Pfeiffer <Sammy.Pfeiffer at student.uts.edu.au>
"""
class GrayScaler(object):
def __init__(self, name_topic_in, name_topic_out):
self.cv_bridge = CvBridge()
rospy.loginfo("Converting Images from topic " + name_topic_in +
" to grayscale, output topic: " + name_topic_out)
self.pub = rospy.Publisher(name_topic_out, Image, queue_size=5)
self.sub = rospy.Subscriber(
name_topic_in, Image, self.image_cb, queue_size=5)
def image_cb(self, img_msg):
# Transform to cv2/numpy image
img_in_cv2 = self.cv_bridge.imgmsg_to_cv2(
img_msg, desired_encoding='passthrough')
# Transform to grayscale,
# available encodings: http://docs.ros.org/jade/api/sensor_msgs/html/image__encodings_8h_source.html
if "rgb" in img_msg.encoding:
gray_img = cv2.cvtColor(img_in_cv2, cv2.COLOR_RGB2GRAY)
elif "bgr" in img_msg.encoding:
gray_img = cv2.cvtColor(img_in_cv2, cv2.COLOR_BGR2GRAY)
# Transform back to Image message
gray_img_msg = self.cv_bridge.cv2_to_imgmsg(
gray_img, encoding="mono8")
self.pub.publish(gray_img_msg)
if __name__ == '__main__':
argv = rospy.myargv(sys.argv)
if len(argv) != 3:
print("Usage:")
print(argv[0] + " name_topic_in name_topic_out")
print("Converts a RGB/BGR name_topic_in Image topic into a Grayscale name_topic_out Image topic")
exit(0)
rospy.init_node("image_to_grayscale", anonymous=True)
gs = GrayScaler(argv[1], argv[2])
rospy.spin()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment