Test keypoint matching and pose estimation.
#include <iostream>
#include <visp3/core/vpConfig.h>
#if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020301)
#include <visp3/vision/vpKeyPoint.h>
#include <visp3/core/vpImage.h>
#include <visp3/io/vpImageIo.h>
#include <visp3/gui/vpDisplayX.h>
#include <visp3/gui/vpDisplayGTK.h>
#include <visp3/gui/vpDisplayGDI.h>
#include <visp3/gui/vpDisplayOpenCV.h>
#include <visp3/io/vpVideoReader.h>
#include <visp3/core/vpIoTools.h>
#include <visp3/mbt/vpMbEdgeTracker.h>
#include <visp3/io/vpParseArgv.h>
#define GETOPTARGS "cdh"
void usage(const char *name, const char *badparam);
bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
void usage(const char *name, const char *badparam)
{
fprintf(stdout, "\n\
Test keypoints matching.\n\
\n\
SYNOPSIS\n\
%s [-c] [-d] [-h]\n", name);
fprintf(stdout, "\n\
OPTIONS: \n\
\n\
-c\n\
Disable the mouse click. Useful to automaze the \n\
execution of this program without humain intervention.\n\
\n\
-d \n\
Turn off the display.\n\
\n\
-h\n\
Print the help.\n");
if (badparam)
fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
}
bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
{
const char *optarg_;
int c;
switch (c) {
case 'c': click_allowed = false; break;
case 'd': display = false; break;
case 'h': usage(argv[0], NULL); return false; break;
default:
usage(argv[0], optarg_);
return false; break;
}
}
if ((c == 1) || (c == -1)) {
usage(argv[0], NULL);
std::cerr << "ERROR: " << std::endl;
std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
return false;
}
return true;
}
int main(int argc, const char ** argv) {
try {
std::string env_ipath;
bool opt_click_allowed = true;
bool opt_display = true;
if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
exit (-1);
}
if(env_ipath.empty()) {
std::cerr << "Please set the VISP_INPUT_IMAGE_PATH environment variable value." << std::endl;
return -1;
}
#if defined VISP_HAVE_X11
#elif defined VISP_HAVE_GTK
#elif defined VISP_HAVE_GDI
#else
#endif
if (opt_display) {
display.
init(I, 0, 0,
"ORB keypoints matching and pose estimation");
}
bool usexml = false;
#ifdef VISP_HAVE_XML2
usexml = true;
#endif
if (! usexml) {
}
if (opt_display && opt_click_allowed) {
}
else
{
vpHomogeneousMatrix cMoi(0.02044769891, 0.1101505452, 0.5078963719, 2.063603907, 1.110231561, -0.4392789872);
}
vpKeyPoint keypoints(
"ORB",
"ORB",
"BruteForce-Hamming");
#if (VISP_HAVE_OPENCV_VERSION >= 0x020400)
keypoints.setMatcher("FlannBased");
#if (VISP_HAVE_OPENCV_VERSION < 0x030000)
keypoints.setDetectorParameter("ORB", "nLevels", 1);
#else
cv::Ptr<cv::ORB> orb_detector = keypoints.getDetector("ORB").dynamicCast<cv::ORB>();
if(orb_detector != NULL) {
orb_detector->setNLevels(1);
}
#endif
#endif
std::vector<cv::KeyPoint> trainKeyPoints;
double elapsedTime;
keypoints.detect(I, trainKeyPoints, elapsedTime);
std::vector<vpPolygon> polygons;
std::vector<std::vector<vpPoint> > roisPt;
std::pair<std::vector<vpPolygon>, std::vector<std::vector<vpPoint> > > pair = tracker.
getPolygonFaces(
false);
polygons = pair.first;
roisPt = pair.second;
std::vector<cv::Point3f> points3f;
keypoints.buildReference(I, trainKeyPoints, points3f, false, 1);
cMo.
buildFrom(0.02651282185, -0.03713587374, 0.6873765919, 2.314744454, 0.3492296488, -0.1226054828);
keypoints.detect(I, trainKeyPoints, elapsedTime);
polygons = pair.first;
roisPt = pair.second;
keypoints.buildReference(I, trainKeyPoints, points3f, true, 2);
cMo.
buildFrom(0.02965448956, -0.07283091786, 0.7253526051, 2.300529617, -0.4286674806, 0.1788761025);
keypoints.detect(I, trainKeyPoints, elapsedTime);
polygons = pair.first;
roisPt = pair.second;
keypoints.buildReference(I, trainKeyPoints, points3f, true, 3);
#if defined VISP_HAVE_X11
#elif defined VISP_HAVE_GTK
#elif defined VISP_HAVE_GDI
#else
#endif
keypoints.createImageMatching(I, IMatching);
if (opt_display) {
display2.
init(IMatching, 0, (
int)I.
getHeight() + 80,
"IMatching");
}
bool opt_click = false;
double error;
if(opt_display) {
keypoints.insertImageMatching(I, IMatching);
}
if(keypoints.matchPoint(I, cam, cMo, error, elapsedTime)) {
if(opt_display) {
std::vector<vpImagePoint> ransacInliers = keypoints.getRansacInliers();
std::vector<vpImagePoint> ransacOutliers = keypoints.getRansacOutliers();
for(std::vector<vpImagePoint>::const_iterator it = ransacInliers.begin(); it != ransacInliers.end(); ++it) {
imPt.set_u(imPt.get_u() + I.
getWidth());
}
for(std::vector<vpImagePoint>::const_iterator it = ransacOutliers.begin(); it != ransacOutliers.end(); ++it) {
imPt.set_u(imPt.get_u() + I.
getWidth());
}
keypoints.displayMatching(I, IMatching);
}
}
if(opt_display) {
}
if (opt_click_allowed && opt_display) {
if(opt_click) {
opt_click = false;
}
} else {
opt_click = true;
}
break;
}
}
}
}
}
std::cerr << e.
what() << std::endl;
return -1;
}
std::cout << "testKeyPoint-2 is ok !" << std::endl;
return 0;
}
#else
int main() {
std::cerr << "You need OpenCV library." << std::endl;
return 0;
}
#endif