ViSP  2.9.0
tutorial-ibvs-4pts-image-tracking.cpp
1 
2 #include <visp/vpDisplayX.h>
3 #include <visp/vpDisplayGDI.h>
4 #include <visp/vpFeatureBuilder.h>
5 #include <visp/vpImageIo.h>
6 #include <visp/vpImageSimulator.h>
7 #include <visp/vpServo.h>
8 #include <visp/vpServoDisplay.h>
9 #include <visp/vpSimulatorCamera.h>
10 
11 void display_trajectory(const vpImage<unsigned char> &I, const std::vector<vpDot2> &dot);
12 
19 {
20 public:
26  vpVirtualGrabber(const std::string &filename, const vpCameraParameters &cam)
27  {
28  // The target is a square 20cm by 2cm square
29  // Initialise the 3D coordinates of the target corners
30  for (int i = 0; i < 4; i++) X_[i].resize(3);
31  // Top left Top right Bottom right Bottom left
32  X_[0][0] = -0.1; X_[1][0] = 0.1; X_[2][0] = 0.1; X_[3][0] = -0.1;
33  X_[0][1] = -0.1; X_[1][1] = -0.1; X_[2][1] = 0.1; X_[3][1] = 0.1;
34  X_[0][2] = 0; X_[1][2] = 0; X_[2][2] = 0; X_[3][2] = 0;
35 
36  vpImageIo::read(target_, filename);
37 
38  // Initialize the image simulator
39  cam_ = cam;
41  sim_.init(target_, X_);
42  }
43 
53  {
54  sim_.setCleanPreviousImage(true);
55  sim_.setCameraPosition(cMo);
56  sim_.getImage(I, cam_);
57  }
58 
59 private:
60  vpColVector X_[4]; // 3D coordinates of the target corners
61  vpImageSimulator sim_;
62  vpImage<unsigned char> target_; // image of the target
63  vpCameraParameters cam_;
64 };
65 
66 
67 void display_trajectory(const vpImage<unsigned char> &I, const std::vector<vpDot2> &dot)
68 {
69  static std::vector<vpImagePoint> traj[4];
70  for (unsigned int i=0; i<4; i++) {
71  traj[i].push_back(dot[i].getCog());
72  }
73  for (unsigned int i=0; i<4; i++) {
74  for (unsigned int j=1; j<traj[i].size(); j++) {
75  vpDisplay::displayLine(I, traj[i][j-1], traj[i][j], vpColor::green);
76  }
77  }
78 }
79 
80 
81 int main()
82 {
83 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI)
84  try {
85  vpHomogeneousMatrix cdMo(0, 0, 0.75, 0, 0, 0);
86  vpHomogeneousMatrix cMo(0.15, -0.1, 1., vpMath::rad(10), vpMath::rad(-10), vpMath::rad(50));
87 
88  vpImage<unsigned char> I(480, 640, 255);
89  vpCameraParameters cam(840, 840, I.getWidth()/2, I.getHeight()/2);
90 
91  std::vector<vpPoint> point(4) ;
92  point[0].setWorldCoordinates(-0.1,-0.1, 0);
93  point[1].setWorldCoordinates( 0.1,-0.1, 0);
94  point[2].setWorldCoordinates( 0.1, 0.1, 0);
95  point[3].setWorldCoordinates(-0.1, 0.1, 0);
96 
97  vpServo task ;
100  task.setLambda(0.5);
101 
102  vpVirtualGrabber g("./target_square.pgm", cam);
103  g.acquire(I, cMo);
104 
105 #if defined(VISP_HAVE_X11)
106  vpDisplayX d(I, 0, 0, "Current camera view");
107 #elif defined(VISP_HAVE_GDI)
108  vpDisplayGDI d(I, 0, 0, "Current camera view");
109 #else
110  std::cout << "No image viewer is available..." << std::endl;
111 #endif
112 
115  "Click in the 4 dots to initialise the tracking and start the servo",
116  vpColor::red);
117  vpDisplay::flush(I);
118 
119  vpFeaturePoint p[4], pd[4];
120  std::vector<vpDot2> dot(4);
121 
122  for (unsigned int i = 0 ; i < 4 ; i++) {
123  point[i].track(cdMo);
124  vpFeatureBuilder::create(pd[i], point[i]);
125 
126  dot[i].setGraphics(true);
127  dot[i].initTracking(I);
128  vpDisplay::flush(I);
129  vpFeatureBuilder::create(p[i], cam, dot[i].getCog());
130 
131  task.addFeature(p[i], pd[i]);
132  }
133 
134  vpHomogeneousMatrix wMc, wMo;
135  vpSimulatorCamera robot;
136  robot.setSamplingTime(0.040);
137  robot.getPosition(wMc);
138  wMo = wMc * cMo;
139 
140  for (; ; ) {
141  robot.getPosition(wMc);
142  cMo = wMc.inverse() * wMo;
143 
144  g.acquire(I, cMo);
145 
147 
148  for (unsigned int i = 0 ; i < 4 ; i++) {
149  dot[i].track(I);
150  vpFeatureBuilder::create(p[i], cam, dot[i].getCog());
151 
152  vpColVector cP;
153  point[i].changeFrame(cMo, cP) ;
154  p[i].set_Z(cP[2]);
155  }
156 
157  vpColVector v = task.computeControlLaw();
158 
159  display_trajectory(I, dot);
162 
163  vpDisplay::flush(I);
164  if (vpDisplay::getClick(I, false))
165  break;
166 
167  vpTime::wait( robot.getSamplingTime() * 1000);
168  }
169  task.kill();
170  }
171  catch(vpException e) {
172  std::cout << "Catch an exception: " << e << std::endl;
173  }
174 #endif
175 }
176 
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:159
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.
void getImage(vpImage< unsigned char > &I, const vpCameraParameters &cam)
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:132
Define the X11 console to display images.
Definition: vpDisplayX.h:152
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: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...
vpVirtualGrabber(const std::string &filename, const vpCameraParameters &cam)
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:1994
static const vpColor red
Definition: vpColor.h:167
virtual void setSamplingTime(const double &delta_t)
void kill()
Definition: vpServo.cpp:189
void setCameraPosition(const vpHomogeneousMatrix &cMt)
vpColVector computeControlLaw()
Definition: vpServo.cpp:902
void setInterpolationType(const vpInterpolationType interplt)
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
Generic class defining intrinsic camera parameters.
void getPosition(vpHomogeneousMatrix &wMc) const
void setLambda(double c)
Definition: vpServo.h:370
Class which enables to project an image in the 3D space and get the view of a virtual camera...
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
void setCleanPreviousImage(const bool &clean, const vpColor &color=vpColor::white)
virtual void displayCharString(const vpImagePoint &ip, const char *text, const vpColor &color=vpColor::green)=0
vpHomogeneousMatrix inverse() const
unsigned int getHeight() const
Definition: vpImage.h:150
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:278
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:220
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)