1 #include "header/tracker.h" 3 MonoTracker::MonoTracker(std::string callerName, std::string cameraName,
6 this->callerName = callerName;
7 this->cameraName = cameraName;
10 boost::filesystem::path path(__FILE__);
11 path.remove_filename();
12 sourcePath = path.string();
15 tracker.setTrackerType(trackerType);
17 tracker.loadConfigFile(sourcePath + configFile + cameraName +
".xml");
20 tracker.loadModel(sourcePath + caoModelNoCil);
21 tracker.setOgreVisibilityTest(
false);
22 tracker.setDisplayFeatures(
true);
25 tracker.setProjectionErrorComputation(
true);
27 std::cout <<
"[" << callerName <<
"][MONOTRACKER " << cameraName <<
"]" 32 MonoTracker::~MonoTracker(){
35 vpXmlParser::cleanup();
39 int MonoTracker::initTrackingByClick(vpImage<unsigned char> *I){
41 tracker.initClick(*I, sourcePath+initFileClick+cameraName +
".init",
true);
43 MonoTracker::monoTrackInit_priv(I);
44 keypoint_detection.loadConfigFile(sourcePath+configFileDetector);
45 keypoint_detection.loadLearningData(sourcePath+learnData+cameraName+
".bin",
true);
47 std::cout <<
"[" << callerName <<
"][MONOTRACKER " << cameraName <<
"]" 48 <<
" init by Click done\n";
52 int MonoTracker::initTrackingByPoint(vpImage<unsigned char> *I){
54 tracker.initFromPoints(*I, sourcePath+initFile_w2D+cameraName+
".init");
56 MonoTracker::monoTrackInit_priv(I);
57 keypoint_detection.loadConfigFile(sourcePath+configFileDetector);
58 keypoint_detection.loadLearningData(sourcePath+learnData+cameraName+
".bin",
true);
60 std::cout <<
"[" << callerName <<
"][MONOTRACKER " << cameraName <<
"]" 61 <<
" init by Point done\n";
65 int MonoTracker::monoTrackInit_priv(vpImage<unsigned char> *I){
69 vpCameraParameters cam;
70 vpHomogeneousMatrix cMo;
77 tracker.getCameraParameters(cam);
81 vpKeyPoint keypoint_learning;
82 keypoint_learning.loadConfigFile(sourcePath+configFileDetector);
84 std::vector<cv::KeyPoint> trainKeyPoints;
86 keypoint_learning.detect(*I, trainKeyPoints, elapsedTime);
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);
92 vpDisplay::displayText(*I, 10, 10,
"All Keypoints...", vpColor::red);
93 vpDisplay::displayText(*I, 30, 10,
"Click to continue with detection...", vpColor::red);
95 vpDisplay::getClick(*I,
true);
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);
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);
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);
121 }
catch (vpException &e) {
122 std::cout <<
"[" << callerName <<
"][MONOTRACKER " << cameraName <<
"]" 123 <<
"Init_priv exception: " << e << std::endl;
130 int MonoTracker::monoTrack(vpImage<unsigned char> *I,
131 vpHomogeneousMatrix *cMo,
132 double *error,
double* elapsedTime){
136 vpCameraParameters cam;
137 tracker.getCameraParameters(cam);
139 if (keypoint_detection.matchPoint(*I, cam, *cMo, *error, *elapsedTime)) {
140 tracker.setPose(*I, *cMo);
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;
151 }
catch (vpException &e) {
152 std::cout <<
"[" << callerName <<
"][MONOTRACKER " << cameraName <<
"]" 153 <<
"tracking exception: " << e << std::endl;
160 void MonoTracker::getCameraParams(vpCameraParameters *param){
161 tracker.getCameraParameters(*param);
164 void MonoTracker::display(vpImage<unsigned char> *I){
166 vpHomogeneousMatrix cThole;
167 tracker.getPose(cThole);
169 vpCameraParameters cam;
170 tracker.getCameraParameters(cam);
172 tracker.display(*I, cThole, cam, vpColor::red, 2 ,
true);
178 StereoTracker::StereoTracker(std::string callerName, std::vector<std::string> cameraNames,
179 std::map<std::string, vpHomogeneousMatrix> mapCameraTransf,
180 std::vector<int> trackerTypes) {
182 this->callerName = callerName;
183 this->cameraNames = cameraNames;
186 boost::filesystem::path path(__FILE__);
187 path.remove_filename();
188 sourcePath = path.string();
190 tracker =
new vpMbGenericTracker(cameraNames, trackerTypes);
192 std::map<std::string, std::string> mapOfConfigFiles;
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;
200 tracker->loadConfigFile(mapOfConfigFiles);
202 tracker->loadModel(sourcePath+caoModelNoCil);
204 tracker->setCameraTransformationMatrix(mapCameraTransf);
206 tracker->setOgreVisibilityTest(
false);
207 tracker->setDisplayFeatures(
true);
209 std::cout <<
"[" << callerName <<
"][STEREOTRACKER] created\n";
213 StereoTracker::~StereoTracker(){
217 vpXmlParser::cleanup();
221 int StereoTracker::initTrackingByClick(
222 std::map<std::string,
const vpImage<unsigned char>*> mapOfImages){
224 std::map<std::string, std::string> mapOfInitFiles;
226 for (
int i=0; i< cameraNames.size(); i++){
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;
237 tracker->initClick(mapOfImages, mapOfInitFiles,
true);
238 }
catch (
const vpException &e) {
239 std::cerr <<
"[" << callerName <<
"][STEREOTRACKER] Catch a ViSP exception: " << e.what() << std::endl;
245 int StereoTracker::initTrackingByPoint(
246 std::map<std::string,
const vpImage<unsigned char>*> mapOfImages){
248 std::map<std::string, std::string> mapOfInitFiles;
250 for (
int i=0; i< cameraNames.size(); i++){
252 if (cameraNames.at(i).compare(
"rangeRight") != 0) {
253 std::string initFileName =
254 sourcePath+initFile_w2D+cameraNames.at(i)+
".init";
256 mapOfInitFiles[cameraNames.at(i)] = initFileName;
261 tracker->initFromPoints(mapOfImages, mapOfInitFiles);
262 }
catch (
const vpException &e) {
263 std::cerr <<
"[" << callerName <<
"][STEREOTRACKER] Catch a ViSP exception: " << e.what() << std::endl;
269 int StereoTracker::stereoTrack(std::map<std::string,
const vpImage<unsigned char>*> mapOfImages,
270 std::map<std::string, vpHomogeneousMatrix> *mapOfcameraToObj){
273 tracker->track(mapOfImages);
274 tracker->getPose(*mapOfcameraToObj);
276 }
catch (
const vpException &e) {
277 std::cerr <<
"[" << callerName <<
"][STEREOTRACKER] Catch a ViSP exception: " << e.what() << std::endl;
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){
287 tracker->track(mapOfImages, mapOfPointclouds);
288 tracker->getPose(*mapOfcameraToObj);
290 }
catch (
const vpException &e) {
291 std::cerr <<
"[" << callerName <<
"][STEREOTRACKER] Catch a ViSP exception: " << e.what() << std::endl;
296 void StereoTracker::getCamerasParams(std::map<std::string, vpCameraParameters> *mapOfCameraParams){
298 tracker->getCameraParameters(*mapOfCameraParams);
302 void StereoTracker::display(std::map<std::string,
const vpImage<unsigned char>*> mapOfImages){
304 std::map<std::string, vpHomogeneousMatrix> mapOfcameraToObj;
305 tracker->getPose(mapOfcameraToObj);
307 std::map<std::string, vpCameraParameters> mapOfCamParams;
308 tracker->getCameraParameters(mapOfCamParams);
310 tracker->display(mapOfImages, mapOfcameraToObj, mapOfCamParams, vpColor::red, 2,
true);