Bridging OpenCV and ROS

Introduction

In the previous tutorial, we have seen working with the basics of the OpenCV and ROS. In this tutorial, we can get to know about linking the Open CV with the ROS and performing the various operations.

In ROS, the operations of the OpenCV library are done by collecting the images or the videos from the topics published by the drivers of the hardware camera module. So, the images obtained from the OpenCV are imported into the ROS using the concept of the publisher and subscriber. 

Moreover, in ROS the format of the images being processed is quite different to that of the OpenCV. To exchange the image formats between the OpenCV and ROS and vice versa, fortunately, the cv bridge will help in transferring the image across these two different platforms.

Sending an image from ROS to Opencv

Let us perform a small project or assignment to link the image capture by the ROS to be imported to the OpenCV.

Initially, we need to run the ROS master node

-->  roscore

Now, execute the following command to perform the switching of the USB cam on your hardware setup.

--> rosrun usb_cam usb_cam_node _pixel_format:=yuyv

Now, the following ROS command has to be executed in the other terminal to start the ROS topic.

--> rosrun image_view image_view image:=/usb_cam/image_raw

Let us have the look on to the code to send the image from the ROS to the Open CV using the CV bridge.

Code

import rospy
import cv2
from std_msgs.msg import String
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
import sys

bridge = CvBridge()

def image_callback(ros_image):
  print 'got an image'
  global bridge
  #convert ros_image into an opencv-compatible image
  try:
    cv_image = bridge.imgmsg_to_cv2(ros_image, "bgr8")
  except CvBridgeError as e:
      print(e)
  #from now on, you can work exactly like with opencv
  (rows,cols,channels) = cv_image.shape
  if cols > 200 and rows > 200 :
      cv2.circle(cv_image, (100,100),90, 255)
  font = cv2.FONT_HERSHEY_SIMPLEX
  cv2.putText(cv_image,'Webcam Activated with ROS & OpenCV!',(10,350), font, 1,(255,255,255),2,cv2.LINE_AA)
  cv2.imshow("Image window", cv_image)
  cv2.waitKey(3)

  
def main(args):
  rospy.init_node('image_converter', anonymous=True)
  #for turtlebot3 waffle
  #image_topic="/camera/rgb/image_raw/compressed"
  #for usb cam
  #image_topic="/usb_cam/image_raw"
  image_sub = rospy.Subscriber("/usb_cam/image_raw",Image, image_callback)
  try:
    rospy.spin()
  except KeyboardInterrupt:
    print("Shutting down")
  cv2.destroyAllWindows()

if __name__ == '__main__':
    main(sys.argv)

Now, perform the rosrun of this python file and on the successful execution of this code results in the transformation of the image from the ROS to the OpenCV and gives a message that the image is received properly.

This completes the end of this tutorial. With this bridging between the Open CV and ROS, various embedded projects can be linked easily with the open computer vision, to work efficiently. For any sort of doubts or the questions in this article, you can let us know through the comment box. 

Spread knowledge

Leave a Comment

Your email address will not be published. Required fields are marked *