43 #include <visp/vpMbtKltPolygon.h>
45 #ifdef VISP_HAVE_OPENCV
58 initPoints = std::map<int, vpImagePoint>();
59 curPoints = std::map<int, vpImagePoint>();
60 curPointsInd = std::map<int, int>();
85 initPoints = std::map<int, vpImagePoint>();
86 curPoints = std::map<int, vpImagePoint>();
87 curPointsInd = std::map<int, int>();
88 std::vector<vpImagePoint> roi;
91 for (
unsigned int i = 0; i < static_cast<unsigned int>(_tracker.
getNbFeatures()); i ++){
96 if(isInside(roi, y_tmp, x_tmp)){
99 curPointsInd[id] = (int)i;
129 curPoints = std::map<int, vpImagePoint>();
130 curPointsInd = std::map<int, int>();
132 for (
unsigned int i = 0; i < static_cast<unsigned int>(_tracker.
getNbFeatures()); i++){
134 if(isTrackedFeature(
id)){
135 curPoints[id] =
vpImagePoint(static_cast<double>(y),static_cast<double>(x));
136 curPointsInd[id] = (int)i;
141 if(nbPointsCur >= minNbPoint) enoughPoints =
true;
142 else enoughPoints =
false;
160 unsigned int index = 0;
162 std::map<int, vpImagePoint>::const_iterator iter = curPoints.begin();
163 for( ; iter != curPoints.end(); iter++){
165 double i_cur(iter->second.get_i()), j_cur(iter->second.get_j());
167 double x_cur(0), y_cur(0);
174 double x0_transform, y0_transform ;
175 computeP_mu_t(x0, y0, x0_transform, y0_transform, H );
177 double invZ = compute_1_over_Z(x_cur, y_cur);
179 _J[2*
index][0] = - invZ;
181 _J[2*
index][2] = x_cur * invZ;
182 _J[2*
index][3] = x_cur * y_cur;
183 _J[2*
index][4] = -(1+x_cur*x_cur);
184 _J[2*
index][5] = y_cur;
186 _J[2*index+1][0] = 0;
187 _J[2*index+1][1] = - invZ;
188 _J[2*index+1][2] = y_cur * invZ;
189 _J[2*index+1][3] = (1+y_cur*y_cur);
190 _J[2*index+1][4] = - y_cur * x_cur;
191 _J[2*index+1][5] = - x_cur;
193 _R[2*
index] = (x0_transform - x_cur);
194 _R[2*index+1] = (y0_transform - y_cur);
200 vpMbtKltPolygon::compute_1_over_Z(
const double x,
const double y)
202 double num = cRc0_0n[0] * x + cRc0_0n[1] * y + cRc0_0n[2];
203 double den = -(d0 - dt);
220 vpMbtKltPolygon::computeP_mu_t(
const double x_in,
const double y_in,
double& x_out,
double& y_out,
const vpMatrix& _cHc0)
222 double p_mu_t_2 = x_in * _cHc0[2][0] + y_in * _cHc0[2][1] + _cHc0[2][2];
224 if( fabs(p_mu_t_2) < std::numeric_limits<double>::epsilon()){
230 x_out = (x_in * _cHc0[0][0] + y_in * _cHc0[0][1] + _cHc0[0][2]) / p_mu_t_2;
231 y_out = (x_in * _cHc0[1][0] + y_in * _cHc0[1][1] + _cHc0[1][2]) / p_mu_t_2;
256 vpGEMM(ctransc0, N, -invd0, cRc0, 1.0, _cHc0, VP_GEMM_B_T);
257 _cHc0 /= _cHc0[2][2];
279 for (
unsigned int i = 0; i < 3; i += 1){
280 dt += ctransc0[i] * (N_cur[i]);
292 vpMbtKltPolygon::isTrackedFeature(
const int _id)
302 std::map<int, vpImagePoint>::iterator iter = initPoints.find(_id);
303 if(iter != initPoints.end())
320 int width = _mask->width;
321 int i_min, i_max, j_min, j_max;
322 cam.
computeFov((
unsigned)_mask->width, (
unsigned)_mask->height);
324 std::vector<vpImagePoint> roi;
329 if(i_min > _mask->height){
332 if(i_max > _mask->height){
333 i_max = _mask->height;
335 if(j_min > _mask->width){
338 if(j_max > _mask->width){
339 j_max = _mask->width;
342 double shiftBorder_d = (double) _shiftBorder;
343 char* ptrData = _mask->imageData + i_min*width+j_min;
344 for(
int i=i_min; i< i_max; i++){
345 double i_d = (double) i;
346 for(
int j=j_min; j< j_max; j++){
347 double j_d = (double) j;
348 if(_shiftBorder != 0){
349 if( vpMbtKltPolygon::isInside(roi, i_d, j_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)
352 && vpMbtKltPolygon::isInside(roi, i_d+shiftBorder_d, j_d-shiftBorder_d)
353 && vpMbtKltPolygon::isInside(roi, i_d-shiftBorder_d, j_d-shiftBorder_d) ){
361 if(vpMbtKltPolygon::isInside(roi, i, j)){
369 ptrData += width - j_max + j_min;
384 std::map<int, vpImagePoint> tmp;
385 std::map<int, int> tmp2;
386 unsigned int nbSupp = 0;
390 std::map<int, vpImagePoint>::const_iterator iter = curPoints.begin();
391 for( ; iter != curPoints.end(); iter++){
392 if(_w[k] > threshold_outlier && _w[k+1] > threshold_outlier){
394 tmp[iter->first] =
vpImagePoint(iter->second.get_i(), iter->second.get_j());
395 tmp2[iter->first] = curPointsInd[iter->first];
400 initPoints.erase(iter->first);
407 curPoints = std::map<int, vpImagePoint>();
408 curPointsInd = std::map<int, int>();
412 if(nbPointsCur >= minNbPoint) enoughPoints =
true;
413 else enoughPoints =
false;
425 std::map<int, vpImagePoint>::const_iterator iter = curPoints.begin();
426 for( ; iter != curPoints.end(); iter++){
429 iP.
set_i(static_cast<double>(iter->second.get_i()));
430 iP.
set_j(static_cast<double>(iter->second.get_j()));
437 sprintf(ide,
"%ld", static_cast<long int>(
id));
450 std::map<int, vpImagePoint>::const_iterator iter = curPoints.begin();
451 for( ; iter != curPoints.end(); iter++){
454 iP.
set_i(static_cast<double>(iter->second.get_i()));
455 iP.
set_j(static_cast<double>(iter->second.get_j()));
462 sprintf(ide,
"%ld", static_cast<long int>(
id));
471 bool vpMbtKltPolygon::intersect(
const vpImagePoint& p1,
const vpImagePoint& p2,
const double i_test,
const double j_test,
const double i,
const double j)
475 double ex = j - j_test;
476 double ey = i - i_test;
478 double den = dx * ey - dy * ex;
481 t = -( ey * ( p1.
get_j() - j_test ) + ex * ( -p1.
get_i() + i_test ) ) / den;
482 u = -( dx * ( -p1.
get_i() + i_test ) + dy * ( p1.
get_j() - j_test ) ) / den;
487 return ( t >= std::numeric_limits<double>::epsilon() && t < 1.0 && u >= std::numeric_limits<double>::epsilon() && u < 1.0);
490 bool vpMbtKltPolygon::isInside(
const std::vector<vpImagePoint>& roi,
const double i,
const double j)
492 double i_test = 100000.;
493 double j_test = 100000.;
494 unsigned int nbInter = 0;
495 bool computeAgain =
true;
498 computeAgain =
false;
499 for(
unsigned int k=0; k< roi.size(); k++){
501 if(vpMbtKltPolygon::intersect(roi[k], roi[(k+1)%roi.size()], i, j, i_test, j_test)){
517 return ((nbInter%2) == 1);
520 #endif // VISP_HAVE_OPENCV
void set_j(const double j)
Definition of the vpMatrix class.
void init(const vpKltOpencv &_tracker)
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
void set_i(const double i)
int index
Index of the polygon. Cannot be unsigned int because deafult value is -1.
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.
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()
void updateMask(IplImage *_mask, unsigned int _nb=255, unsigned int _shiftBorder=0)
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
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