Camera Image Display Example

Jump to: navigation , search

Introduction

The camera display example demonstrates the display of cameras on Sawyer robot. The user can choose to display unrectified image or not, as well as the Canny edge detection image. If you would like to follow along with the actual source code for the example on GitHub, it can be found through this link for camera image display example.

Usage

Start the camera display example program, you can specify the camera name (right_hand_camera or head_camera), use of the raw image (unrectified) topic or use of streaming the Canny edge detection image. The default camera name is "head_camera". The camera image will be rectified image without Canny edge detection by default.

Run the example as following command:

$ rosrun intera_examples camera_display.py

Arguments

Important Arguments:

-c or --camera : Choose camera name for display

-r or --raw : Use the "raw" (unrectified) image streaming topic (default is rectified)

-e or --edge : Apply the Canny edge detection algorithm to streamed image

usage: camera_display.py [-h] [-c {head_camera,right_hand_camera}] [-r] [-e]

Camera Display Example
   

optional arguments:
  -h, --help            show this help message and exit
  -c {head_camera,right_hand_camera}, --camera {head_camera,right_hand_camera}
                        Setup Camera Name for Camera Display
  -r, --raw             Specify use of the raw image (unrectified) topic
  -e, --edge            Streaming the Canny edge detection image

View Image Results

Here listed sample image results by using the right_hand_camera on robot arm.

Chairsmall nr.png Chairsmall.png

The first image shows the original camera image, with typical lens distortio. The second image uses the OpenCV camera calibration parameters to rectify the image, making it undistorted. In our example, by default, streaming the rectified image instead of raw image unless you specify raw image by adding argument -r or --raw.

Chairsmall edge nr.png Chairsmall edge.png

The images above are streaming the Canny edge detection image, the first one showing raw image with Canny edge detection, the second one showing rectified image.


Code Walkthrough

Now, let's break down the code.

  1. import argparse
  2. import numpy as np
  3.  
  4. import cv2
  5. from cv_bridge import CvBridge, CvBridgeError
  6.  
  7. import rospy
  8. import intera_interface

This imports the intera interface for accessing the camera class.

  1. def show_image_callback(img_data, (edge_detection, window_name)):
  2.     """The callback function to show image by using CvBridge and cv
  3.    """
  4.     bridge = CvBridge()
  5.     try:
  6.         cv_image = bridge.imgmsg_to_cv2(img_data, "bgr8")
  7.     except CvBridgeError, err:
  8.         rospy.logerr(err)
  9.         return
  10.     if edge_detection == True:
  11.         gray = cv2.cvtColor(cv_image, cv2.COLOR_BGR2GRAY)
  12.         blurred = cv2.GaussianBlur(gray, (3, 3), 0)
  13.         # customize the second and the third argument, minVal and maxVal
  14.         # in function cv2.Canny if needed
  15.         get_edge = cv2.Canny(blurred, 10, 100)
  16.         cv_image = np.hstack([get_edge])
  17.     edge_str = "(Edge Detection)" if edge_detection else ''
  18.     cv_win_name = ' '.join([window_name, edge_str])
  19.     cv2.namedWindow(cv_win_name, 0)
  20.     # refresh the image on the screen
  21.     cv2.imshow(cv_win_name, cv_image)
  22. cv2.waitKey(3)

An instance of the CvBridge, bridge is created. Convert the image message to cv2. If the user choose to show image edge detection, the function will convert cv_image to black/white image and blur the image by using GaussianBlur then get the image edge by implementing the Canny method. Note: the image will always refresh so close the image window will not shutdown the image window.

  1. def main():
  2.     """Camera Display Example
  3.    """
  4.     rp = intera_interface.RobotParams()
  5.     valid_cameras = rp.get_camera_names()
  6.     if not valid_cameras:
  7.         rp.log_message(("Cannot detect any camera_config"
  8.             " parameters on this robot. Exiting."), "ERROR")
  9.         return
  10.     arg_fmt = argparse.RawDescriptionHelpFormatter
  11.     parser = argparse.ArgumentParser(formatter_class=arg_fmt,
  12.                                      description=main.__doc__)
  13.     parser.add_argument(
  14.         '-c', '--camera', type=str, default="head_camera",
  15.         choices=valid_cameras, help='Setup Camera Name for Camera Display')
  16.     parser.add_argument(
  17.         '-r', '--raw', action='store_true',
  18.         help='Specify use of the raw image (unrectified) topic')
  19.     parser.add_argument(
  20.         '-e', '--edge', action='store_true',
  21.         help='Streaming the Canny edge detection image')
  22.     args = parser.parse_args()

Three optional arguments camera, raw and edge are captured from the command line arguments.

  1.     print("Initializing node... ")
  2.     rospy.init_node('camera_display', anonymous=True)
  3.     camera = intera_interface.Cameras()
  4.     if not camera.verify_camera_exists(args.camera):
  5.         rospy.logerr("Invalid camera name, exiting the example.")
  6.         return
  7.     camera.start_streaming(args.camera)
  8.     rectify_image = not args.raw
  9.     use_canny_edge = args.edge
  10.     camera.set_callback(args.camera, show_image_callback,
  11.         rectify_image=rectify_image, callback_args=(use_canny_edge, args.camera))
  12.  
  13.     def clean_shutdown():
  14.         print("Shutting down camera_display node.")
  15.         cv2.destroyAllWindows()
  16.  
  17.     rospy.on_shutdown(clean_shutdown)
  18.     rospy.loginfo("Camera_display node running. Ctrl-c to quit")
  19.     rospy.spin()
  20.  
  21.  
  22. if __name__ == '__main__':
  23. main()

The node is initialized and an instance of the camera class is created. After verified the camera name, the camera start streaming, the callback function being set, the callback function show_image_callback will be called. Press Ctrl-C to quit the example.