ViSP  2.10.0
tutorial-ibvs-4pts-ogre-tracking.cpp
1 
2 #include <visp/vpDisplayX.h>
3 #include <visp/vpDisplayOpenCV.h>
4 #include <visp/vpDisplayGDI.h>
5 #include <visp/vpAROgre.h>
6 #include <visp/vpFeatureBuilder.h>
7 #include <visp/vpPose.h>
8 #include <visp/vpServo.h>
9 #include <visp/vpServoDisplay.h>
10 #include <visp/vpSimulatorCamera.h>
11 
12 void display_trajectory(const vpImage<unsigned char> &I, const std::vector<vpDot2> &dot, unsigned int thickness);
13 #if defined(VISP_HAVE_OGRE)
14 void ogre_get_render_image(vpAROgre &ogre, const vpImage<unsigned char> &background,
16 #endif
17 
18 void display_trajectory(const vpImage<unsigned char> &I, const std::vector<vpDot2> &dot, unsigned int thickness)
19 {
20  static std::vector<vpImagePoint> traj[4];
21  for (unsigned int i=0; i<4; i++) {
22  traj[i].push_back(dot[i].getCog());
23  }
24  for (unsigned int i=0; i<4; i++) {
25  for (unsigned int j=1; j<traj[i].size(); j++) {
26  vpDisplay::displayLine(I, traj[i][j-1], traj[i][j], vpColor::green, thickness);
27  }
28  }
29 }
30 
31 #if defined(VISP_HAVE_OGRE)
32 void ogre_get_render_image(vpAROgre &ogre, const vpImage<unsigned char> &background,
34 {
35  static vpImage<vpRGBa> Irender; // Image from ogre scene rendering
36  ogre.display(background, cMo);
37  ogre.getRenderingOutput(Irender, cMo);
38 
39  vpImageConvert::convert(Irender, I);
40  // Due to the light that was added to the scene, we need to threshold the image
41  vpImageTools::binarise(I, (unsigned char)254, (unsigned char)255, (unsigned char)0, (unsigned char)255, (unsigned char)255);
42 }
43 #endif
44 
45 int main()
46 {
47 #if defined(VISP_HAVE_OGRE) && (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
48  try {
49  unsigned int thickness = 3;
50 
51  vpHomogeneousMatrix cdMo(0, 0, 0.75, 0, 0, 0);
52  vpHomogeneousMatrix cMo(0.15, -0.1, 1., vpMath::rad(10), vpMath::rad(-10), vpMath::rad(50));
53 
54  // Color image used as background texture.
55  vpImage<unsigned char> background(480, 640, 255);
56 
57  // Parameters of our camera
58  vpCameraParameters cam(840, 840, background.getWidth()/2, background.getHeight()/2);
59 
60  // Define the target as 4 points
61  std::vector<vpPoint> point(4) ;
62  point[0].setWorldCoordinates(-0.1,-0.1, 0);
63  point[1].setWorldCoordinates( 0.1,-0.1, 0);
64  point[2].setWorldCoordinates( 0.1, 0.1, 0);
65  point[3].setWorldCoordinates(-0.1, 0.1, 0);
66 
67  // Our object
68  // A simulator with the camera parameters defined above,
69  // and the background image size
70  vpAROgre ogre;
71  ogre.setCameraParameters(cam);
72  ogre.setShowConfigDialog(false);
73  ogre.addResource("./"); // Add the path to the Sphere.mesh resource
74  ogre.init(background, false, true);
75  //ogre.setWindowPosition(680, 400);
76 
77  // Create the scene that contains 4 spheres
78  // Sphere.mesh contains a sphere with 1 meter radius
79  std::vector<std::string> name(4);
80  for (int i=0; i<4; i++) {
81  std::ostringstream s; s << "Sphere" << i; name[i] = s.str();
82  ogre.load(name[i], "Sphere.mesh");
83  ogre.setScale(name[i], 0.02f, 0.02f, 0.02f); // Rescale the sphere to 2 cm radius
84  // Set the position of each sphere in the object frame
85  ogre.setPosition(name[i], vpTranslationVector(point[i].get_oX(), point[i].get_oY(), point[i].get_oZ()));
86  ogre.setRotation(name[i], vpRotationMatrix(M_PI/2, 0, 0));
87  }
88 
89  // Add an optional point light source
90  Ogre::Light * light = ogre.getSceneManager()->createLight();
91  light->setDiffuseColour(1, 1, 1); // scaled RGB values
92  light->setSpecularColour(1, 1, 1); // scaled RGB values
93  light->setPosition((Ogre::Real)cdMo[0][3], (Ogre::Real)cdMo[1][3], (Ogre::Real)(-cdMo[2][3]));
94  light->setType(Ogre::Light::LT_POINT);
95 
96  vpServo task ;
99  task.setLambda(0.5);
100 
101  // Image used for the image processing
103 
104  // Render the scene at the desired position
105  ogre_get_render_image(ogre, background, cdMo, I);
106 
107  // Display the image in which we will do the tracking
108 #if defined(VISP_HAVE_X11)
109  vpDisplayX d(I, 0, 0, "Camera view at desired position");
110 #elif defined(VISP_HAVE_GDI)
111  vpDisplayGDI d(I, 0, 0, "Camera view at desired position");
112 #elif defined(VISP_HAVE_OPENCV)
113  vpDisplayOpenCV d(I, 0, 0, "Camera view at desired position");
114 #else
115  std::cout << "No image viewer is available..." << std::endl;
116 #endif
117 
119  vpDisplay::displayText(I, 10, 10, "Click in the 4 dots to learn their positions", vpColor::red);
120  vpDisplay::flush(I);
121 
122  std::vector<vpDot2> dot(4);
123  vpFeaturePoint p[4], pd[4];
124 
125  for (int i = 0 ; i < 4 ; i++) {
126  // Compute the desired feature at the desired position
127  dot[i].setGraphics(true);
128  dot[i].setGraphicsThickness(thickness);
129  dot[i].initTracking(I);
130  vpDisplay::flush(I);
131  vpFeatureBuilder::create(pd[i], cam, dot[i].getCog());
132  }
133 
134  // Render the scene at the initial position
135  ogre_get_render_image(ogre, background, cMo, I);
136 
138  vpDisplay::setTitle(I, "Current camera view");
139  vpDisplay::displayText(I, 10, 10, "Click in the 4 dots to initialise the tracking and start the servo", vpColor::red);
140  vpDisplay::flush(I);
141 
142  for (int i = 0 ; i < 4 ; i++) {
143  // We notice that if we project the scene at a given pose, the pose estimated from
144  // the rendered image differs a little. That's why we cannot simply compute the desired
145  // feature from the desired pose using the next two lines. We will rather compute the
146  // desired position of the features from a learning stage.
147  // point[i].project(cdMo);
148  // vpFeatureBuilder::create(pd[i], point[i]);
149 
150  // Compute the current feature at the initial position
151  dot[i].setGraphics(true);
152  dot[i].initTracking(I);
153  vpDisplay::flush(I);
154  vpFeatureBuilder::create(p[i], cam, dot[i].getCog());
155  }
156 
157  for (int i = 0 ; i < 4 ; i++) {
158  // Set the feature Z coordinate from the pose
159  vpColVector cP;
160  point[i].changeFrame(cMo, cP) ;
161  p[i].set_Z(cP[2]);
162 
163  task.addFeature(p[i], pd[i]);
164  }
165 
166  vpHomogeneousMatrix wMc, wMo;
167  vpSimulatorCamera robot;
168  robot.setSamplingTime(0.040);
169  robot.getPosition(wMc);
170  wMo = wMc * cMo;
171 
172  for (; ; ) {
173  // From the camera position in the world frame we retrieve the object position
174  robot.getPosition(wMc);
175  cMo = wMc.inverse() * wMo;
176 
177  // Update the scene from the new camera position
178  ogre_get_render_image(ogre, background, cMo, I);
179 
181 
182  for (int i = 0 ; i < 4 ; i++) {
183  dot[i].track(I);
184  vpFeatureBuilder::create(p[i], cam, dot[i].getCog());
185  }
186 
187  for (int i = 0 ; i < 4 ; i++) {
188  // Set the feature Z coordinate from the pose
189  vpColVector cP;
190  point[i].changeFrame(cMo, cP) ;
191  p[i].set_Z(cP[2]);
192  }
193 
194  vpColVector v = task.computeControlLaw();
195 
196  display_trajectory(I, dot, thickness);
197  vpServoDisplay::display(task, cam, I, vpColor::green, vpColor::red, thickness+2) ;
199 
200  vpDisplay::flush(I);
201  if (vpDisplay::getClick(I, false))
202  break;
203 
204  vpTime::wait( robot.getSamplingTime() * 1000);
205  }
206  task.kill();
207  }
208  catch(vpException e) {
209  std::cout << "Catch a ViSP exception: " << e << std::endl;
210  }
211  catch(...) {
212  std::cout << "Catch an exception " << std::endl;
213  return 1;
214  }
215 #endif
216 }
217 
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
void setRotation(const std::string &name, const vpRotationMatrix &wRo)
Definition: vpAROgre.cpp:675
unsigned int getWidth() const
Definition: vpImage.h:161
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:255
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Definition: vpDisplay.cpp:887
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:2232
Ogre::SceneManager * getSceneManager()
Definition: vpAROgre.h:162
virtual void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMw)
Definition: vpAROgre.cpp:588
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:119
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:210
void setCameraParameters(const vpCameraParameters &cameraP)
Definition: vpAROgre.cpp:628
The vpDisplayOpenCV allows to display image using the opencv library.
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:1031
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
vpHomogeneousMatrix inverse() const
void setScale(const std::string &name, const float factorx, const float factory, const float factorz)
Definition: vpAROgre.cpp:745
void addResource(const std::string &resourceLocation)
Definition: vpAROgre.h:124
unsigned int getHeight() const
Definition: vpImage.h:152
void setPosition(const std::string &name, const vpTranslationVector &wTo)
Definition: vpAROgre.cpp:651
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:638
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)