Visual Servoing Platform  version 3.1.0
vpMeTracker.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 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  * Moving edges.
33  *
34  * Authors:
35  * Andrew Comport
36  *
37  *****************************************************************************/
38 
44 #include <visp3/core/vpColor.h>
45 #include <visp3/core/vpDisplay.h>
46 #include <visp3/me/vpMeTracker.h>
47 
48 #include <algorithm>
49 #include <visp3/core/vpDebug.h>
50 #include <visp3/core/vpTrackingException.h>
51 
52 #define DEBUG_LEVEL1 0
53 #define DEBUG_LEVEL2 0
54 
56 {
58  p.resize(2);
60 }
61 
63  : list(), me(NULL), init_range(1), nGoodElement(0), selectDisplay(vpMeSite::NONE)
64 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
65  ,
66  query_range(0), display_point(false)
67 #endif
68 {
69  init();
70 }
71 
73  : vpTracker(meTracker), list(), me(NULL), init_range(1), nGoodElement(0), selectDisplay(vpMeSite::NONE)
74 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
75  ,
76  query_range(0), display_point(false)
77 #endif
78 {
79  init();
80 
81  me = meTracker.me;
82  list = meTracker.list;
83  nGoodElement = meTracker.nGoodElement;
84  init_range = meTracker.init_range;
85  selectDisplay = meTracker.selectDisplay;
86 
87 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
88  display_point = meTracker.display_point;
89  query_range = meTracker.query_range;
90 #endif
91 }
92 
97 {
98  nGoodElement = 0;
99  list.clear();
100 }
101 
103 
105 {
106  list = p_me.list;
107  me = p_me.me;
109  init_range = p_me.init_range;
110  nGoodElement = p_me.nGoodElement;
111 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
112  query_range = p_me.query_range;
114 #endif
115  return *this;
116 }
117 
118 static bool isSuppressZero(const vpMeSite &P) { return (P.getState() == vpMeSite::NO_SUPPRESSION); }
119 
121 {
122  unsigned int number_signal = 0;
123 
124  // Loop through all the points tracked from the contour
125  number_signal = static_cast<unsigned int>(std::count_if(list.begin(), list.end(), isSuppressZero));
126  return number_signal;
127 }
128 
129 unsigned int vpMeTracker::totalNumberOfSignal() { return (unsigned int)list.size(); }
130 
131 int vpMeTracker::outOfImage(int i, int j, int half, int rows, int cols)
132 {
133  return (!((i > half + 2) && (i < rows - (half + 2)) && (j > half + 2) && (j < cols - (half + 2))));
134 }
135 
136 int vpMeTracker::outOfImage(const vpImagePoint &iP, int half, int rows, int cols)
137 {
138  int i = vpMath::round(iP.get_i());
139  int j = vpMath::round(iP.get_j());
140  return (!((i > half + 2) && (i < rows - (half + 2)) && (j > half + 2) && (j < cols - (half + 2))));
141 }
142 
151 {
152  if (!me) {
153  vpDERROR_TRACE(2, "Tracking error: Moving edges not initialized");
154  throw(vpTrackingException(vpTrackingException::initializationError, "Moving edges not initialized"));
155  }
156 
157  // Must set range to 0
158  unsigned int range_tmp = me->getRange();
160 
161  nGoodElement = 0;
162 
163  int d = 0;
164  vpImagePoint ip1, ip2;
165 
166  // Loop through list of sites to track
167  for (std::list<vpMeSite>::iterator it = list.begin(); it != list.end(); ++it) {
168  vpMeSite refp = *it; // current reference pixel
169 
170  d++;
171  // If element hasn't been suppressed
172  if (refp.getState() == vpMeSite::NO_SUPPRESSION) {
173  try {
174  refp.track(I, me, false);
175  } catch (...) {
176  // EM verifier quel signal est de sortie !!!
177  vpERROR_TRACE("Error caught");
178  throw;
179  }
180  if (refp.getState() == vpMeSite::NO_SUPPRESSION)
181  nGoodElement++;
182  }
183 
184 #if (DEBUG_LEVEL2)
185  {
186  double a, b;
187  a = refp.i_1 - refp.i;
188  b = refp.j_1 - refp.j;
189  if (refp.getState() == vpMeSite::NO_SUPPRESSION) {
190  ip1.set_i(refp.i);
191  ip1.set_j(refp.j);
192  ip2.set_i(refp.i + a);
193  ip2.set_j(refp.j + b);
195  }
196  }
197 #endif
198  *it = refp;
199  }
200 
201  /*
202  if (res != OK)
203  {
204  std::cout<< "In vpMeTracker::initTracking(): " ;
205  switch (res)
206  {
207  case ERR_TRACKING:
208  std::cout << "vpMeTracker::initTracking:Track return ERR_TRACKING " <<
209  std::endl ; break ; case fatalError: std::cout <<
210  "vpMeTracker::initTracking:Track return fatalError" << std::endl ; break ;
211  default:
212  std::cout << "vpMeTracker::initTracking:Track return error " << res <<
213  std::endl ;
214  }
215  return res ;
216  }
217  */
218 
219  me->setRange(range_tmp);
220 }
221 
232 {
233  if (!me) {
234  vpDERROR_TRACE(2, "Tracking error: Moving edges not initialized");
235  throw(vpTrackingException(vpTrackingException::initializationError, "Moving edges not initialized"));
236  }
237 
238  if (list.empty()) {
239  vpDERROR_TRACE(2, "Tracking error: too few pixel to track");
240  throw(vpTrackingException(vpTrackingException::notEnoughPointError, "too few pixel to track"));
241  }
242 
243  vpImagePoint ip1, ip2;
244  nGoodElement = 0;
245  // int d =0;
246  // Loop through list of sites to track
247  for (std::list<vpMeSite>::iterator it = list.begin(); it != list.end(); ++it) {
248  vpMeSite s = *it; // current reference pixel
249 
250  // d++ ;
251  // If element hasn't been suppressed
252  if (s.getState() == vpMeSite::NO_SUPPRESSION) {
253 
254  try {
255  // vpERROR_TRACE("%d",d ) ;
256  // vpERROR_TRACE("range %d",me->range) ;
257  s.track(I, me, true);
258  } catch (vpTrackingException) {
259  vpERROR_TRACE("catch exception ");
261  }
262 
263  if (s.getState() != vpMeSite::THRESHOLD) {
264  nGoodElement++;
265 
266 #if (DEBUG_LEVEL2)
267  {
268  double a, b;
269  a = s.i_1 - s.i;
270  b = s.j_1 - s.j;
271  if (s.getState() == vpMeSite::NO_SUPPRESSION) {
272  ip1.set_i(s.i);
273  ip1.set_j(s.j);
274  ip2.set_i(s.i + a * 5);
275  ip2.set_j(s.j + b * 5);
277  }
278  }
279 #endif
280  }
281  *it = s;
282  }
283  }
284 }
285 
302 {
303 #if (DEBUG_LEVEL1)
304  {
305  std::cout << "begin vpMeTracker::displayList() " << std::endl;
306  std::cout << " There are " << list.size() << " sites in the list " << std::endl;
307  }
308 #endif
309  for (std::list<vpMeSite>::const_iterator it = list.begin(); it != list.end(); ++it) {
310  vpMeSite p_me = *it;
311  p_me.display(I);
312  }
313 }
314 
321 void vpMeTracker::display(const vpImage<unsigned char> &I, vpColVector &w, unsigned int &index_w)
322 {
323  for (std::list<vpMeSite>::iterator it = list.begin(); it != list.end(); ++it) {
324  vpMeSite P = *it;
325 
326  if (P.getState() == vpMeSite::NO_SUPPRESSION) {
327  P.weight = w[index_w];
328  index_w++;
329  }
330 
331  *it = P;
332  }
333  display(I);
334 }
335 
336 #undef DEBUG_LEVEL1
337 #undef DEBUG_LEVEL2
double get_i() const
Definition: vpImagePoint.h:204
vpMeSiteState getState() const
Definition: vpMeSite.h:188
void display(const vpImage< unsigned char > &I)
Definition: vpMeSite.cpp:609
unsigned int numberOfSignal()
unsigned int totalNumberOfSignal()
Performs search in a given direction(normal) for a given distance(pixels) for a given &#39;site&#39;...
Definition: vpMeSite.h:71
#define vpERROR_TRACE
Definition: vpDebug.h:393
int i
Definition: vpMeSite.h:86
void reset()
Definition: vpMeTracker.cpp:96
static const vpColor green
Definition: vpColor.h:183
static int round(const double x)
Definition: vpMath.h:235
Class that defines what is a feature generic tracker.
Definition: vpTracker.h:64
void set_i(const double ii)
Definition: vpImagePoint.h:167
std::list< vpMeSite > list
Definition: vpMeTracker.h:74
Error that can be emited by the vpTracker class and its derivates.
int outOfImage(int i, int j, int half, int row, int cols)
static void displayArrow(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color=vpColor::white, unsigned int w=4, unsigned int h=2, unsigned int thickness=1)
double get_j() const
Definition: vpImagePoint.h:215
vpMeTracker & operator=(vpMeTracker &f)
void track(const vpImage< unsigned char > &I)
Track sampled pixels.
bool display_point
Definition: vpMeTracker.h:167
Contains abstract elements for a Distance to Feature type feature.
Definition: vpMeTracker.h:65
int nGoodElement
Definition: vpMeTracker.h:78
void setState(const vpMeSiteState &flag)
Definition: vpMeSite.h:174
int j_1
Definition: vpMeSite.h:87
void set_j(const double jj)
Definition: vpImagePoint.h:178
#define vpDERROR_TRACE
Definition: vpDebug.h:464
int j
Definition: vpMeSite.h:86
double weight
Definition: vpMeSite.h:98
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
virtual ~vpMeTracker()
void track(const vpImage< unsigned char > &im, const vpMe *me, const bool test_contraste=true)
Definition: vpMeSite.cpp:362
vpMe * me
Moving edges initialisation parameters.
Definition: vpMeTracker.h:76
void initTracking(const vpImage< unsigned char > &I)
void init()
Default initialization.
Definition: vpTracker.cpp:47
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
void setRange(const unsigned int &r)
Definition: vpMe.h:271
int i_1
Definition: vpMeSite.h:87
vpMeSite::vpMeSiteDisplayType selectDisplay
Definition: vpMeTracker.h:81
unsigned int getRange() const
Definition: vpMe.h:179
virtual void display(const vpImage< unsigned char > &I, vpColor col)=0
unsigned int init_range
Definition: vpMeTracker.h:77
vpColVector p
Definition: vpTracker.h:71
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:241