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;
147 template<
typename Type>
148 void run_test(
const std::string &env_ipath,
bool opt_click_allowed,
bool opt_display,
160 #if defined VISP_HAVE_X11 162 #elif defined VISP_HAVE_GTK 164 #elif defined VISP_HAVE_GDI 172 display.
init(I, 0, 0,
"ORB keypoints matching");
214 if (opt_display && opt_click_allowed) {
217 vpHomogeneousMatrix cMoi(0.02044769891, 0.1101505452, 0.5078963719, 2.063603907, 1.110231561, -0.4392789872);
226 cv::Ptr<cv::FeatureDetector> detector;
227 cv::Ptr<cv::DescriptorExtractor> extractor;
228 cv::Ptr<cv::DescriptorMatcher> matcher;
230 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000) 231 detector = cv::ORB::create(500, 1.2f, 1);
232 extractor = cv::ORB::create(500, 1.2f, 1);
233 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020301) 234 detector = cv::FeatureDetector::create(
"ORB");
235 extractor = cv::DescriptorExtractor::create(
"ORB");
237 matcher = cv::DescriptorMatcher::create(
"BruteForce-Hamming");
239 #if (VISP_HAVE_OPENCV_VERSION >= 0x020400 && VISP_HAVE_OPENCV_VERSION < 0x030000) 240 detector->set(
"nLevels", 1);
244 std::vector<cv::KeyPoint> trainKeyPoints;
247 detector->detect(matImg, trainKeyPoints);
250 std::vector<vpPolygon> polygons;
251 std::vector<std::vector<vpPoint> > roisPt;
252 std::pair<std::vector<vpPolygon>, std::vector<std::vector<vpPoint> > > pair = tracker.
getPolygonFaces(
false);
253 polygons = pair.first;
254 roisPt = pair.second;
257 std::vector<cv::Point3f> points3f;
261 cv::Mat trainDescriptors;
262 extractor->compute(matImg, trainKeyPoints, trainDescriptors);
264 if (trainKeyPoints.size() != (size_t)trainDescriptors.rows || trainKeyPoints.size() != points3f.size()) {
274 bool opt_click =
false;
276 while ((opt_display && !g.
end()) || (!opt_display && g.
getFrameIndex() < 30)) {
280 std::vector<cv::KeyPoint> queryKeyPoints;
281 detector->detect(matImg, queryKeyPoints);
283 cv::Mat queryDescriptors;
284 extractor->compute(matImg, queryKeyPoints, queryDescriptors);
286 std::vector<std::vector<cv::DMatch> > knn_matches;
287 std::vector<cv::DMatch> matches;
288 matcher->knnMatch(queryDescriptors, trainDescriptors, knn_matches, 2);
289 for (std::vector<std::vector<cv::DMatch> >::const_iterator it = knn_matches.begin(); it != knn_matches.end();
291 if (it->size() > 1) {
292 double ratio = (*it)[0].distance / (*it)[1].distance;
294 matches.push_back((*it)[0]);
300 for (std::vector<cv::DMatch>::const_iterator it = matches.begin(); it != matches.end(); ++it) {
301 vpPoint pt(points3f[(
size_t)(it->trainIdx)].x, points3f[(
size_t)(it->trainIdx)].y,
302 points3f[(
size_t)(it->trainIdx)].z);
304 double x = 0.0, y = 0.0;
306 queryKeyPoints[(
size_t)(it->queryIdx)].pt.y, x, y);
313 bool is_pose_estimated =
false;
314 if (estimated_pose.
npt >= 4) {
316 unsigned int nb_inliers = (
unsigned int)(0.6 * estimated_pose.
npt);
321 is_pose_estimated =
true;
323 is_pose_estimated =
false;
332 for (std::vector<cv::DMatch>::const_iterator it = matches.begin(); it != matches.end(); ++it) {
333 vpImagePoint leftPt(trainKeyPoints[(
size_t)it->trainIdx].pt.y, trainKeyPoints[(
size_t)it->trainIdx].pt.x);
334 vpImagePoint rightPt(queryKeyPoints[(
size_t)it->queryIdx].pt.y,
335 queryKeyPoints[(
size_t)it->queryIdx].pt.x + Iref.
getWidth());
339 if (is_pose_estimated) {
350 if (opt_click_allowed && opt_display) {
376 int main(
int argc,
const char **argv)
379 std::string env_ipath;
380 bool opt_click_allowed =
true;
381 bool opt_display =
true;
384 if (getOptions(argc, argv, opt_click_allowed, opt_display) ==
false) {
392 if (env_ipath.empty()) {
393 std::cerr <<
"Please set the VISP_INPUT_IMAGE_PATH environment " 403 std::cout <<
"-- Test on gray level images" << std::endl;
404 run_test(env_ipath, opt_click_allowed, opt_display, I, Imatch, Iref);
410 std::cout <<
"-- Test on color images" << std::endl;
411 run_test(env_ipath, opt_click_allowed, opt_display, I, Imatch, Iref);
415 std::cerr << e.
what() << std::endl;
419 std::cout <<
"testKeyPoint-4 is ok !" << std::endl;
425 std::cerr <<
"You need OpenCV library." << std::endl;
virtual std::pair< std::vector< vpPolygon >, std::vector< std::vector< vpPoint > > > getPolygonFaces(bool orderPolygons=true, bool useVisibility=true, bool clipPolygon=false)
void setMovingEdge(const vpMe &me)
bool computePose(vpPoseMethodType method, vpHomogeneousMatrix &cMo, bool(*func)(const vpHomogeneousMatrix &)=NULL)
long getFrameIndex() const
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
virtual void loadModel(const std::string &modelFile, bool verbose=false, const vpHomogeneousMatrix &T=vpHomogeneousMatrix())
unsigned int getWidth() const
virtual unsigned int getClipping() const
virtual void setAngleDisappear(const double &a)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
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).
void setSampleStep(const double &s)
void setNbTotalSample(const int &nb)
virtual void initClick(const vpImage< unsigned char > &I, const std::string &initFile, bool displayHelp=false, const vpHomogeneousMatrix &T=vpHomogeneousMatrix())
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 initFromPose(const vpImage< unsigned char > &I, const std::string &initFile)
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 a 3D point in the object frame and allows forward projection of a 3D point in the ...
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)
const char * what() const
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)
virtual void setFarClippingDistance(const double &dist)
virtual void setAngleAppear(const double &a)
void insert(const vpImage< Type > &src, const vpImagePoint &topLeft)
static double rad(double deg)
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
unsigned int npt
Number of point used in pose computation.
virtual void getCameraParameters(vpCameraParameters &cam) const
void setRansacMaxTrials(const int &rM)
void setRansacNbInliersToReachConsensus(const unsigned int &nbC)
void setMu2(const double &mu_2)
virtual void loadConfigFile(const std::string &configFile, bool verbose=true)
virtual void getPose(vpHomogeneousMatrix &cMo) 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()
virtual void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, const vpColor &col, unsigned int thickness=1, bool displayFullModel=false)
void setThreshold(const double &t)
void setFileName(const std::string &filename)
unsigned int getHeight() const
virtual void setCameraParameters(const vpCameraParameters &cam)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
void setRange(const unsigned int &r)
virtual void setClipping(const unsigned int &flags)
void addPoint(const vpPoint &P)
static void displayLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1, bool segment=true)
Definition of the vpImage class member functions.