Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
tutorial-detection-object-mbt-deprecated.cpp
1
2#include <visp3/core/vpConfig.h>
3#include <visp3/core/vpIoTools.h>
4#include <visp3/gui/vpDisplayGDI.h>
5#include <visp3/gui/vpDisplayOpenCV.h>
6#include <visp3/gui/vpDisplayX.h>
7#include <visp3/io/vpVideoReader.h>
8#include <visp3/mbt/vpMbEdgeTracker.h>
9#include <visp3/vision/vpKeyPoint.h>
10
11int main(int argc, char **argv)
12{
13#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_FEATURES2D)
15 try {
16 std::string videoname = "teabox.mp4";
17
18 for (int i = 0; i < argc; i++) {
19 if (std::string(argv[i]) == "--name")
20 videoname = std::string(argv[i + 1]);
21 else if (std::string(argv[i]) == "--help") {
22 std::cout << "\nUsage: " << argv[0] << " [--name <video name>] [--help]\n" << std::endl;
23 return EXIT_SUCCESS;
24 }
25 }
26 std::string parentname = vpIoTools::getParent(videoname);
27 std::string objectname = vpIoTools::getNameWE(videoname);
28
29 if (!parentname.empty())
30 objectname = parentname + "/" + objectname;
31
32 std::cout << "Video name: " << videoname << std::endl;
33 std::cout << "Tracker requested config files: " << objectname << ".[init,"
34 << "xml,"
35 << "cao or wrl]" << std::endl;
36 std::cout << "Tracker optional config files: " << objectname << ".[ppm]" << std::endl;
37
41
43 g.setFileName(videoname);
44 g.open(I);
45
46#if defined(VISP_HAVE_X11)
47 vpDisplayX display;
48#elif defined(VISP_HAVE_GDI)
49 vpDisplayGDI display;
50#elif defined(HAVE_OPENCV_HIGHGUI)
51 vpDisplayOpenCV display;
52#else
53 std::cout << "No image viewer is available..." << std::endl;
54 return EXIT_FAILURE;
55#endif
56
57 display.init(I, 100, 100, "Model-based edge tracker");
58
59 vpMbEdgeTracker tracker;
60 bool usexml = false;
61 if (vpIoTools::checkFilename(objectname + ".xml")) {
62 tracker.loadConfigFile(objectname + ".xml");
63 tracker.getCameraParameters(cam);
64 usexml = true;
65 }
66 if (!usexml) {
67 vpMe me;
68 me.setMaskSize(5);
69 me.setMaskNumber(180);
70 me.setRange(8);
72 me.setThreshold(20);
73 me.setMu1(0.5);
74 me.setMu2(0.5);
75 me.setSampleStep(4);
76 me.setNbTotalSample(250);
77 tracker.setMovingEdge(me);
78 cam.initPersProjWithoutDistortion(839, 839, 325, 243);
79 tracker.setCameraParameters(cam);
80 tracker.setAngleAppear(vpMath::rad(70));
81 tracker.setAngleDisappear(vpMath::rad(80));
82 tracker.setNearClippingDistance(0.1);
83 tracker.setFarClippingDistance(100.0);
85 }
86
87 tracker.setOgreVisibilityTest(false);
88 if (vpIoTools::checkFilename(objectname + ".cao"))
89 tracker.loadModel(objectname + ".cao");
90 else if (vpIoTools::checkFilename(objectname + ".wrl"))
91 tracker.loadModel(objectname + ".wrl");
92 tracker.setDisplayFeatures(true);
93 tracker.initClick(I, objectname + ".init", true);
94 tracker.track(I);
96
98#if (defined(VISP_HAVE_OPENCV_NONFREE) || defined(VISP_HAVE_OPENCV_XFEATURES2D)) || \
99 (VISP_HAVE_OPENCV_VERSION >= 0x030411 && CV_MAJOR_VERSION < 4) || (VISP_HAVE_OPENCV_VERSION >= 0x040400)
100 std::string detectorName = "SIFT";
101 std::string extractorName = "SIFT";
102 std::string matcherName = "BruteForce";
103 std::string configurationFile = "detection-config-SIFT.xml";
104#else
105 std::string detectorName = "FAST";
106 std::string extractorName = "ORB";
107 std::string matcherName = "BruteForce-Hamming";
108 std::string configurationFile = "detection-config.xml";
109#endif
111
113 vpKeyPoint keypoint_learning;
115 if (usexml) {
117 keypoint_learning.loadConfigFile(configurationFile);
119 }
120 else {
122 keypoint_learning.setDetector(detectorName);
123 keypoint_learning.setExtractor(extractorName);
124 keypoint_learning.setMatcher(matcherName);
126 }
127
129 std::vector<cv::KeyPoint> trainKeyPoints;
130 double elapsedTime;
131 keypoint_learning.detect(I, trainKeyPoints, elapsedTime);
133
135 std::vector<vpPolygon> polygons;
136 std::vector<std::vector<vpPoint> > roisPt;
137 std::pair<std::vector<vpPolygon>, std::vector<std::vector<vpPoint> > > pair = tracker.getPolygonFaces(false);
138 polygons = pair.first;
139 roisPt = pair.second;
140
141 std::vector<cv::Point3f> points3f;
142 tracker.getPose(cMo);
143 vpKeyPoint::compute3DForPointsInPolygons(cMo, cam, trainKeyPoints, polygons, roisPt, points3f);
145
147 keypoint_learning.buildReference(I, trainKeyPoints, points3f);
149
151 keypoint_learning.saveLearningData("teabox_learning_data.bin", true);
153
156 for (std::vector<cv::KeyPoint>::const_iterator it = trainKeyPoints.begin(); it != trainKeyPoints.end(); ++it) {
157 vpDisplay::displayCross(I, (int)it->pt.y, (int)it->pt.x, 4, vpColor::red);
158 }
159 vpDisplay::displayText(I, 10, 10, "Learning step: keypoints are detected on visible teabox faces", vpColor::red);
160 vpDisplay::displayText(I, 30, 10, "Click to continue with detection...", vpColor::red);
162 vpDisplay::getClick(I, true);
164
166 vpKeyPoint keypoint_detection;
167 if (usexml) {
168 keypoint_detection.loadConfigFile(configurationFile);
169 }
170 else {
171 keypoint_detection.setDetector(detectorName);
172 keypoint_detection.setExtractor(extractorName);
173 keypoint_detection.setMatcher(matcherName);
175 keypoint_detection.setMatchingRatioThreshold(0.8);
176 keypoint_detection.setUseRansacVVS(true);
177 keypoint_detection.setUseRansacConsensusPercentage(true);
178 keypoint_detection.setRansacConsensusPercentage(20.0);
179 keypoint_detection.setRansacIteration(200);
180 keypoint_detection.setRansacThreshold(0.005);
181 }
183
185 keypoint_detection.loadLearningData("teabox_learning_data.bin", true);
187
188 double error;
189 bool click_done = false;
190
191 while (!g.end()) {
192 g.acquire(I);
194
195 vpDisplay::displayText(I, 10, 10, "Detection and localization in process...", vpColor::red);
196
198 if (keypoint_detection.matchPoint(I, cam, cMo, error, elapsedTime)) {
200
202 tracker.setPose(I, cMo);
205 tracker.display(I, cMo, cam, vpColor::red, 2);
206 vpDisplay::displayFrame(I, cMo, cam, 0.025, vpColor::none, 3);
208 }
209
210 vpDisplay::displayText(I, 30, 10, "A click to exit.", vpColor::red);
212 if (vpDisplay::getClick(I, false)) {
213 click_done = true;
214 break;
215 }
216 }
217 if (!click_done)
219 }
220 catch (const vpException &e) {
221 std::cout << "Catch an exception: " << e << std::endl;
222 }
223#else
224 (void)argc;
225 (void)argv;
226 std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
227#endif
228
229 return EXIT_SUCCESS;
230}
Generic class defining intrinsic camera parameters.
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
static const vpColor red
Definition vpColor.h:211
static const vpColor none
Definition vpColor.h:223
Display for windows using GDI (available on any windows 32 platform).
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition vpDisplayX.h:132
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
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, const vpImagePoint &offset=vpImagePoint(0, 0), const std::string &frameName="", const vpColor &textColor=vpColor::black, const vpImagePoint &textOffset=vpImagePoint(15, 15))
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
static void flush(const vpImage< unsigned char > &I)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
error that can be emitted by ViSP classes.
Definition vpException.h:59
Implementation of an homogeneous matrix and operations on such kind of matrices.
Definition of the vpImage class member functions.
Definition vpImage.h:135
static bool checkFilename(const std::string &filename)
static std::string getNameWE(const std::string &pathname)
static std::string getParent(const std::string &pathname)
Class that allows keypoints detection (and descriptors extraction) and matching thanks to OpenCV libr...
Definition vpKeyPoint.h:212
unsigned int matchPoint(const vpImage< unsigned char > &I)
void setRansacConsensusPercentage(double percentage)
Definition vpKeyPoint.h:991
void setFilterMatchingType(const vpFilterMatchingType &filterType)
Definition vpKeyPoint.h:927
void setUseRansacVVS(bool ransacVVS)
void setExtractor(const vpFeatureDescriptorType &extractorType)
Definition vpKeyPoint.h:823
void loadLearningData(const std::string &filename, bool binaryMode=false, bool append=false)
void setRansacThreshold(double threshold)
void detect(const vpImage< unsigned char > &I, std::vector< cv::KeyPoint > &keyPoints, const vpRect &rectangle=vpRect())
void setMatcher(const std::string &matcherName)
Definition vpKeyPoint.h:899
static void compute3DForPointsInPolygons(const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, std::vector< cv::KeyPoint > &candidates, const std::vector< vpPolygon > &polygons, const std::vector< std::vector< vpPoint > > &roisPt, std::vector< cv::Point3f > &points, cv::Mat *descriptors=NULL)
void saveLearningData(const std::string &filename, bool binaryMode=false, bool saveTrainingImages=true)
void setUseRansacConsensusPercentage(bool usePercentage)
void setMatchingRatioThreshold(double ratio)
Definition vpKeyPoint.h:976
@ ratioDistanceThreshold
Definition vpKeyPoint.h:221
void setDetector(const vpFeatureDetectorType &detectorType)
Definition vpKeyPoint.h:765
unsigned int buildReference(const vpImage< unsigned char > &I)
void loadConfigFile(const std::string &configFile)
void setRansacIteration(int nbIter)
static double rad(double deg)
Definition vpMath.h:116
Make the complete tracking of an object by using its CAD model.
virtual void setCameraParameters(const vpCameraParameters &cam)
virtual void setNearClippingDistance(const double &dist)
virtual void setFarClippingDistance(const double &dist)
virtual void track(const vpImage< unsigned char > &I)
virtual void loadConfigFile(const std::string &configFile, bool verbose=true)
virtual void setClipping(const unsigned int &flags)
virtual void setOgreVisibilityTest(const bool &v)
virtual void setPose(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cdMo)
void setMovingEdge(const vpMe &me)
virtual void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, const vpColor &col, unsigned int thickness=1, bool displayFullModel=false)
virtual void getCameraParameters(vpCameraParameters &cam) const
virtual void setDisplayFeatures(bool displayF)
virtual void getPose(vpHomogeneousMatrix &cMo) const
virtual void setAngleDisappear(const double &a)
virtual void initClick(const vpImage< unsigned char > &I, const std::string &initFile, bool displayHelp=false, const vpHomogeneousMatrix &T=vpHomogeneousMatrix())
virtual void loadModel(const std::string &modelFile, bool verbose=false, const vpHomogeneousMatrix &T=vpHomogeneousMatrix())
virtual void setAngleAppear(const double &a)
virtual std::pair< std::vector< vpPolygon >, std::vector< std::vector< vpPoint > > > getPolygonFaces(bool orderPolygons=true, bool useVisibility=true, bool clipPolygon=false)
virtual unsigned int getClipping() const
Definition vpMe.h:122
void setMu1(const double &mu_1)
Definition vpMe.h:353
void setSampleStep(const double &s)
Definition vpMe.h:390
void setRange(const unsigned int &r)
Definition vpMe.h:383
void setLikelihoodThresholdType(const vpLikelihoodThresholdType likelihood_threshold_type)
Definition vpMe.h:445
void setMaskSize(const unsigned int &a)
Definition vpMe.cpp:452
void setNbTotalSample(const int &nb)
Definition vpMe.h:367
void setMu2(const double &mu_2)
Definition vpMe.h:360
@ NORMALIZED_THRESHOLD
Easy-to-use normalized likelihood threshold corresponding to the minimal luminance contrast to consid...
Definition vpMe.h:132
void setMaskNumber(const unsigned int &a)
Definition vpMe.cpp:445
void setThreshold(const double &t)
Definition vpMe.h:435
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
void acquire(vpImage< vpRGBa > &I)
void open(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)