Visual Servoing Platform  version 3.6.1 under development (2025-02-18)
tutorial-ibvs-4pts-image-tracking.cpp
1 
2 #include <visp3/core/vpConfig.h>
3 #include <visp3/gui/vpDisplayFactory.h>
4 #include <visp3/io/vpImageIo.h>
5 #include <visp3/robot/vpImageSimulator.h>
6 #include <visp3/robot/vpSimulatorCamera.h>
7 #include <visp3/visual_features/vpFeatureBuilder.h>
8 #include <visp3/vs/vpServo.h>
9 #include <visp3/vs/vpServoDisplay.h>
10 
11 #ifdef ENABLE_VISP_NAMESPACE
12 using namespace VISP_NAMESPACE_NAME;
13 #endif
14 
21 {
22 public:
28  vpVirtualGrabber(const std::string &filename, const vpCameraParameters &cam) : sim_(), target_(), cam_()
29  {
30  // The target is a square 20cm by 2cm square
31  // Initialise the 3D coordinates of the target corners
32  for (int i = 0; i < 4; i++)
33  X_[i].resize(3);
34  // Top left Top right Bottom right Bottom left
35  X_[0][0] = -0.1;
36  X_[1][0] = 0.1;
37  X_[2][0] = 0.1;
38  X_[3][0] = -0.1;
39  X_[0][1] = -0.1;
40  X_[1][1] = -0.1;
41  X_[2][1] = 0.1;
42  X_[3][1] = 0.1;
43  X_[0][2] = 0;
44  X_[1][2] = 0;
45  X_[2][2] = 0;
46  X_[3][2] = 0;
47 
48  vpImageIo::read(target_, filename);
49 
50  // Initialize the image simulator
51  cam_ = cam;
52  sim_.setInterpolationType(vpImageSimulator::BILINEAR_INTERPOLATION);
53  sim_.init(target_, X_);
54  }
55 
65  {
66  sim_.setCleanPreviousImage(true);
67  sim_.setCameraPosition(cMo);
68  sim_.getImage(I, cam_);
69  }
70 
71 private:
72  vpColVector X_[4]; // 3D coordinates of the target corners
73  vpImageSimulator sim_;
74  vpImage<unsigned char> target_; // image of the target
75  vpCameraParameters cam_;
76 };
77 
78 void display_trajectory(const vpImage<unsigned char> &I, const std::vector<vpDot2> &dot)
79 {
80  static std::vector<vpImagePoint> traj[4];
81  for (unsigned int i = 0; i < 4; i++) {
82  traj[i].push_back(dot[i].getCog());
83  }
84  for (unsigned int i = 0; i < 4; i++) {
85  for (unsigned int j = 1; j < traj[i].size(); j++) {
86  vpDisplay::displayLine(I, traj[i][j - 1], traj[i][j], vpColor::green);
87  }
88  }
89 }
90 
91 int main()
92 {
93 #if defined(VISP_HAVE_DISPLAY)
94 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
95  std::shared_ptr<vpDisplay> display;
96 #else
97  vpDisplay *display = nullptr;
98 #endif
99  try {
100  vpHomogeneousMatrix cdMo(0, 0, 0.75, 0, 0, 0);
101  vpHomogeneousMatrix cMo(0.15, -0.1, 1., vpMath::rad(10), vpMath::rad(-10), vpMath::rad(50));
102 
103  vpImage<unsigned char> I(480, 640, 255);
104  vpCameraParameters cam(840, 840, I.getWidth() / 2, I.getHeight() / 2);
105 
106  std::vector<vpPoint> point;
107  point.push_back(vpPoint(-0.1, -0.1, 0));
108  point.push_back(vpPoint(0.1, -0.1, 0));
109  point.push_back(vpPoint(0.1, 0.1, 0));
110  point.push_back(vpPoint(-0.1, 0.1, 0));
111 
112  vpServo task;
115  task.setLambda(0.5);
116 
117  vpVirtualGrabber g("./target_square.pgm", cam);
118  g.acquire(I, cMo);
119 
120 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
121  display = vpDisplayFactory::createDisplay(I, 0, 0, "Current camera view");
122 #else
123  display = vpDisplayFactory::allocateDisplay(I, 0, 0, "Current camera view");
124 #endif
125 
127  vpDisplay::displayText(I, 10, 10, "Click in the 4 dots to initialise the tracking and start the servo",
128  vpColor::red);
129  vpDisplay::flush(I);
130 
131  vpFeaturePoint p[4], pd[4];
132  std::vector<vpDot2> dot(4);
133 
134  for (unsigned int i = 0; i < 4; i++) {
135  point[i].track(cdMo);
136  vpFeatureBuilder::create(pd[i], point[i]);
137 
138  dot[i].setGraphics(true);
139  dot[i].initTracking(I);
140  vpDisplay::flush(I);
141  vpFeatureBuilder::create(p[i], cam, dot[i].getCog());
142 
143  task.addFeature(p[i], pd[i]);
144  }
145 
146  vpHomogeneousMatrix wMc, wMo;
147  vpSimulatorCamera robot;
148  robot.setSamplingTime(0.040);
149  robot.getPosition(wMc);
150  wMo = wMc * cMo;
151 
152  for (;;) {
153  robot.getPosition(wMc);
154  cMo = wMc.inverse() * wMo;
155 
156  g.acquire(I, cMo);
157 
159 
160  for (unsigned int i = 0; i < 4; i++) {
161  dot[i].track(I);
162  vpFeatureBuilder::create(p[i], cam, dot[i].getCog());
163 
164  vpColVector cP;
165  point[i].changeFrame(cMo, cP);
166  p[i].set_Z(cP[2]);
167  }
168 
169  vpColVector v = task.computeControlLaw();
170 
171  display_trajectory(I, dot);
174 
175  vpDisplay::flush(I);
176  if (vpDisplay::getClick(I, false))
177  break;
178 
179  vpTime::wait(robot.getSamplingTime() * 1000);
180  }
181  }
182  catch (const vpException &e) {
183  std::cout << "Catch an exception: " << e << std::endl;
184  }
185 #if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
186  if (display != nullptr) {
187  delete display;
188  }
189 #endif
190 #endif
191 }
Generic class defining intrinsic camera parameters.
void init()
Basic initialization with the default parameters.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
static const vpColor red
Definition: vpColor.h:198
static const vpColor green
Definition: vpColor.h:201
Class that defines generic functionalities for display.
Definition: vpDisplay.h:178
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 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 read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:147
Class which enables to project an image in the 3D space and get the view of a virtual camera.
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
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:991
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.
vpVirtualGrabber(const std::string &filename, const vpCameraParameters &cam)
void acquire(vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo)
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.
VISP_EXPORT int wait(double t0, double t)