Visual Servoing Platform  version 3.6.1 under development (2024-04-26)
vpDot2.h
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 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 https://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 *****************************************************************************/
35 
36 /*
37  \file vpDot2.h
38  \brief This tracker is meant to track some zones on a vpImage.
39 */
40 
41 #ifndef vpDot2_hh
42 #define vpDot2_hh
43 
44 #include <visp3/core/vpColor.h>
45 #include <visp3/core/vpImage.h>
46 #include <visp3/core/vpImagePoint.h>
47 #include <visp3/core/vpPolygon.h>
48 #include <visp3/core/vpRect.h>
49 #include <visp3/core/vpTracker.h>
50 
51 #include <list>
52 #include <vector>
53 
123 class VISP_EXPORT vpDot2 : public vpTracker
124 {
125 public:
126  vpDot2();
127  explicit vpDot2(const vpImagePoint &ip);
128  vpDot2(const vpDot2 &twinDot);
129 
130  static vpMatrix defineDots(vpDot2 dot[], const unsigned int &n, const std::string &dotFile, vpImage<unsigned char> &I,
131  vpColor col = vpColor::blue, bool trackDot = true);
132 
133  void display(const vpImage<unsigned char> &I, vpColor color = vpColor::red, unsigned int thickness = 1) const;
134 
144  inline vpColVector get_nij() const
145  {
146  vpColVector nij(3);
147  nij[0] = mu20 / m00;
148  nij[1] = mu11 / m00;
149  nij[2] = mu02 / m00;
150 
151  return nij;
152  }
153 
154  double getArea() const;
155 
161  inline vpRect getBBox() const
162  {
163  vpRect bbox;
164 
165  bbox.setRect(this->bbox_u_min, this->bbox_v_min, this->bbox_u_max - this->bbox_u_min + 1,
166  this->bbox_v_max - this->bbox_v_min + 1);
167 
168  return (bbox);
169  };
170 
176  inline vpImagePoint getCog() const { return cog; }
177 
178  double getDistance(const vpDot2 &distantDot) const;
186  void getEdges(std::list<vpImagePoint> &edges_list) const { edges_list = this->ip_edges_list; };
187 
195  std::list<vpImagePoint> getEdges() const { return (this->ip_edges_list); };
196 
203  double getEllipsoidBadPointsPercentage() const { return allowedBadPointsPercentage_; }
204 
205  double getEllipsoidShapePrecision() const;
206  void getFreemanChain(std::list<unsigned int> &freeman_chain) const;
207 
208  inline double getGamma() const { return this->gamma; };
214  inline unsigned int getGrayLevelMin() const { return gray_level_min; };
220  inline unsigned int getGrayLevelMax() const { return gray_level_max; };
221  double getGrayLevelPrecision() const;
222 
223  double getHeight() const;
224  double getMaxSizeSearchDistancePrecision() const;
225 
229  double getMeanGrayLevel() const { return (this->mean_gray_level); };
230 
234  vpPolygon getPolygon() const { return (vpPolygon(ip_edges_list)); };
235  double getSizePrecision() const;
236  double getWidth() const;
237 
238  void initTracking(const vpImage<unsigned char> &I, unsigned int size = 0);
239  void initTracking(const vpImage<unsigned char> &I, const vpImagePoint &ip, unsigned int size = 0);
240  void initTracking(const vpImage<unsigned char> &I, const vpImagePoint &ip, unsigned int gray_lvl_min,
241  unsigned int gray_lvl_max, unsigned int size = 0);
242 
243  vpDot2 &operator=(const vpDot2 &twinDot);
244  friend VISP_EXPORT std::ostream &operator<<(std::ostream &os, vpDot2 &d);
245 
246  void print(std::ostream &os) { os << *this << std::endl; }
247  void searchDotsInArea(const vpImage<unsigned char> &I, int area_u, int area_v, unsigned int area_w,
248  unsigned int area_h, std::list<vpDot2> &niceDots);
249 
250  void searchDotsInArea(const vpImage<unsigned char> &I, std::list<vpDot2> &niceDots);
251 
252  void setArea(const double &area);
256  inline void setCog(const vpImagePoint &ip) { this->cog = ip; }
257 
271  void setComputeMoments(bool activate) { compute_moment = activate; }
272 
285  void setEllipsoidBadPointsPercentage(const double &percentage = 0.0)
286  {
287  if (percentage < 0.)
288  allowedBadPointsPercentage_ = 0.;
289  else if (percentage > 1.)
290  allowedBadPointsPercentage_ = 1.;
291  else
292  allowedBadPointsPercentage_ = percentage;
293  }
294 
295  void setEllipsoidShapePrecision(const double &ellipsoidShapePrecision);
296 
310  void setGraphics(bool activate) { graphics = activate; }
311 
318  void setGraphicsThickness(unsigned int t) { this->thickness = t; };
319 
330  inline void setGrayLevelMin(const unsigned int &min)
331  {
332  if (min > 255)
333  this->gray_level_min = 255;
334  else
335  this->gray_level_min = min;
336  };
337 
346  inline void setGrayLevelMax(const unsigned int &max)
347  {
348  if (max > 255)
349  this->gray_level_max = 255;
350  else
351  this->gray_level_max = max;
352  };
353 
354  void setGrayLevelPrecision(const double &grayLevelPrecision);
355  void setHeight(const double &height);
356  void setMaxSizeSearchDistancePrecision(const double &maxSizeSearchDistancePrecision);
357  void setSizePrecision(const double &sizePrecision);
358  void setWidth(const double &width);
359 
360  void track(const vpImage<unsigned char> &I, bool canMakeTheWindowGrow = true);
361  void track(const vpImage<unsigned char> &I, vpImagePoint &cog, bool canMakeTheWindowGrow = true);
362 
363  static void trackAndDisplay(vpDot2 dot[], const unsigned int &n, vpImage<unsigned char> &I,
364  std::vector<vpImagePoint> &cogs, vpImagePoint *cogStar = nullptr);
365 
366 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
367 public:
368 #else
369 private:
370 #endif
371  double m00;
379  double m10;
387  double m01;
395  double m11;
402  double m20;
411  double m02;
420  double mu11;
425  double mu20;
430  double mu02;
436 private:
437  virtual bool isValid(const vpImage<unsigned char> &I, const vpDot2 &wantedDot);
438 
439  virtual bool hasGoodLevel(const vpImage<unsigned char> &I, const unsigned int &u, const unsigned int &v) const;
440  virtual bool hasReverseLevel(const vpImage<unsigned char> &I, const unsigned int &u, const unsigned int &v) const;
441 
442  virtual vpDot2 *getInstance();
443 
444  void init();
445 
446  bool computeParameters(const vpImage<unsigned char> &I, const double &u = -1.0, const double &v = -1.0);
447 
448  bool findFirstBorder(const vpImage<unsigned char> &I, const unsigned int &u, const unsigned int &v,
449  unsigned int &border_u, unsigned int &border_v);
450  void computeMeanGrayLevel(const vpImage<unsigned char> &I);
451 
458  unsigned int getFirstBorder_u() const { return this->firstBorder_u; }
459 
466  unsigned int getFirstBorder_v() const { return this->firstBorder_v; }
467 
468  bool computeFreemanChainElement(const vpImage<unsigned char> &I, const unsigned int &u, const unsigned int &v,
469  unsigned int &element);
470  void computeFreemanParameters(const int &u_p, const int &v_p, unsigned int &element, int &du, int &dv, float &dS,
471  float &dMu, float &dMv, float &dMuv, float &dMu2, float &dMv2);
472  void updateFreemanPosition(unsigned int &u, unsigned int &v, const unsigned int &dir);
473 
474  bool isInImage(const vpImage<unsigned char> &I) const;
475  bool isInImage(const vpImage<unsigned char> &I, const vpImagePoint &ip) const;
476 
477  bool isInArea(const unsigned int &u, const unsigned int &v) const;
478 
479  void getGridSize(unsigned int &gridWidth, unsigned int &gridHeight);
480  void setArea(const vpImage<unsigned char> &I, int u, int v, unsigned int w, unsigned int h);
481  void setArea(const vpImage<unsigned char> &I);
482  void setArea(const vpRect &a);
483 
484  unsigned char getMeanGrayLevel(vpImage<unsigned char> &I) const;
486  vpImagePoint cog;
487 
488  double width;
489  double height;
490  double surface;
491  unsigned int gray_level_min; // minumum gray level for the dot. Pixel with lower level don't belong to this dot.
492 
493  unsigned int gray_level_max; // maximum gray level for the dot. Pixel with higher level don't belong to this dot.
494  double mean_gray_level; // Mean gray level of the dot
495  double grayLevelPrecision;
496  double gamma;
497  double sizePrecision;
498  double ellipsoidShapePrecision;
499  double maxSizeSearchDistancePrecision;
500  double allowedBadPointsPercentage_;
501  // Area where the dot is to search
502  vpRect area;
503 
504  // other
505  std::list<unsigned int> direction_list;
506  std::list<vpImagePoint> ip_edges_list;
507 
508  // flag
509  bool compute_moment; // true moment are computed
510  bool graphics; // true for graphic overlay display
511 
512  unsigned int thickness; // Graphics thickness
513 
514  // Bounding box
515  int bbox_u_min, bbox_u_max, bbox_v_min, bbox_v_max;
516 
517  // The first point coordinate on the dot border
518  unsigned int firstBorder_u;
519  unsigned int firstBorder_v;
520 
521  // Static funtions
522 public:
523  static void display(const vpImage<unsigned char> &I, const vpImagePoint &cog,
524  const std::list<vpImagePoint> &edges_list, vpColor color = vpColor::red,
525  unsigned int thickness = 1);
526  static void display(const vpImage<vpRGBa> &I, const vpImagePoint &cog, const std::list<vpImagePoint> &edges_list,
527  vpColor color = vpColor::red, unsigned int thickness = 1);
528 };
529 
530 #endif
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
Class to define RGB colors available for display functionalities.
Definition: vpColor.h:152
static const vpColor red
Definition: vpColor.h:211
static const vpColor blue
Definition: vpColor.h:217
This tracker is meant to track a blob (connex pixels with same gray level) on a vpImage.
Definition: vpDot2.h:124
unsigned int getGrayLevelMin() const
Definition: vpDot2.h:214
unsigned int getGrayLevelMax() const
Definition: vpDot2.h:220
double m02
Definition: vpDot2.h:411
double m01
Definition: vpDot2.h:387
void setGraphics(bool activate)
Definition: vpDot2.h:310
void getEdges(std::list< vpImagePoint > &edges_list) const
Definition: vpDot2.h:186
double mu11
Definition: vpDot2.h:420
void setGraphicsThickness(unsigned int t)
Definition: vpDot2.h:318
double getGamma() const
Definition: vpDot2.h:208
double m20
Definition: vpDot2.h:402
double m00
Definition: vpDot2.h:371
void print(std::ostream &os)
Definition: vpDot2.h:246
void setGrayLevelMax(const unsigned int &max)
Definition: vpDot2.h:346
double getEllipsoidBadPointsPercentage() const
Definition: vpDot2.h:203
double m11
Definition: vpDot2.h:395
void setGrayLevelMin(const unsigned int &min)
Definition: vpDot2.h:330
vpPolygon getPolygon() const
Definition: vpDot2.h:234
vpRect getBBox() const
Definition: vpDot2.h:161
void setCog(const vpImagePoint &ip)
Definition: vpDot2.h:256
vpImagePoint getCog() const
Definition: vpDot2.h:176
double m10
Definition: vpDot2.h:379
void setEllipsoidBadPointsPercentage(const double &percentage=0.0)
Definition: vpDot2.h:285
double mu02
Definition: vpDot2.h:430
double mu20
Definition: vpDot2.h:425
double getMeanGrayLevel() const
Definition: vpDot2.h:229
void setComputeMoments(bool activate)
Definition: vpDot2.h:271
std::list< vpImagePoint > getEdges() const
Definition: vpDot2.h:195
vpColVector get_nij() const
Definition: vpDot2.h:144
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:146
Defines a generic 2D polygon.
Definition: vpPolygon.h:97
Defines a rectangle in the plane.
Definition: vpRect.h:76
void setRect(double l, double t, double w, double h)
Definition: vpRect.h:330
Class that defines what is a feature generic tracker.
Definition: vpTracker.h:59
vpTracker & operator=(const vpTracker &tracker)
Copy operator.
Definition: vpTracker.cpp:50
void init()
Default initialization.
Definition: vpTracker.cpp:44
void display(vpImage< unsigned char > &I, const std::string &title)
Display a gray-scale image.