Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
vpDot2.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Track a white dot.
33  *
34  * Authors:
35  * Fabien Spindler
36  * Anthony Saunier
37  *
38  *****************************************************************************/
39 
45 //#define DEBUG
46 
47 #include <visp3/core/vpDisplay.h>
48 
49 // exception handling
50 #include <visp3/core/vpIoTools.h>
51 #include <visp3/core/vpMath.h>
52 #include <visp3/core/vpTrackingException.h>
53 
54 #include <cmath> // std::fabs
55 #include <iostream>
56 #include <limits> // numeric_limits
57 #include <math.h>
58 #include <visp3/blob/vpDot2.h>
59 
60 /******************************************************************************
61  *
62  * CONSTRUCTORS AND DESTRUCTORS
63  *
64  *****************************************************************************/
70 void vpDot2::init()
71 {
72  cog.set_u(0);
73  cog.set_v(0);
74 
75  width = 0;
76  height = 0;
77  surface = 0;
78  mean_gray_level = 0;
79  gray_level_min = 128;
80  gray_level_max = 255;
81  grayLevelPrecision = 0.80;
82  gamma = 1.5;
83 
84  sizePrecision = 0.65;
85  ellipsoidShapePrecision = 0.65;
86  maxSizeSearchDistancePrecision = 0.65;
88  m00 = m11 = m02 = m20 = m10 = m01 = 0.;
89  mu11 = mu02 = mu20 = 0.;
90 
91  bbox_u_min = bbox_u_max = bbox_v_min = bbox_v_max = 0;
92 
93  firstBorder_u = 0;
94  firstBorder_v = 0;
95 
96  compute_moment = false;
97  graphics = false;
98  thickness = 1;
99 }
100 
105  : m00(0.), m10(0.), m01(0.), m11(0.), m20(0.), m02(0.), mu11(0.), mu20(0.), mu02(0.), cog(), width(0), height(0),
106  surface(0), gray_level_min(128), gray_level_max(255), mean_gray_level(0), grayLevelPrecision(0.8), gamma(1.5),
107  sizePrecision(0.65), ellipsoidShapePrecision(0.65), maxSizeSearchDistancePrecision(0.65),
108  allowedBadPointsPercentage_(0.), area(), direction_list(), ip_edges_list(), compute_moment(false), graphics(false),
109  thickness(1), bbox_u_min(0), bbox_u_max(0), bbox_v_min(0), bbox_v_max(0), firstBorder_u(0), firstBorder_v()
110 {
111 }
112 
122  : m00(0.), m10(0.), m01(0.), m11(0.), m20(0.), m02(0.), mu11(0.), mu20(0.), mu02(0.), cog(ip), width(0), height(0),
123  surface(0), gray_level_min(128), gray_level_max(255), mean_gray_level(0), grayLevelPrecision(0.8), gamma(1.5),
124  sizePrecision(0.65), ellipsoidShapePrecision(0.65), maxSizeSearchDistancePrecision(0.65),
125  allowedBadPointsPercentage_(0.), area(), direction_list(), ip_edges_list(), compute_moment(false), graphics(false),
126  thickness(1), bbox_u_min(0), bbox_u_max(0), bbox_v_min(0), bbox_v_max(0), firstBorder_u(0), firstBorder_v()
127 {
128 }
129 
133 vpDot2::vpDot2(const vpDot2 &twinDot)
134  : vpTracker(twinDot), m00(0.), m10(0.), m01(0.), m11(0.), m20(0.), m02(0.), mu11(0.), mu20(0.), mu02(0.), cog(),
135  width(0), height(0), surface(0), gray_level_min(128), gray_level_max(255), mean_gray_level(0),
136  grayLevelPrecision(0.8), gamma(1.5), sizePrecision(0.65), ellipsoidShapePrecision(0.65),
137  maxSizeSearchDistancePrecision(0.65), allowedBadPointsPercentage_(0.), area(), direction_list(), ip_edges_list(),
138  compute_moment(false), graphics(false), thickness(1), bbox_u_min(0), bbox_u_max(0), bbox_v_min(0), bbox_v_max(0),
139  firstBorder_u(0), firstBorder_v()
140 {
141  *this = twinDot;
142 }
143 
148 {
149  cog = twinDot.cog;
150 
151  width = twinDot.width;
152  height = twinDot.height;
153  surface = twinDot.surface;
154  gray_level_min = twinDot.gray_level_min;
155  gray_level_max = twinDot.gray_level_max;
156  mean_gray_level = twinDot.mean_gray_level;
157  grayLevelPrecision = twinDot.grayLevelPrecision;
158  gamma = twinDot.gamma;
159  ;
160  sizePrecision = twinDot.sizePrecision;
161  ellipsoidShapePrecision = twinDot.ellipsoidShapePrecision;
162  maxSizeSearchDistancePrecision = twinDot.maxSizeSearchDistancePrecision;
163  allowedBadPointsPercentage_ = twinDot.allowedBadPointsPercentage_;
164  area = twinDot.area;
165 
166  direction_list = twinDot.direction_list;
167  ip_edges_list = twinDot.ip_edges_list;
168 
169  compute_moment = twinDot.compute_moment;
170  graphics = twinDot.graphics;
171  thickness = twinDot.thickness;
172 
173  bbox_u_min = twinDot.bbox_u_min;
174  bbox_u_max = twinDot.bbox_u_max;
175  bbox_v_min = twinDot.bbox_v_min;
176  bbox_v_max = twinDot.bbox_v_max;
177 
178  firstBorder_u = twinDot.firstBorder_u;
179  firstBorder_v = twinDot.firstBorder_v;
180 
181  m00 = twinDot.m00;
182  m01 = twinDot.m01;
183  m11 = twinDot.m11;
184  m10 = twinDot.m10;
185  m02 = twinDot.m02;
186  m20 = twinDot.m20;
187 
188  mu11 = twinDot.mu11;
189  mu20 = twinDot.mu20;
190  mu02 = twinDot.mu02;
191 
192  return (*this);
193 }
194 
199 
200 /******************************************************************************
201  *
202  * PUBLIC METHODS
203  *****************************************************************************/
204 
212 void vpDot2::display(const vpImage<unsigned char> &I, vpColor color, unsigned int t) const
213 {
214  vpDisplay::displayCross(I, cog, 3 * t + 8, color, t);
215  std::list<vpImagePoint>::const_iterator it;
216 
217  for (it = ip_edges_list.begin(); it != ip_edges_list.end(); ++it) {
218  vpDisplay::displayPoint(I, *it, color);
219  }
220 }
221 
253 void vpDot2::initTracking(const vpImage<unsigned char> &I, unsigned int size)
254 {
255  while (vpDisplay::getClick(I, cog) != true) {}
256 
257  unsigned int i = (unsigned int)cog.get_i();
258  unsigned int j = (unsigned int)cog.get_j();
259 
260  double Ip = pow((double)I[i][j] / 255, 1 / gamma);
261 
262  if (Ip - (1 - grayLevelPrecision) < 0) {
263  gray_level_min = 0;
264  } else {
265  gray_level_min = (unsigned int)(255 * pow(Ip - (1 - grayLevelPrecision), gamma));
266  if (gray_level_min > 255)
267  gray_level_min = 255;
268  }
269  gray_level_max = (unsigned int)(255 * pow(Ip + (1 - grayLevelPrecision), gamma));
270  if (gray_level_max > 255)
271  gray_level_max = 255;
272 
273  setWidth(size);
274  setHeight(size);
275 
276  try {
277  track(I);
278  } catch (const vpException &e) {
279  // vpERROR_TRACE("Error caught") ;
280  throw(e);
281  }
282 }
283 
311 void vpDot2::initTracking(const vpImage<unsigned char> &I, const vpImagePoint &ip, unsigned int size)
312 {
313  cog = ip;
314 
315  unsigned int i = (unsigned int)cog.get_i();
316  unsigned int j = (unsigned int)cog.get_j();
317 
318  double Ip = pow((double)I[i][j] / 255, 1 / gamma);
319 
320  if (Ip - (1 - grayLevelPrecision) < 0) {
321  gray_level_min = 0;
322  } else {
323  gray_level_min = (unsigned int)(255 * pow(Ip - (1 - grayLevelPrecision), gamma));
324  if (gray_level_min > 255)
325  gray_level_min = 255;
326  }
327  gray_level_max = (unsigned int)(255 * pow(Ip + (1 - grayLevelPrecision), gamma));
328  if (gray_level_max > 255)
329  gray_level_max = 255;
330 
331  setWidth(size);
332  setHeight(size);
333 
334  try {
335  track(I);
336  } catch (const vpException &e) {
337  // vpERROR_TRACE("Error caught") ;
338  throw(e);
339  }
340 }
341 
381 void vpDot2::initTracking(const vpImage<unsigned char> &I, const vpImagePoint &ip, unsigned int gray_lvl_min,
382  unsigned int gray_lvl_max, unsigned int size)
383 {
384  cog = ip;
385 
386  this->gray_level_min = gray_lvl_min;
387  this->gray_level_max = gray_lvl_max;
388 
389  setWidth(size);
390  setHeight(size);
391 
392  try {
393  track(I);
394  } catch (const vpException &e) {
395  // vpERROR_TRACE("Error caught") ;
396  throw(e);
397  }
398 }
399 
439 {
440  m00 = m11 = m02 = m20 = m10 = m01 = 0;
441 
442  // First, we will estimate the position of the tracked point
443 
444  // Set the search area to the entire image
445  setArea(I);
446 
447  // create a copy of the dot to search
448  // This copy can be saw as the previous dot used to check if the current one
449  // found with computeParameters() is similar to the previous one (see
450  // isValid() function). If the found dot is not similar (or valid), we use
451  // this copy to set the current found dot to the previous one (see below).
452  vpDot2 wantedDot(*this);
453 
454  // vpDEBUG_TRACE(0, "Previous dot: ");
455  // vpDEBUG_TRACE(0, "u: %f v: %f", get_u(), get_v());
456  // vpDEBUG_TRACE(0, "w: %f h: %f", getWidth(), getHeight());
457  bool found = computeParameters(I, cog.get_u(), cog.get_v());
458 
459  if (found) {
460  // test if the found dot is valid (ie similar to the previous one)
461  found = isValid(I, wantedDot);
462  if (!found) {
463  *this = wantedDot;
464  // std::cout << "The found dot is not valid" << std::endl;
465  }
466  }
467 
468  if (!found) {
469  // vpDEBUG_TRACE(0, "Search the dot in a biggest window around the
470  // last position"); vpDEBUG_TRACE(0, "Bad computed dot: ");
471  // vpDEBUG_TRACE(0, "u: %f v: %f", get_u(), get_v());
472  // vpDEBUG_TRACE(0, "w: %f h: %f", getWidth(), getHeight());
473 
474  // if estimation was wrong (get an error tracking), look for the dot
475  // closest from the estimation,
476  // i.e. search for dots in an a region of interest around the this dot and
477  // get the first element in the area.
478 
479  // first get the size of the search window from the dot size
480  double searchWindowWidth, searchWindowHeight;
481  // if( getWidth() == 0 || getHeight() == 0 )
482  if (std::fabs(getWidth()) <= std::numeric_limits<double>::epsilon() ||
483  std::fabs(getHeight()) <= std::numeric_limits<double>::epsilon()) {
484  searchWindowWidth = 80.;
485  searchWindowHeight = 80.;
486  } else {
487  searchWindowWidth = getWidth() * 5;
488  searchWindowHeight = getHeight() * 5;
489  }
490  std::list<vpDot2> candidates;
491  searchDotsInArea(I, (int)(this->cog.get_u() - searchWindowWidth / 2.0),
492  (int)(this->cog.get_v() - searchWindowHeight / 2.0), (unsigned int)searchWindowWidth,
493  (unsigned int)searchWindowHeight, candidates);
494 
495  // if the vector is empty, that mean we didn't find any candidate
496  // in the area, return an error tracking.
497  if (candidates.empty()) {
498  // vpERROR_TRACE("No dot was found") ;
500  }
501 
502  // otherwise we've got our dot, update this dot's parameters
503  vpDot2 movingDot = candidates.front();
504 
505  setCog(movingDot.getCog());
506  setArea(movingDot.getArea());
507  setWidth(movingDot.getWidth());
508  setHeight(movingDot.getHeight());
509 
510  // Update the moments
511  m00 = movingDot.m00;
512  m01 = movingDot.m01;
513  m10 = movingDot.m10;
514  m11 = movingDot.m11;
515  m20 = movingDot.m20;
516  m02 = movingDot.m02;
517 
518  // Update the bounding box
519  bbox_u_min = movingDot.bbox_u_min;
520  bbox_u_max = movingDot.bbox_u_max;
521  bbox_v_min = movingDot.bbox_v_min;
522  bbox_v_max = movingDot.bbox_v_max;
523  }
524  // else {
525  // // test if the found dot is valid,
526  // if( ! isValid( I, wantedDot ) ) {
527  // *this = wantedDot;
528  // vpERROR_TRACE("The found dot is invalid:",
529  // "- could be a problem of size (width or height) or "
530  // " surface (number of pixels) which differ too much "
531  // " to the previous one "
532  // "- or a problem of the shape which is not ellipsoid if "
533  // " use setEllipsoidShapePrecision(double
534  // ellipsoidShapePrecision) "
535  // " which is the default case. "
536  // " To track a non ellipsoid shape use
537  // setEllipsoidShapePrecision(0)") ;
538  // throw(vpTrackingException(vpTrackingException::featureLostError,
539  // "The found dot is invalid")) ;
540  // }
541  // }
542 
543  // if this dot is partially out of the image, return an error tracking.
544  if (!isInImage(I)) {
545  // vpERROR_TRACE("The center of gravity of the dot is not in the image") ;
547  "The center of gravity of the dot is not in the image"));
548  }
549 
550  // Get dots center of gravity
551  // unsigned int u = (unsigned int) this->cog.get_u();
552  // unsigned int v = (unsigned int) this->cog.get_v();
553  // Updates the min and max gray levels for the next iteration
554  // double Ip = pow((double)I[v][u]/255,1/gamma);
555  double Ip = pow(getMeanGrayLevel() / 255, 1 / gamma);
556  // printf("current value of gray level center : %i\n", I[v][u]);
557 
558  // getMeanGrayLevel(I);
559  if (Ip - (1 - grayLevelPrecision) < 0) {
560  gray_level_min = 0;
561  } else {
562  gray_level_min = (unsigned int)(255 * pow(Ip - (1 - grayLevelPrecision), gamma));
563  if (gray_level_min > 255)
564  gray_level_min = 255;
565  }
566  gray_level_max = (unsigned int)(255 * pow(Ip + (1 - grayLevelPrecision), gamma));
567  if (gray_level_max > 255)
568  gray_level_max = 255;
569 
570  // printf("%i %i \n",gray_level_max,gray_level_min);
571  if (graphics) {
572  // display a red cross at the center of gravity's location in the image.
573 
574  vpDisplay::displayCross(I, this->cog, 3 * thickness + 8, vpColor::red, thickness);
575  // vpDisplay::flush(I);
576  }
577 }
578 
598 {
599  track(I);
600 
601  ip = this->cog;
602 }
603 
606 
612 double vpDot2::getWidth() const { return width; }
613 
619 double vpDot2::getHeight() const { return height; }
620 
626 double vpDot2::getArea() const { return fabs(surface); }
627 
633 double vpDot2::getGrayLevelPrecision() const { return grayLevelPrecision; }
634 
640 double vpDot2::getSizePrecision() const { return sizePrecision; }
641 
649 double vpDot2::getEllipsoidShapePrecision() const { return ellipsoidShapePrecision; }
650 
657 double vpDot2::getMaxSizeSearchDistancePrecision() const { return maxSizeSearchDistancePrecision; }
658 
662 double vpDot2::getDistance(const vpDot2 &distantDot) const
663 {
664  vpImagePoint cogDistantDot = distantDot.getCog();
665  double diff_u = this->cog.get_u() - cogDistantDot.get_u();
666  double diff_v = this->cog.get_v() - cogDistantDot.get_v();
667  return sqrt(diff_u * diff_u + diff_v * diff_v);
668 }
669 
671 
681 void vpDot2::setWidth(const double &w) { this->width = w; }
682 
693 void vpDot2::setHeight(const double &h) { this->height = h; }
694 
705 void vpDot2::setArea(const double &a) { this->surface = a; }
706 
724 void vpDot2::setGrayLevelPrecision(const double &precision)
725 {
726  double epsilon = 0.05;
727  if (grayLevelPrecision < epsilon) {
728  this->grayLevelPrecision = epsilon;
729  } else if (grayLevelPrecision > 1) {
730  this->grayLevelPrecision = 1.0;
731  } else {
732  this->grayLevelPrecision = precision;
733  }
734 }
752 void vpDot2::setSizePrecision(const double &precision)
753 {
754  if (sizePrecision < 0) {
755  this->sizePrecision = 0;
756  } else if (sizePrecision > 1) {
757  this->sizePrecision = 1.0;
758  } else {
759  this->sizePrecision = precision;
760  }
761 }
762 
795 void vpDot2::setEllipsoidShapePrecision(const double &precision)
796 {
797 
798  if (ellipsoidShapePrecision < 0) {
799  this->ellipsoidShapePrecision = 0;
800  } else if (ellipsoidShapePrecision > 1) {
801  this->ellipsoidShapePrecision = 1.0;
802  } else {
803  this->ellipsoidShapePrecision = precision;
804  }
805 }
806 
822 void vpDot2::setMaxSizeSearchDistancePrecision(const double &precision)
823 {
824  double epsilon = 0.05;
825  if (maxSizeSearchDistancePrecision < epsilon) {
826  this->maxSizeSearchDistancePrecision = epsilon;
827  } else if (maxSizeSearchDistancePrecision > 1) {
828  this->maxSizeSearchDistancePrecision = 1.0;
829  } else {
830  this->maxSizeSearchDistancePrecision = precision;
831  }
832 }
833 
842 void vpDot2::setArea(const vpImage<unsigned char> &I) { setArea(I, 0, 0, I.getWidth(), I.getHeight()); }
843 
856 void vpDot2::setArea(const vpImage<unsigned char> &I, int u, int v, unsigned int w, unsigned int h)
857 {
858  unsigned int image_w = I.getWidth();
859  unsigned int image_h = I.getHeight();
860 
861  // Bounds the area to the image
862  if (u < 0)
863  u = 0;
864  else if (u >= (int)image_w)
865  u = (int)image_w - 1;
866  if (v < 0)
867  v = 0;
868  else if (v >= (int)image_h)
869  v = (int)image_h - 1;
870 
871  if (((unsigned int)u + w) > image_w)
872  w = image_w - (unsigned int)u - 1;
873  if (((unsigned int)v + h) > image_h)
874  h = image_h - (unsigned int)v - 1;
875 
876  area.setRect(u, v, w, h);
877 }
878 
886 void vpDot2::setArea(const vpRect &a) { area = a; }
887 
889 
941 void vpDot2::searchDotsInArea(const vpImage<unsigned char> &I, std::list<vpDot2> &niceDots)
942 {
943  searchDotsInArea(I, 0, 0, I.getWidth(), I.getHeight(), niceDots);
944 }
945 
968 void vpDot2::searchDotsInArea(const vpImage<unsigned char> &I, int area_u, int area_v, unsigned int area_w,
969  unsigned int area_h, std::list<vpDot2> &niceDots)
970 
971 {
972  // clear the list of nice dots
973  niceDots.clear();
974 
975  // Fit the input area in the image; we keep only the common part between
976  // this area and the image.
977  setArea(I, area_u, area_v, area_w, area_h);
978 
979  // compute the size of the search grid
980  unsigned int gridWidth;
981  unsigned int gridHeight;
982  getGridSize(gridWidth, gridHeight);
983 
984  if (graphics) {
985  // Display the area were the dot is search
986  vpDisplay::displayRectangle(I, area, vpColor::blue, false, thickness);
987  // vpDisplay::flush(I);
988  }
989 
990 #ifdef DEBUG
992  vpDisplay::flush(I);
993 #endif
994  // start the search loop; for all points of the search grid,
995  // test if the pixel belongs to a valid dot.
996  // if it is so eventually add it to the vector of valid dots.
997  std::list<vpDot2> badDotsVector;
998  std::list<vpDot2>::iterator itnice;
999  std::list<vpDot2>::iterator itbad;
1000 
1001  vpDot2 *dotToTest = NULL;
1002  vpDot2 tmpDot;
1003 
1004  unsigned int area_u_min = (unsigned int)area.getLeft();
1005  unsigned int area_u_max = (unsigned int)area.getRight();
1006  unsigned int area_v_min = (unsigned int)area.getTop();
1007  unsigned int area_v_max = (unsigned int)area.getBottom();
1008 
1009  unsigned int u, v;
1010  vpImagePoint cogTmpDot;
1011 
1012  for (v = area_v_min; v < area_v_max; v = v + gridHeight) {
1013  for (u = area_u_min; u < area_u_max; u = u + gridWidth) {
1014  // if the pixel we're in doesn't have the right color (outside the
1015  // graylevel interval), no need to check further, just get to the
1016  // next grid intersection.
1017  if (!hasGoodLevel(I, u, v))
1018  continue;
1019 
1020  // Test if an other germ is inside the bounding box of a dot previously
1021  // detected
1022  bool good_germ = true;
1023 
1024  itnice = niceDots.begin();
1025  while (itnice != niceDots.end() && good_germ == true) {
1026  tmpDot = *itnice;
1027 
1028  cogTmpDot = tmpDot.getCog();
1029  double u0 = cogTmpDot.get_u();
1030  double v0 = cogTmpDot.get_v();
1031  double half_w = tmpDot.getWidth() / 2.;
1032  double half_h = tmpDot.getHeight() / 2.;
1033 
1034  if (u >= (u0 - half_w) && u <= (u0 + half_w) && v >= (v0 - half_h) && v <= (v0 + half_h)) {
1035  // Germ is in a previously detected dot
1036  good_germ = false;
1037  }
1038  ++itnice;
1039  }
1040 
1041  if (!good_germ)
1042  continue;
1043 
1044  // Compute the right border position for this possible germ
1045  unsigned int border_u;
1046  unsigned int border_v;
1047  if (findFirstBorder(I, u, v, border_u, border_v) == false) {
1048  // germ is not good.
1049  // Jump all the pixels between v,u and v,
1050  // dotToTest->getFirstBorder_u()
1051  u = border_u;
1052  v = border_v;
1053  continue;
1054  }
1055 
1056  itbad = badDotsVector.begin();
1057 #define vpBAD_DOT_VALUE (*itbad)
1058  vpImagePoint cogBadDot;
1059 
1060  while (itbad != badDotsVector.end() && good_germ == true) {
1061  if ((double)u >= vpBAD_DOT_VALUE.bbox_u_min && (double)u <= vpBAD_DOT_VALUE.bbox_u_max &&
1062  (double)v >= vpBAD_DOT_VALUE.bbox_v_min && (double)v <= vpBAD_DOT_VALUE.bbox_v_max) {
1063  std::list<vpImagePoint>::const_iterator it_edges = ip_edges_list.begin();
1064  while (it_edges != ip_edges_list.end() && good_germ == true) {
1065  // Test if the germ belong to a previously detected dot:
1066  // - from the germ go right to the border and compare this
1067  // position to the list of pixels of previously detected dots
1068  cogBadDot = *it_edges;
1069  // if( border_u == cogBadDot.get_u() && v == cogBadDot.get_v()) {
1070  if ((std::fabs(border_u - cogBadDot.get_u()) <=
1071  vpMath::maximum(std::fabs((double)border_u), std::fabs(cogBadDot.get_u())) *
1072  std::numeric_limits<double>::epsilon()) &&
1073  (std::fabs(v - cogBadDot.get_v()) <=
1074  vpMath::maximum(std::fabs((double)v), std::fabs(cogBadDot.get_v())) *
1075  std::numeric_limits<double>::epsilon())) {
1076  good_germ = false;
1077  }
1078  ++it_edges;
1079  }
1080  }
1081  ++itbad;
1082  }
1083 #undef vpBAD_DOT_VALUE
1084 
1085  if (!good_germ) {
1086  // Jump all the pixels between v,u and v,
1087  // dotToTest->getFirstBorder_u()
1088  u = border_u;
1089  v = border_v;
1090  continue;
1091  }
1092 
1093  vpTRACE(4, "Try germ (%d, %d)", u, v);
1094 
1095  vpImagePoint germ;
1096  germ.set_u(u);
1097  germ.set_v(v);
1098 
1099  // otherwise estimate the width, height and surface of the dot we
1100  // created, and test it.
1101  if (dotToTest != NULL)
1102  delete dotToTest;
1103  dotToTest = getInstance();
1104  dotToTest->setCog(germ);
1105  dotToTest->setGrayLevelMin(getGrayLevelMin());
1106  dotToTest->setGrayLevelMax(getGrayLevelMax());
1108  dotToTest->setSizePrecision(getSizePrecision());
1109  dotToTest->setGraphics(graphics);
1110  dotToTest->setGraphicsThickness(thickness);
1111  dotToTest->setComputeMoments(true);
1112  dotToTest->setArea(area);
1113  dotToTest->setEllipsoidShapePrecision(ellipsoidShapePrecision);
1114  dotToTest->setEllipsoidBadPointsPercentage(allowedBadPointsPercentage_);
1115 
1116  // first compute the parameters of the dot.
1117  // if for some reasons this caused an error tracking
1118  // (dot partially out of the image...), check the next intersection
1119  if (dotToTest->computeParameters(I) == false) {
1120  // Jump all the pixels between v,u and v,
1121  // dotToTest->getFirstBorder_u()
1122  u = border_u;
1123  v = border_v;
1124  continue;
1125  }
1126  // if the dot to test is valid,
1127  if (dotToTest->isValid(I, *this)) {
1128  vpImagePoint cogDotToTest = dotToTest->getCog();
1129  // Compute the distance to the center. The center used here is not the
1130  // area center available by area.getCenter(area_center_u,
1131  // area_center_v) but the center of the input area which may be
1132  // partially outside the image.
1133 
1134  double area_center_u = area_u + area_w / 2.0 - 0.5;
1135  double area_center_v = area_v + area_h / 2.0 - 0.5;
1136 
1137  double thisDiff_u = cogDotToTest.get_u() - area_center_u;
1138  double thisDiff_v = cogDotToTest.get_v() - area_center_v;
1139  double thisDist = sqrt(thisDiff_u * thisDiff_u + thisDiff_v * thisDiff_v);
1140 
1141  bool stopLoop = false;
1142  itnice = niceDots.begin();
1143 
1144  while (itnice != niceDots.end() && stopLoop == false) {
1145  tmpDot = *itnice;
1146 
1147  // double epsilon = 0.001; // detecte +sieurs points
1148  double epsilon = 3.0;
1149  // if the center of the dot is the same than the current
1150  // don't add it, test the next point of the grid
1151  cogTmpDot = tmpDot.getCog();
1152 
1153  if (fabs(cogTmpDot.get_u() - cogDotToTest.get_u()) < epsilon &&
1154  fabs(cogTmpDot.get_v() - cogDotToTest.get_v()) < epsilon) {
1155  stopLoop = true;
1156  // Jump all the pixels between v,u and v,
1157  // tmpDot->getFirstBorder_u()
1158  u = border_u;
1159  v = border_v;
1160  continue;
1161  }
1162 
1163  double otherDiff_u = cogTmpDot.get_u() - area_center_u;
1164  double otherDiff_v = cogTmpDot.get_v() - area_center_v;
1165  double otherDist = sqrt(otherDiff_u * otherDiff_u + otherDiff_v * otherDiff_v);
1166 
1167  // if the distance of the curent vector element to the center
1168  // is greater than the distance of this dot to the center,
1169  // then add this dot before the current vector element.
1170  if (otherDist > thisDist) {
1171  niceDots.insert(itnice, *dotToTest);
1172  ++itnice;
1173  stopLoop = true;
1174  // Jump all the pixels between v,u and v,
1175  // tmpDot->getFirstBorder_u()
1176  u = border_u;
1177  v = border_v;
1178  continue;
1179  }
1180  ++itnice;
1181  }
1182  vpTRACE(4, "End while (%d, %d)", u, v);
1183 
1184  // if we reached the end of the vector without finding the dot
1185  // or inserting it, insert it now.
1186  if (itnice == niceDots.end() && stopLoop == false) {
1187  niceDots.push_back(*dotToTest);
1188  }
1189  } else {
1190  // Store bad dots
1191  badDotsVector.push_front(*dotToTest);
1192  }
1193  }
1194  }
1195  if (dotToTest != NULL)
1196  delete dotToTest;
1197 }
1198 
1219 bool vpDot2::isValid(const vpImage<unsigned char> &I, const vpDot2 &wantedDot)
1220 {
1221  double size_precision = wantedDot.getSizePrecision();
1222  double ellipsoidShape_precision = wantedDot.getEllipsoidShapePrecision();
1223 
1224  //
1225  // First, check the width, height and surface of the dot. Those parameters
1226  // must be the same.
1227  //
1228  // if ( (wantedDot.getWidth() != 0)
1229  // && (wantedDot.getHeight() != 0)
1230  // && (wantedDot.getArea() != 0) )
1231  if ((std::fabs(wantedDot.getWidth()) > std::numeric_limits<double>::epsilon()) &&
1232  (std::fabs(wantedDot.getHeight()) > std::numeric_limits<double>::epsilon()) &&
1233  (std::fabs(wantedDot.getArea()) > std::numeric_limits<double>::epsilon()))
1234  // if (size_precision!=0){
1235  {
1236  if (std::fabs(size_precision) > std::numeric_limits<double>::epsilon()) {
1237  double epsilon = 0.001;
1238 #ifdef DEBUG
1239  std::cout << "test size precision......................\n";
1240  std::cout << "wanted dot: "
1241  << "w=" << wantedDot.getWidth() << " h=" << wantedDot.getHeight() << " s=" << wantedDot.getArea()
1242  << " precision=" << size_precision << " epsilon=" << epsilon << std::endl;
1243  std::cout << "dot found: "
1244  << "w=" << getWidth() << " h=" << getHeight() << " s=" << getArea() << std::endl;
1245 #endif
1246 
1247  if ((wantedDot.getWidth() * size_precision - epsilon < getWidth()) == false) {
1248  vpDEBUG_TRACE(3, "Bad width > for dot (%g, %g)", cog.get_u(), cog.get_v());
1249 #ifdef DEBUG
1250  printf("Bad width > for dot (%g, %g)\n", cog.get_u(), cog.get_v());
1251 #endif
1252  return false;
1253  }
1254 
1255  if ((getWidth() < wantedDot.getWidth() / (size_precision + epsilon)) == false) {
1256  vpDEBUG_TRACE(3, "Bad width > for dot (%g, %g)", cog.get_u(), cog.get_v());
1257 #ifdef DEBUG
1258  printf("Bad width %g > %g for dot (%g, %g)\n", getWidth(), wantedDot.getWidth() / (size_precision + epsilon),
1259  cog.get_u(), cog.get_v());
1260 #endif
1261  return false;
1262  }
1263 
1264  if ((wantedDot.getHeight() * size_precision - epsilon < getHeight()) == false) {
1265  vpDEBUG_TRACE(3, "Bad height > for dot (%g, %g)", cog.get_u(), cog.get_v());
1266 #ifdef DEBUG
1267  printf("Bad height %g > %g for dot (%g, %g)\n", wantedDot.getHeight() * size_precision - epsilon, getHeight(),
1268  cog.get_u(), cog.get_v());
1269 #endif
1270  return false;
1271  }
1272 
1273  if ((getHeight() < wantedDot.getHeight() / (size_precision + epsilon)) == false) {
1274  vpDEBUG_TRACE(3, "Bad height > for dot (%g, %g)", cog.get_u(), cog.get_v());
1275 #ifdef DEBUG
1276  printf("Bad height %g > %g for dot (%g, %g)\n", getHeight(), wantedDot.getHeight() / (size_precision + epsilon),
1277  cog.get_u(), cog.get_v());
1278 #endif
1279  return false;
1280  }
1281 
1282  if ((wantedDot.getArea() * (size_precision * size_precision) - epsilon < getArea()) == false) {
1283  vpDEBUG_TRACE(3, "Bad surface > for dot (%g, %g)", cog.get_u(), cog.get_v());
1284 #ifdef DEBUG
1285  printf("Bad surface %g > %g for dot (%g, %g)\n",
1286  wantedDot.getArea() * (size_precision * size_precision) - epsilon, getArea(), cog.get_u(), cog.get_v());
1287 #endif
1288  return false;
1289  }
1290 
1291  if ((getArea() < wantedDot.getArea() / (size_precision * size_precision + epsilon)) == false) {
1292  vpDEBUG_TRACE(3, "Bad surface > for dot (%g, %g)", cog.get_u(), cog.get_v());
1293 #ifdef DEBUG
1294  printf("Bad surface %g < %g for dot (%g, %g)\n", getArea(),
1295  wantedDot.getArea() / (size_precision * size_precision + epsilon), cog.get_u(), cog.get_v());
1296 #endif
1297  return false;
1298  }
1299  }
1300  }
1301  //
1302  // Now we can proceed to more advanced (and costy) checks.
1303  // First check there is a white (>level) elipse within dot
1304  // Then check the dot is surrounded by a black ellipse.
1305  //
1306  int nb_point_to_test = 20; // Nb points to test on inner and outside ellipsoid
1307  int nb_bad_points = 0;
1308  int nb_max_bad_points = (int)(nb_point_to_test * allowedBadPointsPercentage_);
1309  double step_angle = 2 * M_PI / nb_point_to_test;
1310 
1311  // if (ellipsoidShape_precision != 0 && compute_moment) {
1312  if (std::fabs(ellipsoidShape_precision) > std::numeric_limits<double>::epsilon() && compute_moment) {
1313  // std::cout << "test shape precision......................\n";
1314  // See F. Chaumette. Image moments: a general and useful set of features
1315  // for visual servoing. IEEE Trans. on Robotics, 20(4):713-723, August
1316  // 2004.
1317 
1318  // mu11 = m11 - m00 * xg * yg = m11 - m00 * m10/m00 * m01/m00
1319  // = m11 - m10 * m01 / m00
1320  // mu20 = m20 - m00 * xg^2 = m20 - m00 * m10/m00 * m10/m00
1321  // = m20 - m10^2 / m00
1322  // mu02 = m02 - m01^2 / m00
1323  // alpha = 1/2 arctan( 2 * mu11 / (mu20 - mu02) )
1324  //
1325  // a1^2 = 2 / m00 * (mu02 + mu20 + sqrt( (mu20 - mu02)^2 + 4mu11^2) )
1326  //
1327  // a2^2 = 2 / m00 * (mu02 + mu20 - sqrt( (mu20 - mu02)^2 + 4mu11^2) )
1328 
1329  // we compute parameters of the estimated ellipse
1330  double tmp1 = (m01 * m01 - m10 * m10) / m00 + (m20 - m02);
1331  double tmp2 = m11 - m10 * m01 / m00;
1332  double Sqrt = sqrt(tmp1 * tmp1 + 4 * tmp2 * tmp2);
1333  double a1 = sqrt(2 / m00 * ((m20 + m02) - (m10 * m10 + m01 * m01) / m00 + Sqrt));
1334  double a2 = sqrt(2 / m00 * ((m20 + m02) - (m10 * m10 + m01 * m01) / m00 - Sqrt));
1335  double alpha = 0.5 * atan2(2 * (m11 * m00 - m10 * m01), ((m20 - m02) * m00 - m10 * m10 + m01 * m01));
1336 
1337  // to be able to track small dots, minorize the ellipsoid radius for the
1338  // inner test
1339  a1 -= 1.0;
1340  a2 -= 1.0;
1341 
1342  double innerCoef = ellipsoidShape_precision;
1343  unsigned int u, v;
1344  double cog_u = this->cog.get_u();
1345  double cog_v = this->cog.get_v();
1346 
1347  vpImagePoint ip;
1348  nb_bad_points = 0;
1349  for (double theta = 0.; theta < 2 * M_PI; theta += step_angle) {
1350  u = (unsigned int)(cog_u + innerCoef * (a1 * cos(alpha) * cos(theta) - a2 * sin(alpha) * sin(theta)));
1351  v = (unsigned int)(cog_v + innerCoef * (a1 * sin(alpha) * cos(theta) + a2 * cos(alpha) * sin(theta)));
1352  if (!this->hasGoodLevel(I, u, v)) {
1353 // vpTRACE("Inner circle pixel (%d, %d) has bad level for dot (%g, %g)",
1354 // u, v, cog_u, cog_v);
1355 #ifdef DEBUG
1356  printf("Inner circle pixel (%u, %u) has bad level for dot (%g, %g): "
1357  "%d not in [%u, %u]\n",
1358  u, v, cog_u, cog_v, I[v][u], gray_level_min, gray_level_max);
1359 #endif
1360  // return false;
1361  nb_bad_points++;
1362  }
1363  if (graphics) {
1364  for (unsigned int t = 0; t < thickness; t++) {
1365  ip.set_u(u + t);
1366  ip.set_v(v);
1368  }
1369  }
1370 #ifdef DEBUG
1372  vpDisplay::flush(I);
1373 #endif
1374  }
1375  if (nb_bad_points > nb_max_bad_points) {
1376 #ifdef DEBUG
1377  printf("Inner ellipse has %d bad points. Max allowed is %d\n", nb_bad_points, nb_max_bad_points);
1378 #endif
1379  return false;
1380  }
1381  // to be able to track small dots, maximize the ellipsoid radius for the
1382  // inner test
1383  a1 += 2.0;
1384  a2 += 2.0;
1385 
1386  double outCoef = 2 - ellipsoidShape_precision; // 1.6;
1387  nb_bad_points = 0;
1388  for (double theta = 0.; theta < 2 * M_PI; theta += step_angle) {
1389  u = (unsigned int)(cog_u + outCoef * (a1 * cos(alpha) * cos(theta) - a2 * sin(alpha) * sin(theta)));
1390  v = (unsigned int)(cog_v + outCoef * (a1 * sin(alpha) * cos(theta) + a2 * cos(alpha) * sin(theta)));
1391 #ifdef DEBUG
1392  // vpDisplay::displayRectangle(I, area, vpColor::yellow);
1393  vpDisplay::displayCross(I, (int)v, (int)u, 7, vpColor::purple);
1394  vpDisplay::flush(I);
1395 #endif
1396  // If outside the area, continue
1397  if ((double)u < area.getLeft() || (double)u > area.getRight() || (double)v < area.getTop() ||
1398  (double)v > area.getBottom()) {
1399  continue;
1400  }
1401  if (!this->hasReverseLevel(I, u, v)) {
1402 // vpTRACE("Outside circle pixel (%d, %d) has bad level for dot (%g,
1403 // %g)", u, v, cog_u, cog_v);
1404 #ifdef DEBUG
1405  printf("Outside circle pixel (%u, %u) has bad level for dot (%g, "
1406  "%g): %d not in [%u, %u]\n",
1407  u, v, cog_u, cog_v, I[v][u], gray_level_min, gray_level_max);
1408 #endif
1409  nb_bad_points++;
1410  // return false;
1411  }
1412  if (graphics) {
1413  for (unsigned int t = 0; t < thickness; t++) {
1414  ip.set_u(u + t);
1415  ip.set_v(v);
1416 
1418  }
1419  }
1420  }
1421  }
1422  if (nb_bad_points > nb_max_bad_points) {
1423 #ifdef DEBUG
1424  printf("Outside ellipse has %d bad points. Max allowed is %d\n", nb_bad_points, nb_max_bad_points);
1425 #endif
1426  return false;
1427  }
1428 
1429  return true;
1430 }
1431 
1450 bool vpDot2::hasGoodLevel(const vpImage<unsigned char> &I, const unsigned int &u, const unsigned int &v) const
1451 {
1452  if (!isInArea(u, v))
1453  return false;
1454 
1455  if (I[v][u] >= gray_level_min && I[v][u] <= gray_level_max) {
1456  return true;
1457  } else {
1458  return false;
1459  }
1460 }
1461 
1474 bool vpDot2::hasReverseLevel(const vpImage<unsigned char> &I, const unsigned int &u, const unsigned int &v) const
1475 {
1476 
1477  if (!isInArea(u, v))
1478  return false;
1479 
1480  if (I[v][u] < gray_level_min || I[v][u] > gray_level_max) {
1481  return true;
1482  } else {
1483  return false;
1484  }
1485 }
1486 
1495 vpDot2 *vpDot2::getInstance() { return new vpDot2(); }
1496 
1512 void vpDot2::getFreemanChain(std::list<unsigned int> &freeman_chain) const { freeman_chain = direction_list; }
1513 
1514 /******************************************************************************
1515  *
1516  * PRIVATE METHODS
1517  *
1518  ******************************************************************************/
1519 
1551 bool vpDot2::computeParameters(const vpImage<unsigned char> &I, const double &_u, const double &_v)
1552 {
1553  direction_list.clear();
1554  ip_edges_list.clear();
1555 
1556  double est_u = _u; // estimated
1557  double est_v = _v;
1558 
1559  // if u has default value, set it to the actual center value
1560  // if( est_u == -1.0 )
1561  if (std::fabs(est_u + 1.0) <= vpMath::maximum(std::fabs(est_u), 1.) * std::numeric_limits<double>::epsilon()) {
1562  est_u = this->cog.get_u();
1563  }
1564 
1565  // if v has default value, set it to the actual center value
1566  // if( est_v == -1.0 )
1567  if (std::fabs(est_v + 1.0) <= vpMath::maximum(std::fabs(est_v), 1.) * std::numeric_limits<double>::epsilon()) {
1568  est_v = this->cog.get_v();
1569  }
1570 
1571  // if the estimated position of the dot is out of the image, not need to
1572  // continue, return an error tracking
1573  if (!isInArea((unsigned int)est_u, (unsigned int)est_v)) {
1574  vpDEBUG_TRACE(3,
1575  "Initial pixel coordinates (%d, %d) for dot tracking are "
1576  "not in the area",
1577  (int)est_u, (int)est_v);
1578  return false;
1579  }
1580 
1581  bbox_u_min = (int)I.getWidth();
1582  bbox_u_max = 0;
1583  bbox_v_min = (int)I.getHeight();
1584  bbox_v_max = 0;
1585 
1586  // if the first point doesn't have the right level then there's no point to
1587  // continue.
1588  if (!hasGoodLevel(I, (unsigned int)est_u, (unsigned int)est_v)) {
1589  vpDEBUG_TRACE(3, "Can't find a dot from pixel (%d, %d) coordinates", (int)est_u, (int)est_v);
1590  return false;
1591  }
1592 
1593  // find the border
1594 
1595  if (!findFirstBorder(I, (unsigned int)est_u, (unsigned int)est_v, this->firstBorder_u, this->firstBorder_v)) {
1596 
1597  vpDEBUG_TRACE(3, "Can't find first border (%d, %d) coordinates", (int)est_u, (int)est_v);
1598  return false;
1599  }
1600 
1601  unsigned int dir = 6;
1602 
1603  // Determine the first element of the Freeman chain
1604  computeFreemanChainElement(I, this->firstBorder_u, this->firstBorder_v, dir);
1605  unsigned int firstDir = dir;
1606 
1607  // if we are now out of the image, return an error tracking
1608  if (!isInArea(this->firstBorder_u, this->firstBorder_v)) {
1609  vpDEBUG_TRACE(3, "Border pixel coordinates (%d, %d) of the dot are not in the area", this->firstBorder_u,
1610  this->firstBorder_v);
1611  return false;
1612  }
1613 
1614  // store the new direction and dot border coordinates.
1615  direction_list.push_back(dir);
1616  vpImagePoint ip;
1617  ip.set_u(this->firstBorder_u);
1618  ip.set_v(this->firstBorder_v);
1619 
1620  ip_edges_list.push_back(ip);
1621 
1622  int border_u = (int)this->firstBorder_u;
1623  int border_v = (int)this->firstBorder_v;
1624 
1625  // vpTRACE("-----------------------------------------");
1626  // vpTRACE("first border_u: %d border_v: %d dir: %d",
1627  // this->firstBorder_u, this->firstBorder_v,firstDir);
1628  int du, dv;
1629  float dS, dMu, dMv, dMuv, dMu2, dMv2;
1630  m00 = 0.0;
1631  m10 = 0.0;
1632  m01 = 0.0;
1633  m11 = 0.0;
1634  m20 = 0.0;
1635  m02 = 0.0;
1636  // while we didn't come back to the first point, follow the border
1637  do {
1638  // if it was asked, show the border
1639  if (graphics) {
1640  for (int t = 0; t < (int)thickness; t++) {
1641  ip.set_u(border_u + t);
1642  ip.set_v(border_v);
1643 
1645  }
1646  // vpDisplay::flush(I);
1647  }
1648 #ifdef DEBUG
1649  vpDisplay::displayPoint(I, border_v, border_u, vpColor::red);
1650  vpDisplay::flush(I);
1651 #endif
1652  // Determine the increments for the parameters
1653  computeFreemanParameters(border_u, border_v, dir, du, dv,
1654  dS, // surface
1655  dMu, dMv, // first order moments
1656  dMuv, dMu2, dMv2); // second order moment
1657 
1658  // Update the parameters
1659  border_u += du; // Next position on the border
1660  border_v += dv;
1661  m00 += dS; // enclosed area
1662  m10 += dMu; // First order moment along v axis
1663  m01 += dMv; // First order moment along u axis
1664  if (compute_moment) {
1665  m11 += dMuv; // Second order moment
1666  m20 += dMu2; // Second order moment along v axis
1667  m02 += dMv2; // Second order moment along u axis
1668  }
1669  // if we are now out of the image, return an error tracking
1670  if (!isInArea((unsigned int)border_u, (unsigned int)border_v)) {
1671 
1672  vpDEBUG_TRACE(3, "Dot (%d, %d) is not in the area", border_u, border_v);
1673  // Can Occur on a single pixel dot located on the top border
1674  return false;
1675  }
1676 
1677  // store the new direction and dot border coordinates.
1678 
1679  direction_list.push_back(dir);
1680 
1681  ip.set_u(border_u);
1682  ip.set_v(border_v);
1683  ip_edges_list.push_back(ip);
1684 
1685  // vpDisplay::getClick(I);
1686 
1687  // update the extreme point of the dot.
1688  if (border_v < bbox_v_min)
1689  bbox_v_min = border_v;
1690  if (border_v > bbox_v_max)
1691  bbox_v_max = border_v;
1692  if (border_u < bbox_u_min)
1693  bbox_u_min = border_u;
1694  if (border_u > bbox_u_max)
1695  bbox_u_max = border_u;
1696 
1697  // move around the tracked entity by following the border.
1698  if (computeFreemanChainElement(I, (unsigned int)border_u, (unsigned int)border_v, dir) == false) {
1699  vpDEBUG_TRACE(3, "Can't compute Freeman chain for dot (%d, %d)", border_u, border_v);
1700  return false;
1701  }
1702 
1703  // vpTRACE("border_u: %d border_v: %d dir: %d", border_u, border_v,
1704  // dir);
1705 
1706  } while ((getFirstBorder_u() != (unsigned int)border_u || getFirstBorder_v() != (unsigned int)border_v ||
1707  firstDir != dir) &&
1708  isInArea((unsigned int)border_u, (unsigned int)border_v));
1709 
1710 #ifdef VP_DEBUG
1711 #if VP_DEBUG_MODE == 3
1712  vpDisplay::flush(I);
1713 #endif
1714 #endif
1715 
1716  // if the surface is one or zero , the center of gravity wasn't properly
1717  // detected. Return an error tracking.
1718  // if( m00 == 0 || m00 == 1 )
1719  if (std::fabs(m00) <= std::numeric_limits<double>::epsilon() ||
1720  std::fabs(m00 - 1.) <= vpMath::maximum(std::fabs(m00), 1.) * std::numeric_limits<double>::epsilon()) {
1721  vpDEBUG_TRACE(3, "The center of gravity of the dot wasn't properly detected");
1722  return false;
1723  } else // compute the center
1724  {
1725  // this magic formula gives the coordinates of the center of gravity
1726  double tmpCenter_u = m10 / m00;
1727  double tmpCenter_v = m01 / m00;
1728 
1729  // Updates the central moments
1730  if (compute_moment) {
1731  mu11 = m11 - tmpCenter_u * m01;
1732  mu02 = m02 - tmpCenter_v * m01;
1733  mu20 = m20 - tmpCenter_u * m10;
1734  }
1735 
1736  // check the center is in the image... never know...
1737  // if( !hasGoodLevel( I, (unsigned int)tmpCenter_u,
1738  // (unsigned int)tmpCenter_v ) )
1739  // {
1740  // vpDEBUG_TRACE(3, "The center of gravity of the dot (%g, %g) has
1741  // not a good in level", tmpCenter_u, tmpCenter_v); return false;
1742  // }
1743 
1744  cog.set_u(tmpCenter_u);
1745  cog.set_v(tmpCenter_v);
1746  }
1747 
1748  width = bbox_u_max - bbox_u_min + 1;
1749  height = bbox_v_max - bbox_v_min + 1;
1750  surface = m00;
1751 
1752  computeMeanGrayLevel(I);
1753  return true;
1754 }
1755 
1771 bool vpDot2::findFirstBorder(const vpImage<unsigned char> &I, const unsigned int &u, const unsigned int &v,
1772  unsigned int &border_u, unsigned int &border_v)
1773 {
1774  // find the border
1775 
1776  // NOTE:
1777  // from here we use int and not double. This is because we don't have
1778  // rounding problems and it's actually more a trouble than smth else to
1779  // work with double when navigating around the dot.
1780  border_u = u;
1781  border_v = v;
1782  double epsilon = 0.001;
1783 
1784 #ifdef DEBUG
1785  std::cout << "gray level: " << gray_level_min << " " << gray_level_max << std::endl;
1786 #endif
1787  while (hasGoodLevel(I, border_u + 1, border_v) && border_u < area.getRight() /*I.getWidth()*/) {
1788  // if the width of this dot was initialised and we already crossed the dot
1789  // on more than the max possible width, no need to continue, return an
1790  // error tracking
1791  if (getWidth() > 0 && (border_u - u) > getWidth() / (getMaxSizeSearchDistancePrecision() + epsilon)) {
1792  vpDEBUG_TRACE(3,
1793  "The found dot (%d, %d, %d) has a greater width than the "
1794  "required one",
1795  u, v, border_u);
1796  return false;
1797  }
1798 #ifdef DEBUG
1799  vpDisplay::displayPoint(I, (int)border_v, (int)border_u + 1, vpColor::green);
1800  vpDisplay::flush(I);
1801 #endif
1802 
1803  border_u++;
1804  }
1805  return true;
1806 }
1807 
1826 bool vpDot2::computeFreemanChainElement(const vpImage<unsigned char> &I, const unsigned int &u, const unsigned int &v,
1827  unsigned int &element)
1828 {
1829 
1830  if (hasGoodLevel(I, u, v)) {
1831  unsigned int _u = u;
1832  unsigned int _v = v;
1833  // get the point on the right of the point passed in
1834  updateFreemanPosition(_u, _v, (element + 2) % 8);
1835  if (hasGoodLevel(I, _u, _v)) {
1836  element = (element + 2) % 8; // turn right
1837  } else {
1838  unsigned int _u1 = u;
1839  unsigned int _v1 = v;
1840  updateFreemanPosition(_u1, _v1, (element + 1) % 8);
1841 
1842  if (hasGoodLevel(I, _u1, _v1)) {
1843  element = (element + 1) % 8; // turn diag right
1844  } else {
1845  unsigned int _u2 = u;
1846  unsigned int _v2 = v;
1847  updateFreemanPosition(_u2, _v2, element); // same direction
1848 
1849  if (hasGoodLevel(I, _u2, _v2)) {
1850  // element = element; // keep same dir
1851  } else {
1852  unsigned int _u3 = u;
1853  unsigned int _v3 = v;
1854  updateFreemanPosition(_u3, _v3, (element + 7) % 8); // diag left
1855 
1856  if (hasGoodLevel(I, _u3, _v3)) {
1857  element = (element + 7) % 8; // turn diag left
1858  } else {
1859  unsigned int _u4 = u;
1860  unsigned int _v4 = v;
1861  updateFreemanPosition(_u4, _v4, (element + 6) % 8); // left
1862 
1863  if (hasGoodLevel(I, _u4, _v4)) {
1864  element = (element + 6) % 8; // turn left
1865  } else {
1866  unsigned int _u5 = u;
1867  unsigned int _v5 = v;
1868  updateFreemanPosition(_u5, _v5, (element + 5) % 8); // left
1869 
1870  if (hasGoodLevel(I, _u5, _v5)) {
1871  element = (element + 5) % 8; // turn diag down
1872  } else {
1873  unsigned int _u6 = u;
1874  unsigned int _v6 = v;
1875  updateFreemanPosition(_u6, _v6, (element + 4) % 8); // left
1876 
1877  if (hasGoodLevel(I, _u6, _v6)) {
1878  element = (element + 4) % 8; // turn down
1879  } else {
1880  unsigned int _u7 = u;
1881  unsigned int _v7 = v;
1882  updateFreemanPosition(_u7, _v7, (element + 3) % 8); // diag
1883 
1884  if (hasGoodLevel(I, _u7, _v7)) {
1885  element = (element + 3) % 8; // turn diag right down
1886  } else {
1887  // No neighbor with a good level
1888  //
1889  return false;
1890  }
1891  }
1892  }
1893  }
1894  }
1895  }
1896  }
1897  }
1898  }
1899 
1900  else {
1901  return false;
1902  }
1903 
1904  return true;
1905 }
1906 
1938 void vpDot2::computeFreemanParameters(const int &u_p, const int &v_p, unsigned int &element, int &du, int &dv,
1939  float &dS, float &dMu, float &dMv, float &dMuv, float &dMu2, float &dMv2)
1940 {
1941  du = 0;
1942  dv = 0;
1943  dMuv = 0;
1944  dMu2 = 0;
1945  dMv2 = 0;
1946 
1947  /*
1948  3 2 1
1949  \ | /
1950  \|/
1951  4 ------- 0
1952  /|\
1953  / | \
1954  5 6 7
1955  */
1956  switch (element) {
1957  case 0: // go right
1958  du = 1;
1959  dS = (float)v_p;
1960  dMu = 0.0;
1961  dMv = (float)(0.5 * v_p * v_p);
1962  if (compute_moment) {
1963  dMuv = (float)(0.25 * v_p * v_p * (2 * u_p + 1));
1964  dMu2 = 0;
1965  dMv2 = (float)(1.0 / 3. * v_p * v_p * v_p);
1966  }
1967  break;
1968 
1969  case 1: // go right top
1970  du = 1;
1971  dv = 1;
1972  dS = (float)(v_p + 0.5);
1973  dMu = -(float)(0.5 * u_p * (u_p + 1) + 1.0 / 6.0);
1974  dMv = (float)(0.5 * v_p * (v_p + 1) + 1.0 / 6.0);
1975  if (compute_moment) {
1976  float half_u_p = (float)(0.5 * u_p);
1977  dMuv = (float)(v_p * v_p * (0.25 + half_u_p) + v_p * (1. / 3. + half_u_p) + 1. / 6. * u_p + 0.125);
1978  dMu2 = (float)(-1. / 3. * u_p * (u_p * u_p + 1.5 * u_p + 1.) - 1. / 12.0);
1979  dMv2 = (float)(1. / 3. * v_p * (v_p * v_p + 1.5 * v_p + 1.) + 1. / 12.0);
1980  }
1981  break;
1982 
1983  case 2: // go top
1984  dv = 1;
1985  dS = 0.0;
1986  dMu = (float)(-0.5 * u_p * u_p);
1987  dMv = 0.0;
1988  if (compute_moment) {
1989  dMuv = 0;
1990  dMu2 = (float)(-1.0 / 3. * u_p * u_p * u_p);
1991  dMv2 = 0;
1992  }
1993  break;
1994 
1995  case 3:
1996  du = -1;
1997  dv = 1;
1998  dS = (float)(-v_p - 0.5);
1999  dMu = -(float)(0.5 * u_p * (u_p - 1) + 1.0 / 6.0);
2000  dMv = -(float)(0.5 * v_p * (v_p + 1) + 1.0 / 6.0);
2001  if (compute_moment) {
2002  float half_u_p = (float)(0.5 * u_p);
2003  dMuv = (float)(v_p * v_p * (0.25 - half_u_p) + v_p * (1. / 3. - half_u_p) - 1. / 6. * u_p + 0.125);
2004  dMu2 = (float)(-1. / 3. * u_p * (u_p * u_p - 1.5 * u_p + 1.) - 1. / 12.0);
2005  dMv2 = (float)(-1. / 3. * v_p * (v_p * v_p + 1.5 * v_p + 1.) - 1. / 12.0);
2006  }
2007  break;
2008 
2009  case 4:
2010  du = -1;
2011  dS = (float)(-v_p);
2012  dMv = (float)(-0.5 * v_p * v_p);
2013  dMu = 0.0;
2014  if (compute_moment) {
2015  dMuv = (float)(-0.25 * v_p * v_p * (2 * u_p - 1));
2016  dMu2 = 0;
2017  dMv2 = (float)(-1.0 / 3. * v_p * v_p * v_p);
2018  }
2019  break;
2020 
2021  case 5:
2022  du = -1;
2023  dv = -1;
2024  dS = (float)(-v_p + 0.5);
2025  dMu = (float)(0.5 * u_p * (u_p - 1) + 1.0 / 6.0);
2026  dMv = (float)(-(0.5 * v_p * (v_p - 1) + 1.0 / 6.0));
2027  if (compute_moment) {
2028  float half_u_p = (float)(0.5 * u_p);
2029  dMuv = (float)(v_p * v_p * (0.25 - half_u_p) - v_p * (1. / 3. - half_u_p) - 1. / 6. * u_p + 0.125);
2030  dMu2 = (float)(1. / 3. * u_p * (u_p * u_p - 1.5 * u_p + 1.) - 1. / 12.0);
2031  dMv2 = (float)(-1. / 3. * v_p * (v_p * v_p - 1.5 * v_p + 1.) - 1. / 12.0);
2032  }
2033  break;
2034 
2035  case 6:
2036  dv = -1;
2037  dS = 0.0;
2038  dMu = (float)(0.5 * u_p * u_p);
2039  dMv = 0.0;
2040  if (compute_moment) {
2041  dMuv = 0;
2042  dMu2 = (float)(1.0 / 3. * u_p * u_p * u_p);
2043  dMv2 = 0;
2044  }
2045  break;
2046 
2047  case 7:
2048  du = 1;
2049  dv = -1;
2050  dS = (float)(v_p - 0.5);
2051  dMu = (float)(0.5 * u_p * (u_p + 1) + 1.0 / 6.0);
2052  dMv = (float)(0.5 * v_p * (v_p - 1) + 1.0 / 6.0);
2053  if (compute_moment) {
2054  float half_u_p = (float)(0.5 * u_p);
2055  dMuv = (float)(v_p * v_p * (0.25 + half_u_p) - v_p * (1. / 3. + half_u_p) + 1. / 6. * u_p + 0.125);
2056  dMu2 = (float)(1. / 3. * u_p * (u_p * u_p + 1.5 * u_p + 1.) + 1. / 12.0);
2057  dMv2 = (float)(1. / 3. * v_p * (v_p * v_p - 1.5 * v_p + 1.) - 1. / 12.0);
2058  }
2059  break;
2060  }
2061 }
2062 
2076 void vpDot2::updateFreemanPosition(unsigned int &u, unsigned int &v, const unsigned int &dir)
2077 {
2078  switch (dir) {
2079  case 0:
2080  u += 1;
2081  break;
2082  case 1:
2083  u += 1;
2084  v += 1;
2085  break;
2086  case 2:
2087  v += 1;
2088  break;
2089  case 3:
2090  u -= 1;
2091  v += 1;
2092  break;
2093  case 4:
2094  u -= 1;
2095  break;
2096  case 5:
2097  u -= 1;
2098  v -= 1;
2099  break;
2100  case 6:
2101  v -= 1;
2102  break;
2103  case 7:
2104  u += 1;
2105  v -= 1;
2106  break;
2107  }
2108 }
2109 
2121 bool vpDot2::isInImage(const vpImage<unsigned char> &I) const { return isInImage(I, cog); }
2122 
2134 bool vpDot2::isInImage(const vpImage<unsigned char> &I, const vpImagePoint &ip) const
2135 {
2136  unsigned int h = I.getHeight();
2137  unsigned int w = I.getWidth();
2138  double u = ip.get_u();
2139  double v = ip.get_v();
2140 
2141  if (u < 0 || u >= w)
2142  return false;
2143  if (v < 0 || v >= h)
2144  return false;
2145  return true;
2146 }
2147 
2159 bool vpDot2::isInArea(const unsigned int &u, const unsigned int &v) const
2160 {
2161  unsigned int area_u_min = (unsigned int)area.getLeft();
2162  unsigned int area_u_max = (unsigned int)area.getRight();
2163  unsigned int area_v_min = (unsigned int)area.getTop();
2164  unsigned int area_v_max = (unsigned int)area.getBottom();
2165 
2166  if (u < area_u_min || u > area_u_max)
2167  return false;
2168  if (v < area_v_min || v > area_v_max)
2169  return false;
2170  return true;
2171 }
2172 
2184 void vpDot2::getGridSize(unsigned int &gridWidth, unsigned int &gridHeight)
2185 {
2186  // first get the research grid width and height Note that
2187  // 1/sqrt(2)=cos(pi/4). The grid squares should be small enough to be
2188  // contained in the dot. We gent this here if the dot is a perfect disc.
2189  // More accurate criterium to define the grid should be implemented if
2190  // necessary
2191  gridWidth = (unsigned int)(getWidth() * getMaxSizeSearchDistancePrecision() / sqrt(2.));
2192  gridHeight = (unsigned int)(getHeight() * getMaxSizeSearchDistancePrecision() / sqrt(2.0));
2193 
2194  if (gridWidth == 0)
2195  gridWidth = 1;
2196  if (gridHeight == 0)
2197  gridHeight = 1;
2198 }
2199 
2212 void vpDot2::computeMeanGrayLevel(const vpImage<unsigned char> &I)
2213 {
2214  int cog_u = (int)cog.get_u();
2215  int cog_v = (int)cog.get_v();
2216 
2217  unsigned int sum_value = 0;
2218  unsigned int nb_pixels = 0;
2219 
2220  for (unsigned int i = (unsigned int)this->bbox_u_min; i <= (unsigned int)this->bbox_u_max; i++) {
2221  unsigned int pixel_gray = (unsigned int)I[(unsigned int)cog_v][i];
2222  if (pixel_gray >= getGrayLevelMin() && pixel_gray <= getGrayLevelMax()) {
2223  sum_value += pixel_gray;
2224  nb_pixels++;
2225  }
2226  }
2227  for (unsigned int i = (unsigned int)this->bbox_v_min; i <= (unsigned int)this->bbox_v_max; i++) {
2228  unsigned char pixel_gray = I[i][(unsigned int)cog_u];
2229  if (pixel_gray >= getGrayLevelMin() && pixel_gray <= getGrayLevelMax()) {
2230  sum_value += pixel_gray;
2231  nb_pixels++;
2232  }
2233  }
2234  if (nb_pixels < 10) { // could be good to choose the min nb points from area of dot
2235  // add diagonals points to have enough point
2236  int imin, imax;
2237  if ((cog_u - bbox_u_min) > (cog_v - bbox_v_min)) {
2238  imin = cog_v - bbox_v_min;
2239  } else {
2240  imin = cog_u - bbox_u_min;
2241  }
2242  if ((bbox_u_max - cog_u) > (bbox_v_max - cog_v)) {
2243  imax = bbox_v_max - cog_v;
2244  } else {
2245  imax = bbox_u_max - cog_u;
2246  }
2247  for (int i = -imin; i <= imax; i++) {
2248  unsigned int pixel_gray = (unsigned int)I[(unsigned int)(cog_v + i)][(unsigned int)(cog_u + i)];
2249  if (pixel_gray >= getGrayLevelMin() && pixel_gray <= getGrayLevelMax()) {
2250  sum_value += pixel_gray;
2251  nb_pixels++;
2252  }
2253  }
2254 
2255  if ((cog_u - bbox_u_min) > (bbox_v_max - cog_v)) {
2256  imin = bbox_v_max - cog_v;
2257  } else {
2258  imin = cog_u - bbox_u_min;
2259  }
2260  if ((bbox_u_max - cog_u) > (cog_v - bbox_v_min)) {
2261  imax = cog_v - bbox_v_min;
2262  } else {
2263  imax = bbox_u_max - cog_u;
2264  }
2265 
2266  for (int i = -imin; i <= imax; i++) {
2267  unsigned char pixel_gray = I[(unsigned int)(cog_v - i)][(unsigned int)(cog_u + i)];
2268  if (pixel_gray >= getGrayLevelMin() && pixel_gray <= getGrayLevelMax()) {
2269  sum_value += pixel_gray;
2270  nb_pixels++;
2271  }
2272  }
2273  }
2274 
2275  if (nb_pixels == 0) {
2276  // should never happen
2277  throw(vpTrackingException(vpTrackingException::notEnoughPointError, "No point was found"));
2278  } else {
2279  mean_gray_level = sum_value / nb_pixels;
2280  }
2281 }
2282 
2301 vpMatrix vpDot2::defineDots(vpDot2 dot[], const unsigned int &n, const std::string &dotFile, vpImage<unsigned char> &I,
2302  vpColor col, bool trackDot)
2303 {
2304  vpMatrix Cogs(n, 2);
2305  vpImagePoint cog;
2306  unsigned int i;
2307  bool fromFile = vpIoTools::checkFilename(dotFile.c_str());
2308  if (fromFile) {
2309  vpMatrix::loadMatrix(dotFile, Cogs);
2310  std::cout << Cogs.getRows() << " dots loaded from file " << dotFile << std::endl;
2311  }
2312 
2313  // test number of cogs in file
2314  if (Cogs.getRows() < n) {
2315  std::cout << "Dot file has a wrong number of dots : redefining them" << std::endl;
2316  fromFile = false;
2317  }
2318 
2319  // read from file and tracks the dots
2320  if (fromFile) {
2321  try {
2322  for (i = 0; i < n; ++i) {
2323  cog.set_uv(Cogs[i][0], Cogs[i][1]);
2324  dot[i].setGraphics(true);
2325  dot[i].setCog(cog);
2326  if (trackDot) {
2327  dot[i].initTracking(I, cog);
2328  dot[i].track(I);
2329  vpDisplay::displayCross(I, cog, 10, col);
2330  }
2331  }
2332  } catch (...) {
2333  std::cout << "Cannot track dots from file" << std::endl;
2334  fromFile = false;
2335  }
2336  vpDisplay::flush(I);
2337 
2338  // check that dots are far away ones from the other
2339  for (i = 0; i < n && fromFile; ++i) {
2340  double d = sqrt(vpMath::sqr(dot[i].getHeight()) + vpMath::sqr(dot[i].getWidth()));
2341  for (unsigned int j = 0; j < n && fromFile; ++j)
2342  if (j != i)
2343  if (dot[i].getDistance(dot[j]) < d) {
2344  fromFile = false;
2345  std::cout << "Dots from file seem incoherent" << std::endl;
2346  }
2347  }
2348  }
2349 
2350  if (!fromFile) {
2351  vpDisplay::display(I);
2352  vpDisplay::flush(I);
2353 
2354  std::cout << "Click on the " << n << " dots clockwise starting from upper/left dot..." << std::endl;
2355  for (i = 0; i < n; i++) {
2356  if (trackDot) {
2357  dot[i].setGraphics(true);
2358  dot[i].initTracking(I);
2359  cog = dot[i].getCog();
2360  } else {
2361  vpDisplay::getClick(I, cog);
2362  dot[i].setCog(cog);
2363  }
2364  Cogs[i][0] = cog.get_u();
2365  Cogs[i][1] = cog.get_v();
2366  vpDisplay::displayCross(I, cog, 10, col);
2367  vpDisplay::flush(I);
2368  }
2369  }
2370 
2371  if (!fromFile && (dotFile != "")) {
2372  vpMatrix::saveMatrix(dotFile, Cogs);
2373  std::cout << Cogs.getRows() << " dots written to file " << dotFile << std::endl;
2374  }
2375 
2376  // back to non graphic mode
2377  for (i = 0; i < n; ++i)
2378  dot[i].setGraphics(false);
2379 
2380  return Cogs;
2381 }
2382 
2399 void vpDot2::trackAndDisplay(vpDot2 dot[], const unsigned int &n, vpImage<unsigned char> &I,
2400  std::vector<vpImagePoint> &cogs, vpImagePoint *cogStar)
2401 {
2402  unsigned int i;
2403  // tracking
2404  for (i = 0; i < n; ++i) {
2405  dot[i].track(I);
2406  cogs.push_back(dot[i].getCog());
2407  }
2408  // trajectories
2409  for (i = n; i < cogs.size(); ++i)
2410  vpDisplay::displayCircle(I, cogs[i], 4, vpColor::green, true);
2411  // initial position
2412  for (i = 0; i < n; ++i)
2413  vpDisplay::displayCircle(I, cogs[i], 4, vpColor::blue, true);
2414  // if exists, desired position
2415  if (cogStar != NULL)
2416  for (i = 0; i < n; ++i) {
2417  vpDisplay::displayDotLine(I, cogStar[i], dot[i].getCog(), vpColor::red);
2418  vpDisplay::displayCircle(I, cogStar[i], 4, vpColor::red, true);
2419  }
2420  vpDisplay::flush(I);
2421 }
2422 
2438  const std::list<vpImagePoint> &edges_list, vpColor color, unsigned int thickness)
2439 {
2440  vpDisplay::displayCross(I, cog, 3 * thickness + 8, color, thickness);
2441  std::list<vpImagePoint>::const_iterator it;
2442 
2443  for (it = edges_list.begin(); it != edges_list.end(); ++it) {
2444  vpDisplay::displayPoint(I, *it, color);
2445  }
2446 }
2447 
2462 void vpDot2::display(const vpImage<vpRGBa> &I, const vpImagePoint &cog, const std::list<vpImagePoint> &edges_list,
2463  vpColor color, unsigned int thickness)
2464 {
2465  vpDisplay::displayCross(I, cog, 3 * thickness + 8, color, thickness);
2466  std::list<vpImagePoint>::const_iterator it;
2467 
2468  for (it = edges_list.begin(); it != edges_list.end(); ++it) {
2469  vpDisplay::displayPoint(I, *it, color);
2470  }
2471 }
2472 
2478 VISP_EXPORT std::ostream &operator<<(std::ostream &os, vpDot2 &d) { return (os << "(" << d.getCog() << ")"); }
double getWidth() const
Definition: vpDot2.cpp:612
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:104
double m02
Definition: vpDot2.h:391
double getTop() const
Definition: vpRect.h:175
void setGrayLevelMax(const unsigned int &max)
Definition: vpDot2.h:331
double get_v() const
Definition: vpImagePoint.h:274
void searchDotsInArea(const vpImage< unsigned char > &I, int area_u, int area_v, unsigned int area_w, unsigned int area_h, std::list< vpDot2 > &niceDots)
Definition: vpDot2.cpp:968
void getFreemanChain(std::list< unsigned int > &freeman_chain) const
Definition: vpDot2.cpp:1512
static bool loadMatrix(const std::string &filename, vpArray2D< double > &M, const bool binary=false, char *header=NULL)
Definition: vpMatrix.h:618
double mu02
Definition: vpDot2.h:410
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
double get_i() const
Definition: vpImagePoint.h:204
unsigned int getWidth() const
Definition: vpImage.h:239
void setMaxSizeSearchDistancePrecision(const double &maxSizeSearchDistancePrecision)
Definition: vpDot2.cpp:822
Class to define colors available for display functionnalities.
Definition: vpColor.h:120
void setEllipsoidBadPointsPercentage(const double &percentage=0.0)
Definition: vpDot2.h:270
double getGrayLevelPrecision() const
Definition: vpDot2.cpp:633
vpDot2 & operator=(const vpDot2 &twinDot)
Definition: vpDot2.cpp:147
double get_u() const
Definition: vpImagePoint.h:263
void setCog(const vpImagePoint &ip)
Definition: vpDot2.h:241
error that can be emited by ViSP classes.
Definition: vpException.h:71
double m11
Definition: vpDot2.h:375
static void displayPoint(const vpImage< unsigned char > &I, const vpImagePoint &ip, const vpColor &color, unsigned int thickness=1)
double getHeight() const
Definition: vpDot2.cpp:619
double getArea() const
Definition: vpDot2.cpp:626
double getEllipsoidShapePrecision() const
Definition: vpDot2.cpp:649
double getRight() const
Definition: vpRect.h:162
double getDistance(const vpDot2 &distantDot) const
Definition: vpDot2.cpp:662
static const vpColor green
Definition: vpColor.h:183
This tracker is meant to track a blob (connex pixels with same gray level) on a vpImage.
Definition: vpDot2.h:126
double m01
Definition: vpDot2.h:367
void track(const vpImage< unsigned char > &I)
Definition: vpDot2.cpp:438
static void flush(const vpImage< unsigned char > &I)
double get_j() const
Definition: vpImagePoint.h:215
double getSizePrecision() const
Definition: vpDot2.cpp:640
static const vpColor red
Definition: vpColor.h:180
Class that defines what is a feature generic tracker.
Definition: vpTracker.h:64
static Type maximum(const Type &a, const Type &b)
Definition: vpMath.h:137
vpImagePoint getCog() const
Definition: vpDot2.h:161
friend VISP_EXPORT std::ostream & operator<<(std::ostream &os, vpDot2 &d)
Definition: vpDot2.cpp:2478
void setGraphicsThickness(unsigned int t)
Definition: vpDot2.h:301
static bool checkFilename(const char *filename)
Definition: vpIoTools.cpp:675
double getBottom() const
Definition: vpRect.h:94
void setGrayLevelPrecision(const double &grayLevelPrecision)
Definition: vpDot2.cpp:724
Error that can be emited by the vpTracker class and its derivates.
double mu11
Definition: vpDot2.h:400
double getMaxSizeSearchDistancePrecision() const
Definition: vpDot2.cpp:657
void set_u(const double u)
Definition: vpImagePoint.h:226
double m20
Definition: vpDot2.h:382
#define vpTRACE
Definition: vpDebug.h:416
static double sqr(double x)
Definition: vpMath.h:108
static void display(const vpImage< unsigned char > &I)
void display(const vpImage< unsigned char > &I, vpColor color=vpColor::red, unsigned int thickness=1) const
Definition: vpDot2.cpp:212
void set_v(const double v)
Definition: vpImagePoint.h:237
unsigned int getGrayLevelMin() const
Definition: vpDot2.h:201
void setComputeMoments(const bool activate)
Definition: vpDot2.h:256
void setArea(const double &area)
Definition: vpDot2.cpp:705
static void displayRectangle(const vpImage< unsigned char > &I, const vpImagePoint &topLeft, unsigned int width, unsigned int height, const vpColor &color, bool fill=false, unsigned int thickness=1)
unsigned int getRows() const
Definition: vpArray2D.h:156
vpDot2()
Definition: vpDot2.cpp:104
void setEllipsoidShapePrecision(const double &ellipsoidShapePrecision)
Definition: vpDot2.cpp:795
void setGrayLevelMin(const unsigned int &min)
Definition: vpDot2.h:314
static void displayCircle(const vpImage< unsigned char > &I, const vpImagePoint &center, unsigned int radius, const vpColor &color, bool fill=false, unsigned int thickness=1)
void setWidth(const double &width)
Definition: vpDot2.cpp:681
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
static void trackAndDisplay(vpDot2 dot[], const unsigned int &n, vpImage< unsigned char > &I, std::vector< vpImagePoint > &cogs, vpImagePoint *cogStar=NULL)
Definition: vpDot2.cpp:2399
void setSizePrecision(const double &sizePrecision)
Definition: vpDot2.cpp:752
void initTracking(const vpImage< unsigned char > &I, unsigned int size=0)
Definition: vpDot2.cpp:253
static bool saveMatrix(const std::string &filename, const vpArray2D< double > &M, const bool binary=false, const char *header="")
Definition: vpMatrix.h:653
void setHeight(const double &height)
Definition: vpDot2.cpp:693
double m10
Definition: vpDot2.h:359
#define vpDEBUG_TRACE
Definition: vpDebug.h:487
unsigned int getHeight() const
Definition: vpImage.h:178
void set_uv(const double u, const double v)
Definition: vpImagePoint.h:248
double mu20
Definition: vpDot2.h:405
Defines a rectangle in the plane.
Definition: vpRect.h:78
double getMeanGrayLevel() const
Definition: vpDot2.h:215
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
double m00
Definition: vpDot2.h:351
unsigned int getGrayLevelMax() const
Definition: vpDot2.h:207
static const vpColor purple
Definition: vpColor.h:191
void setRect(double l, double t, double w, double h)
Definition: vpRect.h:268
double getLeft() const
Definition: vpRect.h:156
static vpMatrix defineDots(vpDot2 dot[], const unsigned int &n, const std::string &dotFile, vpImage< unsigned char > &I, vpColor col=vpColor::blue, bool trackDot=true)
Definition: vpDot2.cpp:2301
void setGraphics(const bool activate)
Definition: vpDot2.h:294
static void displayDotLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)
virtual ~vpDot2()
Definition: vpDot2.cpp:198
static const vpColor blue
Definition: vpColor.h:186