The aim of this example is to show how to retrieve images from NAO’s cameras. The images will be displayed in a window, using OpenCV library. Press ESC to exit the application.
The whole example is available here: getimage.zip
/**
*
* This example demonstrates how to get images from the robot remotely and how
* to display them on your screen using opencv.
*
* Copyright Aldebaran Robotics
*/
// Aldebaran includes.
#include <alproxies/alvideodeviceproxy.h>
#include <alvision/alimage.h>
#include <alvision/alvisiondefinitions.h>
#include <alerror/alerror.h>
// Opencv includes.
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <iostream>
#include <string>
using namespace AL;
/**
* \brief Shows images retrieved from the robot.
*
* \param robotIp the IP adress of the robot
*/
void showImages(const std::string& robotIp)
{
/** Create a proxy to ALVideoDevice on the robot.*/
ALVideoDeviceProxy camProxy(robotIp, 9559);
/** Subscribe a client image requiring 320*240 and BGR colorspace.*/
const std::string clientName = camProxy.subscribe("test", kQVGA, kBGRColorSpace, 30);
/** Create an iplimage header to wrap into an opencv image.*/
IplImage* imgHeader = cvCreateImageHeader(cvSize(320, 240), 8, 3);
/** Create a OpenCV window to display the images. */
cvNamedWindow("images");
/** Main loop. Exit when pressing ESC.*/
while ((char) cvWaitKey(30) != 27)
{
/** Retrieve an image from the camera.
* The image is returned in the form of a container object, with the
* following fields:
* 0 = width
* 1 = height
* 2 = number of layers
* 3 = colors space index (see alvisiondefinitions.h)
* 4 = time stamp (seconds)
* 5 = time stamp (micro seconds)
* 6 = image buffer (size of width * height * number of layers)
*/
ALValue img = camProxy.getImageRemote(clientName);
/** Access the image buffer (6th field) and assign it to the opencv image
* container. */
imgHeader->imageData = (char*)img[6].GetBinary();
/** Tells to ALVideoDevice that it can give back the image buffer to the
* driver. Optional after a getImageRemote but MANDATORY after a getImageLocal.*/
camProxy.releaseImage(clientName);
/** Display the iplImage on screen.*/
cvShowImage("images", imgHeader);
}
/** Cleanup.*/
camProxy.unsubscribe(clientName);
cvReleaseImageHeader(&imgHeader);
}
int main(int argc, char* argv[])
{
if (argc < 2)
{
std::cerr << "Usage 'getimages robotIp'" << std::endl;
return 1;
}
const std::string robotIp(argv[1]);
try
{
showImages(robotIp);
}
catch (const AL::ALError& e)
{
std::cerr << "Caught exception " << e.what() << std::endl;
}
return 0;
}
The corresponding CMakeLists.txt file is the following:
##
# Copyright (C) 2010 Aldebaran Robotics
cmake_minimum_required(VERSION 2.6.4 FATAL_ERROR)
project(getimages)
# this lets you find the qibuild cmake frameworl
include("qibuild.cmake")
# Here, we create an executable named "getimages" from the cpp file.
qi_create_bin(getimages getimages.cpp)
# Here we say that our executable depends on
# - ALCOMMON (main naoqi lib)
# - ALVISION (for vision definitions)
# - OPENCV (display)
#
# It automatically links with the corresponding libraries and make their headers
# available.
qi_use_lib(getimages ALCOMMON ALVISION OPENCV)
Note
When running CMake, you might have to replace the default OpenCV libraries by the ones on your system. To do that, switch to Advanced view in Cmake-GUI and replace the arguments of OPENCV_INCLUDE_DIR and OPENCV_LIBRARIES with the OpenCV paths of your system.