Aldebaran documentation What's new in NAOqi 2.4.3?

Optimized access to images

<< return to C++ examples

Principle

This section explains how to implement an optimized access to NAO’s images when the module is remote.

This idea is that a remote call is less efficient, so we make a direct call to retrieve the images: the buffer containing the current image is taken directly.

To retrieve the images, subscribe as usual using a proxy to ALVideoDevice, and specify the framerate, resolution etc.

To prevent NAO from erasing the buffer we want to use, we have to lock it. That means that after processing the image, the buffer has to be released. So it is important to check that the image processing does not take too long compared to the framerate to avoid blocking the frame grabbing process.

Example: OptimizedImage module

This example implements a module retrieved images optimally. No further processing is done.

The whole example is available here: optimizedimage.zip

Source:

optimizedimage.cpp

/**
 *
 * Version : $Id$
 * This file was generated by Aldebaran Robotics ModuleGenerator
 */

#include "optimizedimage.h"

#include <alvalue/alvalue.h>
#include <alcommon/alproxy.h>
#include <alcommon/albroker.h>

#include <alvision/alvisiondefinitions.h>


OptimizedImage::OptimizedImage(
  boost::shared_ptr<AL::ALBroker> broker,
  const std::string& name):
    AL::ALModule(broker, name),
    fVideoProxy(AL::ALVideoDeviceProxy(broker)),
    fGVMId("GVM")
{
  setModuleDescription("This is an autogenerated module, this descriptio needs to be updated.");

}

OptimizedImage::~OptimizedImage() {
  fVideoProxy.unsubscribe(fGVMId);
  delete fImagePointer;
}

void OptimizedImage::init() {
  fGVMId = fVideoProxy.subscribe(fGVMId, AL::kVGA, AL::kRGBColorSpace, 5);
}

void OptimizedImage::optimizedImageProcessing() {
  /** Retrieve a pointer to the image. */
  fImagePointer = (AL::ALImage*) fVideoProxy.getImageLocal(fGVMId);

  /** Do whatever processing you want... */

  /** Do not forget to release the image. */
  fVideoProxy.releaseImage(fGVMId);
}

Main:

main.cpp

/**
 * @author
 *
 * \section Description
 * This file was generated by Aldebaran Robotics ModuleGenerator
 */

#include <signal.h>
#include <boost/shared_ptr.hpp>
#include <alcommon/albroker.h>
#include <alcommon/almodule.h>
#include <alcommon/albrokermanager.h>
#include <alcommon/altoolsmain.h>

#include "optimizedimage.h"


#ifdef OPTIMIZEDIMAGE_IS_REMOTE
# define ALCALL
#else
# ifdef _WIN32
#  define ALCALL __declspec(dllexport)
# else
#  define ALCALL
#  endif
#endif

extern "C"
{
  ALCALL int _createModule(boost::shared_ptr<AL::ALBroker> pBroker)
  {
    // init broker with the main broker instance
    // from the parent executable
    AL::ALBrokerManager::setInstance(pBroker->fBrokerManager.lock());
    AL::ALBrokerManager::getInstance()->addBroker(pBroker);
    AL::ALModule::createModule<OptimizedImage>( pBroker, "OptimizedImage" );

    return 0;
  }

  ALCALL int _closeModule()
  {
    return 0;
  }
}

#ifdef OPTIMIZEDIMAGE_IS_REMOTE
  int main(int argc, char *argv[])
  {
    // pointer to createModule
    TMainType sig;
    sig = &_createModule;
    // call main
    ALTools::mainFunction("optimizedimage", argc, argv, sig);
  }
#endif

CMakeLists.txt:

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
project(optimizedimage)
find_package(qibuild)

option(OPTIMIZEDIMAGE_IS_REMOTE
  "Module is compiled as a remote module (ON or OFF)"
  ON)

set(_srcs
  main.cpp
  optimizedimage.h
  optimizedimage.cpp
)


if(OPTIMIZEDIMAGE_IS_REMOTE)
  add_definitions(" -DOPTIMIZEDIMAGE_IS_REMOTE")
  qi_create_bin(optimizedimage ${_srcs})
else()
  qi_create_lib(optimizedimage SHARED ${_srcs} SUBFOLDER naoqi)
endif()


qi_use_lib(optimizedimage ALCOMMON ALPROXIES ALVISION)