#include <visp3/core/vpConfig.h>
#include <visp3/core/vpDebug.h>
#include <visp3/core/vpMath.h>
#include <visp3/core/vpRotationMatrix.h>
#include <visp3/core/vpThetaUVector.h>
#include <visp3/vision/vpHomography.h>
#include <visp3/core/vpDebug.h>
#include <visp3/core/vpHomogeneousMatrix.h>
#include <visp3/core/vpMath.h>
#include <visp3/core/vpPoint.h>
#include <stdlib.h>
#include <visp3/core/vpRansac.h>
#include <visp3/io/vpParseArgv.h>
#define GETOPTARGS "h"
#ifdef ENABLE_VISP_NAMESPACE
#endif
void usage(const char *name, const char *badparam);
bool getOptions(int argc, const char **argv);
void usage(const char *name, const char *badparam)
{
fprintf(stdout, "\n\
Test the Ransac homography estimation algorithm.\n\
\n\
SYNOPSIS\n\
%s [-h]\n",
name);
fprintf(stdout, "\n\
OPTIONS: Default\n\
-h\n\
Print the help.\n");
if (badparam) {
fprintf(stderr, "ERROR: \n");
fprintf(stderr, "\nBad parameter [%s]\n", badparam);
}
}
bool getOptions(int argc, const char **argv)
{
const char *optarg_;
int c;
switch (c) {
case 'h':
usage(argv[0], nullptr);
return false;
break;
default:
usage(argv[0], optarg_);
return false;
break;
}
}
if ((c == 1) || (c == -1)) {
usage(argv[0], nullptr);
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)
{
#if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
try {
if (getOptions(argc, argv) == false) {
return EXIT_FAILURE;
}
double L = 0.1;
unsigned int nbpt = 11;
std::vector<vpPoint> P(nbpt);
std::vector<double> xa(nbpt), ya(nbpt), xb(nbpt), yb(nbpt);
P[0].setWorldCoordinates(-L, -L, 0);
P[1].setWorldCoordinates(2 * L, -L, 0);
P[2].setWorldCoordinates(L, L, 0);
P[3].setWorldCoordinates(-L, 3 * L, 0);
P[4].setWorldCoordinates(0, 0, L);
P[5].setWorldCoordinates(L, -2 * L, L);
P[6].setWorldCoordinates(L, -4 * L, 2 * L);
P[7].setWorldCoordinates(-2 * L, -L, -3 * L);
P[8].setWorldCoordinates(-5 * L, -5 * L, 0);
P[9].setWorldCoordinates(-2 * L, +3 * L, 4 * L);
P[10].setWorldCoordinates(-2 * L, -0.5 * L, 0);
std::vector<bool> inliers_ground_truth(nbpt, false);
inliers_ground_truth[0] = true;
inliers_ground_truth[1] = true;
inliers_ground_truth[2] = true;
inliers_ground_truth[3] = true;
inliers_ground_truth[8] = true;
inliers_ground_truth[10] = true;
for (unsigned int i = 0; i < nbpt; i++) {
P[i].project(aMo);
xa[i] = P[i].get_x();
ya[i] = P[i].get_y();
}
for (unsigned int i = 0; i < nbpt; i++) {
P[i].project(bMo);
xb[i] = P[i].get_x();
yb[i] = P[i].get_y();
}
std::cout << "-------------------------------" << std::endl;
std::cout << "Compare with built homography H = R + t/d n " << std::endl;
std::cout << "aHb built from the displacement: \n" << aHb_built / aHb_built[2][2] << std::endl;
aHb_built.computeDisplacement(aRb, aTb, n);
std::cout << "Rotation aRb: " << std::endl;
std::cout << aRb << std::endl;
std::cout << "Translation: aTb" << std::endl;
std::cout << (aTb).t() << std::endl;
std::cout << "Normal to the plane: n" << std::endl;
std::cout << (n).t() << std::endl;
std::cout << "-------------------------------" << std::endl;
std::vector<bool> inliers;
double residual;
std::cout << "aHb estimated using ransac:\n" << aHb << std::endl;
std::cout << "Inliers indexes (should be 0,1,2,3,8,10): ";
for (unsigned int i = 0; i < inliers.size(); i++)
if (inliers[i])
std::cout << i << ",";
std::cout << std::endl;
if (inliers == inliers_ground_truth) {
std::cout << "Ransac estimation succeed" << std::endl;
return EXIT_SUCCESS;
}
else {
std::cout << "Ransac estimation fails" << std::endl;
return EXIT_FAILURE;
}
}
std::cout << "Catch an exception: " << e << std::endl;
return EXIT_FAILURE;
}
#else
(void)argc;
(void)argv;
std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
return EXIT_SUCCESS;
#endif
}
Implementation of column vector and the associated operations.
error that can be emitted by ViSP classes.
Implementation of an homogeneous matrix and operations on such kind of matrices.
Implementation of an homography and operations on homographies.
static bool ransac(const std::vector< double > &xb, const std::vector< double > &yb, const std::vector< double > &xa, const std::vector< double > &ya, vpHomography &aHb, std::vector< bool > &inliers, double &residual, unsigned int nbInliersConsensus, double threshold, bool normalization=true)
static double rad(double deg)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
This class defines the container for a plane geometrical structure.
Implementation of a rotation matrix and operations on such kind of matrices.
Class that consider the case of a translation vector.