Tutorial: How to use ROS grabber

Table of Contents

Introduction

Let us consider the following classical ViSP example provided in tutorial-visp-grabber-1394.cpp and given here after. Thanks to the vp1394TwoGrabber class provided in ViSP library it allows to grab images from a firewire camera.

#include <visp/vp1394TwoGrabber.h>
#include <visp/vpDisplayX.h>
#include <visp/vpImage.h>
int main()
{
#ifdef VISP_HAVE_DC1394_2
try {
vpImage<unsigned char> I; // Create a gray level image container
bool reset = true; // Enable bus reset during construction (default)
vp1394TwoGrabber g(reset); // Create a grabber based on libdc1394-2.x third party lib
g.setVideoMode(vp1394TwoGrabber::vpVIDEO_MODE_640x480_MONO8);
g.setFramerate(vp1394TwoGrabber::vpFRAMERATE_60);
g.open(I);
std::cout << "Image size: " << I.getWidth() << " " << I.getHeight() << std::endl;
#ifdef VISP_HAVE_X11
vpDisplayX d(I);
#else
std::cout << "No image viewer is available..." << std::endl;
#endif
while(1) {
g.acquire(I);
vpDisplay::display(I);
vpDisplay::flush(I);
if (vpDisplay::getClick(I, false))
break;
}
}
catch(vpException e) {
std::cout << "Catch an exception: " << e << std::endl;
}
#endif
}

This example is described in ViSP image frame grabbing tutorial pages.

To build this example ViSP should be installed. If not, depending on your ROS distro it could be done like:

sudo apt-get install ros-hydro-visp

Now to build this example, just run the following commands in a terminal:

cd ~/catkin_ws/src/visp_ros/tutorial/grabber/visp
cmake .
make

To run this example, hit in the same terminal:

./tutorial-visp-grabber-1394

This example works only if you have a firewire camera. If you want to use an usb camera, you need to modify the previous source by introducing vpV4l2Grabber class. This may be fastidious. That is why, in the next section we show how to use a more generic grabber class based on ROS and called vpROSGrabber.

How to grab images from ROS

visp_ros package provides vpROSGrabber class that is able to handle any images published on a ROS topic. To be able to display these images, the previous example need to me modified as in tutorial-ros-grabber.cpp given below:

#include <visp/vpDisplayX.h>
#include <visp/vpImage.h>
int main()
{
try {
//vpImage<unsigned char> I; // Create a gray level image container
vpImage<vpRGBa> I; // Create a color image container
vpROSGrabber g; // Create a grabber based on ROS
g.setCameraInfoTopic("/camera/camera_info");
g.setImageTopic("/camera/image_raw");
g.setRectify(true);
g.open(I);
std::cout << "Image size: " << I.getWidth() << " " << I.getHeight() << std::endl;
#ifdef VISP_HAVE_X11
vpDisplayX d(I);
#else
std::cout << "No image viewer is available..." << std::endl;
#endif
while(1) {
g.acquire(I);
vpDisplay::display(I);
vpDisplay::flush(I);
if (vpDisplay::getClick(I, false))
break;
}
}
catch(vpException e) {
std::cout << "Catch an exception: " << e << std::endl;
}
}

Compared to the example based fully on ViSP and given in the Introduction, once declared we simply indicate which are the camera_info and image topics:

g.setCameraInfoTopic("/camera/camera_info");
g.setImageTopic("/camera/image_raw");

If camera_info parameters are provided including distorsion, the image is rectified:

g.setRectify(true);

Next as usual with ViSP, a new image is acquired using:

g.acquire(I);

Now to build this example, install visp_ros catkin package and setup the environment as described in the tutorial: Howto build and install visp_ros.

source ~/catkin_ws/install/setup.bash

Build the example by entering the following commands in a terminal:

cd ~/catkin_ws/src/visp_ros/tutorial/grabber/ros
cmake .
make

Now run the example:

./tutorial-ros-grabber
[ERROR] [1397815271.796496797]: [registerPublisher] Failed to contact master at [127.0.0.1:11311]. Retrying...

The grabber is waiting for images published on topic /camera/image_raw. To this end let us consider the following launch file camera-firewire.launch.

<launch>
<!-- Launch the firewire camera acquisition node -->
<node pkg="camera1394" type="camera1394_node" name="my_camera1394_node" args="_video_mode:=640x480_rgb8" >
</node>
</launch>

When launched, you will see the images:

cd ~/catkin_ws/src/visp_ros/tutorial/grabber/ros
roslaunch camera-firewire.launch


visp_ros
Author(s): Francois Pasteau, Fabien Spindler
autogenerated on Mon Mar 2 2015 01:32:56