ViSP  2.10.0
vpMeTracker.cpp
1 /****************************************************************************
2 *
3 * $Id: vpMeTracker.cpp 4797 2014-07-23 15:52:28Z fspindle $
4 *
5 * This file is part of the ViSP software.
6 * Copyright (C) 2005 - 2014 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  : list(), me(NULL), init_range(1), nGoodElement(0), selectDisplay(vpMeSite::NONE)
70 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
71  , query_range (0), display_point(false)
72 #endif
73 {
74  init();
75 }
76 
78  : vpTracker(meTracker),
79  list(), me(NULL), init_range(1), nGoodElement(0), selectDisplay(vpMeSite::NONE)
80 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
81  , query_range (0), display_point(false)
82 #endif
83 {
84  init();
85 
86  me = meTracker.me;
87  list = meTracker.list;
88  nGoodElement = meTracker.nGoodElement;
89  init_range = meTracker.init_range;
90  selectDisplay = meTracker.selectDisplay;
91 
92  #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
93  display_point = meTracker.display_point;
94  query_range = meTracker.query_range;
95  #endif
96 }
97 
102 {
103  nGoodElement = 0;
104  list.clear();
105 }
106 
108 {
109  reset();
110 }
111 
114 {
115  list = p_me.list;
116  me = p_me.me;
118 
119  return *this;
120 }
121 
122 static bool isSuppressZero(const vpMeSite& P){
123  return (P.getState() == vpMeSite::NO_SUPPRESSION);
124 }
125 
126 unsigned int
128 {
129  unsigned int number_signal=0;
130 
131  // Loop through all the points tracked from the contour
132  number_signal = static_cast<unsigned int>(std::count_if(list.begin(), list.end(), isSuppressZero));
133  return number_signal;
134 }
135 
136 unsigned int
138 {
139  return (unsigned int)list.size();
140 }
141 
142 int
143 vpMeTracker::outOfImage(int i, int j, int half, int rows, int cols)
144 {
145  return (! ((i> half+2) &&
146  (i< rows -(half+2)) &&
147  (j>half+2) &&
148  (j<cols-(half+2)))
149  ) ;
150 }
151 
152 int
153 vpMeTracker::outOfImage(vpImagePoint iP, int half, int rows, int cols)
154 {
155  int i = vpMath::round(iP.get_i());
156  int j = vpMath::round(iP.get_j());
157  return (! ((i> half+2) &&
158  (i< rows -(half+2)) &&
159  (j>half+2) &&
160  (j<cols-(half+2)))
161  ) ;
162 }
163 
164 
171 void
173 {
174  if (!me) {
175  vpDERROR_TRACE(2, "Tracking error: Moving edges not initialized");
177  "Moving edges not initialized")) ;
178  }
179 
180  // Must set range to 0
181  unsigned int range_tmp = me->getRange();
183 
184  nGoodElement=0;
185 
186  int d = 0;
187  vpImagePoint ip1, ip2;
188 
189  // Loop through list of sites to track
190  for(std::list<vpMeSite>::iterator it=list.begin(); it!=list.end(); ++it){
191  vpMeSite refp = *it;//current reference pixel
192 
193  d++ ;
194  // If element hasn't been suppressed
195  if(refp.getState() == vpMeSite::NO_SUPPRESSION)
196  {
197  try {
198  refp.track(I,me,false);
199  }
200  catch(...)
201  {
202  // EM verifier quel signal est de sortie !!!
203  vpERROR_TRACE("Error caught") ;
204  throw ;
205  }
207  }
208 
209 
210 #if (DEBUG_LEVEL2)
211  {
212  double a,b ;
213  a = refp.i_1 - refp.i ;
214  b = refp.j_1 - refp.j ;
215  if(refp.getState() == vpMeSite::NO_SUPPRESSION) {
216  ip1.set_i( refp.i );
217  ip1.set_j( refp.j );
218  ip2.set_i( refp.i+a );
219  ip2.set_j( refp.j+b );
221  }
222  }
223 #endif
224  *it = refp;
225  }
226 
227  /*
228  if (res != OK)
229  {
230  std::cout<< "In vpMeTracker::initTracking(): " ;
231  switch (res)
232  {
233  case ERR_TRACKING:
234  std::cout << "vpMeTracker::initTracking:Track return ERR_TRACKING " << std::endl ;
235  break ;
236  case fatalError:
237  std::cout << "vpMeTracker::initTracking:Track return fatalError" << std::endl ;
238  break ;
239  default:
240  std::cout << "vpMeTracker::initTracking:Track return error " << res << std::endl ;
241  }
242  return res ;
243  }
244  */
245 
246  me->setRange(range_tmp);
247 }
248 
257 void
259 {
260  if (!me) {
261  vpDERROR_TRACE(2, "Tracking error: Moving edges not initialized");
263  "Moving edges not initialized")) ;
264  }
265 
266  if (list.empty())
267  {
268  vpDERROR_TRACE(2, "Tracking error: too few pixel to track");
270  "too few pixel to track")) ;
271 
272  }
273 
274  vpImagePoint ip1, ip2;
275  nGoodElement=0;
276  // int d =0;
277  // Loop through list of sites to track
278  for(std::list<vpMeSite>::iterator it=list.begin(); it!=list.end(); ++it){
279  vpMeSite s = *it;//current reference pixel
280 
281  // d++ ;
282  // If element hasn't been suppressed
284  {
285 
286  try{
287  // vpERROR_TRACE("%d",d ) ;
288  // vpERROR_TRACE("range %d",me->range) ;
289  s.track(I,me,true);
290  }
291  catch(vpTrackingException)
292  {
293  vpERROR_TRACE("catch exception ") ;
295  }
296 
297  if(s.getState() != vpMeSite::THRESHOLD)
298  {
299  nGoodElement++;
300 
301 #if (DEBUG_LEVEL2)
302  {
303  double a,b ;
304  a = s.i_1 - s.i ;
305  b = s.j_1 - s.j ;
307  ip1.set_i( s.i );
308  ip1.set_j( s.j );
309  ip2.set_i( s.i+a*5 );
310  ip2.set_j( s.j+b*5 );
312  }
313  }
314 #endif
315 
316  }
317  *it = s;
318  }
319  }
320 }
321 
334 void
336 {
337 #if (DEBUG_LEVEL1)
338  {
339  std::cout <<"begin vpMeTracker::displayList() " << std::endl ;
340  std::cout<<" There are "<<list.size()<< " sites in the list " << std::endl ;
341  }
342 #endif
343  for(std::list<vpMeSite>::const_iterator it=list.begin(); it!=list.end(); ++it){
344  vpMeSite p_me = *it;
345  p_me.display(I);
346  }
347 }
348 
355 void
356 vpMeTracker::display(const vpImage<unsigned char>& I,vpColVector &w, unsigned int &index_w)
357 {
358  for(std::list<vpMeSite>::iterator it=list.begin(); it!=list.end(); ++it){
359  vpMeSite P = *it;
360 
362  {
363  P.weight = w[index_w];
364  index_w++;
365  }
366 
367  *it = P;
368  }
369  display(I);
370 }
371 
372 #undef DEBUG_LEVEL1
373 #undef DEBUG_LEVEL2
374 
375 
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:195
void display(const vpImage< unsigned char > &I)
Definition: vpMeSite.cpp:645
unsigned int numberOfSignal()
#define vpERROR_TRACE
Definition: vpDebug.h:395
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)
int i
Definition: vpMeSite.h:98
#define vpDERROR_TRACE
Definition: vpDebug.h:459
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:206
Class that defines what is a feature generic tracker.
Definition: vpTracker.h:69
vpMeSiteState getState() const
Definition: vpMeSite.h:202
void set_i(const double ii)
Definition: vpImagePoint.h:159
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:176
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
void set_j(const double jj)
Definition: vpImagePoint.h:170
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()
void track(const vpImage< unsigned char > &im, const vpMe *me, const bool test_contraste=true)
Definition: vpMeSite.cpp:381
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:93
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:98