Example of the HLM (Malis) homography estimation algorithm with a planar object using vpHomography class.
#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 <stdlib.h>
#include <visp3/core/vpDebug.h>
#include <visp3/core/vpHomogeneousMatrix.h>
#include <visp3/core/vpMath.h>
#include <visp3/core/vpPoint.h>
#include <visp3/io/vpParseArgv.h>
#define GETOPTARGS "h"
#define L 0.1
#define nbpt 5
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 HLM (Malis) homography estimation algorithm with a planar object.\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], 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 {
if (getOptions(argc, argv) == false) {
exit(-1);
}
std::vector<double> xa(nbpt), ya(nbpt);
std::vector<double> xb(nbpt), yb(nbpt);
for (unsigned int i = 0; i < nbpt; i++) {
aP[i] = P[i];
}
for (unsigned int i = 0; i < nbpt; i++) {
bP[i] = P[i];
}
std::cout << "-------------------------------" << std::endl;
std::cout << "aMb " << std::endl << aMb << std::endl;
std::cout << "-------------------------------" << std::endl;
aHb /= aHb[2][2];
std::cout << "aHb computed using the Malis paralax algorithm: \n" << aHb << std::endl;
std::cout << "-------------------------------" << std::endl;
std::cout << "extract R, T and n " << std::endl;
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::cout << "Compare with built homography H = R + t/d " << std::endl;
std::cout << "aHb built from the displacement " << std::endl;
std::cout << std::endl << 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::cout << "test if ap = aHb bp" << std::endl;
for (unsigned int i = 0; i < nbpt; i++) {
std::cout << "Point " << i << std::endl;
std::cout << "(";
std::cout << ") = (";
p = aHb * bP[i];
}
std::cout << "-------------------------------" << std::endl;
std::cout << "test displacement" << std::endl;
std::list<vpRotationMatrix> laRb;
std::list<vpTranslationVector> laTb;
std::list<vpColVector> lnb;
std::list<vpRotationMatrix>::const_iterator it_laRb = laRb.begin();
std::list<vpTranslationVector>::const_iterator it_laTb = laTb.begin();
std::list<vpColVector>::const_iterator it_lnb = lnb.begin();
int k = 1;
while (it_lnb != lnb.end()) {
std::cout << "Solution " << k++ << std::endl;
aRb = *it_laRb;
aTb = *it_laTb;
n = *it_lnb;
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;
++it_laRb;
++it_laTb;
++it_lnb;
}
return EXIT_SUCCESS;
std::cout << "Catch an exception: " << e << std::endl;
return EXIT_FAILURE;
}
}