ViSP  2.10.0
tutorial-detection-object-mbt.cpp
1 #include <visp/vpConfig.h>
3 #include <visp/vpDisplayX.h>
4 #include <visp/vpDisplayGDI.h>
5 #include <visp/vpDisplayOpenCV.h>
6 #include <visp/vpMbEdgeTracker.h>
7 #include <visp/vpVideoReader.h>
8 #include <visp/vpKeyPoint.h>
9 
10 
11 int main(int argc, char ** argv) {
12 #if defined(VISP_HAVE_OPENCV) && ((VISP_HAVE_OPENCV_VERSION >= 0x020100) || defined(VISP_HAVE_FFMPEG))
13  try {
15  std::string videoname = "teabox.mpg";
16 
17  for (int i=0; i<argc; i++) {
18  if (std::string(argv[i]) == "--name")
19  videoname = std::string(argv[i+1]);
20  else if (std::string(argv[i]) == "--help") {
21  std::cout << "\nUsage: " << argv[0] << " [--name <video name>] [--help]\n" << std::endl;
22  return 0;
23  }
24  }
25  std::string parentname = vpIoTools::getParent(videoname);
26  std::string objectname = vpIoTools::getNameWE(videoname);
27 
28  if(! parentname.empty())
29  objectname = parentname + "/" + objectname;
30 
31  std::cout << "Video name: " << videoname << std::endl;
32  std::cout << "Tracker requested config files: " << objectname
33  << ".[init,"
34 #ifdef VISP_HAVE_XML2
35  << "xml,"
36 #endif
37  << "cao or wrl]" << std::endl;
38  std::cout << "Tracker optional config files: " << objectname << ".[ppm]" << std::endl;
39 
43 
44  vpVideoReader g;
45  g.setFileName(videoname);
46  g.open(I);
47 
48 #if defined(VISP_HAVE_X11)
49  vpDisplayX display;
50 #elif defined(VISP_HAVE_GDI)
51  vpDisplayGDI display;
52 #elif defined(VISP_HAVE_OPENCV)
53  vpDisplayOpenCV display;
54 #else
55  std::cout << "No image viewer is available..." << std::endl;
56  return 0;
57 #endif
58 
59  display.init(I, 100, 100,"Model-based edge tracker");
60 
61  vpMbEdgeTracker tracker;
62  bool usexml = false;
63 #ifdef VISP_HAVE_XML2
64  if(vpIoTools::checkFilename(objectname + ".xml")) {
65  tracker.loadConfigFile(objectname + ".xml");
66  tracker.getCameraParameters(cam);
67  usexml = true;
68  }
69 #endif
70  if (! usexml) {
71  vpMe me;
72  me.setMaskSize(5);
73  me.setMaskNumber(180);
74  me.setRange(8);
75  me.setThreshold(10000);
76  me.setMu1(0.5);
77  me.setMu2(0.5);
78  me.setSampleStep(4);
79  me.setNbTotalSample(250);
80  tracker.setMovingEdge(me);
81  cam.initPersProjWithoutDistortion(839, 839, 325, 243);
82  tracker.setCameraParameters(cam);
83  tracker.setAngleAppear( vpMath::rad(70) );
84  tracker.setAngleDisappear( vpMath::rad(80) );
85  tracker.setNearClippingDistance(0.1);
86  tracker.setFarClippingDistance(100.0);
88  }
89 
90  tracker.setOgreVisibilityTest(false);
91  if(vpIoTools::checkFilename(objectname + ".cao"))
92  tracker.loadModel(objectname + ".cao");
93  else if(vpIoTools::checkFilename(objectname + ".wrl"))
94  tracker.loadModel(objectname + ".wrl");
95  tracker.setDisplayFeatures(true);
96  tracker.initClick(I, objectname + ".init", true);
97  tracker.track(I);
99 
101  std::string detectorName = "FAST";
102  std::string extractorName = "ORB";
103  std::string matcherName = "BruteForce-Hamming";
104  std::string configurationFile = "detection-config.xml";
105 
106 #if (defined(VISP_HAVE_OPENCV_NONFREE) || defined(VISP_HAVE_OPENCV_XFEATURES2D))
107  detectorName = "SIFT";
108  extractorName = "SIFT";
109  matcherName = "BruteForce";
110  configurationFile = "detection-config-SIFT.xml";
111 #endif
112 
115  vpKeyPoint keypoint_learning;
117  if(usexml) {
119 #ifdef VISP_HAVE_XML2
120  keypoint_learning.loadConfigFile(configurationFile);
121 #endif
122  } else {
125  keypoint_learning.setDetector(detectorName);
126  keypoint_learning.setExtractor(extractorName);
127  keypoint_learning.setMatcher(matcherName);
129  }
131 
133  std::vector<cv::KeyPoint> trainKeyPoints;
134  double elapsedTime;
135  keypoint_learning.detect(I, trainKeyPoints, elapsedTime);
137 
139  std::vector<vpPolygon> polygons;
140  std::vector<std::vector<vpPoint> > roisPt;
141  std::pair<std::vector<vpPolygon>, std::vector<std::vector<vpPoint> > > pair = tracker.getPolygonFaces(false);
142  polygons = pair.first;
143  roisPt = pair.second;
144 
145  std::vector<cv::Point3f> points3f;
146  tracker.getPose(cMo);
147  vpKeyPoint::compute3DForPointsInPolygons(cMo, cam, trainKeyPoints, polygons, roisPt, points3f);
149 
151  keypoint_learning.buildReference(I, trainKeyPoints, points3f);
153 
155  keypoint_learning.saveLearningData("teabox_learning_data.bin", true);
157 
160  for(std::vector<cv::KeyPoint>::const_iterator it = trainKeyPoints.begin(); it != trainKeyPoints.end(); ++it) {
161  vpDisplay::displayCross(I, (int) it->pt.y, (int) it->pt.x, 4, vpColor::red);
162  }
163  vpDisplay::displayText(I, 10, 10, "Learning step: keypoints are detected on visible teabox faces", vpColor::red);
164  vpDisplay::displayText(I, 30, 10, "Click to continue with detection...", vpColor::red);
165  vpDisplay::flush(I);
166  vpDisplay::getClick(I, true);
168 
170  vpKeyPoint keypoint_detection;
171  if(usexml) {
172 #ifdef VISP_HAVE_XML2
173  keypoint_detection.loadConfigFile(configurationFile);
174 #endif
175  } else {
176  keypoint_detection.setDetector(detectorName);
177  keypoint_detection.setExtractor(extractorName);
178  keypoint_detection.setMatcher(matcherName);
180  keypoint_detection.setMatchingRatioThreshold(0.8);
181  keypoint_detection.setUseRansacVVS(true);
182  keypoint_detection.setUseRansacConsensusPercentage(true);
183  keypoint_detection.setRansacConsensusPercentage(20.0);
184  keypoint_detection.setRansacIteration(200);
185  keypoint_detection.setRansacThreshold(0.005);
186  }
188 
190  keypoint_detection.loadLearningData("teabox_learning_data.bin", true);
192 
193  double error;
194  bool click_done = false;
195 
196  while(! g.end()) {
197  g.acquire(I);
199 
200  vpDisplay::displayText(I, 10, 10, "Detection and localization in process...", vpColor::red);
201 
203  if(keypoint_detection.matchPoint(I, cam, cMo, error, elapsedTime)) {
205 
207  tracker.setPose(I, cMo);
210  tracker.display(I, cMo, cam, vpColor::red, 2);
211  vpDisplay::displayFrame(I, cMo, cam, 0.025, vpColor::none, 3);
213  }
214 
215  vpDisplay::displayText(I, 30, 10, "A click to exit.", vpColor::red);
216  vpDisplay::flush(I);
217  if (vpDisplay::getClick(I, false)) {
218  click_done = true;
219  break;
220  }
221  }
222  if (! click_done)
224 #ifdef VISP_HAVE_XML2
226 #endif
227 #if defined(VISP_HAVE_COIN) && (COIN_MAJOR_VERSION == 3)
228  SoDB::finish();
229 #endif
230  }
231  catch(vpException &e) {
232  std::cout << "Catch an exception: " << e << std::endl;
233  }
234 #else
235  (void)argc;
236  (void)argv;
237  std::cout << "Install OpenCV or ffmpeg and rebuild ViSP to use this example." << std::endl;
238 #endif
239 
240  return 0;
241 }
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
void setRansacIteration(const int nbIter)
Definition: vpKeyPoint.h:647
void setMovingEdge(const vpMe &me)
void setUseRansacVVS(const bool ransacVVS)
Definition: vpKeyPoint.h:732
virtual unsigned int getClipping() const
Definition: vpMbTracker.h:220
virtual void setAngleDisappear(const double &a)
Definition: vpMbTracker.h:400
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
void track(const vpImage< unsigned char > &I)
void detect(const vpImage< unsigned char > &I, std::vector< cv::KeyPoint > &keyPoints, double &elapsedTime, const vpRect &rectangle=vpRect())
Definition: vpKeyPoint.cpp:860
void setMaskNumber(const unsigned int &a)
Definition: vpMe.cpp:480
void setRansacThreshold(const double threshold)
Definition: vpKeyPoint.h:686
void setExtractor(const std::string &extractorName)
Definition: vpKeyPoint.h:528
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:132
void setSampleStep(const double &s)
Definition: vpMe.h:271
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Definition: vpDisplay.cpp:887
void setNbTotalSample(const int &nb)
Definition: vpMe.h:199
Define the X11 console to display images.
Definition: vpDisplayX.h:152
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
static const vpColor none
Definition: vpColor.h:179
error that can be emited by ViSP classes.
Definition: vpException.h:76
Contains predetermined masks for sites and holds moving edges tracking parameters.
Definition: vpMe.h:70
Make the complete tracking of an object by using its CAD model.
virtual void setPose(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cdMo)
static void compute3DForPointsInPolygons(const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, std::vector< cv::KeyPoint > &candidate, std::vector< vpPolygon > &polygons, std::vector< std::vector< vpPoint > > &roisPt, std::vector< cv::Point3f > &points, cv::Mat *descriptors=NULL)
Definition: vpKeyPoint.cpp:514
virtual void setCameraParameters(const vpCameraParameters &camera)
static std::string getParent(const std::string &pathname)
Definition: vpIoTools.cpp:1220
void loadConfigFile(const std::string &configFile)
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2232
void setMu1(const double &mu_1)
Definition: vpMe.h:171
static const vpColor red
Definition: vpColor.h:167
void initPersProjWithoutDistortion(const double px, const double py, const double u0, const double v0)
virtual void setNearClippingDistance(const double &dist)
virtual void setOgreVisibilityTest(const bool &v)
static bool checkFilename(const char *filename)
Definition: vpIoTools.cpp:465
void setMatcher(const std::string &matcherName)
Definition: vpKeyPoint.h:575
void open(vpImage< vpRGBa > &I)
void setMaskSize(const unsigned int &a)
Definition: vpMe.cpp:488
void getPose(vpHomogeneousMatrix &cMo_) const
Definition: vpMbTracker.h:328
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:210
The vpDisplayOpenCV allows to display image using the opencv library.
virtual void displayCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)=0
virtual void getCameraParameters(vpCameraParameters &camera) const
Definition: vpMbTracker.h:213
Generic class defining intrinsic camera parameters.
void acquire(vpImage< vpRGBa > &I)
unsigned int buildReference(const vpImage< unsigned char > &I)
Definition: vpKeyPoint.cpp:281
static void displayFrame(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, double size, const vpColor &color=vpColor::none, unsigned int thickness=1)
Definition: vpDisplay.cpp:375
virtual void setFarClippingDistance(const double &dist)
void setFileName(const char *filename)
virtual void setAngleAppear(const double &a)
Definition: vpMbTracker.h:389
virtual void initClick(const vpImage< unsigned char > &I, const std::string &initFile, const bool displayHelp=false)
static double rad(double deg)
Definition: vpMath.h:100
unsigned int matchPoint(const vpImage< unsigned char > &I)
void setRansacConsensusPercentage(const double percentage)
Definition: vpKeyPoint.h:634
static void cleanup()
Definition: vpXmlParser.h:220
void setMu2(const double &mu_2)
Definition: vpMe.h:185
void loadLearningData(const std::string &filename, const bool binaryMode=false, const bool append=false)
virtual void loadModel(const char *modelFile, const bool verbose=false)
Class that allows keypoints detection (and descriptors extraction) and matching thanks to OpenCV libr...
Definition: vpKeyPoint.h:212
void saveLearningData(const std::string &filename, const bool binaryMode=false, const bool saveTrainingImages=true)
static std::string getNameWE(const std::string &pathname)
Definition: vpIoTools.cpp:1207
void setThreshold(const double &t)
Definition: vpMe.h:299
void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, const vpColor &col, const unsigned int thickness=1, const bool displayFullModel=false)
void setDetector(const std::string &detectorName)
Definition: vpKeyPoint.h:490
void setUseRansacConsensusPercentage(const bool usePercentage)
Definition: vpKeyPoint.h:723
virtual bool getClick(bool blocking=true)=0
void loadConfigFile(const std::string &configFile)
void setDisplayFeatures(const bool displayF)
Definition: vpMbTracker.h:431
void setFilterMatchingType(const vpFilterMatchingType &filterType)
Definition: vpKeyPoint.h:591
void setRange(const unsigned int &r)
Definition: vpMe.h:229
virtual void setClipping(const unsigned int &flags)
virtual std::pair< std::vector< vpPolygon >, std::vector< std::vector< vpPoint > > > getPolygonFaces(const bool orderPolygons=true, const bool useVisibility=true)
void setMatchingRatioThreshold(const double ratio)
Definition: vpKeyPoint.h:621