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