Visual Servoing Platform  version 3.6.1 under development (2024-12-06)
vpDot2.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
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 https://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  * Track a white dot.
32  */
33 
39 #ifndef VP_DOT2_H
40 #define VP_DOT2_H
41 
42 #include <visp3/core/vpColor.h>
43 #include <visp3/core/vpConfig.h>
44 #include <visp3/core/vpImage.h>
45 #include <visp3/core/vpImagePoint.h>
46 #include <visp3/core/vpPolygon.h>
47 #include <visp3/core/vpRect.h>
48 #include <visp3/core/vpTracker.h>
49 
50 #include <list>
51 #include <vector>
52 
53 BEGIN_VISP_NAMESPACE
54 
124 class VISP_EXPORT vpDot2 : public vpTracker
125 {
126 public:
127  vpDot2();
128  VP_EXPLICIT vpDot2(const vpImagePoint &ip);
129  vpDot2(const vpDot2 &twinDot);
130 
131  static vpMatrix defineDots(vpDot2 dot[], const unsigned int &n, const std::string &dotFile, vpImage<unsigned char> &I,
132  vpColor col = vpColor::blue, bool trackDot = true);
133 
134  void display(const vpImage<unsigned char> &I, vpColor color = vpColor::red, unsigned int thickness = 1) const;
135 
145  inline vpColVector get_nij() const
146  {
147  vpColVector nij(3);
148  const unsigned int index_0 = 0;
149  const unsigned int index_1 = 1;
150  const unsigned int index_2 = 2;
151 
152  nij[index_0] = mu20 / m00;
153  nij[index_1] = mu11 / m00;
154  nij[index_2] = mu02 / m00;
155 
156  return nij;
157  }
158 
159  double getArea() const;
160 
166  inline vpRect getBBox() const
167  {
168  vpRect bbox;
169 
170  bbox.setRect(m_bbox_u_min, m_bbox_v_min, (m_bbox_u_max - m_bbox_u_min) + 1,
171  (m_bbox_v_max - m_bbox_v_min) + 1);
172 
173  return bbox;
174  };
175 
181  inline vpImagePoint getCog() const { return m_cog; }
182 
183  double getDistance(const vpDot2 &distantDot) const;
191  void getEdges(std::list<vpImagePoint> &edges_list) const { edges_list = m_ip_edges_list; };
192 
200  std::list<vpImagePoint> getEdges() const { return m_ip_edges_list; };
201 
208  double getEllipsoidBadPointsPercentage() const { return m_allowedBadPointsPercentage; }
209 
210  double getEllipsoidShapePrecision() const;
211  void getFreemanChain(std::list<unsigned int> &freeman_chain) const;
212 
213  inline double getGamma() const { return m_gamma; };
219  inline unsigned int getGrayLevelMin() const { return m_gray_level_min; };
225  inline unsigned int getGrayLevelMax() const { return m_gray_level_max; };
226  double getGrayLevelPrecision() const;
227 
228  double getHeight() const;
229  double getMaxSizeSearchDistPrecision() const;
230 
234  double getMeanGrayLevel() const { return m_mean_gray_level; };
235 
239  vpPolygon getPolygon() const { return (vpPolygon(m_ip_edges_list)); };
240  double getSizePrecision() const;
241  double getWidth() const;
242 
243  void initTracking(const vpImage<unsigned char> &I, unsigned int size = 0);
244  void initTracking(const vpImage<unsigned char> &I, const vpImagePoint &ip, unsigned int size = 0);
245  void initTracking(const vpImage<unsigned char> &I, const vpImagePoint &ip, unsigned int gray_lvl_min,
246  unsigned int gray_lvl_max, unsigned int size = 0);
247 
248  vpDot2 &operator=(const vpDot2 &twinDot);
249  friend VISP_EXPORT std::ostream &operator<<(std::ostream &os, vpDot2 &d);
250 
251  void print(std::ostream &os) { os << *this << std::endl; }
252  void searchDotsInArea(const vpImage<unsigned char> &I, int area_u, int area_v, unsigned int area_w,
253  unsigned int area_h, std::list<vpDot2> &niceDots);
254 
255  void searchDotsInArea(const vpImage<unsigned char> &I, std::list<vpDot2> &niceDots);
256 
257  void setArea(const double &area);
261  inline void setCog(const vpImagePoint &ip) { m_cog = ip; }
262 
276  void setComputeMoments(bool activate) { m_compute_moment = activate; }
277 
290  void setEllipsoidBadPointsPercentage(const double &percentage = 0.0)
291  {
292  if (percentage < 0.) {
293  m_allowedBadPointsPercentage = 0.;
294  }
295  else if (percentage > 1.) {
296  m_allowedBadPointsPercentage = 1.;
297  }
298  else {
299  m_allowedBadPointsPercentage = percentage;
300  }
301  }
302 
303  void setEllipsoidShapePrecision(const double &ellipsoidShapePrecision);
304 
318  void setGraphics(bool activate) { m_graphics = activate; }
319 
326  void setGraphicsThickness(unsigned int thickness) { m_thickness = thickness; };
327 
338  inline void setGrayLevelMin(const unsigned int &min)
339  {
340  const unsigned int val_max = 255;
341  if (min > val_max) {
342  m_gray_level_min = val_max;
343  }
344  else {
345  m_gray_level_min = min;
346  }
347  };
348 
357  inline void setGrayLevelMax(const unsigned int &max)
358  {
359  const unsigned int val_max = 255;
360  if (max > val_max) {
361  m_gray_level_max = val_max;
362  }
363  else {
364  m_gray_level_max = max;
365  }
366  };
367 
368  void setGrayLevelPrecision(const double &grayLevelPrecision);
369  void setHeight(const double &height);
370  void setMaxSizeSearchDistPrecision(const double &maxSizeSearchDistancePrecision);
371  void setSizePrecision(const double &sizePrecision);
372  void setWidth(const double &width);
373 
374  void track(const vpImage<unsigned char> &I, bool canMakeTheWindowGrow = true);
375  void track(const vpImage<unsigned char> &I, vpImagePoint &cog, bool canMakeTheWindowGrow = true);
376 
377  static void trackAndDisplay(vpDot2 dot[], const unsigned int &n, vpImage<unsigned char> &I,
378  std::vector<vpImagePoint> &cogs, vpImagePoint *cogStar = nullptr);
379 
380  // Static funtions
381  static void display(const vpImage<unsigned char> &I, const vpImagePoint &cog,
382  const std::list<vpImagePoint> &edges_list, vpColor color = vpColor::red,
383  unsigned int thickness = 1);
384  static void display(const vpImage<vpRGBa> &I, const vpImagePoint &cog, const std::list<vpImagePoint> &edges_list,
385  vpColor color = vpColor::red, unsigned int thickness = 1);
386 
387 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
388 public:
389 #else
390 private:
391 #endif
392  double m00;
400  double m10;
408  double m01;
416  double m11;
423  double m20;
432  double m02;
441  double mu11;
446  double mu20;
451  double mu02;
457 private:
458  virtual bool isValid(const vpImage<unsigned char> &I, const vpDot2 &wantedDot);
459 
460  virtual bool hasGoodLevel(const vpImage<unsigned char> &I, const unsigned int &u, const unsigned int &v) const;
461  virtual bool hasReverseLevel(const vpImage<unsigned char> &I, const unsigned int &u, const unsigned int &v) const;
462 
463  virtual vpDot2 *getInstance();
464 
465  void init();
466 
467  bool computeParameters(const vpImage<unsigned char> &I, const double &u = -1.0, const double &v = -1.0);
468 
469  bool findFirstBorder(const vpImage<unsigned char> &I, const unsigned int &u, const unsigned int &v,
470  unsigned int &border_u, unsigned int &border_v);
471  void computeMeanGrayLevel(const vpImage<unsigned char> &I);
472 
479  unsigned int getFirstBorder_u() const { return m_firstBorder_u; }
480 
487  unsigned int getFirstBorder_v() const { return m_firstBorder_v; }
488 
489  bool computeFreemanChainElement(const vpImage<unsigned char> &I, const unsigned int &u, const unsigned int &v,
490  unsigned int &element);
491  void computeFreemanParameters(const int &u_p, const int &v_p, unsigned int &element, int &du, int &dv, float &dS,
492  float &dMu, float &dMv, float &dMuv, float &dMu2, float &dMv2);
493  void updateFreemanPosition(unsigned int &u, unsigned int &v, const unsigned int &dir);
494 
495  bool isInImage(const vpImage<unsigned char> &I) const;
496  bool isInImage(const vpImage<unsigned char> &I, const vpImagePoint &ip) const;
497 
498  bool isInArea(const unsigned int &u, const unsigned int &v) const;
499 
500  void getGridSize(unsigned int &gridWidth, unsigned int &gridHeight);
501  void setArea(const vpImage<unsigned char> &I, int u, int v, unsigned int w, unsigned int h);
502  void setArea(const vpImage<unsigned char> &I);
503  void setArea(const vpRect &a);
504 
505  unsigned char getMeanGrayLevel(vpImage<unsigned char> &I) const;
506 
507  typedef struct vpSearchDotsInAreaGoodGermData
508  {
509  const vpImage<unsigned char> &m_I;
510  const vpRect &m_area;
511  unsigned int &m_u;
512  unsigned int &m_v;
513  std::list<vpDot2> &m_niceDots;
514  std::list<vpDot2> &m_badDotsVector;
515 
516  vpSearchDotsInAreaGoodGermData(const vpImage<unsigned char> &I, const vpRect &area,
517  unsigned int &u, unsigned int &v,
518  std::list<vpDot2> &niceDots, std::list<vpDot2> &badDotsVector)
519  : m_I(I)
520  , m_area(area)
521  , m_u(u)
522  , m_v(v)
523  , m_niceDots(niceDots)
524  , m_badDotsVector(badDotsVector)
525  {
526 
527  }
528  } vpSearchDotsInAreaGoodGermData;
529 
530  void searchDotsAreaGoodGerm(vpSearchDotsInAreaGoodGermData &data);
532  vpImagePoint m_cog;
533 
534  double m_width;
535  double m_height;
536  double m_surface;
537  unsigned int m_gray_level_min; // minumum gray level for the dot. Pixel with lower level don't belong to this dot.
538 
539  unsigned int m_gray_level_max; // maximum gray level for the dot. Pixel with higher level don't belong to this dot.
540  double m_mean_gray_level; // Mean gray level of the dot
541  double m_grayLevelPrecision;
542  double m_gamma;
543  double m_sizePrecision;
544  double m_ellipsoidShapePrecision;
545  double m_maxSizeSearchDistPrecision;
546  double m_allowedBadPointsPercentage;
547  // Area where the dot is to search
548  vpRect m_area;
549 
550  // other
551  std::list<unsigned int> m_direction_list;
552  std::list<vpImagePoint> m_ip_edges_list;
553 
554  // flag
555  bool m_compute_moment; // true moment are computed
556  bool m_graphics; // true for graphic overlay display
557 
558  unsigned int m_thickness; // Graphics thickness
559 
560  // Bounding box
561  int m_bbox_u_min, m_bbox_u_max, m_bbox_v_min, m_bbox_v_max;
562 
563  // The first point coordinate on the dot border
564  unsigned int m_firstBorder_u;
565  unsigned int m_firstBorder_v;
566 
567 };
568 
569 END_VISP_NAMESPACE
570 #endif
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
Class to define RGB colors available for display functionalities.
Definition: vpColor.h:157
static const vpColor red
Definition: vpColor.h:217
static const vpColor blue
Definition: vpColor.h:223
This tracker is meant to track a blob (connex pixels with same gray level) on a vpImage.
Definition: vpDot2.h:125
unsigned int getGrayLevelMin() const
Definition: vpDot2.h:219
unsigned int getGrayLevelMax() const
Definition: vpDot2.h:225
void setGraphics(bool activate)
Definition: vpDot2.h:318
void getEdges(std::list< vpImagePoint > &edges_list) const
Definition: vpDot2.h:191
void setGraphicsThickness(unsigned int thickness)
Definition: vpDot2.h:326
double getGamma() const
Definition: vpDot2.h:213
void print(std::ostream &os)
Definition: vpDot2.h:251
void setGrayLevelMax(const unsigned int &max)
Definition: vpDot2.h:357
double getEllipsoidBadPointsPercentage() const
Definition: vpDot2.h:208
void setGrayLevelMin(const unsigned int &min)
Definition: vpDot2.h:338
vpPolygon getPolygon() const
Definition: vpDot2.h:239
vpRect getBBox() const
Definition: vpDot2.h:166
void setCog(const vpImagePoint &ip)
Definition: vpDot2.h:261
vpImagePoint getCog() const
Definition: vpDot2.h:181
void setEllipsoidBadPointsPercentage(const double &percentage=0.0)
Definition: vpDot2.h:290
double getMeanGrayLevel() const
Definition: vpDot2.h:234
void setComputeMoments(bool activate)
Definition: vpDot2.h:276
std::list< vpImagePoint > getEdges() const
Definition: vpDot2.h:200
vpColVector get_nij() const
Definition: vpDot2.h:145
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:169
Defines a generic 2D polygon.
Definition: vpPolygon.h:103
Defines a rectangle in the plane.
Definition: vpRect.h:79
void setRect(double l, double t, double w, double h)
Definition: vpRect.h:333
Class that defines what is a feature generic tracker.
Definition: vpTracker.h:61
vpTracker & operator=(const vpTracker &tracker)
Copy operator.
Definition: vpTracker.cpp:50
void init()
Default initialization.
Definition: vpTracker.cpp:44