Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
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 http://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  * Moving edges.
32  *
33  * Authors:
34  * Andrew Comport
35  *
36  *****************************************************************************/
37 
45 #include <visp3/me/vpMeTracker.h>
46 #include <visp3/core/vpDisplay.h>
47 #include <visp3/core/vpColor.h>
48 
49 #include <visp3/core/vpTrackingException.h>
50 #include <visp3/core/vpDebug.h>
51 #include <algorithm>
52 
53 #define DEBUG_LEVEL1 0
54 #define DEBUG_LEVEL2 0
55 
56 void
58 {
59  vpTracker::init() ;
60  p.resize(2) ;
62 }
63 
65  : list(), me(NULL), init_range(1), nGoodElement(0), selectDisplay(vpMeSite::NONE)
66 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
67  , query_range (0), display_point(false)
68 #endif
69 {
70  init();
71 }
72 
74  : vpTracker(meTracker),
75  list(), me(NULL), init_range(1), nGoodElement(0), selectDisplay(vpMeSite::NONE)
76 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
77  , query_range (0), display_point(false)
78 #endif
79 {
80  init();
81 
82  me = meTracker.me;
83  list = meTracker.list;
84  nGoodElement = meTracker.nGoodElement;
85  init_range = meTracker.init_range;
86  selectDisplay = meTracker.selectDisplay;
87 
88  #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
89  display_point = meTracker.display_point;
90  query_range = meTracker.query_range;
91  #endif
92 }
93 
98 {
99  nGoodElement = 0;
100  list.clear();
101 }
102 
104 {
105  reset();
106 }
107 
110 {
111  list = p_me.list;
112  me = p_me.me;
114 
115  return *this;
116 }
117 
118 static bool isSuppressZero(const vpMeSite& P){
119  return (P.getState() == vpMeSite::NO_SUPPRESSION);
120 }
121 
122 unsigned int
124 {
125  unsigned int number_signal=0;
126 
127  // Loop through all the points tracked from the contour
128  number_signal = static_cast<unsigned int>(std::count_if(list.begin(), list.end(), isSuppressZero));
129  return number_signal;
130 }
131 
132 unsigned int
134 {
135  return (unsigned int)list.size();
136 }
137 
138 int
139 vpMeTracker::outOfImage(int i, int j, int half, int rows, int cols)
140 {
141  return (! ((i> half+2) &&
142  (i< rows -(half+2)) &&
143  (j>half+2) &&
144  (j<cols-(half+2)))
145  ) ;
146 }
147 
148 int
149 vpMeTracker::outOfImage(vpImagePoint iP, int half, int rows, int cols)
150 {
151  int i = vpMath::round(iP.get_i());
152  int j = vpMath::round(iP.get_j());
153  return (! ((i> half+2) &&
154  (i< rows -(half+2)) &&
155  (j>half+2) &&
156  (j<cols-(half+2)))
157  ) ;
158 }
159 
160 
167 void
169 {
170  if (!me) {
171  vpDERROR_TRACE(2, "Tracking error: Moving edges not initialized");
173  "Moving edges not initialized")) ;
174  }
175 
176  // Must set range to 0
177  unsigned int range_tmp = me->getRange();
179 
180  nGoodElement=0;
181 
182  int d = 0;
183  vpImagePoint ip1, ip2;
184 
185  // Loop through list of sites to track
186  for(std::list<vpMeSite>::iterator it=list.begin(); it!=list.end(); ++it){
187  vpMeSite refp = *it;//current reference pixel
188 
189  d++ ;
190  // If element hasn't been suppressed
191  if(refp.getState() == vpMeSite::NO_SUPPRESSION)
192  {
193  try {
194  refp.track(I,me,false);
195  }
196  catch(...)
197  {
198  // EM verifier quel signal est de sortie !!!
199  vpERROR_TRACE("Error caught") ;
200  throw ;
201  }
203  }
204 
205 
206 #if (DEBUG_LEVEL2)
207  {
208  double a,b ;
209  a = refp.i_1 - refp.i ;
210  b = refp.j_1 - refp.j ;
211  if(refp.getState() == vpMeSite::NO_SUPPRESSION) {
212  ip1.set_i( refp.i );
213  ip1.set_j( refp.j );
214  ip2.set_i( refp.i+a );
215  ip2.set_j( refp.j+b );
217  }
218  }
219 #endif
220  *it = refp;
221  }
222 
223  /*
224  if (res != OK)
225  {
226  std::cout<< "In vpMeTracker::initTracking(): " ;
227  switch (res)
228  {
229  case ERR_TRACKING:
230  std::cout << "vpMeTracker::initTracking:Track return ERR_TRACKING " << std::endl ;
231  break ;
232  case fatalError:
233  std::cout << "vpMeTracker::initTracking:Track return fatalError" << std::endl ;
234  break ;
235  default:
236  std::cout << "vpMeTracker::initTracking:Track return error " << res << std::endl ;
237  }
238  return res ;
239  }
240  */
241 
242  me->setRange(range_tmp);
243 }
244 
253 void
255 {
256  if (!me) {
257  vpDERROR_TRACE(2, "Tracking error: Moving edges not initialized");
259  "Moving edges not initialized")) ;
260  }
261 
262  if (list.empty())
263  {
264  vpDERROR_TRACE(2, "Tracking error: too few pixel to track");
266  "too few pixel to track")) ;
267 
268  }
269 
270  vpImagePoint ip1, ip2;
271  nGoodElement=0;
272  // int d =0;
273  // Loop through list of sites to track
274  for(std::list<vpMeSite>::iterator it=list.begin(); it!=list.end(); ++it){
275  vpMeSite s = *it;//current reference pixel
276 
277  // d++ ;
278  // If element hasn't been suppressed
280  {
281 
282  try{
283  // vpERROR_TRACE("%d",d ) ;
284  // vpERROR_TRACE("range %d",me->range) ;
285  s.track(I,me,true);
286  }
287  catch(vpTrackingException)
288  {
289  vpERROR_TRACE("catch exception ") ;
291  }
292 
293  if(s.getState() != vpMeSite::THRESHOLD)
294  {
295  nGoodElement++;
296 
297 #if (DEBUG_LEVEL2)
298  {
299  double a,b ;
300  a = s.i_1 - s.i ;
301  b = s.j_1 - s.j ;
303  ip1.set_i( s.i );
304  ip1.set_j( s.j );
305  ip2.set_i( s.i+a*5 );
306  ip2.set_j( s.j+b*5 );
308  }
309  }
310 #endif
311 
312  }
313  *it = s;
314  }
315  }
316 }
317 
330 void
332 {
333 #if (DEBUG_LEVEL1)
334  {
335  std::cout <<"begin vpMeTracker::displayList() " << std::endl ;
336  std::cout<<" There are "<<list.size()<< " sites in the list " << std::endl ;
337  }
338 #endif
339  for(std::list<vpMeSite>::const_iterator it=list.begin(); it!=list.end(); ++it){
340  vpMeSite p_me = *it;
341  p_me.display(I);
342  }
343 }
344 
351 void
352 vpMeTracker::display(const vpImage<unsigned char>& I,vpColVector &w, unsigned int &index_w)
353 {
354  for(std::list<vpMeSite>::iterator it=list.begin(); it!=list.end(); ++it){
355  vpMeSite P = *it;
356 
358  {
359  P.weight = w[index_w];
360  index_w++;
361  }
362 
363  *it = P;
364  }
365  display(I);
366 }
367 
368 #undef DEBUG_LEVEL1
369 #undef DEBUG_LEVEL2
370 
371 
unsigned int getRange() const
Definition: vpMe.h:167
double get_i() const
Definition: vpImagePoint.h:199
void display(const vpImage< unsigned char > &I)
Definition: vpMeSite.cpp:637
unsigned int numberOfSignal()
unsigned int totalNumberOfSignal()
Performs search in a given direction(normal) for a given distance(pixels) for a given 'site'...
Definition: vpMeSite.h:72
#define vpERROR_TRACE
Definition: vpDebug.h:391
int outOfImage(int i, int j, int half, int rows, int cols)
int i
Definition: vpMeSite.h:94
void reset()
Definition: vpMeTracker.cpp:97
static const vpColor green
Definition: vpColor.h:166
static int round(const double x)
Definition: vpMath.h:249
double get_j() const
Definition: vpImagePoint.h:210
Class that defines what is a feature generic tracker.
Definition: vpTracker.h:66
vpMeSiteState getState() const
Definition: vpMeSite.h:198
void set_i(const double ii)
Definition: vpImagePoint.h:163
std::list< vpMeSite > list
Definition: vpMeTracker.h:73
Error that can be emited by the vpTracker class and its derivates.
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)
vpMeTracker & operator=(vpMeTracker &f)
void track(const vpImage< unsigned char > &I)
Track sampled pixels.
bool display_point
Definition: vpMeTracker.h:169
Contains abstract elements for a Distance to Feature type feature.
Definition: vpMeTracker.h:64
int nGoodElement
Definition: vpMeTracker.h:77
void setState(const vpMeSiteState &flag)
Definition: vpMeSite.h:185
int j_1
Definition: vpMeSite.h:95
void set_j(const double jj)
Definition: vpImagePoint.h:174
#define vpDERROR_TRACE
Definition: vpDebug.h:455
int j
Definition: vpMeSite.h:94
double weight
Definition: vpMeSite.h:106
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:374
vpMe * me
Moving edges initialisation parameters.
Definition: vpMeTracker.h:75
void initTracking(const vpImage< unsigned char > &I)
void init()
Default initialization.
Definition: vpTracker.cpp:50
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:256
int i_1
Definition: vpMeSite.h:95
vpMeSite::vpMeSiteDisplayType selectDisplay
Definition: vpMeTracker.h:80
virtual void display(const vpImage< unsigned char > &I, vpColor col)=0
unsigned int init_range
Definition: vpMeTracker.h:76
vpColVector p
Definition: vpTracker.h:73
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:225