Visual Servoing Platform  version 3.6.1 under development (2024-11-21)
tutorial-ibvs-4pts-image-tracking.cpp
1 
2 #include <visp3/core/vpConfig.h>
3 #include <visp3/gui/vpDisplayGDI.h>
4 #include <visp3/gui/vpDisplayOpenCV.h>
5 #include <visp3/gui/vpDisplayX.h>
6 #include <visp3/io/vpImageIo.h>
7 #include <visp3/robot/vpImageSimulator.h>
8 #include <visp3/robot/vpSimulatorCamera.h>
9 #include <visp3/visual_features/vpFeatureBuilder.h>
10 #include <visp3/vs/vpServo.h>
11 #include <visp3/vs/vpServoDisplay.h>
12 
13 #ifdef ENABLE_VISP_NAMESPACE
14 using namespace VISP_NAMESPACE_NAME;
15 #endif
16 
17 void display_trajectory(const vpImage<unsigned char> &I, const std::vector<vpDot2> &dot);
18 
25 {
26 public:
32  vpVirtualGrabber(const std::string &filename, const vpCameraParameters &cam) : sim_(), target_(), cam_()
33  {
34  // The target is a square 20cm by 2cm square
35  // Initialise the 3D coordinates of the target corners
36  for (int i = 0; i < 4; i++)
37  X_[i].resize(3);
38  // Top left Top right Bottom right Bottom left
39  X_[0][0] = -0.1;
40  X_[1][0] = 0.1;
41  X_[2][0] = 0.1;
42  X_[3][0] = -0.1;
43  X_[0][1] = -0.1;
44  X_[1][1] = -0.1;
45  X_[2][1] = 0.1;
46  X_[3][1] = 0.1;
47  X_[0][2] = 0;
48  X_[1][2] = 0;
49  X_[2][2] = 0;
50  X_[3][2] = 0;
51 
52  vpImageIo::read(target_, filename);
53 
54  // Initialize the image simulator
55  cam_ = cam;
56  sim_.setInterpolationType(vpImageSimulator::BILINEAR_INTERPOLATION);
57  sim_.init(target_, X_);
58  }
59 
69  {
70  sim_.setCleanPreviousImage(true);
71  sim_.setCameraPosition(cMo);
72  sim_.getImage(I, cam_);
73  }
74 
75 private:
76  vpColVector X_[4]; // 3D coordinates of the target corners
77  vpImageSimulator sim_;
78  vpImage<unsigned char> target_; // image of the target
79  vpCameraParameters cam_;
80 };
81 
82 void display_trajectory(const vpImage<unsigned char> &I, const std::vector<vpDot2> &dot)
83 {
84  static std::vector<vpImagePoint> traj[4];
85  for (unsigned int i = 0; i < 4; i++) {
86  traj[i].push_back(dot[i].getCog());
87  }
88  for (unsigned int i = 0; i < 4; i++) {
89  for (unsigned int j = 1; j < traj[i].size(); j++) {
90  vpDisplay::displayLine(I, traj[i][j - 1], traj[i][j], vpColor::green);
91  }
92  }
93 }
94 
95 int main()
96 {
97 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV)
98  try {
99  vpHomogeneousMatrix cdMo(0, 0, 0.75, 0, 0, 0);
100  vpHomogeneousMatrix cMo(0.15, -0.1, 1., vpMath::rad(10), vpMath::rad(-10), vpMath::rad(50));
101 
102  vpImage<unsigned char> I(480, 640, 255);
103  vpCameraParameters cam(840, 840, I.getWidth() / 2, I.getHeight() / 2);
104 
105  std::vector<vpPoint> point;
106  point.push_back(vpPoint(-0.1, -0.1, 0));
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 
111  vpServo task;
114  task.setLambda(0.5);
115 
116  vpVirtualGrabber g("./target_square.pgm", cam);
117  g.acquire(I, cMo);
118 
119 #if defined(VISP_HAVE_X11)
120  vpDisplayX d(I, 0, 0, "Current camera view");
121 #elif defined(VISP_HAVE_GDI)
122  vpDisplayGDI d(I, 0, 0, "Current camera view");
123 #elif defined(HAVE_OPENCV_HIGHGUI)
124  vpDisplayOpenCV d(I, 0, 0, "Current camera view");
125 #else
126  std::cout << "No image viewer is available..." << std::endl;
127 #endif
128 
130  vpDisplay::displayText(I, 10, 10, "Click in the 4 dots to initialise the tracking and start the servo",
131  vpColor::red);
132  vpDisplay::flush(I);
133 
134  vpFeaturePoint p[4], pd[4];
135  std::vector<vpDot2> dot(4);
136 
137  for (unsigned int i = 0; i < 4; i++) {
138  point[i].track(cdMo);
139  vpFeatureBuilder::create(pd[i], point[i]);
140 
141  dot[i].setGraphics(true);
142  dot[i].initTracking(I);
143  vpDisplay::flush(I);
144  vpFeatureBuilder::create(p[i], cam, dot[i].getCog());
145 
146  task.addFeature(p[i], pd[i]);
147  }
148 
149  vpHomogeneousMatrix wMc, wMo;
150  vpSimulatorCamera robot;
151  robot.setSamplingTime(0.040);
152  robot.getPosition(wMc);
153  wMo = wMc * cMo;
154 
155  for (;;) {
156  robot.getPosition(wMc);
157  cMo = wMc.inverse() * wMo;
158 
159  g.acquire(I, cMo);
160 
162 
163  for (unsigned int i = 0; i < 4; i++) {
164  dot[i].track(I);
165  vpFeatureBuilder::create(p[i], cam, dot[i].getCog());
166 
167  vpColVector cP;
168  point[i].changeFrame(cMo, cP);
169  p[i].set_Z(cP[2]);
170  }
171 
172  vpColVector v = task.computeControlLaw();
173 
174  display_trajectory(I, dot);
177 
178  vpDisplay::flush(I);
179  if (vpDisplay::getClick(I, false))
180  break;
181 
182  vpTime::wait(robot.getSamplingTime() * 1000);
183  }
184  }
185  catch (const vpException &e) {
186  std::cout << "Catch an exception: " << e << std::endl;
187  }
188 #endif
189 }
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: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 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: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.
vpVirtualGrabber(const std::string &filename, const vpCameraParameters &cam)
void acquire(vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo)
VISP_EXPORT int wait(double t0, double t)