ViSP  2.8.0
vpMeTracker.cpp
1 /****************************************************************************
2 *
3 * $Id: vpMeTracker.cpp 4303 2013-07-04 14:14:00Z fspindle $
4 *
5 * This file is part of the ViSP software.
6 * Copyright (C) 2005 - 2013 by INRIA. All rights reserved.
7 *
8 * This software is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * ("GPL") version 2 as published by the Free Software Foundation.
11 * See the file LICENSE.txt at the root directory of this source
12 * distribution for additional information about the GNU GPL.
13 *
14 * For using ViSP with software that can not be combined with the GNU
15 * GPL, please contact INRIA about acquiring a ViSP Professional
16 * Edition License.
17 *
18 * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19 *
20 * This software was developed at:
21 * INRIA Rennes - Bretagne Atlantique
22 * Campus Universitaire de Beaulieu
23 * 35042 Rennes Cedex
24 * France
25 * http://www.irisa.fr/lagadic
26 *
27 * If you have questions regarding the use of this file, please contact
28 * INRIA at visp@inria.fr
29 *
30 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32 *
33 *
34 * Description:
35 * Moving edges.
36 *
37 * Authors:
38 * Andrew Comport
39 *
40 *****************************************************************************/
41 
49 #include <visp/vpMeTracker.h>
50 #include <visp/vpDisplay.h>
51 #include <visp/vpColor.h>
52 
53 #include <visp/vpTrackingException.h>
54 #include <visp/vpDebug.h>
55 #include <algorithm>
56 
57 #define DEBUG_LEVEL1 0
58 #define DEBUG_LEVEL2 0
59 
60 void
62 {
63  vpTracker::init() ;
64  p.resize(2) ;
66 }
67 
69 {
70  init();
71  me = NULL ;
72  nGoodElement = 0;
73  init_range = 1;
74 
75  #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
76  query_range = 0;
77  display_point = false ;
78  #endif
79 }
80 
81 vpMeTracker::vpMeTracker(const vpMeTracker& meTracker):vpTracker(meTracker)
82 {
83  init();
84 
85  me = meTracker.me;
86  list = meTracker.list;
87  nGoodElement = meTracker.nGoodElement;
88  init_range = meTracker.init_range;
89  selectDisplay = meTracker.selectDisplay;
90 
91  #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
92  display_point = meTracker.display_point;
93  query_range = meTracker.query_range;
94  #endif
95 }
96 
98 {
99  list.clear();
100 }
101 
104 {
105  list = p.list;
106  me = p.me;
108 
109  return *this;
110 }
111 
112 static bool isSuppressZero(const vpMeSite& P){
113  return (P.getState() == vpMeSite::NO_SUPPRESSION);
114 }
115 
116 unsigned int
118 {
119  unsigned int number_signal=0;
120 
121  // Loop through all the points tracked from the contour
122  number_signal = static_cast<unsigned int>(std::count_if(list.begin(), list.end(), isSuppressZero));
123  return number_signal;
124 }
125 
126 unsigned int
128 {
129  return (unsigned int)list.size();
130 }
131 
132 int
133 vpMeTracker::outOfImage(int i, int j, int half, int rows, int cols)
134 {
135  return (! ((i> half+2) &&
136  (i< rows -(half+2)) &&
137  (j>half+2) &&
138  (j<cols-(half+2)))
139  ) ;
140 }
141 
142 int
143 vpMeTracker::outOfImage(vpImagePoint iP, int half, int rows, int cols)
144 {
145  int i = vpMath::round(iP.get_i());
146  int j = vpMath::round(iP.get_j());
147  return (! ((i> half+2) &&
148  (i< rows -(half+2)) &&
149  (j>half+2) &&
150  (j<cols-(half+2)))
151  ) ;
152 }
153 
154 
161 void
163 {
164  if (!me) {
165  vpDERROR_TRACE(2, "Tracking error: Moving edges not initialized");
167  "Moving edges not initialized")) ;
168  }
169 
170  // Must set range to 0
171  unsigned int range_tmp = me->getRange();
173 
174  nGoodElement=0;
175 
176  int d = 0;
177  vpImagePoint ip1, ip2;
178 
179  // Loop through list of sites to track
180  for(std::list<vpMeSite>::iterator it=list.begin(); it!=list.end(); ++it){
181  vpMeSite refp = *it;//current reference pixel
182 
183  d++ ;
184  // If element hasn't been suppressed
185  if(refp.getState() == vpMeSite::NO_SUPPRESSION)
186  {
187  try {
188  refp.track(I,me,false);
189  }
190  catch(...)
191  {
192  // EM verifier quel signal est de sortie !!!
193  vpERROR_TRACE("Error caught") ;
194  throw ;
195  }
197  }
198 
199 
200 #if (DEBUG_LEVEL2)
201  {
202  double a,b ;
203  a = refp.i_1 - refp.i ;
204  b = refp.j_1 - refp.j ;
205  if(refp.getState() == vpMeSite::NO_SUPPRESSION) {
206  ip1.set_i( refp.i );
207  ip1.set_j( refp.j );
208  ip2.set_i( refp.i+a );
209  ip2.set_j( refp.j+b );
211  }
212  }
213 #endif
214  *it = refp;
215  }
216 
217  /*
218  if (res != OK)
219  {
220  std::cout<< "In vpMeTracker::initTracking(): " ;
221  switch (res)
222  {
223  case ERR_TRACKING:
224  std::cout << "vpMeTracker::initTracking:Track return ERR_TRACKING " << std::endl ;
225  break ;
226  case fatalError:
227  std::cout << "vpMeTracker::initTracking:Track return fatalError" << std::endl ;
228  break ;
229  default:
230  std::cout << "vpMeTracker::initTracking:Track return error " << res << std::endl ;
231  }
232  return res ;
233  }
234  */
235 
236  me->setRange(range_tmp);
237 }
238 
247 void
249 {
250  if (!me) {
251  vpDERROR_TRACE(2, "Tracking error: Moving edges not initialized");
253  "Moving edges not initialized")) ;
254  }
255 
256  if (list.empty())
257  {
258  vpDERROR_TRACE(2, "Tracking error: too few pixel to track");
260  "too few pixel to track")) ;
261 
262  }
263 
264  vpImagePoint ip1, ip2;
265  nGoodElement=0;
266  // int d =0;
267  // Loop through list of sites to track
268  for(std::list<vpMeSite>::iterator it=list.begin(); it!=list.end(); ++it){
269  vpMeSite s = *it;//current reference pixel
270 
271  // d++ ;
272  // If element hasn't been suppressed
274  {
275 
276  try{
277  // vpERROR_TRACE("%d",d ) ;
278  // vpERROR_TRACE("range %d",me->range) ;
279  s.track(I,me,true);
280  }
281  catch(vpTrackingException)
282  {
283  vpERROR_TRACE("catch exception ") ;
285  }
286 
287  if(s.getState() != vpMeSite::THRESHOLD)
288  {
289  nGoodElement++;
290 
291 #if (DEBUG_LEVEL2)
292  {
293  double a,b ;
294  a = s.i_1 - s.i ;
295  b = s.j_1 - s.j ;
297  ip1.set_i( s.i );
298  ip1.set_j( s.j );
299  ip2.set_i( s.i+a*5 );
300  ip2.set_j( s.j+b*5 );
302  }
303  }
304 #endif
305 
306  }
307  *it = s;
308  }
309  }
310 }
311 
324 void
326 {
327 #if (DEBUG_LEVEL1)
328  {
329  std::cout <<"begin vpMeTracker::displayList() " << std::endl ;
330  std::cout<<" There are "<<list.size()<< " sites in the list " << std::endl ;
331  }
332 #endif
333  for(std::list<vpMeSite>::const_iterator it=list.begin(); it!=list.end(); ++it){
334  vpMeSite p = *it;
335  p.display(I);
336  }
337 }
338 
345 void
346 vpMeTracker::display(const vpImage<unsigned char>& I,vpColVector &w, unsigned int &index_w)
347 {
348  for(std::list<vpMeSite>::iterator it=list.begin(); it!=list.end(); ++it){
349  vpMeSite P = *it;
350 
352  {
353  P.weight = w[index_w];
354  index_w++;
355  }
356 
357  *it = P;
358  }
359  display(I);
360 }
361 
362 #undef DEBUG_LEVEL1
363 #undef DEBUG_LEVEL2
364 
365 
void set_j(const double j)
Definition: vpImagePoint.h:156
unsigned int getRange() const
Definition: vpMe.h:236
virtual void displayArrow(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color=vpColor::white, unsigned int w=4, unsigned int h=2, unsigned int thickness=1)=0
double get_i() const
Definition: vpImagePoint.h:181
void display(const vpImage< unsigned char > &I)
Definition: vpMeSite.cpp:634
unsigned int numberOfSignal()
#define vpERROR_TRACE
Definition: vpDebug.h:379
unsigned int totalNumberOfSignal()
Performs search in a given direction(normal) for a given distance(pixels) for a given 'site'...
Definition: vpMeSite.h:76
int outOfImage(int i, int j, int half, int rows, int cols)
void set_i(const double i)
Definition: vpImagePoint.h:145
int i
Definition: vpMeSite.h:98
#define vpDERROR_TRACE
Definition: vpDebug.h:431
static const vpColor green
Definition: vpColor.h:170
static int round(const double x)
Definition: vpMath.h:228
double get_j() const
Definition: vpImagePoint.h:192
Class that defines what is a feature generic tracker.
Definition: vpTracker.h:69
vpMeSiteState getState() const
Definition: vpMeSite.h:202
std::list< vpMeSite > list
Definition: vpMeTracker.h:80
Error that can be emited by the vpTracker class and its derivates.
vpMeTracker & operator=(vpMeTracker &f)
void track(const vpImage< unsigned char > &I)
Track sampled pixels.
bool display_point
Definition: vpMeTracker.h:174
Contains abstract elements for a Distance to Feature type feature.
Definition: vpMeTracker.h:71
int nGoodElement
Definition: vpMeTracker.h:84
void setState(const vpMeSiteState &flag)
Definition: vpMeSite.h:189
int j_1
Definition: vpMeSite.h:99
int j
Definition: vpMeSite.h:98
double weight
Definition: vpMeSite.h:110
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
virtual ~vpMeTracker()
Definition: vpMeTracker.cpp:97
void track(const vpImage< unsigned char > &im, const vpMe *me, const bool test_contraste=true)
Definition: vpMeSite.cpp:370
vpMe * me
Moving edges initialisation parameters.
Definition: vpMeTracker.h:82
void initTracking(const vpImage< unsigned char > &I)
void init()
Default initialization.
Definition: vpTracker.cpp:54
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:92
void setRange(const unsigned int &r)
Definition: vpMe.h:229
int i_1
Definition: vpMeSite.h:99
vpMeSite::vpMeSiteDisplayType selectDisplay
Definition: vpMeTracker.h:87
virtual void display(const vpImage< unsigned char > &I, vpColor col)=0
unsigned int init_range
Definition: vpMeTracker.h:83
vpColVector p
Definition: vpTracker.h:78
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:94