42 #include <visp/vpMbKltTracker.h>
44 #ifdef VISP_HAVE_OPENCV
47 : cur(NULL), c0Mo(), angleAppears(0), angleDisappears(0), compute_interaction(true),
48 firstInitialisation(true), maskBorder(5), lambda(0.8), maxIter(200), threshold_outlier(0.5),
49 percentGood(0.6), useOgre(false), ctTc0(), tracker(), faces(), firstTrack(false),
50 distNearClip(0.01), distFarClip(100), clippingFlag(
vpMbtPolygon::NO_CLIPPING)
90 bool reInitialisation =
false;
120 IplImage* mask = cvCreateImage(cvSize((
int)I.
getWidth(), (int)I.
getHeight()), IPL_DEPTH_8U, 1);
123 unsigned char val = 255;
124 for (
unsigned int i = 0; i <
faces.
size(); i += 1){
125 if(
faces[i]->isVisible())
131 for (
unsigned int i = 0; i <
faces.
size(); i += 1){
132 if(
faces[i]->isVisible()){
137 cvReleaseImage(&mask);
150 cvReleaseImage(&
cur);
184 #ifdef VISP_HAVE_OGRE
196 std::vector<vpImagePoint>
199 std::vector<vpImagePoint> kltPoints;
217 std::map<int, vpImagePoint>
220 std::map<int, vpImagePoint> kltPoints;
255 for (
unsigned int i = 0; i <
faces.
size(); i += 1){
256 faces[i]->setCameraParameters(camera);
273 #ifndef VISP_HAVE_OGRE
275 std::cout <<
"WARNING: ViSP doesn't have Ogre3D, basic visibility test will be used. setOgreVisibilityTest() set to false." << std::endl;
293 bool reInitialisation =
false;
297 #ifdef VISP_HAVE_OGRE
304 if(reInitialisation){
305 std::cout <<
"WARNING: Visibility changed, must reinitialize to update pose" << std::endl;
319 CvPoint2D32f* initial_guess = NULL;
320 initial_guess = (CvPoint2D32f*)cvAlloc((
unsigned int)
tracker.
getMaxFeatures()*
sizeof(initial_guess[0]));
322 for (
unsigned int i = 0; i <
faces.
size(); i += 1){
323 if(
faces[i]->isVisible() &&
faces[i]->hasEnoughPoints()){
331 double invDc = 1.0 / plan.
getD();
335 vpGEMM(cdtc, Nc, -invDc, cdRc, 1.0, cdHc, VP_GEMM_B_T);
342 std::map<int, vpImagePoint>::const_iterator iter =
faces[i]->getCurrentPoints().begin();
343 for( ; iter !=
faces[i]->getCurrentPoints().end(); iter++){
345 cdp[0] = iter->second.get_j(); cdp[1] = iter->second.get_i(); cdp[2] = 1.0;
347 double p_mu_t_2 = cdp[0] * cdGc[2][0] + cdp[1] * cdGc[2][1] + cdGc[2][2];
349 if( fabs(p_mu_t_2) < std::numeric_limits<double>::epsilon()){
355 cdp[0] = (cdp[0] * cdGc[0][0] + cdp[1] * cdGc[0][1] + cdGc[0][2]) / p_mu_t_2;
356 cdp[1] = (cdp[0] * cdGc[1][0] + cdp[1] * cdGc[1][1] + cdGc[1][2]) / p_mu_t_2;
359 initial_guess[(
faces[i]->getCurrentPointsInd())[iter->first]].x = (
float)cdp[0];
360 initial_guess[(
faces[i]->getCurrentPointsInd())[iter->first]].y = (
float)cdp[1];
367 if(initial_guess) cvFree(&initial_guess);
368 initial_guess = NULL;
384 vpTRACE(
"Far clipping value cannot be inferior than near clipping value. Far clipping won't be considered.");
386 vpTRACE(
"Far clipping value cannot be inferior than 0. Far clipping won't be considered.");
390 for (
unsigned int i = 0; i <
faces.
size(); i ++){
405 vpTRACE(
"Near clipping value cannot be superior than far clipping value. Near clipping won't be considered.");
407 vpTRACE(
"Near clipping value cannot be inferior than 0. Near clipping won't be considered.");
411 for (
unsigned int i = 0; i <
faces.
size(); i ++){
428 for (
unsigned int i = 0; i <
faces.
size(); i ++)
441 if( corners.size() > 2){
444 polygon->
setNbPoint((
unsigned int)corners.size());
446 for(
unsigned int j = 0; j < corners.size(); j++) {
484 for (
unsigned int i = 0; i <
faces.
size(); i += 1){
485 if(
faces[i]->isVisible()){
488 if(
faces[i]->hasEnoughPoints()){
489 nbInfos +=
faces[i]->getNbPointsCur();
504 bool reInitialisation =
false;
506 unsigned int initialNumber = 0;
507 unsigned int currentNumber = 0;
508 unsigned int shift = 0;
509 for (
unsigned int i = 0; i <
faces.
size(); i += 1){
510 if(
faces[i]->isVisible()){
511 initialNumber +=
faces[i]->getInitialNumberPoint();
512 if(
faces[i]->hasEnoughPoints()){
515 shift += 2*
faces[i]->getNbPointsCur();
517 currentNumber +=
faces[i]->getNbPointsCur();
527 double value =
percentGood * (double)initialNumber;
528 if((
double)currentNumber < value){
530 reInitialisation =
true;
536 #ifdef VISP_HAVE_OGRE
572 double normRes_1 = -1;
573 unsigned int iter = 0;
576 J.
resize(2*nbInfos, 6, 0);
578 while( ((
int)((normRes - normRes_1)*1e8) != 0 ) && (iter<
maxIter) ){
580 unsigned int shift = 0;
581 for (
unsigned int i = 0; i <
faces.
size(); i += 1){
582 if(
faces[i]->isVisible() &&
faces[i]->hasEnoughPoints()){
587 faces[i]->computeInteractionMatrixAndResidu(subR, subJ);
589 std::cerr <<
"exception while tracking face " << i << std::endl;
593 shift += 2*
faces[i]->getNbPointsCur();
614 for (
unsigned int i = 0; i < static_cast<unsigned int>(R.
getRows()); i += 1){
615 w_true = w[i] * w[i];
621 for(
unsigned int i=0; i<static_cast<unsigned int>(R.
getRows()); i++){
622 for(
unsigned int j=0; j<6; j++){
656 unsigned int nbInfos;
657 unsigned int nbFaceUsed;
660 if(nbInfos < 4 || nbFaceUsed == 0){
738 #ifdef VISP_HAVE_XML2
753 std::cout <<
" *********** Parsing XML for MBT KLT Tracker ************ " << std::endl;
754 xmlp.
parse(configFile);
786 vpTRACE(
"You need the libXML2 to read the config file %s", configFile);
802 const vpColor& col,
const unsigned int thickness,
const bool displayFullModel)
809 for (
unsigned int i = 0; i <
faces.
size(); i += 1){
810 if(displayFullModel ||
faces[i]->isVisible())
812 faces[i]->changeFrame(cMo_);
813 faces[i]->computeRoiClipped(c);
814 std::vector<std::pair<vpImagePoint,unsigned int> > roi;
815 faces[i]->getRoiClipped(c, roi);
817 for (
unsigned int j = 0; j < roi.size(); j += 1){
826 ip2 = roi[(j+1)%roi.size()].first;
833 faces[i]->displayPrimitive(I);
838 #ifdef VISP_HAVE_OGRE
856 const vpColor& col ,
const unsigned int thickness,
const bool displayFullModel)
863 for (
unsigned int i = 0; i <
faces.
size(); i += 1){
864 if(displayFullModel ||
faces[i]->isVisible())
866 faces[i]->changeFrame(cMo_);
867 faces[i]->computeRoiClipped(c);
868 std::vector<std::pair<vpImagePoint,unsigned int> > roi;
869 faces[i]->getRoiClipped(c, roi);
871 for (
unsigned int j = 0; j < roi.size(); j += 1){
880 ip2 = roi[(j+1)%roi.size()].first;
887 faces[i]->displayPrimitive(I);
892 #ifdef VISP_HAVE_OGRE
909 unsigned int nbTotalPoints = 0;
910 for (
unsigned int i = 0; i <
faces.
size(); i += 1){
911 if(
faces[i]->isVisible()){
912 nbTotalPoints +=
faces[i]->getNbPointsCur();
916 if(nbTotalPoints < 10){
917 std::cerr <<
"test tracking failed (too few points to realize a good tracking)." << std::endl;
919 "test tracking failed (too few points to realize a good tracking).");
923 #endif //VISP_HAVE_OPENCV
void setKltOpencv(const vpKltOpencv &t)
bool compute_interaction
If true, compute the interaction matrix at each iteration of the minimization. Otherwise, compute it only on the first iteration.
virtual void track(const vpImage< unsigned char > &I)
Implementation of a polygon of the model containing points of interest. It is used by the model-based...
vpMatrix get_K_inverse() const
void setWindowName(const Ogre::String &n)
int getPyramidLevels() const
Get the number of pyramid levels.
unsigned int getMaskBorder() const
void setQuality(const double &q)
Definition of the vpMatrix class.
vpMatrix covarianceMatrix
Covariance matrix.
double distFarClip
Distance for near clipping.
virtual ~vpMbKltTracker()
void setQuality(double input)
double getHarrisParam() const
void resize(const unsigned int nrows, const unsigned int ncols, const bool nullify=true)
void track(const IplImage *I)
double distNearClip
Distance for near clipping.
unsigned int clippingFlag
Flags specifying which clipping to used.
void MEstimator(const vpRobustEstimatorType method, const vpColVector &residues, vpColVector &weights)
Compute the weights according a residue vector and a PsiFunction.
int getMaxFeatures() const
Get Max number of features.
void parse(const char *filename)
unsigned int getWidth() const
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
void getCameraParameters(vpCameraParameters &_cam) const
unsigned int maxIter
The maximum iteration of the virtual visual servoing stage.
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
IplImage * cur
Temporary OpenCV image for fast conversion.
double getHarrisFreeParameter() const
Get Harris free parameter.
void setIdentity()
Basic initialisation (identity)
Class to define colors available for display functionnalities.
double percentGood
Percentage of good points, according to the initial number, that must have the tracker.
void setBackgroundSizeOgre(const unsigned int &h, const unsigned int &w)
vpHomogeneousMatrix cMo
The current pose.
bool postTracking(const vpImage< unsigned char > &I, vpColVector &w)
unsigned int getBlockSize() const
void setUseHarris(const int input)
double getNearClippingDistance() const
bool modelInitialised
Flag used to ensure that the CAD model is loaded before the initialisation.
error that can be emited by ViSP classes.
void computeJTR(const vpMatrix &J, const vpColVector &R, vpMatrix &JTR)
unsigned int getMaxFeatures() const
void setInitialGuess(CvPoint2D32f **guess_pts)
void setPyramidLevels(const int input)
void setBlockSize(const int input)
Definition of the vpSubMatrix vpSubMatrix class provides a mask on a vpMatrix all properties of vpMat...
unsigned int setVisibleOgre(const vpImage< unsigned char > &I, const vpCameraParameters &cam, const vpHomogeneousMatrix &cMo, const double &angleAppears, const double &angleDisappears, bool &changed)
bool computeCovariance
Flag used to specify if the covariance matrix has to be computed or not.
bool firstInitialisation
Flag to specify whether the init method is called the first or not (specific calls to realize in this...
static vpMatrix computeCovarianceMatrix(const vpMatrix &A, const vpColVector &x, const vpColVector &b)
virtual void reinit(const vpImage< unsigned char > &I)
virtual void testTracking()
bool hasNearClippingDistance() const
virtual void loadConfigFile(const std::string &configFile)
vpCameraParameters cam
The camera parameters.
unsigned int setVisible(const vpImage< unsigned char > &I, const vpCameraParameters &cam, const vpHomogeneousMatrix &cMo, const double &angle, bool &changed)
The vpRotationMatrix considers the particular case of a rotation matrix.
vpHomogeneousMatrix ctTc0
The estimated displacement of the pose between the current instant and the initial position...
double getFarClippingDistance() const
void initOgre(const vpCameraParameters &cam=vpCameraParameters())
bool hasFarClippingDistance() const
This class aims to compute the homography wrt.two images.
void addPoint(const unsigned int n, const vpPoint &P)
vpAROgre * getOgreContext()
std::vector< vpImagePoint > getKltImagePoints() const
void setPyramidLevels(const unsigned int &pL)
Parse an Xml file to extract configuration parameters of a Mbt Klt object.Data parser for the KLT mod...
unsigned int getWindowSize() const
Error that can be emited by the vpTracker class and its derivates.
double getAngleDisappear() const
Implementation of a polygon of the model used by the model-based tracker.
void getFeature(int index, int &id, float &x, float &y) const
void setWindowSize(const int input)
double getQuality() const
Get the quality of the tracker.
void changeFrame(const vpHomogeneousMatrix &cMo)
void setWindowSize(const unsigned int &w)
void setAngleDisappear(const double &adisappear)
void setTrackerId(int tid)
Definition of the vpSubColVector vpSubColVector class provides a mask on a vpColVector all properties...
unsigned int maskBorder
Erosion of the mask.
Generic class defining intrinsic camera parameters.
int getBlockSize() const
Get the block size.
std::map< int, vpImagePoint > getKltImagePointsWithId() const
bool useOgre
Use Ogre3d for visibility tests.
vpMbHiddenFaces< vpMbtKltPolygon > faces
Set of faces describing the object.
void extract(vpRotationMatrix &R) const
void setHarrisParam(const double &hp)
bool firstTrack
First track() called.
void setMinDistance(const double &mD)
void addPolygon(PolygonType *p)
vpKltOpencv tracker
Points tracker.
vpColVector getNormal() const
static double rad(double deg)
double threshold_outlier
Threshold below which the weight associated to a point to consider this one as an outlier...
void diag(const vpColVector &A)
void preTracking(const vpImage< unsigned char > &I, unsigned int &nbInfos, unsigned int &nbFaceUsed)
unsigned int size() const
double lambda
The gain of the virtual visual servoing stage.
void setMaxFeatures(const unsigned int &mF)
void initTracking(const IplImage *I, const IplImage *mask=NULL)
double getQuality() const
static double deg(double rad)
bool displayFeatures
If true, the features are displayed.
void setCameraParameters(const vpCameraParameters &cam)
Class that provides a data structure for the column vectors as well as a set of operations on these v...
virtual void setOgreVisibilityTest(const bool &v)
void setAngleAppear(const double &aappear)
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV.
vpHomogeneousMatrix inverse() const
virtual void setNearClippingDistance(const double &dist)
static vpHomogeneousMatrix direct(const vpColVector &v)
int getNbFeatures() const
Get the current number of features.
Contains an M-Estimator and various influence function.
virtual void initFaceFromCorners(const std::vector< vpPoint > &corners, const unsigned int indexFace=-1)
double angleAppears
Angle used to detect a face appearance.
unsigned int getHeight() const
virtual void setClipping(const unsigned int &flags)
vpMatrix pseudoInverse(double svThreshold=1e-6) const
Compute the pseudo inverse of the matrix using the SVD.
virtual void init(const vpImage< unsigned char > &I)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
virtual void displayLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)=0
void setBlockSize(const unsigned int &bs)
This class defines the container for a plane geometrical structure.
double getMinDistance() const
Get Min Distance.
void displayOgre(const vpHomogeneousMatrix &cMo)
double getAngleAppear() const
void setMaxFeatures(const int input)
double angleDisappears
Angle used to detect a face disappearance.
void setThreshold(const double noise_threshold)
unsigned int getRows() const
Return the number of rows of the matrix.
double getMinDistance() const
virtual void setNbPoint(const unsigned int nb)
vpColVector & normalize()
normalise the vector
bool getFovClipping() const
void setMinDistance(double input)
void setIteration(const unsigned int iter)
Set iteration.
void computeVVS(const unsigned int &nbInfos, vpColVector &w)
Class that consider the case of a translation vector.
void setMaskBorder(const unsigned int &mb)
vpHomogeneousMatrix c0Mo
Initial pose.
int getWindowSize() const
Get Max number of features.
virtual void setFarClippingDistance(const double &dist)
unsigned int getPyramidLevels() const
virtual 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)
void setHarrisFreeParameter(double input)
virtual void setPose(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cdMo)
void resize(const unsigned int i, const bool flagNullify=true)
std::vector< PolygonType * > & getPolygon()
virtual void setIndex(const int i)
void computeFov(const unsigned int &w, const unsigned int &h)