43 #include <visp/vpMbtKltPolygon.h>
45 #ifdef VISP_HAVE_OPENCV
52 : H(), N(), N_cur(), invd0(1.), cRc0_0n(), initPoints(), curPoints(), curPointsInd(),
53 nbPointsCur(0), nbPointsInit(0), minNbPoint(4), enoughPoints(false), dt(1.), d0(1.), cam()
55 initPoints = std::map<int, vpImagePoint>();
56 curPoints = std::map<int, vpImagePoint>();
57 curPointsInd = std::map<int, int>();
82 initPoints = std::map<int, vpImagePoint>();
83 curPoints = std::map<int, vpImagePoint>();
84 curPointsInd = std::map<int, int>();
85 std::vector<vpImagePoint> roi;
88 for (
unsigned int i = 0; i < static_cast<unsigned int>(_tracker.
getNbFeatures()); i ++){
93 if(isInside(roi, y_tmp, x_tmp)){
96 curPointsInd[id] = (int)i;
126 curPoints = std::map<int, vpImagePoint>();
127 curPointsInd = std::map<int, int>();
129 for (
unsigned int i = 0; i < static_cast<unsigned int>(_tracker.
getNbFeatures()); i++){
131 if(isTrackedFeature(
id)){
132 curPoints[id] =
vpImagePoint(static_cast<double>(y),static_cast<double>(x));
133 curPointsInd[id] = (int)i;
138 if(nbPointsCur >= minNbPoint) enoughPoints =
true;
139 else enoughPoints =
false;
157 unsigned int index_ = 0;
159 std::map<int, vpImagePoint>::const_iterator iter = curPoints.begin();
160 for( ; iter != curPoints.end(); iter++){
162 double i_cur(iter->second.get_i()), j_cur(iter->second.get_j());
164 double x_cur(0), y_cur(0);
171 double x0_transform, y0_transform ;
172 computeP_mu_t(x0, y0, x0_transform, y0_transform, H );
174 double invZ = compute_1_over_Z(x_cur, y_cur);
176 _J[2*index_][0] = - invZ;
178 _J[2*index_][2] = x_cur * invZ;
179 _J[2*index_][3] = x_cur * y_cur;
180 _J[2*index_][4] = -(1+x_cur*x_cur);
181 _J[2*index_][5] = y_cur;
183 _J[2*index_+1][0] = 0;
184 _J[2*index_+1][1] = - invZ;
185 _J[2*index_+1][2] = y_cur * invZ;
186 _J[2*index_+1][3] = (1+y_cur*y_cur);
187 _J[2*index_+1][4] = - y_cur * x_cur;
188 _J[2*index_+1][5] = - x_cur;
190 _R[2*index_] = (x0_transform - x_cur);
191 _R[2*index_+1] = (y0_transform - y_cur);
197 vpMbtKltPolygon::compute_1_over_Z(
const double x,
const double y)
199 double num = cRc0_0n[0] * x + cRc0_0n[1] * y + cRc0_0n[2];
200 double den = -(d0 - dt);
217 vpMbtKltPolygon::computeP_mu_t(
const double x_in,
const double y_in,
double& x_out,
double& y_out,
const vpMatrix& _cHc0)
219 double p_mu_t_2 = x_in * _cHc0[2][0] + y_in * _cHc0[2][1] + _cHc0[2][2];
221 if( fabs(p_mu_t_2) < std::numeric_limits<double>::epsilon()){
227 x_out = (x_in * _cHc0[0][0] + y_in * _cHc0[0][1] + _cHc0[0][2]) / p_mu_t_2;
228 y_out = (x_in * _cHc0[1][0] + y_in * _cHc0[1][1] + _cHc0[1][2]) / p_mu_t_2;
254 vpGEMM(ctransc0, N, -invd0, cRc0, 1.0, cHc0, VP_GEMM_B_T);
277 for (
unsigned int i = 0; i < 3; i += 1){
278 dt += ctransc0[i] * (N_cur[i]);
290 vpMbtKltPolygon::isTrackedFeature(
const int _id)
300 std::map<int, vpImagePoint>::iterator iter = initPoints.find(_id);
301 if(iter != initPoints.end())
318 int width = _mask->width;
319 int i_min, i_max, j_min, j_max;
320 cam.
computeFov((
unsigned)_mask->width, (
unsigned)_mask->height);
322 std::vector<vpImagePoint> roi;
327 if(i_min > _mask->height){
330 if(i_max > _mask->height){
331 i_max = _mask->height;
333 if(j_min > _mask->width){
336 if(j_max > _mask->width){
337 j_max = _mask->width;
340 double shiftBorder_d = (double) _shiftBorder;
341 unsigned char* ptrData = (
unsigned char*)_mask->imageData + i_min*width+j_min;
342 for(
int i=i_min; i< i_max; i++){
343 double i_d = (double) i;
344 for(
int j=j_min; j< j_max; j++){
345 double j_d = (double) j;
346 if(_shiftBorder != 0){
347 if( vpMbtKltPolygon::isInside(roi, i_d, j_d)
348 && vpMbtKltPolygon::isInside(roi, i_d+shiftBorder_d, j_d+shiftBorder_d)
349 && vpMbtKltPolygon::isInside(roi, i_d-shiftBorder_d, j_d+shiftBorder_d)
350 && vpMbtKltPolygon::isInside(roi, i_d+shiftBorder_d, j_d-shiftBorder_d)
351 && vpMbtKltPolygon::isInside(roi, i_d-shiftBorder_d, j_d-shiftBorder_d) ){
359 if(vpMbtKltPolygon::isInside(roi, i, j)){
367 ptrData += width - j_max + j_min;
382 std::map<int, vpImagePoint> tmp;
383 std::map<int, int> tmp2;
384 unsigned int nbSupp = 0;
388 std::map<int, vpImagePoint>::const_iterator iter = curPoints.begin();
389 for( ; iter != curPoints.end(); iter++){
390 if(_w[k] > threshold_outlier && _w[k+1] > threshold_outlier){
392 tmp[iter->first] =
vpImagePoint(iter->second.get_i(), iter->second.get_j());
393 tmp2[iter->first] = curPointsInd[iter->first];
398 initPoints.erase(iter->first);
405 curPoints = std::map<int, vpImagePoint>();
406 curPointsInd = std::map<int, int>();
410 if(nbPointsCur >= minNbPoint) enoughPoints =
true;
411 else enoughPoints =
false;
423 std::map<int, vpImagePoint>::const_iterator iter = curPoints.begin();
424 for( ; iter != curPoints.end(); iter++){
427 iP.
set_i(static_cast<double>(iter->second.get_i()));
428 iP.
set_j(static_cast<double>(iter->second.get_j()));
435 sprintf(ide,
"%ld", static_cast<long int>(
id));
448 std::map<int, vpImagePoint>::const_iterator iter = curPoints.begin();
449 for( ; iter != curPoints.end(); iter++){
452 iP.
set_i(static_cast<double>(iter->second.get_i()));
453 iP.
set_j(static_cast<double>(iter->second.get_j()));
460 sprintf(ide,
"%ld", static_cast<long int>(
id));
469 bool vpMbtKltPolygon::intersect(
const vpImagePoint& p1,
const vpImagePoint& p2,
const double i_test,
const double j_test,
const double i,
const double j)
473 double ex = j - j_test;
474 double ey = i - i_test;
476 double den = dx * ey - dy * ex;
479 if(std::fabs(den) > std::fabs(den)*std::numeric_limits<double>::epsilon()){
480 t = -( ey * ( p1.
get_j() - j_test ) + ex * ( -p1.
get_i() + i_test ) ) / den;
481 u = -( dx * ( -p1.
get_i() + i_test ) + dy * ( p1.
get_j() - j_test ) ) / den;
486 return ( t >= std::numeric_limits<double>::epsilon() && t < 1.0 && u >= std::numeric_limits<double>::epsilon() && u < 1.0);
489 bool vpMbtKltPolygon::isInside(
const std::vector<vpImagePoint>& roi,
const double i,
const double j)
491 double i_test = 100000.;
492 double j_test = 100000.;
493 unsigned int nbInter = 0;
494 bool computeAgain =
true;
497 computeAgain =
false;
498 for(
unsigned int k=0; k< roi.size(); k++){
500 if(vpMbtKltPolygon::intersect(roi[k], roi[(k+1)%roi.size()], i, j, i_test, j_test)){
516 return ((nbInter%2) == 1);
519 #endif // VISP_HAVE_OPENCV
Definition of the vpMatrix class.
void init(const vpKltOpencv &_tracker)
void updateMask(IplImage *_mask, unsigned char _nb=255, unsigned int _shiftBorder=0)
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
error that can be emited by ViSP classes.
void displayPrimitive(const vpImage< unsigned char > &_I)
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...
void removeOutliers(const vpColVector &weight, const double &threshold_outlier)
void getRoiClipped(const vpCameraParameters &cam, std::vector< vpImagePoint > &roi)
static int round(const double x)
The vpRotationMatrix considers the particular case of a rotation matrix.
This class aims to compute the homography wrt.two images.
void set_i(const double ii)
unsigned int computeNbDetectedCurrent(const vpKltOpencv &_tracker)
void computeInteractionMatrixAndResidu(vpColVector &_R, vpMatrix &_J)
void getFeature(int index, int &id, float &x, float &y) const
virtual ~vpMbtKltPolygon()
virtual void displayCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)=0
void extract(vpRotationMatrix &R) const
void computeHomography(const vpHomogeneousMatrix &_cTc0, vpHomography &cHc0)
void computeRoiClipped(const vpCameraParameters &cam=vpCameraParameters())
vpColVector getNormal() const
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...
virtual void displayCharString(const vpImagePoint &ip, const char *text, const vpColor &color=vpColor::green)=0
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV.
int getNbFeatures() const
Get the current number of features.
static void getMinMaxRoi(const std::vector< vpImagePoint > &roi, int &i_min, int &i_max, int &j_min, int &j_max)
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.
bool isvisible
flag to specify whether the face is visible or not
vpColVector & normalize()
normalise the vector
Class that consider the case of a translation vector.
void computeFov(const unsigned int &w, const unsigned int &h)
vpPoint * p
corners in the object frame