Visual Servoing Platform  version 3.4.0
vpDot2.h
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  *
37  *****************************************************************************/
38 
39 /*
40  \file vpDot2.h
41  \brief This tracker is meant to track some zones on a vpImage.
42 */
43 
44 #ifndef vpDot2_hh
45 #define vpDot2_hh
46 
47 #include <visp3/core/vpColor.h>
48 #include <visp3/core/vpImage.h>
49 #include <visp3/core/vpImagePoint.h>
50 #include <visp3/core/vpPolygon.h>
51 #include <visp3/core/vpRect.h>
52 #include <visp3/core/vpTracker.h>
53 
54 #include <list>
55 #include <vector>
56 
126 class VISP_EXPORT vpDot2 : public vpTracker
127 {
128 public:
129  vpDot2();
130  explicit vpDot2(const vpImagePoint &ip);
131  vpDot2(const vpDot2 &twinDot);
132  virtual ~vpDot2();
133 
134  static vpMatrix defineDots(vpDot2 dot[], const unsigned int &n, const std::string &dotFile, vpImage<unsigned char> &I,
135  vpColor col = vpColor::blue, bool trackDot = true);
136 
137  void display(const vpImage<unsigned char> &I, vpColor color = vpColor::red, unsigned int thickness = 1) const;
138 
148  inline vpColVector get_nij() const
149  {
150  vpColVector nij(3);
151  nij[0] = mu20 / m00;
152  nij[1] = mu11 / m00;
153  nij[2] = mu02 / m00;
154 
155  return nij;
156  }
157 
158  double getArea() const;
166  inline vpRect getBBox() const
167  {
168  vpRect bbox;
169 
170  bbox.setRect(this->bbox_u_min, this->bbox_v_min, this->bbox_u_max - this->bbox_u_min + 1,
171  this->bbox_v_max - this->bbox_v_min + 1);
172 
173  return (bbox);
174  };
180  inline vpImagePoint getCog() const { return cog; }
181 
182  double getDistance(const vpDot2 &distantDot) const;
192  void getEdges(std::list<vpImagePoint> &edges_list) const { edges_list = this->ip_edges_list; };
202  std::list<vpImagePoint> getEdges() const { return (this->ip_edges_list); };
209  double getEllipsoidBadPointsPercentage() const { return allowedBadPointsPercentage_; }
210 
211  double getEllipsoidShapePrecision() const;
212  void getFreemanChain(std::list<unsigned int> &freeman_chain) const;
213 
214  inline double getGamma() const { return this->gamma; };
220  inline unsigned int getGrayLevelMin() const { return gray_level_min; };
226  inline unsigned int getGrayLevelMax() const { return gray_level_max; };
227  double getGrayLevelPrecision() const;
228 
229  double getHeight() const;
230  double getMaxSizeSearchDistancePrecision() const;
234  double getMeanGrayLevel() const { return (this->mean_gray_level); };
238  vpPolygon getPolygon() const { return (vpPolygon(ip_edges_list)); };
239  double getSizePrecision() const;
240  double getWidth() const;
241 
242  void initTracking(const vpImage<unsigned char> &I, unsigned int size = 0);
243  void initTracking(const vpImage<unsigned char> &I, const vpImagePoint &ip, unsigned int size = 0);
244  void initTracking(const vpImage<unsigned char> &I, const vpImagePoint &ip, unsigned int gray_lvl_min,
245  unsigned int gray_lvl_max, unsigned int size = 0);
246 
247  vpDot2 &operator=(const vpDot2 &twinDot);
248  friend VISP_EXPORT std::ostream &operator<<(std::ostream &os, vpDot2 &d);
249 
250  void print(std::ostream &os) { os << *this << std::endl; }
251  void searchDotsInArea(const vpImage<unsigned char> &I, int area_u, int area_v, unsigned int area_w,
252  unsigned int area_h, std::list<vpDot2> &niceDots);
253 
254  void searchDotsInArea(const vpImage<unsigned char> &I, std::list<vpDot2> &niceDots);
255 
256  void setArea(const double &area);
260  inline void setCog(const vpImagePoint &ip) { this->cog = ip; }
276  void setComputeMoments(bool activate) { compute_moment = activate; }
277 
290  void setEllipsoidBadPointsPercentage(const double &percentage = 0.0)
291  {
292  if (percentage < 0.)
293  allowedBadPointsPercentage_ = 0.;
294  else if (percentage > 1.)
295  allowedBadPointsPercentage_ = 1.;
296  else
297  allowedBadPointsPercentage_ = percentage;
298  }
299 
300  void setEllipsoidShapePrecision(const double &ellipsoidShapePrecision);
314  void setGraphics(bool activate) { graphics = activate; }
321  void setGraphicsThickness(unsigned int t) { this->thickness = t; };
334  inline void setGrayLevelMin(const unsigned int &min)
335  {
336  if (min > 255)
337  this->gray_level_min = 255;
338  else
339  this->gray_level_min = min;
340  };
341 
351  inline void setGrayLevelMax(const unsigned int &max)
352  {
353  if (max > 255)
354  this->gray_level_max = 255;
355  else
356  this->gray_level_max = max;
357  };
358  void setGrayLevelPrecision(const double &grayLevelPrecision);
359  void setHeight(const double &height);
360  void setMaxSizeSearchDistancePrecision(const double &maxSizeSearchDistancePrecision);
361  void setSizePrecision(const double &sizePrecision);
362  void setWidth(const double &width);
363 
364  void track(const vpImage<unsigned char> &I, bool canMakeTheWindowGrow = true);
365  void track(const vpImage<unsigned char> &I, vpImagePoint &cog, bool canMakeTheWindowGrow = true);
366 
367  static void trackAndDisplay(vpDot2 dot[], const unsigned int &n, vpImage<unsigned char> &I,
368  std::vector<vpImagePoint> &cogs, vpImagePoint *cogStar = NULL);
369 
370 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
371 public:
372 #else
373 private:
374 #endif
375  double m00;
383  double m10;
391  double m01;
399  double m11;
406  double m20;
415  double m02;
424  double mu11;
429  double mu20;
434  double mu02;
440 private:
441  virtual bool isValid(const vpImage<unsigned char> &I, const vpDot2 &wantedDot);
442 
443  virtual bool hasGoodLevel(const vpImage<unsigned char> &I, const unsigned int &u, const unsigned int &v) const;
444  virtual bool hasReverseLevel(const vpImage<unsigned char> &I, const unsigned int &u, const unsigned int &v) const;
445 
446  virtual vpDot2 *getInstance();
447 
448  void init();
449 
450  bool computeParameters(const vpImage<unsigned char> &I, const double &u = -1.0, const double &v = -1.0);
451 
452  bool findFirstBorder(const vpImage<unsigned char> &I, const unsigned int &u, const unsigned int &v,
453  unsigned int &border_u, unsigned int &border_v);
454  void computeMeanGrayLevel(const vpImage<unsigned char> &I);
455 
464  unsigned int getFirstBorder_u() const { return this->firstBorder_u; }
473  unsigned int getFirstBorder_v() const { return this->firstBorder_v; }
474 
475  bool computeFreemanChainElement(const vpImage<unsigned char> &I, const unsigned int &u, const unsigned int &v,
476  unsigned int &element);
477  void computeFreemanParameters(const int &u_p, const int &v_p, unsigned int &element, int &du, int &dv, float &dS,
478  float &dMu, float &dMv, float &dMuv, float &dMu2, float &dMv2);
479  void updateFreemanPosition(unsigned int &u, unsigned int &v, const unsigned int &dir);
480 
481  bool isInImage(const vpImage<unsigned char> &I) const;
482  bool isInImage(const vpImage<unsigned char> &I, const vpImagePoint &ip) const;
483 
484  bool isInArea(const unsigned int &u, const unsigned int &v) const;
485 
486  void getGridSize(unsigned int &gridWidth, unsigned int &gridHeight);
487  void setArea(const vpImage<unsigned char> &I, int u, int v, unsigned int w, unsigned int h);
488  void setArea(const vpImage<unsigned char> &I);
489  void setArea(const vpRect &a);
490 
491  unsigned char getMeanGrayLevel(vpImage<unsigned char> &I) const;
493  vpImagePoint cog;
494 
495  double width;
496  double height;
497  double surface;
498  unsigned int gray_level_min; // minumum gray level for the dot.
499  // pixel with lower level don't belong
500  // to this dot.
501 
502  unsigned int gray_level_max; // maximum gray level for the dot.
503  // pixel with higher level don't belong
504  // to this dot.
505  double mean_gray_level; // Mean gray level of the dot
506  double grayLevelPrecision;
507  double gamma;
508  double sizePrecision;
509  double ellipsoidShapePrecision;
510  double maxSizeSearchDistancePrecision;
511  double allowedBadPointsPercentage_;
512  // Area where the dot is to search
513  vpRect area;
514 
515  // other
516  std::list<unsigned int> direction_list;
517  std::list<vpImagePoint> ip_edges_list;
518 
519  // flag
520  bool compute_moment; // true moment are computed
521  bool graphics; // true for graphic overlay display
522 
523  unsigned int thickness; // Graphics thickness
524 
525  // Bounding box
526  int bbox_u_min, bbox_u_max, bbox_v_min, bbox_v_max;
527 
528  // The first point coodinate on the dot border
529  unsigned int firstBorder_u;
530  unsigned int firstBorder_v;
531 
532  // Static funtions
533 public:
534  static void display(const vpImage<unsigned char> &I, const vpImagePoint &cog,
535  const std::list<vpImagePoint> &edges_list, vpColor color = vpColor::red,
536  unsigned int thickness = 1);
537  static void display(const vpImage<vpRGBa> &I, const vpImagePoint &cog, const std::list<vpImagePoint> &edges_list,
538  vpColor color = vpColor::red, unsigned int thickness = 1);
539 };
540 
541 #endif
vpTracker & operator=(const vpTracker &tracker)
Copy operator.
Definition: vpTracker.cpp:53
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:153
double m02
Definition: vpDot2.h:415
void setGrayLevelMax(const unsigned int &max)
Definition: vpDot2.h:351
double mu02
Definition: vpDot2.h:434
Class to define RGB colors available for display functionnalities.
Definition: vpColor.h:157
void setEllipsoidBadPointsPercentage(const double &percentage=0.0)
Definition: vpDot2.h:290
void setGraphics(bool activate)
Definition: vpDot2.h:314
void setCog(const vpImagePoint &ip)
Definition: vpDot2.h:260
double m11
Definition: vpDot2.h:399
void print(std::ostream &os)
Definition: vpDot2.h:250
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:391
static const vpColor red
Definition: vpColor.h:217
Class that defines what is a feature generic tracker.
Definition: vpTracker.h:64
vpImagePoint getCog() const
Definition: vpDot2.h:180
void setGraphicsThickness(unsigned int t)
Definition: vpDot2.h:321
Defines a generic 2D polygon.
Definition: vpPolygon.h:103
double mu11
Definition: vpDot2.h:424
double m20
Definition: vpDot2.h:406
vpColVector get_nij() const
Definition: vpDot2.h:148
unsigned int getGrayLevelMin() const
Definition: vpDot2.h:220
std::list< vpImagePoint > getEdges() const
Definition: vpDot2.h:202
vpPolygon getPolygon() const
Definition: vpDot2.h:238
void setGrayLevelMin(const unsigned int &min)
Definition: vpDot2.h:334
Implementation of column vector and the associated operations.
Definition: vpColVector.h:130
vpRect getBBox() const
Definition: vpDot2.h:166
void init()
Default initialization.
Definition: vpTracker.cpp:47
double m10
Definition: vpDot2.h:383
double mu20
Definition: vpDot2.h:429
void setComputeMoments(bool activate)
Definition: vpDot2.h:276
Defines a rectangle in the plane.
Definition: vpRect.h:79
double getMeanGrayLevel() const
Definition: vpDot2.h:234
double getEllipsoidBadPointsPercentage() const
Definition: vpDot2.h:209
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:87
double m00
Definition: vpDot2.h:375
double getGamma() const
Definition: vpDot2.h:214
unsigned int getGrayLevelMax() const
Definition: vpDot2.h:226
void setRect(double l, double t, double w, double h)
Definition: vpRect.h:334
void getEdges(std::list< vpImagePoint > &edges_list) const
Definition: vpDot2.h:192
static const vpColor blue
Definition: vpColor.h:223