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