AUV-Coop-Assembly
Master Thesis for Robotics Engineering. Cooperative peg-in-hole assembly with two underwater vehicles guided by vision
tracker.cpp
1 #include "header/tracker.h"
2 
3 MonoTracker::MonoTracker(std::string callerName, std::string cameraName,
4  int trackerType){
5 
6  this->callerName = callerName;
7  this->cameraName = cameraName;
8 
9  // path of source to find config files
10  boost::filesystem::path path(__FILE__);
11  path.remove_filename();
12  sourcePath = path.string();
13 
14 
15  tracker.setTrackerType(trackerType);
16 
17  tracker.loadConfigFile(sourcePath + configFile + cameraName + ".xml");
18 
19  //WARNING: Cannot set pose when model contains cylinder(s). This feature is not implemented yet.
20  tracker.loadModel(sourcePath + caoModelNoCil);
21  tracker.setOgreVisibilityTest(false);
22  tracker.setDisplayFeatures(true);
23 
24  // Tells if the tracker has to compute the projection error
25  tracker.setProjectionErrorComputation(true);
26 
27  std::cout << "[" << callerName << "][MONOTRACKER " << cameraName << "]"
28  << " created\n";
29 
30 }
31 
32 MonoTracker::~MonoTracker(){
33  //To clean up memory allocated by the xml library,
34  //the user has to call this before the exit().
35  vpXmlParser::cleanup();
36 }
37 
38 
39 int MonoTracker::initTrackingByClick(vpImage<unsigned char> *I){
40 
41  tracker.initClick(*I, sourcePath+initFileClick+cameraName + ".init", true);
42 
43  MonoTracker::monoTrackInit_priv(I);
44  keypoint_detection.loadConfigFile(sourcePath+configFileDetector);
45  keypoint_detection.loadLearningData(sourcePath+learnData+cameraName+".bin", true);
46 
47  std::cout << "[" << callerName << "][MONOTRACKER " << cameraName << "]"
48  << " init by Click done\n";
49 
50 }
51 
52 int MonoTracker::initTrackingByPoint(vpImage<unsigned char> *I){
53 
54  tracker.initFromPoints(*I, sourcePath+initFile_w2D+cameraName+".init");
55 
56  MonoTracker::monoTrackInit_priv(I);
57  keypoint_detection.loadConfigFile(sourcePath+configFileDetector);
58  keypoint_detection.loadLearningData(sourcePath+learnData+cameraName+".bin", true);
59 
60  std::cout << "[" << callerName << "][MONOTRACKER " << cameraName << "]"
61  << " init by Point done\n";
62 
63 }
64 
65 int MonoTracker::monoTrackInit_priv(vpImage<unsigned char> *I){
66 
67  try {
68 
69  vpCameraParameters cam;
70  vpHomogeneousMatrix cMo;
71 
72 // vpDisplayOpenCV display;
73 
74 // display.init(*I, 300, 300, "Mono tracker "+cameraName);
75 
76 
77  tracker.getCameraParameters(cam);
78 
79  tracker.track(*I);
80 
81  vpKeyPoint keypoint_learning;
82  keypoint_learning.loadConfigFile(sourcePath+configFileDetector);
83 
84  std::vector<cv::KeyPoint> trainKeyPoints;
85  double elapsedTime;
86  keypoint_learning.detect(*I, trainKeyPoints, elapsedTime);
87  // display found points
88  vpDisplay::display(*I);
89  for (std::vector<cv::KeyPoint>::const_iterator it = trainKeyPoints.begin(); it != trainKeyPoints.end(); ++it) {
90  vpDisplay::displayCross(*I, (int)it->pt.y, (int)it->pt.x, 4, vpColor::red);
91  }
92  vpDisplay::displayText(*I, 10, 10, "All Keypoints...", vpColor::red);
93  vpDisplay::displayText(*I, 30, 10, "Click to continue with detection...", vpColor::red);
94  vpDisplay::flush(*I);
95  vpDisplay::getClick(*I, true);
96 
97  std::vector<vpPolygon> polygons;
98  std::vector<std::vector<vpPoint> > roisPt;
99  std::pair<std::vector<vpPolygon>, std::vector<std::vector<vpPoint> > > pair
100  = tracker.getPolygonFaces(false);
101  polygons = pair.first;
102  roisPt = pair.second;
103  std::vector<cv::Point3f> points3f;
104  tracker.getPose(cMo);
105  vpKeyPoint::compute3DForPointsInPolygons(cMo, cam, trainKeyPoints,
106  polygons, roisPt, points3f);
107  keypoint_learning.buildReference(*I, trainKeyPoints, points3f);
108  keypoint_learning.saveLearningData(sourcePath+learnData+cameraName+".bin", true);
109 
110  // display found points
111  vpDisplay::display(*I);
112  for (std::vector<cv::KeyPoint>::const_iterator it = trainKeyPoints.begin(); it != trainKeyPoints.end(); ++it) {
113  vpDisplay::displayCross(*I, (int)it->pt.y, (int)it->pt.x, 4, vpColor::red);
114  }
115  vpDisplay::displayText(*I, 10, 10, "Keypoints only on block...", vpColor::red);
116  vpDisplay::displayText(*I, 30, 10, "Click to continue with detection...", vpColor::red);
117  vpDisplay::flush(*I);
118  vpDisplay::getClick(*I, true);
119 
120 
121  } catch (vpException &e) {
122  std::cout << "[" << callerName << "][MONOTRACKER " << cameraName << "]"
123  << "Init_priv exception: " << e << std::endl;
124  }
125 
126  return 0;
127 }
128 
129 
130 int MonoTracker::monoTrack(vpImage<unsigned char> *I,
131  vpHomogeneousMatrix *cMo,
132  double *error, double* elapsedTime){
133 
134  try {
135 
136  vpCameraParameters cam;
137  tracker.getCameraParameters(cam);
138 
139  if (keypoint_detection.matchPoint(*I, cam, *cMo, *error, *elapsedTime)) {
140  tracker.setPose(*I, *cMo);
141 
142  std::cout << "keypoint erro: " << *error << std::endl;
143  std::cout << "projectERRO: " << tracker.getProjectionError() << "\n";
144  std::cout << "Residual: " << sqrt( (tracker.getError()).sumSquare()) << std::endl;
145  std::cout << "Residual normalized: "
146  << sqrt( (tracker.getError()).sumSquare())/tracker.getError().size() << std::endl;
147  }
148 
149 
150 
151  } catch (vpException &e) {
152  std::cout << "[" << callerName << "][MONOTRACKER " << cameraName << "]"
153  << "tracking exception: " << e << std::endl;
154  }
155 
156  return 0;
157 
158 }
159 
160 void MonoTracker::getCameraParams(vpCameraParameters *param){
161  tracker.getCameraParameters(*param);
162 }
163 
164 void MonoTracker::display(vpImage<unsigned char> *I){
165 
166  vpHomogeneousMatrix cThole;
167  tracker.getPose(cThole);
168 
169  vpCameraParameters cam;
170  tracker.getCameraParameters(cam);
171 
172  tracker.display(*I, cThole, cam, vpColor::red, 2 , true);
173 }
174 
175 /* ************************************************************************************************************/
176 
177 
178 StereoTracker::StereoTracker(std::string callerName, std::vector<std::string> cameraNames,
179  std::map<std::string, vpHomogeneousMatrix> mapCameraTransf,
180  std::vector<int> trackerTypes) {
181 
182  this->callerName = callerName;
183  this->cameraNames = cameraNames;
184 
185  // path of source to find config files
186  boost::filesystem::path path(__FILE__);
187  path.remove_filename();
188  sourcePath = path.string();
189 
190  tracker = new vpMbGenericTracker(cameraNames, trackerTypes);
191 
192  std::map<std::string, std::string> mapOfConfigFiles;
193 
194  for (int i=0; i< cameraNames.size(); i++){
195  std::string configFileName =
196  sourcePath+configFile+cameraNames.at(i)+".xml";
197  mapOfConfigFiles[cameraNames.at(i)] = configFileName;
198  }
199 
200  tracker->loadConfigFile(mapOfConfigFiles);
201 
202  tracker->loadModel(sourcePath+caoModelNoCil);
203 
204  tracker->setCameraTransformationMatrix(mapCameraTransf);
205 
206  tracker->setOgreVisibilityTest(false);
207  tracker->setDisplayFeatures(true);
208 
209  std::cout << "[" << callerName << "][STEREOTRACKER] created\n";
210 
211 }
212 
213 StereoTracker::~StereoTracker(){
214  delete tracker;
215  //To clean up memory allocated by the xml library,
216  //the user has to call this before the exit().
217  vpXmlParser::cleanup();
218 }
219 
220 
221 int StereoTracker::initTrackingByClick(
222  std::map<std::string, const vpImage<unsigned char>*> mapOfImages){
223 
224  std::map<std::string, std::string> mapOfInitFiles;
225 
226  for (int i=0; i< cameraNames.size(); i++){
227 
228  //dont store init file for depth camera, visp says it is not need
229  if (cameraNames.at(i).compare("rangeRight") != 0) {
230  std::string initFileName =
231  sourcePath+initFileClick+cameraNames.at(i)+".init";
232  mapOfInitFiles[cameraNames.at(i)] = initFileName;
233  }
234  }
235 
236  try{
237  tracker->initClick(mapOfImages, mapOfInitFiles, true);
238  } catch (const vpException &e) {
239  std::cerr << "[" << callerName << "][STEREOTRACKER] Catch a ViSP exception: " << e.what() << std::endl;
240  }
241 
242  return 0;
243 }
244 
245 int StereoTracker::initTrackingByPoint(
246  std::map<std::string, const vpImage<unsigned char>*> mapOfImages){
247 
248  std::map<std::string, std::string> mapOfInitFiles;
249 
250  for (int i=0; i< cameraNames.size(); i++){
251  //dont store init file for depth camera, visp says it is not need
252  if (cameraNames.at(i).compare("rangeRight") != 0) {
253  std::string initFileName =
254  sourcePath+initFile_w2D+cameraNames.at(i)+".init";
255 
256  mapOfInitFiles[cameraNames.at(i)] = initFileName;
257  }
258  }
259 
260  try{
261  tracker->initFromPoints(mapOfImages, mapOfInitFiles);
262  } catch (const vpException &e) {
263  std::cerr << "[" << callerName << "][STEREOTRACKER] Catch a ViSP exception: " << e.what() << std::endl;
264  }
265 
266 }
267 
268 
269 int StereoTracker::stereoTrack(std::map<std::string, const vpImage<unsigned char>*> mapOfImages,
270  std::map<std::string, vpHomogeneousMatrix> *mapOfcameraToObj){
271  try {
272 
273  tracker->track(mapOfImages);
274  tracker->getPose(*mapOfcameraToObj);
275 
276  } catch (const vpException &e) {
277  std::cerr << "[" << callerName << "][STEREOTRACKER] Catch a ViSP exception: " << e.what() << std::endl;
278  }
279  return 0;
280 }
281 
282 int StereoTracker::stereoTrack(std::map<std::string, const vpImage<unsigned char>*> mapOfImages,
283  std::map<std::string, pcl::PointCloud< pcl::PointXYZ >::ConstPtr> mapOfPointclouds,
284  std::map<std::string, vpHomogeneousMatrix> *mapOfcameraToObj){
285  try {
286 
287  tracker->track(mapOfImages, mapOfPointclouds);
288  tracker->getPose(*mapOfcameraToObj);
289 
290  } catch (const vpException &e) {
291  std::cerr << "[" << callerName << "][STEREOTRACKER] Catch a ViSP exception: " << e.what() << std::endl;
292  }
293  return 0;
294 }
295 
296 void StereoTracker::getCamerasParams(std::map<std::string, vpCameraParameters> *mapOfCameraParams){
297 
298  tracker->getCameraParameters(*mapOfCameraParams);
299 
300 }
301 
302 void StereoTracker::display(std::map<std::string, const vpImage<unsigned char>*> mapOfImages){
303 
304  std::map<std::string, vpHomogeneousMatrix> mapOfcameraToObj;
305  tracker->getPose(mapOfcameraToObj);
306 
307  std::map<std::string, vpCameraParameters> mapOfCamParams;
308  tracker->getCameraParameters(mapOfCamParams);
309 
310  tracker->display(mapOfImages, mapOfcameraToObj, mapOfCamParams, vpColor::red, 2, true);
311 }
312 
313 
314 
315 
316 
317 
318 
319