Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
tutorial-ibvs-4pts-ogre-tracking.cpp
1 
2 #include <visp3/core/vpConfig.h>
3 #ifdef VISP_HAVE_MODULE_AR
4 #include <visp3/ar/vpAROgre.h>
5 #endif
6 #include <visp3/gui/vpDisplayX.h>
7 #include <visp3/gui/vpDisplayOpenCV.h>
8 #include <visp3/gui/vpDisplayGDI.h>
9 #include <visp3/visual_features/vpFeatureBuilder.h>
10 #include <visp3/vision/vpPose.h>
11 #include <visp3/vs/vpServo.h>
12 #include <visp3/vs/vpServoDisplay.h>
13 #include <visp3/robot/vpSimulatorCamera.h>
14 #include <visp3/blob/vpDot2.h>
15 
16 void display_trajectory(const vpImage<unsigned char> &I, const std::vector<vpDot2> &dot, unsigned int thickness);
17 #if defined(VISP_HAVE_OGRE)
18 void ogre_get_render_image(vpAROgre &ogre, const vpImage<unsigned char> &background,
20 #endif
21 
22 void display_trajectory(const vpImage<unsigned char> &I, const std::vector<vpDot2> &dot, unsigned int thickness)
23 {
24  static std::vector<vpImagePoint> traj[4];
25  for (unsigned int i=0; i<4; i++) {
26  traj[i].push_back(dot[i].getCog());
27  }
28  for (unsigned int i=0; i<4; i++) {
29  for (unsigned int j=1; j<traj[i].size(); j++) {
30  vpDisplay::displayLine(I, traj[i][j-1], traj[i][j], vpColor::green, thickness);
31  }
32  }
33 }
34 
35 #if defined(VISP_HAVE_OGRE)
36 void ogre_get_render_image(vpAROgre &ogre, const vpImage<unsigned char> &background,
38 {
39  static vpImage<vpRGBa> Irender; // Image from ogre scene rendering
40  ogre.display(background, cMo);
41  ogre.getRenderingOutput(Irender, cMo);
42 
43  vpImageConvert::convert(Irender, I);
44  // Due to the light that was added to the scene, we need to threshold the image
45  vpImageTools::binarise(I, (unsigned char)254, (unsigned char)255, (unsigned char)0, (unsigned char)255, (unsigned char)255);
46 }
47 #endif
48 
49 int main()
50 {
51 #if defined(VISP_HAVE_OGRE) && (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
52  try {
53  unsigned int thickness = 3;
54 
55  vpHomogeneousMatrix cdMo(0, 0, 0.75, 0, 0, 0);
56  vpHomogeneousMatrix cMo(0.15, -0.1, 1., vpMath::rad(10), vpMath::rad(-10), vpMath::rad(50));
57 
58  // Color image used as background texture.
59  vpImage<unsigned char> background(480, 640, 255);
60 
61  // Parameters of our camera
62  vpCameraParameters cam(840, 840, background.getWidth()/2, background.getHeight()/2);
63 
64  // Define the target as 4 points
65  std::vector<vpPoint> point;
66  point.push_back( vpPoint(-0.1,-0.1, 0) );
67  point.push_back( vpPoint( 0.1,-0.1, 0) );
68  point.push_back( vpPoint( 0.1, 0.1, 0) );
69  point.push_back( vpPoint(-0.1, 0.1, 0) );
70 
71  // Our object
72  // A simulator with the camera parameters defined above,
73  // and the background image size
74  vpAROgre ogre;
75  ogre.setCameraParameters(cam);
76  ogre.setShowConfigDialog(false);
77  ogre.addResource("./"); // Add the path to the Sphere.mesh resource
78  ogre.init(background, false, true);
79  //ogre.setWindowPosition(680, 400);
80 
81  // Create the scene that contains 4 spheres
82  // Sphere.mesh contains a sphere with 1 meter radius
83  std::vector<std::string> name(4);
84  for (unsigned int i=0; i<4; i++) {
85  std::ostringstream s; s << "Sphere" << i; name[i] = s.str();
86  ogre.load(name[i], "Sphere.mesh");
87  ogre.setScale(name[i], 0.02f, 0.02f, 0.02f); // Rescale the sphere to 2 cm radius
88  // Set the position of each sphere in the object frame
89  ogre.setPosition(name[i], vpTranslationVector(point[i].get_oX(), point[i].get_oY(), point[i].get_oZ()));
90  ogre.setRotation(name[i], vpRotationMatrix(M_PI/2, 0, 0));
91  }
92 
93  // Add an optional point light source
94  Ogre::Light * light = ogre.getSceneManager()->createLight();
95  light->setDiffuseColour(1, 1, 1); // scaled RGB values
96  light->setSpecularColour(1, 1, 1); // scaled RGB values
97  light->setPosition((Ogre::Real)cdMo[0][3], (Ogre::Real)cdMo[1][3], (Ogre::Real)(-cdMo[2][3]));
98  light->setType(Ogre::Light::LT_POINT);
99 
100  vpServo task ;
103  task.setLambda(0.5);
104 
105  // Image used for the image processing
107 
108  // Render the scene at the desired position
109  ogre_get_render_image(ogre, background, cdMo, I);
110 
111  // Display the image in which we will do the tracking
112 #if defined(VISP_HAVE_X11)
113  vpDisplayX d(I, 0, 0, "Camera view at desired position");
114 #elif defined(VISP_HAVE_GDI)
115  vpDisplayGDI d(I, 0, 0, "Camera view at desired position");
116 #elif defined(VISP_HAVE_OPENCV)
117  vpDisplayOpenCV d(I, 0, 0, "Camera view at desired position");
118 #else
119  std::cout << "No image viewer is available..." << std::endl;
120 #endif
121 
123  vpDisplay::displayText(I, 10, 10, "Click in the 4 dots to learn their positions", vpColor::red);
124  vpDisplay::flush(I);
125 
126  std::vector<vpDot2> dot(4);
127  vpFeaturePoint p[4], pd[4];
128 
129  for (unsigned int i = 0 ; i < 4 ; i++) {
130  // Compute the desired feature at the desired position
131  dot[i].setGraphics(true);
132  dot[i].setGraphicsThickness(thickness);
133  dot[i].initTracking(I);
134  vpDisplay::flush(I);
135  vpFeatureBuilder::create(pd[i], cam, dot[i].getCog());
136  }
137 
138  // Render the scene at the initial position
139  ogre_get_render_image(ogre, background, cMo, I);
140 
142  vpDisplay::setTitle(I, "Current camera view");
143  vpDisplay::displayText(I, 10, 10, "Click in the 4 dots to initialise the tracking and start the servo", vpColor::red);
144  vpDisplay::flush(I);
145 
146  for (unsigned int i = 0 ; i < 4 ; i++) {
147  // We notice that if we project the scene at a given pose, the pose estimated from
148  // the rendered image differs a little. That's why we cannot simply compute the desired
149  // feature from the desired pose using the next two lines. We will rather compute the
150  // desired position of the features from a learning stage.
151  // point[i].project(cdMo);
152  // vpFeatureBuilder::create(pd[i], point[i]);
153 
154  // Compute the current feature at the initial position
155  dot[i].setGraphics(true);
156  dot[i].initTracking(I);
157  vpDisplay::flush(I);
158  vpFeatureBuilder::create(p[i], cam, dot[i].getCog());
159  }
160 
161  for (unsigned int i = 0 ; i < 4 ; i++) {
162  // Set the feature Z coordinate from the pose
163  vpColVector cP;
164  point[i].changeFrame(cMo, cP) ;
165  p[i].set_Z(cP[2]);
166 
167  task.addFeature(p[i], pd[i]);
168  }
169 
170  vpHomogeneousMatrix wMc, wMo;
171  vpSimulatorCamera robot;
172  robot.setSamplingTime(0.040);
173  robot.getPosition(wMc);
174  wMo = wMc * cMo;
175 
176  for (; ; ) {
177  // From the camera position in the world frame we retrieve the object position
178  robot.getPosition(wMc);
179  cMo = wMc.inverse() * wMo;
180 
181  // Update the scene from the new camera position
182  ogre_get_render_image(ogre, background, cMo, I);
183 
185 
186  for (unsigned int i = 0 ; i < 4 ; i++) {
187  dot[i].track(I);
188  vpFeatureBuilder::create(p[i], cam, dot[i].getCog());
189  }
190 
191  for (unsigned int i = 0 ; i < 4 ; i++) {
192  // Set the feature Z coordinate from the pose
193  vpColVector cP;
194  point[i].changeFrame(cMo, cP) ;
195  p[i].set_Z(cP[2]);
196  }
197 
198  vpColVector v = task.computeControlLaw();
199 
200  display_trajectory(I, dot, thickness);
201  vpServoDisplay::display(task, cam, I, vpColor::green, vpColor::red, thickness+2) ;
203 
204  vpDisplay::flush(I);
205  if (vpDisplay::getClick(I, false))
206  break;
207 
208  vpTime::wait( robot.getSamplingTime() * 1000);
209  }
210  task.kill();
211  }
212  catch(vpException &e) {
213  std::cout << "Catch a ViSP exception: " << e << std::endl;
214  }
215  catch(...) {
216  std::cout << "Catch an exception " << std::endl;
217  return 1;
218  }
219 #endif
220 }
221 
static void binarise(vpImage< Type > &I, Type threshold1, Type threshold2, Type value1, Type value2, Type value3, const bool useLUT=true)
Definition: vpImageTools.h:423
void setRotation(const std::string &sceneName, const vpRotationMatrix &wRo)
Definition: vpAROgre.cpp:709
VISP_EXPORT int wait(double t0, double t)
Definition: vpTime.cpp:157
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
unsigned int getWidth() const
Definition: vpImage.h:226
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
double getSamplingTime() const
Implementation of an homogeneous matrix and operations on such kind of matrices.
Class that defines the simplest robot: a free flying camera.
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
void setShowConfigDialog(const bool showConfigDialog)
Definition: vpAROgre.h:253
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:153
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, const unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:512
error that can be emited by ViSP classes.
Definition: vpException.h:73
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
Implementation of an augmented reality viewer using Ogre3D 3rd party.
Definition: vpAROgre.h:89
static const vpColor green
Definition: vpColor.h:166
static void flush(const vpImage< unsigned char > &I)
Ogre::SceneManager * getSceneManager()
Definition: vpAROgre.h:161
virtual void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMw)
Definition: vpAROgre.cpp:622
static const vpColor red
Definition: vpColor.h:163
Class that defines what is a point.
Definition: vpPoint.h:59
Implementation of a rotation matrix and operations on such kind of matrices.
virtual void setSamplingTime(const double &delta_t)
void kill()
Definition: vpServo.cpp:191
vpColVector computeControlLaw()
Definition: vpServo.cpp:954
virtual void init(vpImage< unsigned char > &I, bool bufferedKeys=false, bool hidden=false)
Definition: vpAROgre.cpp:115
static void display(const vpImage< unsigned char > &I)
void setCameraParameters(const vpCameraParameters &cameraP)
Definition: vpAROgre.cpp:662
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
void setScale(const std::string &sceneName, const float factorx, const float factory, const float factorz)
Definition: vpAROgre.cpp:775
Generic class defining intrinsic camera parameters.
void setLambda(double c)
Definition: vpServo.h:391
void getRenderingOutput(vpImage< vpRGBa > &I, const vpHomogeneousMatrix &cMo)
Definition: vpAROgre.cpp:1061
vpHomogeneousMatrix getPosition() const
void load(const std::string &entityName, const std::string &model)
Definition: vpAROgre.cpp:672
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:585
static double rad(double deg)
Definition: vpMath.h:104
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
void setPosition(const std::string &sceneName, const vpTranslationVector &wTo)
Definition: vpAROgre.cpp:685
vpHomogeneousMatrix inverse() const
void addResource(const std::string &resourceLocation)
Definition: vpAROgre.h:123
unsigned int getHeight() const
Definition: vpImage.h:175
void set_Z(const double Z)
static void displayLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
static void setTitle(const vpImage< unsigned char > &I, const std::string &windowtitle)
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:222
Class that consider the case of a translation vector.
static void display(const vpServo &s, const vpCameraParameters &cam, const vpImage< unsigned char > &I, vpColor currentColor=vpColor::green, vpColor desiredColor=vpColor::red, unsigned int thickness=1)