42 #include <visp/vpMbtDistanceKltPoints.h>
44 #if (defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100))
51 : H(), N(), N_cur(), invd0(1.), cRc0_0n(), initPoints(), curPoints(), curPointsInd(),
52 nbPointsCur(0), nbPointsInit(0), minNbPoint(4), enoughPoints(false), dt(1.), d0(1.),
55 initPoints = std::map<int, vpImagePoint>();
56 curPoints = std::map<int, vpImagePoint>();
57 curPointsInd = std::map<int, int>();
80 initPoints = std::map<int, vpImagePoint>();
81 curPoints = std::map<int, vpImagePoint>();
82 curPointsInd = std::map<int, int>();
83 std::vector<vpImagePoint> roi;
86 for (
unsigned int i = 0; i < static_cast<unsigned int>(_tracker.
getNbFeatures()); i ++){
91 if(isInside(roi, y_tmp, x_tmp)){
94 curPointsInd[id] = (int)i;
100 if(nbPointsCur >= minNbPoint) enoughPoints =
true;
101 else enoughPoints =
false;
107 N = plan.getNormal();
127 curPoints = std::map<int, vpImagePoint>();
128 curPointsInd = std::map<int, int>();
130 for (
unsigned int i = 0; i < static_cast<unsigned int>(_tracker.
getNbFeatures()); i++){
132 if(isTrackedFeature(
id)){
133 curPoints[id] =
vpImagePoint(static_cast<double>(y),static_cast<double>(x));
134 curPointsInd[id] = (int)i;
139 if(nbPointsCur >= minNbPoint) enoughPoints =
true;
140 else enoughPoints =
false;
158 unsigned int index_ = 0;
160 std::map<int, vpImagePoint>::const_iterator iter = curPoints.begin();
161 for( ; iter != curPoints.end(); iter++){
163 double i_cur(iter->second.get_i()), j_cur(iter->second.get_j());
165 double x_cur(0), y_cur(0);
172 double x0_transform, y0_transform ;
173 computeP_mu_t(x0, y0, x0_transform, y0_transform, H );
175 double invZ = compute_1_over_Z(x_cur, y_cur);
177 _J[2*index_][0] = - invZ;
179 _J[2*index_][2] = x_cur * invZ;
180 _J[2*index_][3] = x_cur * y_cur;
181 _J[2*index_][4] = -(1+x_cur*x_cur);
182 _J[2*index_][5] = y_cur;
184 _J[2*index_+1][0] = 0;
185 _J[2*index_+1][1] = - invZ;
186 _J[2*index_+1][2] = y_cur * invZ;
187 _J[2*index_+1][3] = (1+y_cur*y_cur);
188 _J[2*index_+1][4] = - y_cur * x_cur;
189 _J[2*index_+1][5] = - x_cur;
191 _R[2*index_] = (x0_transform - x_cur);
192 _R[2*index_+1] = (y0_transform - y_cur);
198 vpMbtDistanceKltPoints::compute_1_over_Z(
const double x,
const double y)
200 double num = cRc0_0n[0] * x + cRc0_0n[1] * y + cRc0_0n[2];
201 double den = -(d0 - dt);
218 vpMbtDistanceKltPoints::computeP_mu_t(
const double x_in,
const double y_in,
double& x_out,
double& y_out,
const vpMatrix& _cHc0)
220 double p_mu_t_2 = x_in * _cHc0[2][0] + y_in * _cHc0[2][1] + _cHc0[2][2];
222 if( fabs(p_mu_t_2) < std::numeric_limits<double>::epsilon()){
228 x_out = (x_in * _cHc0[0][0] + y_in * _cHc0[0][1] + _cHc0[0][2]) / p_mu_t_2;
229 y_out = (x_in * _cHc0[1][0] + y_in * _cHc0[1][1] + _cHc0[1][2]) / p_mu_t_2;
255 vpGEMM(ctransc0, N, -invd0, cRc0, 1.0, cHc0, VP_GEMM_B_T);
278 for (
unsigned int i = 0; i < 3; i += 1){
279 dt += ctransc0[i] * (N_cur[i]);
291 vpMbtDistanceKltPoints::isTrackedFeature(
const int _id)
301 std::map<int, vpImagePoint>::iterator iter = initPoints.find(_id);
302 if(iter != initPoints.end())
318 #
if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
323 unsigned char nb,
unsigned int shiftBorder)
325 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
326 int width = mask.cols;
327 int height = mask.rows;
329 int width = mask->width;
330 int height = mask->height;
333 int i_min, i_max, j_min, j_max;
334 std::vector<vpImagePoint> roi;
352 double shiftBorder_d = (double) shiftBorder;
353 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
354 for(
int i=i_min; i< i_max; i++){
355 double i_d = (double) i;
356 for(
int j=j_min; j< j_max; j++){
357 double j_d = (double) j;
358 if(shiftBorder != 0){
359 if( vpMbtDistanceKltPoints::isInside(roi, i_d, j_d)
360 && vpMbtDistanceKltPoints::isInside(roi, i_d+shiftBorder_d, j_d+shiftBorder_d)
361 && vpMbtDistanceKltPoints::isInside(roi, i_d-shiftBorder_d, j_d+shiftBorder_d)
362 && vpMbtDistanceKltPoints::isInside(roi, i_d+shiftBorder_d, j_d-shiftBorder_d)
363 && vpMbtDistanceKltPoints::isInside(roi, i_d-shiftBorder_d, j_d-shiftBorder_d) ){
364 mask.at<
unsigned char>(i,j) = nb;
368 if(vpMbtDistanceKltPoints::isInside(roi, i, j)){
369 mask.at<
unsigned char>(i,j) = nb;
375 unsigned char* ptrData = (
unsigned char*)mask->imageData + i_min*mask->widthStep+j_min;
376 for(
int i=i_min; i< i_max; i++){
377 double i_d = (double) i;
378 for(
int j=j_min; j< j_max; j++){
379 double j_d = (double) j;
380 if(shiftBorder != 0){
381 if( vpMbtDistanceKltPoints::isInside(roi, i_d, j_d)
382 && vpMbtDistanceKltPoints::isInside(roi, i_d+shiftBorder_d, j_d+shiftBorder_d)
383 && vpMbtDistanceKltPoints::isInside(roi, i_d-shiftBorder_d, j_d+shiftBorder_d)
384 && vpMbtDistanceKltPoints::isInside(roi, i_d+shiftBorder_d, j_d-shiftBorder_d)
385 && vpMbtDistanceKltPoints::isInside(roi, i_d-shiftBorder_d, j_d-shiftBorder_d) ){
393 if(vpMbtDistanceKltPoints::isInside(roi, i, j)){
401 ptrData += mask->widthStep - j_max + j_min;
416 std::map<int, vpImagePoint> tmp;
417 std::map<int, int> tmp2;
418 unsigned int nbSupp = 0;
422 std::map<int, vpImagePoint>::const_iterator iter = curPoints.begin();
423 for( ; iter != curPoints.end(); iter++){
424 if(_w[k] > threshold_outlier && _w[k+1] > threshold_outlier){
426 tmp[iter->first] =
vpImagePoint(iter->second.get_i(), iter->second.get_j());
427 tmp2[iter->first] = curPointsInd[iter->first];
432 initPoints.erase(iter->first);
439 curPoints = std::map<int, vpImagePoint>();
440 curPointsInd = std::map<int, int>();
444 if(nbPointsCur >= minNbPoint) enoughPoints =
true;
445 else enoughPoints =
false;
457 std::map<int, vpImagePoint>::const_iterator iter = curPoints.begin();
458 for( ; iter != curPoints.end(); iter++){
461 iP.
set_i(static_cast<double>(iter->second.get_i()));
462 iP.
set_j(static_cast<double>(iter->second.get_j()));
469 sprintf(ide,
"%ld", static_cast<long int>(
id));
482 std::map<int, vpImagePoint>::const_iterator iter = curPoints.begin();
483 for( ; iter != curPoints.end(); iter++){
486 iP.
set_i(static_cast<double>(iter->second.get_i()));
487 iP.
set_j(static_cast<double>(iter->second.get_j()));
494 sprintf(ide,
"%ld", static_cast<long int>(
id));
504 vpMbtDistanceKltPoints::intersect(
const vpImagePoint& p1,
const vpImagePoint& p2,
const double i_test,
const double j_test,
const double i,
const double j)
508 double ex = j - j_test;
509 double ey = i - i_test;
511 double den = dx * ey - dy * ex;
514 if(std::fabs(den) > std::fabs(den)*std::numeric_limits<double>::epsilon()){
515 t = -( ey * ( p1.
get_j() - j_test ) + ex * ( -p1.
get_i() + i_test ) ) / den;
516 u = -( dx * ( -p1.
get_i() + i_test ) + dy * ( p1.
get_j() - j_test ) ) / den;
521 return ( t >= std::numeric_limits<double>::epsilon() && t < 1.0 && u >= std::numeric_limits<double>::epsilon() && u < 1.0);
525 vpMbtDistanceKltPoints::isInside(
const std::vector<vpImagePoint>& roi,
const double i,
const double j)
527 double i_test = 100000.;
528 double j_test = 100000.;
529 unsigned int nbInter = 0;
530 bool computeAgain =
true;
533 computeAgain =
false;
534 for(
unsigned int k=0; k< roi.size(); k++){
536 if(vpMbtDistanceKltPoints::intersect(roi[k], roi[(k+1)%roi.size()], i, j, i_test, j_test)){
552 return ((nbInter%2) == 1);
Definition of the vpMatrix class.
void init(const vpKltOpencv &_tracker)
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...
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
void getRoiClipped(std::vector< vpPoint > &points)
vpPoint & getPoint(const unsigned int _index)
virtual ~vpMbtDistanceKltPoints()
error that can be emited by ViSP classes.
static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)
Point coordinates conversion from pixel coordinates to normalized coordinates in meter...
static int round(const double x)
The vpRotationMatrix considers the particular case of a rotation matrix.
unsigned int computeNbDetectedCurrent(const vpKltOpencv &_tracker)
This class aims to compute the homography wrt.two images.
void set_i(const double ii)
void displayPrimitive(const vpImage< unsigned char > &_I)
virtual void displayCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)=0
void getFeature(const int &index, int &id, float &x, float &y) const
void extract(vpRotationMatrix &R) const
void computeHomography(const vpHomogeneousMatrix &_cTc0, vpHomography &cHc0)
void set_j(const double jj)
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV.
int getNbFeatures() const
Get the number of current features.
static void getMinMaxRoi(const std::vector< vpImagePoint > &roi, int &i_min, int &i_max, int &j_min, int &j_max)
vpMbtPolygon * polygon
Pointer to the polygon that define a face.
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
This class defines the container for a plane geometrical structure.
vpColVector & normalize()
Normalise the vector.
Class that consider the case of a translation vector.
void computeInteractionMatrixAndResidu(vpColVector &_R, vpMatrix &_J)
void updateMask(cv::Mat &mask, unsigned char _nb=255, unsigned int _shiftBorder=0)