Visual Servoing Platform  version 3.0.0
vpMbtDistanceKltPoints.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Klt polygon, containing points of interest.
32  *
33  * Authors:
34  * Romain Tallonneau
35  * Aurelien Yol
36  *
37  *****************************************************************************/
38 
39 #include <visp3/mbt/vpMbtDistanceKltPoints.h>
40 #include <visp3/core/vpPolygon.h>
41 
42 #if defined(VISP_HAVE_MODULE_KLT) && (defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100))
43 
49  : H(), N(), N_cur(), invd0(1.), cRc0_0n(), initPoints(), curPoints(), curPointsInd(),
50  nbPointsCur(0), nbPointsInit(0), minNbPoint(4), enoughPoints(false), dt(1.), d0(1.),
51  cam(), isTrackedKltPoints(true), polygon(NULL), hiddenface(NULL), useScanLine(false)
52 {
53  initPoints = std::map<int, vpImagePoint>();
54  curPoints = std::map<int, vpImagePoint>();
55  curPointsInd = std::map<int, int>();
56 }
57 
63 {}
64 
72 void
74 {
75  // extract ids of the points in the face
76  nbPointsInit = 0;
77  nbPointsCur = 0;
78  initPoints = std::map<int, vpImagePoint>();
79  curPoints = std::map<int, vpImagePoint>();
80  curPointsInd = std::map<int, int>();
81  std::vector<vpImagePoint> roi;
82  polygon->getRoiClipped(cam, roi);
83 
84  for (unsigned int i = 0; i < static_cast<unsigned int>(_tracker.getNbFeatures()); i ++){
85  int id;
86  float x_tmp, y_tmp;
87  _tracker.getFeature((int)i, id, x_tmp, y_tmp);
88 
89  bool add = false;
90 
91  if(useScanLine)
92  {
93  if((unsigned int)y_tmp < hiddenface->getMbScanLineRenderer().getPrimitiveIDs().getHeight() &&
94  (unsigned int)x_tmp < hiddenface->getMbScanLineRenderer().getPrimitiveIDs().getWidth() &&
95  hiddenface->getMbScanLineRenderer().getPrimitiveIDs()[(unsigned int)y_tmp][(unsigned int)x_tmp] == polygon->getIndex())
96  add = true;
97  }
98  else if(vpPolygon::isInside(roi, y_tmp, x_tmp))
99  {
100  add = true;
101  }
102 
103  if(add){
104  initPoints[id] = vpImagePoint(y_tmp, x_tmp);
105  curPoints[id] = vpImagePoint(y_tmp, x_tmp);
106  curPointsInd[id] = (int)i;
107  nbPointsInit++;
108  nbPointsCur++;
109  }
110  }
111 
112  if(nbPointsCur >= minNbPoint) enoughPoints = true;
113  else enoughPoints = false;
114 
115  // initialisation of the value for the computation in SE3
117 
118  d0 = plan.getD();
119  N = plan.getNormal();
120 
121  N.normalize();
122  N_cur = N;
123  invd0 = 1.0 / d0;
124 }
125 
133 unsigned int
135 {
136  int id;
137  float x, y;
138  nbPointsCur = 0;
139  curPoints = std::map<int, vpImagePoint>();
140  curPointsInd = std::map<int, int>();
141 
142  for (unsigned int i = 0; i < static_cast<unsigned int>(_tracker.getNbFeatures()); i++){
143  _tracker.getFeature((int)i, id, x, y);
144  if(isTrackedFeature(id)){
145  curPoints[id] = vpImagePoint(static_cast<double>(y),static_cast<double>(x));
146  curPointsInd[id] = (int)i;
147  nbPointsCur++;
148  }
149  }
150 
151  if(nbPointsCur >= minNbPoint) enoughPoints = true;
152  else enoughPoints = false;
153 
154  return nbPointsCur;
155 }
156 
167 void
169 {
170  unsigned int index_ = 0;
171 
172  std::map<int, vpImagePoint>::const_iterator iter = curPoints.begin();
173  for( ; iter != curPoints.end(); iter++){
174  int id(iter->first);
175  double i_cur(iter->second.get_i()), j_cur(iter->second.get_j());
176 
177  double x_cur(0), y_cur(0);
178  vpPixelMeterConversion::convertPoint(cam, j_cur, i_cur, x_cur, y_cur);
179 
180  vpImagePoint iP0 = initPoints[id];
181  double x0(0), y0(0);
182  vpPixelMeterConversion::convertPoint(cam, iP0, x0, y0);
183 
184  double x0_transform, y0_transform ;// equivalent x and y in the first image (reference)
185  computeP_mu_t(x0, y0, x0_transform, y0_transform, H );
186 
187  double invZ = compute_1_over_Z(x_cur, y_cur);
188 
189  _J[2*index_][0] = - invZ;
190  _J[2*index_][1] = 0;
191  _J[2*index_][2] = x_cur * invZ;
192  _J[2*index_][3] = x_cur * y_cur;
193  _J[2*index_][4] = -(1+x_cur*x_cur);
194  _J[2*index_][5] = y_cur;
195 
196  _J[2*index_+1][0] = 0;
197  _J[2*index_+1][1] = - invZ;
198  _J[2*index_+1][2] = y_cur * invZ;
199  _J[2*index_+1][3] = (1+y_cur*y_cur);
200  _J[2*index_+1][4] = - y_cur * x_cur;
201  _J[2*index_+1][5] = - x_cur;
202 
203  _R[2*index_] = (x0_transform - x_cur);
204  _R[2*index_+1] = (y0_transform - y_cur);
205  index_++;
206  }
207 }
208 
209 double
210 vpMbtDistanceKltPoints::compute_1_over_Z(const double x, const double y)
211 {
212  double num = cRc0_0n[0] * x + cRc0_0n[1] * y + cRc0_0n[2];
213  double den = -(d0 - dt);
214  return num/den;
215 }
216 
229 inline void
230 vpMbtDistanceKltPoints::computeP_mu_t(const double x_in, const double y_in, double& x_out, double& y_out, const vpMatrix& _cHc0)
231 {
232  double p_mu_t_2 = x_in * _cHc0[2][0] + y_in * _cHc0[2][1] + _cHc0[2][2];
233 
234  if( fabs(p_mu_t_2) < std::numeric_limits<double>::epsilon()){
235  x_out = 0.0;
236  y_out = 0.0;
237  throw vpException(vpException::divideByZeroError, "the depth of the point is calculated to zero");
238  }
239 
240  x_out = (x_in * _cHc0[0][0] + y_in * _cHc0[0][1] + _cHc0[0][2]) / p_mu_t_2;
241  y_out = (x_in * _cHc0[1][0] + y_in * _cHc0[1][1] + _cHc0[1][2]) / p_mu_t_2;
242 }
243 
256 void
258 {
259  vpRotationMatrix cRc0;
260  vpTranslationVector ctransc0;
261 
262  _cTc0.extract(cRc0);
263  _cTc0.extract(ctransc0);
264  vpMatrix cHc0 = _cHc0.convert();
265 
266 // vpGEMM(cRc0, 1.0, invd0, cRc0, -1.0, _cHc0, VP_GEMM_A_T);
267  vpGEMM(ctransc0, N, -invd0, cRc0, 1.0, cHc0, VP_GEMM_B_T);
268  cHc0 /= cHc0[2][2];
269 
270  H = cHc0;
271 
272 // vpQuaternionVector NQuat(N[0], N[1], N[2], 0.0);
273 // vpQuaternionVector RotQuat(cRc0);
274 // vpQuaternionVector RotQuatConj(-RotQuat.x(), -RotQuat.y(), -RotQuat.z(), RotQuat.w());
275 // vpQuaternionVector partial = RotQuat * NQuat;
276 // vpQuaternionVector resQuat = (partial * RotQuatConj);
277 //
278 // cRc0_0n = vpColVector(3);
279 // cRc0_0n[0] = resQuat.x();
280 // cRc0_0n[1] = resQuat.y();
281 // cRc0_0n[2] = resQuat.z();
282 
283  cRc0_0n = cRc0*N;
284 
285 // vpPlane p(corners[0], corners[1], corners[2]);
286 // vpColVector Ncur = p.getNormal();
287 // Ncur.normalize();
288  N_cur = cRc0_0n;
289  dt = 0.0;
290  for (unsigned int i = 0; i < 3; i += 1){
291  dt += ctransc0[i] * (N_cur[i]);
292  }
293 }
294 
302 bool
303 vpMbtDistanceKltPoints::isTrackedFeature(const int _id)
304 {
305 // std::map<int, vpImagePoint>::const_iterator iter = initPoints.begin();
306 // while(iter != initPoints.end()){
307 // if(iter->first == _id){
308 // return true;
309 // }
310 // iter++;
311 // }
312 
313  std::map<int, vpImagePoint>::iterator iter = initPoints.find(_id);
314  if(iter != initPoints.end())
315  return true;
316 
317  return false;
318 }
319 
328 void
330 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
331  cv::Mat &mask,
332 #else
333  IplImage* mask,
334 #endif
335  unsigned char nb, unsigned int shiftBorder)
336 {
337 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
338  int width = mask.cols;
339  int height = mask.rows;
340 #else
341  int width = mask->width;
342  int height = mask->height;
343 #endif
344 
345  int i_min, i_max, j_min, j_max;
346  std::vector<vpImagePoint> roi;
347  polygon->getRoiClipped(cam, roi);
348  vpPolygon3D::getMinMaxRoi(roi, i_min, i_max, j_min,j_max);
349 
350  /* check image boundaries */
351  if(i_min > height){ //underflow
352  i_min = 0;
353  }
354  if(i_max > height){
355  i_max = height;
356  }
357  if(j_min > width){ //underflow
358  j_min = 0;
359  }
360  if(j_max > width){
361  j_max = width;
362  }
363 
364  double shiftBorder_d = (double) shiftBorder;
365 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
366  for(int i=i_min; i< i_max; i++){
367  double i_d = (double) i;
368  for(int j=j_min; j< j_max; j++){
369  double j_d = (double) j;
370  if(shiftBorder != 0){
371  if( vpPolygon::isInside(roi, i_d, j_d)
372  && vpPolygon::isInside(roi, i_d+shiftBorder_d, j_d+shiftBorder_d)
373  && vpPolygon::isInside(roi, i_d-shiftBorder_d, j_d+shiftBorder_d)
374  && vpPolygon::isInside(roi, i_d+shiftBorder_d, j_d-shiftBorder_d)
375  && vpPolygon::isInside(roi, i_d-shiftBorder_d, j_d-shiftBorder_d) ){
376  mask.at<unsigned char>(i,j) = nb;
377  }
378  }
379  else{
380  if(vpPolygon::isInside(roi, i, j)){
381  mask.at<unsigned char>(i,j) = nb;
382  }
383  }
384  }
385  }
386 #else
387  unsigned char* ptrData = (unsigned char*)mask->imageData + i_min*mask->widthStep+j_min;
388  for(int i=i_min; i< i_max; i++){
389  double i_d = (double) i;
390  for(int j=j_min; j< j_max; j++){
391  double j_d = (double) j;
392  if(shiftBorder != 0){
393  if( vpPolygon::isInside(roi, i_d, j_d)
394  && vpPolygon::isInside(roi, i_d+shiftBorder_d, j_d+shiftBorder_d)
395  && vpPolygon::isInside(roi, i_d-shiftBorder_d, j_d+shiftBorder_d)
396  && vpPolygon::isInside(roi, i_d+shiftBorder_d, j_d-shiftBorder_d)
397  && vpPolygon::isInside(roi, i_d-shiftBorder_d, j_d-shiftBorder_d) ){
398  *(ptrData++) = nb;
399  }
400  else{
401  ptrData++;
402  }
403  }
404  else{
405  if(vpPolygon::isInside(roi, i, j)){
406  *(ptrData++) = nb;
407  }
408  else{
409  ptrData++;
410  }
411  }
412  }
413  ptrData += mask->widthStep - j_max + j_min;
414  }
415 #endif
416 }
417 
425 void
426 vpMbtDistanceKltPoints::removeOutliers(const vpColVector& _w, const double &threshold_outlier)
427 {
428  std::map<int, vpImagePoint> tmp;
429  std::map<int, int> tmp2;
430  unsigned int nbSupp = 0;
431  unsigned int k = 0;
432 
433  nbPointsCur = 0;
434  std::map<int, vpImagePoint>::const_iterator iter = curPoints.begin();
435  for( ; iter != curPoints.end(); iter++){
436  if(_w[k] > threshold_outlier && _w[k+1] > threshold_outlier){
437 // if(_w[k] > threshold_outlier || _w[k+1] > threshold_outlier){
438  tmp[iter->first] = vpImagePoint(iter->second.get_i(), iter->second.get_j());
439  tmp2[iter->first] = curPointsInd[iter->first];
440  nbPointsCur++;
441  }
442  else{
443  nbSupp++;
444  initPoints.erase(iter->first);
445  }
446 
447  k+=2;
448  }
449 
450  if(nbSupp != 0){
451  curPoints = std::map<int, vpImagePoint>();
452  curPointsInd = std::map<int, int>();
453 
454  curPoints = tmp;
455  curPointsInd = tmp2;
456  if(nbPointsCur >= minNbPoint) enoughPoints = true;
457  else enoughPoints = false;
458  }
459 }
460 
466 void
468 {
469  std::map<int, vpImagePoint>::const_iterator iter = curPoints.begin();
470  for( ; iter != curPoints.end(); iter++){
471  int id(iter->first);
472  vpImagePoint iP;
473  iP.set_i(static_cast<double>(iter->second.get_i()));
474  iP.set_j(static_cast<double>(iter->second.get_j()));
475 
477 
478  iP.set_i( vpMath::round( iP.get_i() + 7 ) );
479  iP.set_j( vpMath::round( iP.get_j() + 7 ) );
480  char ide[10];
481  sprintf(ide, "%ld", static_cast<long int>(id));
482  vpDisplay::displayText(_I, iP, ide, vpColor::red);
483  }
484 }
485 
491 void
493 {
494  std::map<int, vpImagePoint>::const_iterator iter = curPoints.begin();
495  for( ; iter != curPoints.end(); iter++){
496  int id(iter->first);
497  vpImagePoint iP;
498  iP.set_i(static_cast<double>(iter->second.get_i()));
499  iP.set_j(static_cast<double>(iter->second.get_j()));
500 
502 
503  iP.set_i( vpMath::round( iP.get_i() + 7 ) );
504  iP.set_j( vpMath::round( iP.get_j() + 7 ) );
505  char ide[10];
506  sprintf(ide, "%ld", static_cast<long int>(id));
507  vpDisplay::displayText(_I, iP, ide, vpColor::red);
508  }
509 }
510 
511 void
513  const vpColor col, const unsigned int thickness, const bool displayFullModel)
514 {
515  if(polygon->isVisible() || displayFullModel)
516  {
517  std::vector<std::pair<vpPoint,unsigned int> > roi;
519 
520  for (unsigned int j = 0; j < roi.size(); j += 1){
521  if(((roi[(j+1)%roi.size()].second & roi[j].second & vpPolygon3D::NEAR_CLIPPING) == 0) &&
522  ((roi[(j+1)%roi.size()].second & roi[j].second & vpPolygon3D::FAR_CLIPPING) == 0) &&
523  ((roi[(j+1)%roi.size()].second & roi[j].second & vpPolygon3D::DOWN_CLIPPING) == 0) &&
524  ((roi[(j+1)%roi.size()].second & roi[j].second & vpPolygon3D::UP_CLIPPING) == 0) &&
525  ((roi[(j+1)%roi.size()].second & roi[j].second & vpPolygon3D::LEFT_CLIPPING) == 0) &&
526  ((roi[(j+1)%roi.size()].second & roi[j].second & vpPolygon3D::RIGHT_CLIPPING) == 0)){
527 
528  vpImagePoint ip1, ip2;
529  std::vector<std::pair<vpPoint, vpPoint> > linesLst;
530 
531  if(useScanLine && !displayFullModel)
532  hiddenface->computeScanLineQuery(roi[j].first,roi[(j+1)%roi.size()].first,linesLst, true);
533  else
534  linesLst.push_back(std::make_pair(roi[j].first,roi[(j+1)%roi.size()].first));
535 
536  for(unsigned int i = 0 ; i < linesLst.size() ; i++){
537  linesLst[i].first.project();
538  linesLst[i].second.project();
539  vpMeterPixelConversion::convertPoint(camera,linesLst[i].first.get_x(),linesLst[i].first.get_y(),ip1);
540  vpMeterPixelConversion::convertPoint(camera,linesLst[i].second.get_x(),linesLst[i].second.get_y(),ip2);
541 
542  vpDisplay::displayLine(I,ip1,ip2,col, thickness);
543  }
544  }
545  }
546  }
547 }
548 
549 
550 void
552  const vpColor col, const unsigned int thickness, const bool displayFullModel)
553 {
554  if(polygon->isVisible() || displayFullModel)
555  {
556  std::vector<std::pair<vpPoint,unsigned int> > roi;
558 
559  for (unsigned int j = 0; j < roi.size(); j += 1){
560  if(((roi[(j+1)%roi.size()].second & roi[j].second & vpPolygon3D::NEAR_CLIPPING) == 0) &&
561  ((roi[(j+1)%roi.size()].second & roi[j].second & vpPolygon3D::FAR_CLIPPING) == 0) &&
562  ((roi[(j+1)%roi.size()].second & roi[j].second & vpPolygon3D::DOWN_CLIPPING) == 0) &&
563  ((roi[(j+1)%roi.size()].second & roi[j].second & vpPolygon3D::UP_CLIPPING) == 0) &&
564  ((roi[(j+1)%roi.size()].second & roi[j].second & vpPolygon3D::LEFT_CLIPPING) == 0) &&
565  ((roi[(j+1)%roi.size()].second & roi[j].second & vpPolygon3D::RIGHT_CLIPPING) == 0)){
566 
567  vpImagePoint ip1, ip2;
568  std::vector<std::pair<vpPoint, vpPoint> > linesLst;
569 
570  if(useScanLine && !displayFullModel)
571  hiddenface->computeScanLineQuery(roi[j].first,roi[(j+1)%roi.size()].first,linesLst, true);
572  else
573  linesLst.push_back(std::make_pair(roi[j].first,roi[(j+1)%roi.size()].first));
574 
575  for(unsigned int i = 0 ; i < linesLst.size() ; i++){
576  linesLst[i].first.project();
577  linesLst[i].second.project();
578  vpMeterPixelConversion::convertPoint(camera,linesLst[i].first.get_x(),linesLst[i].first.get_y(),ip1);
579  vpMeterPixelConversion::convertPoint(camera,linesLst[i].second.get_x(),linesLst[i].second.get_y(),ip2);
580 
581  vpDisplay::displayLine(I,ip1,ip2,col, thickness);
582  }
583  }
584  }
585  }
586 }
587 
588 #elif !defined(VISP_BUILD_SHARED_LIBS)
589 // Work arround to avoid warning: libvisp_mbt.a(vpMbtDistanceKltPoints.cpp.o) has no symbols
590 void dummy_vpMbKltTracker() {};
591 #endif
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:92
void getRoiClipped(const vpCameraParameters &cam, std::vector< vpImagePoint > &roi)
void init(const vpKltOpencv &_tracker)
double get_i() const
Definition: vpImagePoint.h:190
void removeOutliers(const vpColVector &weight, const double &threshold_outlier)
Implementation of an homogeneous matrix and operations on such kind of matrices.
static void convertPoint(const vpCameraParameters &cam, const double &x, const double &y, double &u, double &v)
Point coordinates conversion from normalized coordinates in meter to pixel coordinates ...
Class to define colors available for display functionnalities.
Definition: vpColor.h:121
vpMbHiddenFaces< vpMbtPolygon > * hiddenface
Pointer to the list of faces.
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Definition: vpDisplay.cpp:888
error that can be emited by ViSP classes.
Definition: vpException.h:73
vpMbScanLine & getMbScanLineRenderer()
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)
Definition: vpMath.h:248
double get_j() const
Definition: vpImagePoint.h:201
static void getMinMaxRoi(const std::vector< vpImagePoint > &roi, int &i_min, int &i_max, int &j_min, int &j_max)
static const vpColor red
Definition: vpColor.h:163
Implementation of a rotation matrix and operations on such kind of matrices.
bool isInside(const vpImagePoint &iP)
Definition: vpPolygon.cpp:246
unsigned int computeNbDetectedCurrent(const vpKltOpencv &_tracker)
Implementation of an homography and operations on homographies.
Definition: vpHomography.h:179
void set_i(const double ii)
Definition: vpImagePoint.h:154
vpColVector & normalize()
vpPoint & getPoint(const unsigned int _index)
void displayPrimitive(const vpImage< unsigned char > &_I)
int getIndex() const
Definition: vpMbtPolygon.h:94
virtual void displayCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)=0
void getPolygonClipped(std::vector< std::pair< vpPoint, unsigned int > > &poly)
Generic class defining intrinsic camera parameters.
void getFeature(const int &index, int &id, float &x, float &y) const
void extract(vpRotationMatrix &R) const
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 >())
void computeHomography(const vpHomogeneousMatrix &_cTc0, vpHomography &cHc0)
void set_j(const double jj)
Definition: vpImagePoint.h:165
void computeScanLineQuery(const vpPoint &a, const vpPoint &b, std::vector< std::pair< vpPoint, vpPoint > > &lines, const bool &displayResults=false)
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV.
Definition: vpKltOpencv.h:75
int getNbFeatures() const
Get the number of current features.
Definition: vpKltOpencv.h:117
vpMatrix convert() const
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 ...
Definition: vpImagePoint.h:88
virtual void displayLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)=0
This class defines the container for a plane geometrical structure.
Definition: vpPlane.h:58
Class that consider the case of a translation vector.
void computeInteractionMatrixAndResidu(vpColVector &_R, vpMatrix &_J)
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)
bool useScanLine
Use scanline rendering.
void updateMask(cv::Mat &mask, unsigned char _nb=255, unsigned int _shiftBorder=0)