42 #include <visp/vpImageConvert.h>
43 #include <visp/vpMbKltTracker.h>
44 #include <visp/vpVelocityTwistMatrix.h>
45 #include <visp/vpTrackingException.h>
47 #if (defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100))
51 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
56 c0Mo(), compute_interaction(true),
57 firstInitialisation(true), maskBorder(5), lambda(0.8), maxIter(200), threshold_outlier(0.5),
58 percentGood(0.6), ctTc0(), tracker(), firstTrack(false), kltPolygons(), cylinders_disp(), circles_disp()
84 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
93 for(std::list<vpMbtDistanceKltPoints*>::const_iterator it=
kltPolygons.begin(); it!=
kltPolygons.end(); ++it){
132 bool reInitialisation =
false;
136 #ifdef VISP_HAVE_OGRE
163 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
164 cv::Mat mask((
int)I.
getRows(), (int)I.
getCols(), CV_8UC1, cv::Scalar(0));
166 IplImage* mask = cvCreateImage(cvSize((
int)I.
getWidth(), (int)I.
getHeight()), IPL_DEPTH_8U, 1);
169 unsigned char val = 255;
172 for(std::list<vpMbtDistanceKltPoints*>::const_iterator it=
kltPolygons.begin(); it!=
kltPolygons.end(); ++it){
184 for(std::list<vpMbtDistanceKltPoints*>::const_iterator it=
kltPolygons.begin(); it!=
kltPolygons.end(); ++it){
190 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
191 cvReleaseImage(&mask);
204 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
206 cvReleaseImage(&
cur);
213 for(std::list<vpMbtDistanceKltPoints*>::const_iterator it=
kltPolygons.begin(); it!=
kltPolygons.end(); ++it){
274 #ifdef VISP_HAVE_OGRE
286 std::vector<vpImagePoint>
289 std::vector<vpImagePoint> kltPoints;
307 std::map<int, vpImagePoint>
310 std::map<int, vpImagePoint> kltPoints;
350 for(std::list<vpMbtDistanceKltPoints*>::const_iterator it=
kltPolygons.begin(); it!=
kltPolygons.end(); ++it){
372 bool reInitialisation =
false;
376 #ifdef VISP_HAVE_OGRE
382 if(reInitialisation){
383 std::cout <<
"WARNING: Visibility changed, must reinitialize to update pose" << std::endl;
390 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
391 std::vector<cv::Point2f> initial_pts;
392 std::vector<long> initial_ids;
394 unsigned int nbp = 0;
395 for(std::list<vpMbtDistanceKltPoints*>::const_iterator it=
kltPolygons.begin(); it!=
kltPolygons.end(); ++it) {
401 CvPoint2D32f* initial_pts = NULL;
402 initial_pts = (CvPoint2D32f*)cvAlloc(nbp*
sizeof(initial_pts[0]));
403 long *initial_ids =
new long [nbp];
404 unsigned int iter_points = 0;
415 unsigned int nbCur = 0;
417 for(std::list<vpMbtDistanceKltPoints*>::const_iterator it=
kltPolygons.begin(); it!=
kltPolygons.end(); ++it) {
429 double invDc = 1.0 / plan.
getD();
433 vpGEMM(cdtc, Nc, -invDc, cdRc, 1.0, cdHc, VP_GEMM_B_T);
440 std::map<int, vpImagePoint>::const_iterator iter = kltpoly->
getCurrentPoints().begin();
444 cdp[0] = iter->second.get_j(); cdp[1] = iter->second.get_i(); cdp[2] = 1.0;
446 double p_mu_t_2 = cdp[0] * cdGc[2][0] + cdp[1] * cdGc[2][1] + cdGc[2][2];
448 if( fabs(p_mu_t_2) < std::numeric_limits<double>::epsilon()){
454 cdp[0] = (cdp[0] * cdGc[0][0] + cdp[1] * cdGc[0][1] + cdGc[0][2]) / p_mu_t_2;
455 cdp[1] = (cdp[0] * cdGc[1][0] + cdp[1] * cdGc[1][1] + cdGc[1][2]) / p_mu_t_2;
458 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
459 cv::Point2f p((
float)cdp[0], (
float)cdp[1]);
460 initial_pts.push_back(p);
463 initial_pts[iter_points].x = (float)cdp[0];
464 initial_pts[iter_points].y = (float)cdp[1];
473 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
478 if(initial_pts) cvFree(&initial_pts);
480 delete [] initial_ids;
483 for(std::list<vpMbtDistanceKltPoints*>::const_iterator it=
kltPolygons.begin(); it!=
kltPolygons.end(); ++it){
550 for(std::list<vpMbtDistanceKltPoints*>::const_iterator it=
kltPolygons.begin(); it!=
kltPolygons.end(); ++it){
571 bool reInitialisation =
false;
573 unsigned int initialNumber = 0;
574 unsigned int currentNumber = 0;
575 unsigned int shift = 0;
578 for(std::list<vpMbtDistanceKltPoints*>::const_iterator it=
kltPolygons.begin(); it!=
kltPolygons.end(); ++it){
597 double value =
percentGood * (double)initialNumber;
598 if((
double)currentNumber < value){
600 reInitialisation =
true;
606 #ifdef VISP_HAVE_OGRE
644 double normRes_1 = -1;
645 unsigned int iter = 0;
648 L.
resize(2*nbInfos, 6, 0);
650 while( ((
int)((normRes - normRes_1)*1e8) != 0 ) && (iter<
maxIter) ){
652 unsigned int shift = 0;
655 for(std::list<vpMbtDistanceKltPoints*>::const_iterator it=
kltPolygons.begin(); it!=
kltPolygons.end(); ++it){
689 LVJ_true = (L*cVo*
oJo);
695 for (
unsigned int i = 0; i < static_cast<unsigned int>(R.
getRows()); i += 1){
702 for(
unsigned int i=0; i<static_cast<unsigned int>(R.
getRows()); i++){
703 for(
unsigned int j=0; j<6; j++){
756 unsigned int nbInfos = 0;
757 unsigned int nbFaceUsed = 0;
766 if(nbInfos < 4 || nbFaceUsed == 0){
844 #ifdef VISP_HAVE_XML2
859 std::cout <<
" *********** Parsing XML for MBT KLT Tracker ************ " << std::endl;
860 xmlp.
parse(configFile);
892 vpTRACE(
"You need the libXML2 to read the config file %s", configFile);
908 const vpColor& col,
const unsigned int thickness,
const bool displayFullModel)
917 for(std::list<vpMbtDistanceKltPoints*>::const_iterator it=
kltPolygons.begin(); it!=
kltPolygons.end(); ++it){
923 std::vector<std::pair<vpImagePoint,unsigned int> > roi;
926 for (
unsigned int j = 0; j < roi.size(); j += 1){
935 ip2 = roi[(j+1)%roi.size()].first;
948 (*it)->display(I, cMo_, camera, col, thickness);
952 (*it)->display(I, cMo_, camera, col, thickness);
955 #ifdef VISP_HAVE_OGRE
973 const vpColor& col ,
const unsigned int thickness,
const bool displayFullModel)
982 for(std::list<vpMbtDistanceKltPoints*>::const_iterator it=
kltPolygons.begin(); it!=
kltPolygons.end(); ++it){
988 std::vector<std::pair<vpImagePoint,unsigned int> > roi;
991 for (
unsigned int j = 0; j < roi.size(); j += 1){
1000 ip2 = roi[(j+1)%roi.size()].first;
1013 (*it)->display(I, cMo_, camera, col, thickness);
1017 (*it)->display(I, cMo_, camera, col, thickness);
1020 #ifdef VISP_HAVE_OGRE
1037 unsigned int nbTotalPoints = 0;
1040 for(std::list<vpMbtDistanceKltPoints*>::const_iterator it=
kltPolygons.begin(); it!=
kltPolygons.end(); ++it){
1047 if(nbTotalPoints < 10){
1048 std::cerr <<
"test tracking failed (too few points to realize a good tracking)." << std::endl;
1050 "test tracking failed (too few points to realize a good tracking).");
1065 const std::string &name)
1081 bool already_here = false ;
1114 const int ,
const std::string &name)
1131 bool already_here = false ;
1183 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
1185 cvReleaseImage(&
cur);
1199 #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.
void addCircle(const vpPoint &P1, const vpPoint &P2, const vpPoint &P3, const double r, const std::string &name="")
virtual void track(const vpImage< unsigned char > &I)
unsigned int getCols() const
vpMatrix get_K_inverse() const
void setWindowName(const Ogre::String &n)
std::list< vpMbtDistanceKltPoints * > kltPolygons
Vector of the cylinders used here only to display the full model.
int getPyramidLevels() const
Get the list of features id.
unsigned int getMaskBorder() const
void setQuality(const double &q)
Definition of the vpMatrix class.
virtual ~vpMbKltTracker()
double getHarrisParam() const
void resize(const unsigned int nrows, const unsigned int ncols, const bool nullify=true)
void changeFrame(const vpHomogeneousMatrix &cMo)
void reInitModel(const vpImage< unsigned char > &I, const std::string &cad_name, const vpHomogeneousMatrix &cMo_, const bool verbose=false)
void setCameraParameters(const vpCameraParameters &camera)
void init(const vpKltOpencv &_tracker)
void MEstimator(const vpRobustEstimatorType method, const vpColVector &residues, vpColVector &weights)
Compute the weights according a residue vector and a PsiFunction.
void setHarrisFreeParameter(double harris_k)
int getMaxFeatures() const
Get the list of lost feature.
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
vpMbHiddenFaces< vpMbtPolygon > faces
Set of faces describing the object.
unsigned int maxIter
The maximum iteration of the virtual visual servoing stage.
void removeOutliers(const vpColVector &weight, const double &threshold_outlier)
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
double getHarrisFreeParameter() const
Get the free parameter of the Harris detector.
void setCameraParameters(const vpCameraParameters &camera)
void setIdentity()
Basic initialisation (identity)
virtual void initFaceFromCorners(vpMbtPolygon &polygon)
Class to define colors available for display functionnalities.
void setMaxFeatures(const int maxCount)
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)
virtual void initCylinder(const vpPoint &, const vpPoint &, const double, const int, const std::string &name="")
vpHomogeneousMatrix cMo
The current pose.
bool postTracking(const vpImage< unsigned char > &I, vpColVector &w)
unsigned int getBlockSize() const
bool hasEnoughPoints() const
void getRoiClipped(std::vector< vpPoint > &points)
double getNearClippingDistance() const
bool modelInitialised
Flag used to ensure that the CAD model is loaded before the initialisation.
void setMinDistance(double minDistance)
void buildFrom(const vpPoint &_p1, const vpPoint &_p2, const vpPoint &_p3, const double r)
error that can be emited by ViSP classes.
void computeJTR(const vpMatrix &J, const vpColVector &R, vpMatrix &JTR)
Manage a cylinder used in the model-based tracker.
unsigned int getMaxFeatures() const
bool useOgre
Use Ogre3d for visibility tests.
virtual void initFromPose(const vpImage< unsigned char > &I, const std::string &initFile)
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...
virtual void reinit(const vpImage< unsigned char > &I)
Implementation of a polygon of the model containing points of interest. It is used by the model-based...
virtual void testTracking()
bool hasNearClippingDistance() const
Class that defines what is a point.
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.
void setQuality(double qualityLevel)
vpHomogeneousMatrix ctTc0
The estimated displacement of the pose between the current instant and the initial position...
std::map< int, int > & getCurrentPointsInd()
double getFarClippingDistance() const
void initOgre(const vpCameraParameters &cam=vpCameraParameters())
bool hasFarClippingDistance() const
unsigned int computeNbDetectedCurrent(const vpKltOpencv &_tracker)
This class aims to compute the homography wrt.two images.
vpAROgre * getOgreContext()
unsigned int getRows() const
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...
Manage a circle used in the model-based tracker.
vpMatrix oJo
The Degrees of Freedom to estimate.
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 setName(const std::string &circle_name)
double getQuality() const
Get the parameter characterizing the minimal accepted quality of image corners.
void changeFrame(const vpHomogeneousMatrix &cMo)
void displayPrimitive(const vpImage< unsigned char > &_I)
void setWindowSize(const unsigned int &w)
vpVelocityTwistMatrix buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
void setAngleDisappear(const double &adisappear)
vpColVector m_error
Error s-s*.
void setTrackerId(int tid)
Does nothing. Just here for compat with previous releases that use OpenCV C api to do the tracking...
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 size of the averaging block used to track the features.
void getFeature(const int &index, int &id, float &x, float &y) const
std::map< int, vpImagePoint > getKltImagePointsWithId() const
unsigned int getInitialNumberPoint() const
void extract(vpRotationMatrix &R) const
void setHarrisParam(const double &hp)
Class that consider the particular case of twist transformation matrix that allows to transform a vel...
bool firstTrack
First track() called.
void computeRoiClipped(const vpCameraParameters &cam=vpCameraParameters())
void setMinDistance(const double &mD)
void initTracking(const cv::Mat &I, const cv::Mat &mask=cv::Mat())
double angleAppears
Angle used to detect a face appearance.
vpKltOpencv tracker
Points tracker.
virtual bool isVisible(const vpHomogeneousMatrix &cMo, const double alpha, const bool &modulo=false, const vpCameraParameters &cam=vpCameraParameters(), const vpImage< unsigned char > &I=vpImage< unsigned char >())
vpColVector getNormal() const
void setPyramidLevels(const int pyrMaxLevel)
std::map< int, vpImagePoint > & getCurrentPoints()
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)
void computeCovarianceMatrix(const vpHomogeneousMatrix &cMoPrev, const vpColVector &deltaS, const vpMatrix &Ls, const vpMatrix &W)
cv::Mat cur
Temporary OpenCV image for fast conversion.
double lambda
The gain of the virtual visual servoing stage.
void setMaxFeatures(const unsigned int &mF)
void computeHomography(const vpHomogeneousMatrix &_cTc0, vpHomography &cHc0)
virtual void initCircle(const vpPoint &, const vpPoint &, const vpPoint &, const double, const int, const std::string &name="")
void setWindowSize(const int winSize)
double getQuality() const
std::list< vpMbtDistanceCircle * > circles_disp
Vector of the circles used here only to display the full model.
static double deg(double rad)
virtual void setCameraParameters(const vpCameraParameters &_cam)
bool displayFeatures
If true, the features are displayed.
virtual void loadModel(const char *modelFile, const bool verbose=false)
void buildFrom(const vpPoint &_p1, const vpPoint &_p2, const double r)
void setCameraParameters(const vpCameraParameters &cam)
std::list< vpMbtDistanceCylinder * > cylinders_disp
Vector of the cylinders used here only to display the full model.
Class that provides a data structure for the column vectors as well as a set of operations on these v...
void setAngleAppear(const double &aappear)
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV.
vpHomogeneousMatrix inverse() const
void setBlockSize(const int blockSize)
static vpHomogeneousMatrix direct(const vpColVector &v)
int getNbFeatures() const
Get the number of current features.
Contains an M-Estimator and various influence function.
double angleDisappears
Angle used to detect a face disappearance.
virtual void initFaceFromLines(vpMbtPolygon &polygon)
vpMbtPolygon * polygon
Pointer to the polygon that define a face.
unsigned int getHeight() const
vpMatrix pseudoInverse(double svThreshold=1e-6) const
Compute the pseudo inverse of the matrix using the SVD.
virtual void setClipping(const unsigned int &flags)
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 setUseHarris(const int useHarrisDetector)
void setBlockSize(const unsigned int &bs)
This class defines the container for a plane geometrical structure.
unsigned int clippingFlag
Flags specifying which clipping to used.
unsigned int getNbPoint() const
double getMinDistance() const
Get the minimal Euclidean distance between detected corners during initialization.
void displayOgre(const vpHomogeneousMatrix &cMo)
double getAngleAppear() const
void addCylinder(const vpPoint &P1, const vpPoint &P2, const double r, const std::string &name="")
unsigned int getCurrentNumberPoints() const
void setThreshold(const double noise_threshold)
unsigned int getRows() const
Return the number of rows of the matrix.
virtual void setFarClippingDistance(const double &dist)
double getMinDistance() const
void setName(const std::string &cyl_name)
vpColVector & normalize()
Normalise the vector.
bool getFovClipping() const
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.
bool isoJoIdentity
Boolean to know if oJo is identity (for fast computation)
void setMaskBorder(const unsigned int &mb)
void computeInteractionMatrixAndResidu(vpColVector &_R, vpMatrix &_J)
vpHomogeneousMatrix c0Mo
Initial pose.
int getWindowSize() const
Get the window size used to refine the corner locations.
void track(const cv::Mat &I)
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)
virtual void setPose(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cdMo)
void updateMask(cv::Mat &mask, unsigned char _nb=255, unsigned int _shiftBorder=0)
void resize(const unsigned int i, const bool flagNullify=true)
vpColVector m_w
Weights used in the robust scheme.
virtual void setNearClippingDistance(const double &dist)
void computeFov(const unsigned int &w, const unsigned int &h)
vpPoint * p
corners in the object frame