ViSP  2.9.0
vpMeTracker.cpp
1 /****************************************************************************
2 *
3 * $Id: vpMeTracker.cpp 4649 2014-02-07 14:57:11Z 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 
99 {
100  list.clear();
101 }
102 
105 {
106  list = p_me.list;
107  me = p_me.me;
109 
110  return *this;
111 }
112 
113 static bool isSuppressZero(const vpMeSite& P){
114  return (P.getState() == vpMeSite::NO_SUPPRESSION);
115 }
116 
117 unsigned int
119 {
120  unsigned int number_signal=0;
121 
122  // Loop through all the points tracked from the contour
123  number_signal = static_cast<unsigned int>(std::count_if(list.begin(), list.end(), isSuppressZero));
124  return number_signal;
125 }
126 
127 unsigned int
129 {
130  return (unsigned int)list.size();
131 }
132 
133 int
134 vpMeTracker::outOfImage(int i, int j, int half, int rows, int cols)
135 {
136  return (! ((i> half+2) &&
137  (i< rows -(half+2)) &&
138  (j>half+2) &&
139  (j<cols-(half+2)))
140  ) ;
141 }
142 
143 int
144 vpMeTracker::outOfImage(vpImagePoint iP, int half, int rows, int cols)
145 {
146  int i = vpMath::round(iP.get_i());
147  int j = vpMath::round(iP.get_j());
148  return (! ((i> half+2) &&
149  (i< rows -(half+2)) &&
150  (j>half+2) &&
151  (j<cols-(half+2)))
152  ) ;
153 }
154 
155 
162 void
164 {
165  if (!me) {
166  vpDERROR_TRACE(2, "Tracking error: Moving edges not initialized");
168  "Moving edges not initialized")) ;
169  }
170 
171  // Must set range to 0
172  unsigned int range_tmp = me->getRange();
174 
175  nGoodElement=0;
176 
177  int d = 0;
178  vpImagePoint ip1, ip2;
179 
180  // Loop through list of sites to track
181  for(std::list<vpMeSite>::iterator it=list.begin(); it!=list.end(); ++it){
182  vpMeSite refp = *it;//current reference pixel
183 
184  d++ ;
185  // If element hasn't been suppressed
186  if(refp.getState() == vpMeSite::NO_SUPPRESSION)
187  {
188  try {
189  refp.track(I,me,false);
190  }
191  catch(...)
192  {
193  // EM verifier quel signal est de sortie !!!
194  vpERROR_TRACE("Error caught") ;
195  throw ;
196  }
198  }
199 
200 
201 #if (DEBUG_LEVEL2)
202  {
203  double a,b ;
204  a = refp.i_1 - refp.i ;
205  b = refp.j_1 - refp.j ;
206  if(refp.getState() == vpMeSite::NO_SUPPRESSION) {
207  ip1.set_i( refp.i );
208  ip1.set_j( refp.j );
209  ip2.set_i( refp.i+a );
210  ip2.set_j( refp.j+b );
212  }
213  }
214 #endif
215  *it = refp;
216  }
217 
218  /*
219  if (res != OK)
220  {
221  std::cout<< "In vpMeTracker::initTracking(): " ;
222  switch (res)
223  {
224  case ERR_TRACKING:
225  std::cout << "vpMeTracker::initTracking:Track return ERR_TRACKING " << std::endl ;
226  break ;
227  case fatalError:
228  std::cout << "vpMeTracker::initTracking:Track return fatalError" << std::endl ;
229  break ;
230  default:
231  std::cout << "vpMeTracker::initTracking:Track return error " << res << std::endl ;
232  }
233  return res ;
234  }
235  */
236 
237  me->setRange(range_tmp);
238 }
239 
248 void
250 {
251  if (!me) {
252  vpDERROR_TRACE(2, "Tracking error: Moving edges not initialized");
254  "Moving edges not initialized")) ;
255  }
256 
257  if (list.empty())
258  {
259  vpDERROR_TRACE(2, "Tracking error: too few pixel to track");
261  "too few pixel to track")) ;
262 
263  }
264 
265  vpImagePoint ip1, ip2;
266  nGoodElement=0;
267  // int d =0;
268  // Loop through list of sites to track
269  for(std::list<vpMeSite>::iterator it=list.begin(); it!=list.end(); ++it){
270  vpMeSite s = *it;//current reference pixel
271 
272  // d++ ;
273  // If element hasn't been suppressed
275  {
276 
277  try{
278  // vpERROR_TRACE("%d",d ) ;
279  // vpERROR_TRACE("range %d",me->range) ;
280  s.track(I,me,true);
281  }
282  catch(vpTrackingException)
283  {
284  vpERROR_TRACE("catch exception ") ;
286  }
287 
288  if(s.getState() != vpMeSite::THRESHOLD)
289  {
290  nGoodElement++;
291 
292 #if (DEBUG_LEVEL2)
293  {
294  double a,b ;
295  a = s.i_1 - s.i ;
296  b = s.j_1 - s.j ;
298  ip1.set_i( s.i );
299  ip1.set_j( s.j );
300  ip2.set_i( s.i+a*5 );
301  ip2.set_j( s.j+b*5 );
303  }
304  }
305 #endif
306 
307  }
308  *it = s;
309  }
310  }
311 }
312 
325 void
327 {
328 #if (DEBUG_LEVEL1)
329  {
330  std::cout <<"begin vpMeTracker::displayList() " << std::endl ;
331  std::cout<<" There are "<<list.size()<< " sites in the list " << std::endl ;
332  }
333 #endif
334  for(std::list<vpMeSite>::const_iterator it=list.begin(); it!=list.end(); ++it){
335  vpMeSite p_me = *it;
336  p_me.display(I);
337  }
338 }
339 
346 void
347 vpMeTracker::display(const vpImage<unsigned char>& I,vpColVector &w, unsigned int &index_w)
348 {
349  for(std::list<vpMeSite>::iterator it=list.begin(); it!=list.end(); ++it){
350  vpMeSite P = *it;
351 
353  {
354  P.weight = w[index_w];
355  index_w++;
356  }
357 
358  *it = P;
359  }
360  display(I);
361 }
362 
363 #undef DEBUG_LEVEL1
364 #undef DEBUG_LEVEL2
365 
366 
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:194
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:205
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:158
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
void set_j(const double jj)
Definition: vpImagePoint.h:169
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:98
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: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