In this tutorial we will get up and running with ROS (Robot Operating System)  and library OpenCV (Open source computer Vision)…

This is what we require:

ROS do support long list of camera drivers that allow us to access the camera feed parameters like resolution, frame rate etc., and publish raw pictures, which we will additionally filter, method and manipulate by the code. Here, I actually have used uvc_camera USB camera driver written by Ken Tossell.

Testing Webcam

Before moving ahead, it’s necessary to make sure whether or not your webcam works or not in Linux. There are some open-source programs like:

  1. Cheese uses the gstreamer library, which utilises the video4linux2 API to capture video and stills from a webcam. It can also apply some special effects. Type

    to install it.
  2. GUVCView is a graphical front-end for UVC drivers built using GTK+. It is way better than cheese for controlling webcam and recording audio/video. Type

    to install it.

Install Camera Drivers to work with ROS

  1. Open the terminal window, head to the directory wherever you’d prefer to install the camera driver package. In my laptop, I installed it within the home directory (go there by typing cd within the terminal window).
  2. Install the package by typing:
  3. Once it gets installed, type in the terminal:

    If you are now in the uvc_camera directory, then the installation was successful.

Find which device port is the camera connected.

  1. Open Terminal Window
  2. Disconnect the camera from the computer and type the following in the terminal:

    If you don’t have any other cameras connected to your computer you would see no output, else you would see something like,

    Remember the number of devices listed in this step.
  3. Now connect the camera to your computer and repeat step 2. You should see a new device added to the list like:

    So, the camera is connected on /dev/video0.

Gain Root access by typing

it will ask for root password.

Create a new package for image processing

  1. Open a new terminal window and in the home directory, type the following:

    This will create a new directory called tutorialROSOpenCV.
  2. We will now create a launch directory wherever we might store the file accustomed to launch the uvc_cameranode so as for it to begin publishing the photographs, amendment directory:

    Now make a new directory under it called launch:

    This is where we will store the launch file.
  3. Change the directory:

    Create the new launch file by typing:

    Paste the following code in there:


    & then Save and exit. Notice the parameter device with value: /dev/video0. Change this to the actual device path. Refer to the previous section.

  4. Before making this package you need to tell ROS where to find it. So type:

    A text editor should open up. Next add the following line at the end:

    Save and Exit the text editor.
  5. Restart the terminal, gain root access, and type:

    In the rosmake results you should notice:

     

Test the Launch script
In order to make sure that the package uvc_camera is in fact publishing the images, we will now run the node and check the output.

  1. In a new terminal type:

    If your webcam has an integrated light indicator, you should see it light up.
  2. Now we will make sure that the images are being published. Open up another terminal window and type:

    You should see the following topics being published:
  3. Lets look at the topic /camera/image_raw. Type:

    You should see a new window pop-up with a continuous streaming video capture output of the webcam. Press Ctrl-Cto exit. Lets look at the format of this topic. Type

    You should see something like this:

    sensor_msgs/Image is a ROS image format which contains the header, height, width, step, and image data. Refer to:http://www.ros.org/doc/api/sensor_msgs/html/msg/Image.html for more information.
  4. Lets look at the topic/camera/camera_info. Type:

    You should see something like this:

    sensor_msgs/CameraInfo is a ROS information format which contains the header, height, width, distortion model params, binning params and ROI (region of interest) params. Refer to: http://www.ros.org/doc/api/sensor_msgs/html/msg/CameraInfo.html for more info. You can view the contents of this topic by typing:

Write the code for image processing

  1. In a new terminal window, type the following:

    We will now create the source file which will contain the code. Type:

    The following program inverts the colors of the image captured from the webcam. Paste the following code in the file:

    then Save and Exit.
  2. Before we make the program, we have to edit the CMakeLists.txt file to ensure it will build and make an executable file of the program above. Gain root access, and type:

    Then we will edit the CMakeLists file. Type:

    At the end of the file add the following line:

    then Save and Exit.
  3. Now we can compile/make the package. Type:

    You should notice the following:
  4. Make sure that roscore is already running. If not, in the terminal type:

    To run the project, type:

    You should see a window being created titled Image Processed containing the inverted image from the webcam
  5. In order to ensure that the processed image has been published as a rostopic, in a new terminal window type:

    You would see the following:

    To check the image data in rostopic /camera/image_processed, type:

    You should see a new window pop-up with a continuous streaming video capture output of the webcam with the image inverted. Press Ctrl-C to exit.