42 #include <visp3/core/vpConfig.h> 44 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020301) 46 #include <opencv2/core/core.hpp> 47 #include <opencv2/features2d/features2d.hpp> 48 #include <visp3/core/vpHomogeneousMatrix.h> 49 #include <visp3/core/vpImage.h> 50 #include <visp3/core/vpIoTools.h> 51 #include <visp3/gui/vpDisplayGDI.h> 52 #include <visp3/gui/vpDisplayGTK.h> 53 #include <visp3/gui/vpDisplayOpenCV.h> 54 #include <visp3/gui/vpDisplayX.h> 55 #include <visp3/io/vpImageIo.h> 56 #include <visp3/io/vpParseArgv.h> 57 #include <visp3/io/vpVideoReader.h> 58 #include <visp3/mbt/vpMbEdgeTracker.h> 59 #include <visp3/vision/vpKeyPoint.h> 62 #define GETOPTARGS "cdh" 64 void usage(
const char *name,
const char *badparam);
65 bool getOptions(
int argc,
const char **argv,
bool &click_allowed,
bool &display);
75 void usage(
const char *name,
const char *badparam)
78 Test keypoints matching.\n\ 81 %s [-c] [-d] [-h]\n", name);
87 Disable the mouse click. Useful to automate the \n\ 88 execution of this program without human intervention.\n\ 91 Turn off the display.\n\ 97 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
111 bool getOptions(
int argc,
const char **argv,
bool &click_allowed,
bool &display)
119 click_allowed =
false;
125 usage(argv[0], NULL);
130 usage(argv[0], optarg_);
136 if ((c == 1) || (c == -1)) {
138 usage(argv[0], NULL);
139 std::cerr <<
"ERROR: " << std::endl;
140 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
153 int main(
int argc,
const char **argv)
156 std::string env_ipath;
157 bool opt_click_allowed =
true;
158 bool opt_display =
true;
161 if (getOptions(argc, argv, opt_click_allowed, opt_display) ==
false) {
169 if (env_ipath.empty()) {
170 std::cerr <<
"Please set the VISP_INPUT_IMAGE_PATH environment " 187 #if defined VISP_HAVE_X11 189 #elif defined VISP_HAVE_GTK 191 #elif defined VISP_HAVE_GDI 199 display.
init(I, 0, 0,
"ORB keypoints matching");
211 #ifdef VISP_HAVE_XML2 241 if (opt_display && opt_click_allowed) {
244 vpHomogeneousMatrix cMoi(0.02044769891, 0.1101505452, 0.5078963719, 2.063603907, 1.110231561, -0.4392789872);
253 cv::Ptr<cv::FeatureDetector> detector;
254 cv::Ptr<cv::DescriptorExtractor> extractor;
255 cv::Ptr<cv::DescriptorMatcher> matcher;
257 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000) 258 detector = cv::ORB::create(500, 1.2f, 1);
259 extractor = cv::ORB::create(500, 1.2f, 1);
260 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020301) 261 detector = cv::FeatureDetector::create(
"ORB");
262 extractor = cv::DescriptorExtractor::create(
"ORB");
264 matcher = cv::DescriptorMatcher::create(
"BruteForce-Hamming");
266 #if (VISP_HAVE_OPENCV_VERSION >= 0x020400 && VISP_HAVE_OPENCV_VERSION < 0x030000) 267 detector->set(
"nLevels", 1);
271 std::vector<cv::KeyPoint> trainKeyPoints;
274 detector->detect(matImg, trainKeyPoints);
277 std::vector<vpPolygon> polygons;
278 std::vector<std::vector<vpPoint> > roisPt;
279 std::pair<std::vector<vpPolygon>, std::vector<std::vector<vpPoint> > > pair = tracker.
getPolygonFaces(
false);
280 polygons = pair.first;
281 roisPt = pair.second;
284 std::vector<cv::Point3f> points3f;
288 cv::Mat trainDescriptors;
289 extractor->compute(matImg, trainKeyPoints, trainDescriptors);
291 if (trainKeyPoints.size() != (size_t)trainDescriptors.rows || trainKeyPoints.size() != points3f.size()) {
292 std::cerr <<
"Problem with training data size !" << std::endl;
302 bool opt_click =
false;
304 while ((opt_display && !g.
end()) || (!opt_display && g.
getFrameIndex() < 30)) {
308 std::vector<cv::KeyPoint> queryKeyPoints;
309 detector->detect(matImg, queryKeyPoints);
311 cv::Mat queryDescriptors;
312 extractor->compute(matImg, queryKeyPoints, queryDescriptors);
314 std::vector<std::vector<cv::DMatch> > knn_matches;
315 std::vector<cv::DMatch> matches;
316 matcher->knnMatch(queryDescriptors, trainDescriptors, knn_matches, 2);
317 for (std::vector<std::vector<cv::DMatch> >::const_iterator it = knn_matches.begin(); it != knn_matches.end();
319 if (it->size() > 1) {
320 double ratio = (*it)[0].distance / (*it)[1].distance;
322 matches.push_back((*it)[0]);
328 for (std::vector<cv::DMatch>::const_iterator it = matches.begin(); it != matches.end(); ++it) {
329 vpPoint pt(points3f[(
size_t)(it->trainIdx)].x, points3f[(
size_t)(it->trainIdx)].y,
330 points3f[(
size_t)(it->trainIdx)].z);
332 double x = 0.0, y = 0.0;
334 queryKeyPoints[(
size_t)(it->queryIdx)].pt.y, x, y);
341 bool is_pose_estimated =
false;
342 if (estimated_pose.
npt >= 4) {
344 unsigned int nb_inliers = (
unsigned int)(0.6 * estimated_pose.
npt);
349 is_pose_estimated =
true;
351 is_pose_estimated =
false;
360 for (std::vector<cv::DMatch>::const_iterator it = matches.begin(); it != matches.end(); ++it) {
361 vpImagePoint leftPt(trainKeyPoints[(
size_t)it->trainIdx].pt.y, trainKeyPoints[(
size_t)it->trainIdx].pt.x);
362 vpImagePoint rightPt(queryKeyPoints[(
size_t)it->queryIdx].pt.y,
363 queryKeyPoints[(
size_t)it->queryIdx].pt.x + Iref.
getWidth());
367 if (is_pose_estimated) {
378 if (opt_click_allowed && opt_display) {
398 std::cerr << e.
what() << std::endl;
402 std::cout <<
"testKeyPoint-4 is ok !" << std::endl;
408 std::cerr <<
"You need OpenCV library." << std::endl;
virtual unsigned int getClipping() const
void setMovingEdge(const vpMe &me)
virtual void getPose(vpHomogeneousMatrix &cMo_) const
bool computePose(vpPoseMethodType method, vpHomogeneousMatrix &cMo, bool(*func)(const vpHomogeneousMatrix &)=NULL)
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
virtual void setAngleDisappear(const double &a)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
Implementation of an homogeneous matrix and operations on such kind of matrices.
void setMaskNumber(const unsigned int &a)
virtual void setDownScalingFactor(unsigned int scale)
Display for windows using GDI (available on any windows 32 platform).
virtual void loadModel(const std::string &modelFile, const bool verbose=false, const vpHomogeneousMatrix &T=vpHomogeneousMatrix())
void setSampleStep(const double &s)
void setNbTotalSample(const int &nb)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
static const vpColor none
error that can be emited by ViSP classes.
void setRansacThreshold(const double &t)
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)
Make the complete tracking of an object by using its CAD model.
virtual void setPose(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cdMo)
virtual void setCameraParameters(const vpCameraParameters &camera)
virtual void initFromPose(const vpImage< unsigned char > &I, const std::string &initFile)
void loadConfigFile(const std::string &configFile)
static const vpColor green
static void flush(const vpImage< unsigned char > &I)
void setMu1(const double &mu_1)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Class that defines what is a point.
void initPersProjWithoutDistortion(const double px, const double py, const double u0, const double v0)
virtual void setNearClippingDistance(const double &dist)
void open(vpImage< vpRGBa > &I)
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 setMaskSize(const unsigned int &a)
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Class used for pose computation from N points (pose from point only). Some of the algorithms implemen...
Generic class defining intrinsic camera parameters.
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
void acquire(vpImage< vpRGBa > &I)
void resize(const unsigned int h, const unsigned int w)
resize the image : Image initialization
virtual void setFarClippingDistance(const double &dist)
void setFileName(const char *filename)
virtual void setAngleAppear(const double &a)
void insert(const vpImage< Type > &src, const vpImagePoint &topLeft)
const char * what() const
static double rad(double deg)
unsigned int npt
Number of point used in pose computation.
void setRansacMaxTrials(const int &rM)
void setRansacNbInliersToReachConsensus(const unsigned int &nbC)
void setMu2(const double &mu_2)
long getFrameIndex() const
unsigned int getHeight() const
virtual void getCameraParameters(vpCameraParameters &camera) const
static void read(vpImage< unsigned char > &I, const std::string &filename)
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))
unsigned int getDownScalingFactor()
void setThreshold(const double &t)
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)
virtual void initClick(const vpImage< unsigned char > &I, const std::string &initFile, const bool displayHelp=false, const vpHomogeneousMatrix &T=vpHomogeneousMatrix())
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
static void displayLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)
void setRange(const unsigned int &r)
virtual void setClipping(const unsigned int &flags)
void addPoint(const vpPoint &P)
unsigned int getWidth() const
virtual std::pair< std::vector< vpPolygon >, std::vector< std::vector< vpPoint > > > getPolygonFaces(const bool orderPolygons=true, const bool useVisibility=true, const bool clipPolygon=false)