Visual Servoing Platform  version 3.6.1 under development (2024-05-26)

Very simple example of augmented reality based on Ogre3D.

* ViSP, open source Visual Servoing Platform software.
* Copyright (C) 2005 - 2023 by Inria. All rights reserved.
* This software is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* See the file LICENSE.txt at the root directory of this source
* distribution for additional information about the GNU GPL.
* For using ViSP with software that can not be combined with the GNU
* GPL, please contact Inria about acquiring a ViSP Professional
* Edition License.
* See for more information.
* This software was developed at:
* Inria Rennes - Bretagne Atlantique
* Campus Universitaire de Beaulieu
* 35042 Rennes Cedex
* France
* If you have questions regarding the use of this file, please contact
* Inria at
* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
* Description:
* Implementation of a simple augmented reality application using the vpAROgre
* class.
#include <iostream>
#include <visp3/core/vpConfig.h>
//#if defined(VISP_HAVE_OGRE) && (defined(VISP_HAVE_OPENCV) ||
// defined(VISP_HAVE_GDI) || defined(VISP_HAVE_D3D9) || defined(VISP_HAVE_GTK)
//|| (defined(VISP_HAVE_X11) && ! defined(APPLE)))
#if defined(VISP_HAVE_OGRE) && \
(defined(VISP_HAVE_OPENCV) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_D3D9) || defined(VISP_HAVE_GTK) || \
(defined(VISP_HAVE_X11) && !(defined(__APPLE__) && defined(__MACH__))))
//#if defined(VISP_HAVE_X11) && ! defined(APPLE)
#if defined(VISP_HAVE_X11) && !(defined(__APPLE__) && defined(__MACH__))
// produce an error on OSX: ‘typedef int Cursor’
// /usr/X11R6/include/X11/X.h:108: error: ‘Cursor’ has a previous
// declaration as ‘typedef XID Cursor’. That's why it should not be
// used on APPLE platforms
#include <visp3/gui/vpDisplayX.h>
#include <visp3/ar/vpAROgre.h>
#include <visp3/blob/vpDot2.h>
#include <visp3/core/vpDebug.h>
#include <visp3/core/vpImagePoint.h>
#include <visp3/core/vpIoTools.h>
#include <visp3/core/vpPixelMeterConversion.h>
#include <visp3/core/vpPoint.h>
#include <visp3/gui/vpDisplayD3D.h>
#include <visp3/gui/vpDisplayGDI.h>
#include <visp3/gui/vpDisplayGTK.h>
#include <visp3/gui/vpDisplayOpenCV.h>
#include <visp3/io/vpParseArgv.h>
#include <visp3/io/vpVideoReader.h>
#include <visp3/vision/vpPose.h>
// List of allowed command line options
#define GETOPTARGS "ci:p:h"
void usage(const char *name, const char *badparam, std::string ipath, std::string ppath)
std::string ext("png");
std::string ext("pgm");
fprintf(stdout, "\n\
Test augmented reality using the vpAROgre class.\n\
%s [-i <test image path>] [-p <personal image path>]\n\
[-c] [-h]\n", name);
fprintf(stdout, "\n\
OPTIONS: Default\n\
-i <input image path> %s\n\
Set image input path.\n\
From this path read images \n\
\"mire-2/image.%%04d.%s\". These \n\
images come from ViSP-images-x.y.z.tar.gz available \n\
on the ViSP website.\n\
Setting the VISP_INPUT_IMAGE_PATH environment\n\
variable produces the same behaviour than using\n\
this option.\n\
-p <personal image path> %s\n\
Specify a personal sequence containing images \n\
to process.\n\
By image sequence, we mean one file per image.\n\
Example : \"/Temp/visp-images/cube/image.%%04d.%s\"\n\
%%04d is for the image numbering.\n\
Disable the mouse click. Useful to automate the \n\
execution of this program without human intervention.\n\
Print the help.\n",
ipath.c_str(), ext.c_str(), ppath.c_str(), ext.c_str());
if (badparam)
fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath, bool &click_allowed)
const char *optarg_;
int c;
while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
switch (c) {
case 'c':
click_allowed = false;
case 'i':
ipath = optarg_;
case 'p':
ppath = optarg_;
case 'h':
usage(argv[0], nullptr, ipath, ppath);
return false;
usage(argv[0], optarg_, ipath, ppath);
return false;
if ((c == 1) || (c == -1)) {
// standalone param or error
usage(argv[0], nullptr, ipath, ppath);
std::cerr << "ERROR: " << std::endl;
std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
return false;
return true;
void computeInitialPose(vpCameraParameters *mcam, vpImage<unsigned char> &I, vpPose *mPose, vpDot2 *md,
vpImagePoint *mcog, vpHomogeneousMatrix *cMo, vpPoint *mP, const bool &opt_click_allowed)
// ---------------------------------------------------
// Code inspired from ViSP example of camera pose
// ----------------------------------------------------
bool opt_display = true;
//#if defined(VISP_HAVE_X11) && ! defined(APPLE)
#if defined(VISP_HAVE_X11) && !(defined(__APPLE__) && defined(__MACH__))
// produce an error on OSX: ‘typedef int Cursor’
// /usr/X11R6/include/X11/X.h:108: error: ‘Cursor’ has a previous
// declaration as ‘typedef XID Cursor’. That's why it should not be
// used on APPLE platforms
#elif defined(VISP_HAVE_GTK)
#elif defined(VISP_HAVE_GDI)
#elif defined(HAVE_OPENCV_HIGHGUI)
#elif defined(VISP_HAVE_D3D9)
for (unsigned int i = 0; i < 4; i++) {
if (opt_display) {
else {
if (opt_display) {
try {
// Display size is automatically defined by the image (I) size
display.init(I, 100, 100, "Preliminary Pose Calculation");
// display the image
// The image class has a member that specify a pointer toward
// the display that has been initialized in the display declaration
// therefore is is no longer necessary to make a reference to the
// display variable.
// Flush the display
catch (...) {
vpERROR_TRACE("Error while displaying the image");
std::cout << "*************************************************************"
<< std::endl;
std::cout << "*************************** Preliminary Pose Calculation "
<< std::endl;
std::cout << "****************************** Click on the 4 dots "
<< std::endl;
std::cout << "********Dot1 : (-x,-y,0), Dot2 : (x,-y,0), Dot3 : (x,y,0), "
"Dot4 : (-x,y,0)**********"
<< std::endl;
std::cout << "*************************************************************"
<< std::endl;
try {
vpImagePoint ip[4];
if (!opt_click_allowed) {
for (unsigned int i = 0; i < 4; i++) {
// by using setGraphics, we request to see the edges of the dot
// in red on the screen.
// It uses the overlay image plane.
// The default of this setting is that it is time consuming
for (unsigned int j = 0; j < i; j++)
// flush the display buffer
try {
if (opt_click_allowed) {
// std::cout << "click " << i << " " << md[i] << std::endl;
else {
md[i].initTracking(I, ip[i]);
catch (...) {
mcog[i] = md[i].getCog();
// an exception is thrown by the track method if
// - dot is lost
// - the number of pixel is too small
// - too many pixels are detected (this is usual when a "big"
// specularity
// occurs. The threshold can be modified using the
// setNbMaxPoint(int) method
if (opt_display) {
// flush the display buffer
catch (const vpException &e) {
vpERROR_TRACE("Error while tracking dots");
vpCTRACE << e;
if (opt_display) {
// display a red cross (size 10) in the image at the dot center
// of gravity location
// in the vpDisplay class member's when pixel coordinates
// are considered the first element is the row index and the second
// is the column index:
// vpDisplay::displayCross(Image, row index, column index, size, color)
// therefore u and v are inverted wrt to the vpDot specification
// Alternatively, to avoid this problem another set of member have
// been defined in the vpDisplay class.
// If the method name is postfixe with _uv the specification is :
// vpDisplay::displayCross_uv(Image, column index, row index, size,
// color)
for (unsigned int i = 0; i < 4; i++)
// flush the X11 buffer
// --------------------------------------------------------
// Now we will compute the pose
// --------------------------------------------------------
// the list of point is cleared (if that's not done before)
// we set the 3D points coordinates (in meter !) in the object/world frame
double l = 0.06;
double L = 0.07;
mP[0].setWorldCoordinates(-L, -l, 0); // (X,Y,Z)
mP[1].setWorldCoordinates(L, -l, 0);
mP[2].setWorldCoordinates(L, l, 0);
mP[3].setWorldCoordinates(-L, l, 0);
// pixel-> meter conversion
for (unsigned int i = 0; i < 4; i++) {
// u[i]. v[i] are expressed in pixel
// conversion in meter is achieved using
// x = (u-u0)/px
// y = (v-v0)/py
// where px, py, u0, v0 are the intrinsic camera parameters
double x = 0, y = 0;
vpPixelMeterConversion::convertPoint(*mcam, mcog[i], x, y);
// The pose structure is build, we put in the point list the set of point
// here both 2D and 3D world coordinates are known
for (unsigned int i = 0; i < 4; i++) {
mPose->addPoint(mP[i]); // and added to the pose computation point list
// compute the initial pose using Dementhon method followed by a non linear
// minimization method
// Compute initial pose
// Display briefly just to have a glimpse a the ViSP pose
if (opt_display) {
// Display the computed pose
mPose->display(I, *cMo, *mcam, 0.05, vpColor::red);
int main(int argc, const char **argv)
std::string ext("png");
std::string ext("pgm");
try {
std::string env_ipath;
std::string opt_ipath;
std::string ipath;
std::string opt_ppath;
std::string dirname;
std::string filename;
bool opt_click_allowed = true;
// Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
// environment variable value
// Set the default input path
if (!env_ipath.empty())
ipath = env_ipath;
// Read the command line options
if (getOptions(argc, argv, opt_ipath, opt_ppath, opt_click_allowed) == false) {
// Get the option values
if (!opt_ipath.empty())
ipath = opt_ipath;
// Compare ipath and env_ipath. If they differ, we take into account
// the input path coming from the command line option
if (!opt_ipath.empty() && !env_ipath.empty() && opt_ppath.empty()) {
if (ipath != env_ipath) {
std::cout << std::endl << "WARNING: " << std::endl;
std::cout << " Since -i <visp image path=" << ipath << "> "
<< " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
<< " we skip the environment variable." << std::endl;
// Test if an input path is set
if (opt_ipath.empty() && env_ipath.empty() && opt_ppath.empty()) {
usage(argv[0], nullptr, ipath, opt_ppath);
std::cerr << std::endl << "ERROR:" << std::endl;
std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
<< " environment variable to specify the location of the " << std::endl
<< " image path where test images are located." << std::endl
<< " Use -p <personal image path> option if you want to " << std::endl
<< " use personal images." << std::endl
<< std::endl;
std::ostringstream s;
if (opt_ppath.empty()) {
// Set the path location of the image sequence
dirname = vpIoTools::createFilePath(ipath, "mire-2");
// Build the name of the image file
s.setf(std::ios::right, std::ios::adjustfield);
s << "image.%04d.";
s << ext;
filename = vpIoTools::createFilePath(dirname, s.str());
else {
filename = opt_ppath;
// We will read a sequence of images
vpVideoReader grabber;
// Grey level image associated to a display in the initial pose
// computation
// Grey level image to track points
// RGBa image to get background
// Matrix representing camera parameters
// Variables used for pose computation purposes
vpPose mPose;
vpDot2 md[4];
vpImagePoint mcog[4];
vpPoint mP[4];
// CameraParameters we got from calibration
// Keep u0 and v0 as center of the screen
try {
vpCTRACE << "Load: " << filename << std::endl;;
vpCameraParameters mcamTmp(592, 570, grabber.getWidth() / 2, grabber.getHeight() / 2);
// Compute the initial pose of the camera
computeInitialPose(&mcamTmp, Idisplay, &mPose, md, mcog, &cMo, mP, opt_click_allowed);
// Close the framegrabber
// Associate the grabber to the RGBa image;
catch (...) {
std::cerr << std::endl << "ERROR:" << std::endl;
std::cerr << " Cannot read " << filename << std::endl;
std::cerr << " Check your -i " << ipath << " option " << std::endl
<< " or VISP_INPUT_IMAGE_PATH environment variable." << std::endl;
// Create a vpRAOgre object with color background
vpAROgre ogre(mcam, grabber.getWidth(), grabber.getHeight());
// Initialize it
ogre.load("Robot", "robot.mesh");
ogre.setScale("Robot", 0.001f, 0.001f, 0.001f);
ogre.setRotation("Robot", vpRotationMatrix(vpRxyzVector(M_PI / 2, -M_PI / 2, 0)));
// Add an optional point light source
Ogre::Light *light = ogre.getSceneManager()->createLight();
light->setDiffuseColour(1, 1, 1); // scaled RGB values
light->setSpecularColour(1, 1, 1); // scaled RGB values
light->setPosition(-5, -5, 10);
// Rendering loop
while (ogre.continueRendering() && !grabber.end()) {
// Acquire a frame
// Convert it to a grey level image for tracking purpose
// kill the point list
// track the dot
for (int i = 0; i < 4; i++) {
// track the point
md[i].track(I, mcog[i]);
// pixel->meter conversion
double x = 0, y = 0;
// and added to the pose computation point list
// the pose structure has been updated
// the pose is now updated using the virtual visual servoing approach
// Dementhon or lagrange is no longer necessary, pose at the
// previous iteration is sufficient
// Display with ogre
ogre.display(IC, cMo);
// Wait so that the video does not go too fast
// Close the grabber
catch (const vpException &e) {
std::cout << "Catch a ViSP exception: " << e << std::endl;
catch (Ogre::Exception &e) {
std::cout << "Catch an Ogre exception: " << e.getDescription() << std::endl;
catch (...) {
std::cout << "Catch an exception " << std::endl;
int main()
#if (!(defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI)))
std::cout << "You do not have X11, or GTK, or GDI (Graphical Device Interface) functionalities to display images..."
<< std::endl;
std::cout << "Tip if you are on a unix-like system:" << std::endl;
std::cout << "- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
std::cout << "Tip if you are on a windows-like system:" << std::endl;
std::cout << "- Install GDI, configure again ViSP using cmake and build again this example" << std::endl;
std::cout << "You do not have Ogre functionalities" << std::endl;
std::cout << "Tip:" << std::endl;
std::cout << "- Install Ogre3D, configure again ViSP using cmake and build again this example" << std::endl;
Implementation of an augmented reality viewer using Ogre3D 3rd party.
Definition: vpAROgre.h:96
Generic class defining intrinsic camera parameters.
void init()
Basic initialization with the default parameters.
static const vpColor red
Definition: vpColor.h:211
Display for windows using Direct3D 3rd party. Thus to enable this class Direct3D should be installed....
Definition: vpDisplayD3D.h:101
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:128
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:128
static void display(const vpImage< unsigned char > &I)
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
static void flush(const vpImage< unsigned char > &I)
This tracker is meant to track a blob (connex pixels with same gray level) on a vpImage.
Definition: vpDot2.h:124
void track(const vpImage< unsigned char > &I, bool canMakeTheWindowGrow=true)
Definition: vpDot2.cpp:435
void setGraphics(bool activate)
Definition: vpDot2.h:313
void display(const vpImage< unsigned char > &I, vpColor color=vpColor::red, unsigned int thickness=1) const
Definition: vpDot2.cpp:212
void setSizePrecision(const double &sizePrecision)
Definition: vpDot2.cpp:747
void setGrayLevelPrecision(const double &grayLevelPrecision)
Definition: vpDot2.cpp:717
vpImagePoint getCog() const
Definition: vpDot2.h:176
void initTracking(const vpImage< unsigned char > &I, unsigned int size=0)
Definition: vpDot2.cpp:254
error that can be emitted by ViSP classes.
Definition: vpException.h:59
unsigned int getWidth() const
Return the number of columns in the image.
unsigned int getHeight() const
Return the number of rows in the image.
Implementation of an homogeneous matrix and operations on such kind of matrices.
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
void set_j(double jj)
Definition: vpImagePoint.h:305
void set_i(double ii)
Definition: vpImagePoint.h:294
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1834
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:2197
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:77
void set_x(double x)
Set the point x coordinate in the image plane.
Definition: vpPoint.cpp:504
void setWorldCoordinates(double oX, double oY, double oZ)
Definition: vpPoint.cpp:110
void set_y(double y)
Set the point y coordinate in the image plane.
Definition: vpPoint.cpp:506
Class used for pose computation from N points (pose from point only). Some of the algorithms implemen...
Definition: vpPose.h:78
void addPoint(const vpPoint &P)
Definition: vpPose.cpp:93
Definition: vpPose.h:99
Definition: vpPose.h:93
void clearPoint()
Definition: vpPose.cpp:86
static void display(vpImage< unsigned char > &I, vpHomogeneousMatrix &cMo, vpCameraParameters &cam, double size, vpColor col=vpColor::none)
Definition: vpPose.cpp:559
bool computePose(vpPoseMethodType method, vpHomogeneousMatrix &cMo, bool(*func)(const vpHomogeneousMatrix &)=nullptr)
Definition: vpPose.cpp:341
Implementation of a rotation matrix and operations on such kind of matrices.
Implementation of a rotation vector as Euler angle minimal representation.
Definition: vpRxyzVector.h:177
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
void acquire(vpImage< vpRGBa > &I)
void open(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)
void setFirstFrameIndex(const long first_frame)
#define vpCTRACE
Definition: vpDebug.h:331
#define vpERROR_TRACE
Definition: vpDebug.h:384
void display(vpImage< unsigned char > &I, const std::string &title)
Display a gray-scale image.
VISP_EXPORT int wait(double t0, double t)