Visual Servoing Platform  version 3.0.0
tutorial-ibvs-4pts-image-tracking.cpp
1 
2 #include <visp3/gui/vpDisplayX.h>
3 #include <visp3/gui/vpDisplayOpenCV.h>
4 #include <visp3/gui/vpDisplayGDI.h>
5 #include <visp3/visual_features/vpFeatureBuilder.h>
6 #include <visp3/io/vpImageIo.h>
7 #include <visp3/robot/vpImageSimulator.h>
8 #include <visp3/vs/vpServo.h>
9 #include <visp3/vs/vpServoDisplay.h>
10 #include <visp3/robot/vpSimulatorCamera.h>
11 
12 void display_trajectory(const vpImage<unsigned char> &I, const std::vector<vpDot2> &dot);
13 
20 {
21 public:
27  vpVirtualGrabber(const std::string &filename, const vpCameraParameters &cam)
28  : 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++) X_[i].resize(3);
33  // Top left Top right Bottom right Bottom left
34  X_[0][0] = -0.1; X_[1][0] = 0.1; X_[2][0] = 0.1; X_[3][0] = -0.1;
35  X_[0][1] = -0.1; X_[1][1] = -0.1; X_[2][1] = 0.1; X_[3][1] = 0.1;
36  X_[0][2] = 0; X_[1][2] = 0; X_[2][2] = 0; X_[3][2] = 0;
37 
38  vpImageIo::read(target_, filename);
39 
40  // Initialize the image simulator
41  cam_ = cam;
43  sim_.init(target_, X_);
44  }
45 
55  {
56  sim_.setCleanPreviousImage(true);
57  sim_.setCameraPosition(cMo);
58  sim_.getImage(I, cam_);
59  }
60 
61 private:
62  vpColVector X_[4]; // 3D coordinates of the target corners
63  vpImageSimulator sim_;
64  vpImage<unsigned char> target_; // image of the target
65  vpCameraParameters cam_;
66 };
67 
68 
69 void display_trajectory(const vpImage<unsigned char> &I, const std::vector<vpDot2> &dot)
70 {
71  static std::vector<vpImagePoint> traj[4];
72  for (unsigned int i=0; i<4; i++) {
73  traj[i].push_back(dot[i].getCog());
74  }
75  for (unsigned int i=0; i<4; i++) {
76  for (unsigned int j=1; j<traj[i].size(); j++) {
77  vpDisplay::displayLine(I, traj[i][j-1], traj[i][j], vpColor::green);
78  }
79  }
80 }
81 
82 
83 int main()
84 {
85 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV)
86  try {
87  vpHomogeneousMatrix cdMo(0, 0, 0.75, 0, 0, 0);
88  vpHomogeneousMatrix cMo(0.15, -0.1, 1., vpMath::rad(10), vpMath::rad(-10), vpMath::rad(50));
89 
90  vpImage<unsigned char> I(480, 640, 255);
91  vpCameraParameters cam(840, 840, I.getWidth()/2, I.getHeight()/2);
92 
93  std::vector<vpPoint> point;
94  point.push_back( vpPoint(-0.1,-0.1, 0) );
95  point.push_back( vpPoint( 0.1,-0.1, 0) );
96  point.push_back( vpPoint( 0.1, 0.1, 0) );
97  point.push_back( vpPoint(-0.1, 0.1, 0) );
98 
99  vpServo task ;
102  task.setLambda(0.5);
103 
104  vpVirtualGrabber g("./target_square.pgm", cam);
105  g.acquire(I, cMo);
106 
107 #if defined(VISP_HAVE_X11)
108  vpDisplayX d(I, 0, 0, "Current camera view");
109 #elif defined(VISP_HAVE_GDI)
110  vpDisplayGDI d(I, 0, 0, "Current camera view");
111 #elif defined(VISP_HAVE_OPENCV)
112  vpDisplayOpenCV d(I, 0, 0, "Current camera view");
113 #else
114  std::cout << "No image viewer is available..." << std::endl;
115 #endif
116 
118  vpDisplay::displayText(I, 10, 10,
119  "Click in the 4 dots to initialise the tracking and start the servo",
120  vpColor::red);
121  vpDisplay::flush(I);
122 
123  vpFeaturePoint p[4], pd[4];
124  std::vector<vpDot2> dot(4);
125 
126  for (unsigned int i = 0 ; i < 4 ; i++) {
127  point[i].track(cdMo);
128  vpFeatureBuilder::create(pd[i], point[i]);
129 
130  dot[i].setGraphics(true);
131  dot[i].initTracking(I);
132  vpDisplay::flush(I);
133  vpFeatureBuilder::create(p[i], cam, dot[i].getCog());
134 
135  task.addFeature(p[i], pd[i]);
136  }
137 
138  vpHomogeneousMatrix wMc, wMo;
139  vpSimulatorCamera robot;
140  robot.setSamplingTime(0.040);
141  robot.getPosition(wMc);
142  wMo = wMc * cMo;
143 
144  for (; ; ) {
145  robot.getPosition(wMc);
146  cMo = wMc.inverse() * wMo;
147 
148  g.acquire(I, cMo);
149 
151 
152  for (unsigned int i = 0 ; i < 4 ; i++) {
153  dot[i].track(I);
154  vpFeatureBuilder::create(p[i], cam, dot[i].getCog());
155 
156  vpColVector cP;
157  point[i].changeFrame(cMo, cP) ;
158  p[i].set_Z(cP[2]);
159  }
160 
161  vpColVector v = task.computeControlLaw();
162 
163  display_trajectory(I, dot);
166 
167  vpDisplay::flush(I);
168  if (vpDisplay::getClick(I, false))
169  break;
170 
171  vpTime::wait( robot.getSamplingTime() * 1000);
172  }
173  task.kill();
174  }
175  catch(vpException e) {
176  std::cout << "Catch an exception: " << e << std::endl;
177  }
178 #endif
179 }
180 
VISP_EXPORT int wait(double t0, double t)
Definition: vpTime.cpp:150
void init(const vpImage< unsigned char > &I, vpColVector *X)
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
unsigned int getWidth() const
Definition: vpImage.h:161
double getSamplingTime() const
Implementation of an homogeneous matrix and operations on such kind of matrices.
Class that defines the simplest robot: a free flying camera.
void getImage(vpImage< unsigned char > &I, const vpCameraParameters &cam)
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Definition: vpDisplay.cpp:888
Define the X11 console to display images.
Definition: vpDisplayX.h:148
void acquire(vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo)
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, const unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:446
error that can be emited by ViSP classes.
Definition: vpException.h:73
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
vpVirtualGrabber(const std::string &filename, const vpCameraParameters &cam)
static const vpColor green
Definition: vpColor.h:166
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2233
static const vpColor red
Definition: vpColor.h:163
Class that defines what is a point.
Definition: vpPoint.h:59
virtual void setSamplingTime(const double &delta_t)
void kill()
Definition: vpServo.cpp:186
void setCameraPosition(const vpHomogeneousMatrix &cMt)
vpColVector computeControlLaw()
Definition: vpServo.cpp:899
void setInterpolationType(const vpInterpolationType interplt)
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
The vpDisplayOpenCV allows to display image using the opencv library.
Generic class defining intrinsic camera parameters.
void setLambda(double c)
Definition: vpServo.h:390
Class which enables to project an image in the 3D space and get the view of a virtual camera...
vpHomogeneousMatrix getPosition() const
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:519
static double rad(double deg)
Definition: vpMath.h:104
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
void setCleanPreviousImage(const bool &clean, const vpColor &color=vpColor::white)
vpHomogeneousMatrix inverse() const
unsigned int getHeight() const
Definition: vpImage.h:152
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)
static void read(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:274
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:217
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)