Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpFeatureEllipse.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  * 2D ellipse visual feature.
32  *
33  * Authors:
34  * Eric Marchand
35  *
36  *****************************************************************************/
37 
38 
45 #include <visp3/visual_features/vpBasicFeature.h>
46 #include <visp3/visual_features/vpFeatureEllipse.h>
47 
48 // Exception
49 #include <visp3/core/vpException.h>
50 #include <visp3/visual_features/vpFeatureException.h>
51 
52 // Debug trace
53 #include <visp3/core/vpDebug.h>
54 
55 // math
56 #include <visp3/core/vpMath.h>
57 
58 
59 
60 #include <visp3/core/vpFeatureDisplay.h>
61 
62 
63 /*
64 
65 
66 
67 attributes and members directly related to the vpBasicFeature needs
68 other functionalities ar useful but not mandatory
69 
70 
71 
72 
73 
74 */
75 
76 void
78 {
79  //feature dimension
80  dim_s = 5 ;
81  nbParameters = 8;
82 
83  // memory allocation
84  s.resize(dim_s) ;
85  if (flags == NULL)
86  flags = new bool[nbParameters];
87  for (unsigned int i = 0; i < nbParameters; i++) flags[i] = false;
88 
89  //default depth values
90  A = B = 0;
91  C =1 ;
92 
93 }
94 
96 {
97  init() ;
98 }
99 
100 
101 
103 vpMatrix
104 vpFeatureEllipse::interaction(const unsigned int select)
105 {
106  vpMatrix L ;
107 
108  L.resize(0,6) ;
109 
111  {
112  for (unsigned int i = 0; i < nbParameters; i++)
113  {
114  if (flags[i] == false)
115  {
116  switch(i){
117  case 0:
118  vpTRACE("Warning !!! The interaction matrix is computed but x was not set yet");
119  break;
120  case 1:
121  vpTRACE("Warning !!! The interaction matrix is computed but y was not set yet");
122  break;
123  case 2:
124  vpTRACE("Warning !!! The interaction matrix is computed but mu20 was not set yet");
125  break;
126  case 3:
127  vpTRACE("Warning !!! The interaction matrix is computed but mu11 was not set yet");
128  break;
129  case 4:
130  vpTRACE("Warning !!! The interaction matrix is computed but mu02 was not set yet");
131  break;
132  case 5:
133  vpTRACE("Warning !!! The interaction matrix is computed but A was not set yet");
134  break;
135  case 6:
136  vpTRACE("Warning !!! The interaction matrix is computed but B was not set yet");
137  break;
138  case 7:
139  vpTRACE("Warning !!! The interaction matrix is computed but C was not set yet");
140  break;
141  default:
142  vpTRACE("Problem during the reading of the variable flags");
143  }
144  }
145  }
146  resetFlags();
147  }
148 
149  double xc = s[0] ;
150  double yc = s[1] ;
151  double mu20 = s[2] ;
152  double mu11 = s[3] ;
153  double mu02 = s[4] ;
154 
155  //eq 39
156  double Z = 1/(A*xc + B*yc + C) ;
157 
158 
159 
160  if (vpFeatureEllipse::selectX() & select )
161  {
162  vpMatrix H(1,6) ; H = 0;
163 
164 
165  H[0][0] = -1/Z;
166  H[0][1] = 0 ;
167  H[0][2] = xc/Z + A*mu20 + B*mu11;
168  H[0][3] = xc*yc + mu11;
169  H[0][4] = -1-vpMath::sqr(xc)-mu20;
170  H[0][5] = yc;
171 
172 
173  L = vpMatrix::stack(L,H) ;
174  }
175 
176  if (vpFeatureEllipse::selectY() & select )
177  {
178  vpMatrix H(1,6) ; H = 0;
179 
180 
181  H[0][0] = 0 ;
182  H[0][1] = -1/Z;
183  H[0][2] = yc/Z + A*mu11 + B*mu02;
184  H[0][3] = 1+vpMath::sqr(yc)+mu02;
185  H[0][4] = -xc*yc - mu11;
186  H[0][5] = -xc;
187 
188  L = vpMatrix::stack(L,H) ;
189  }
190 
191  if (vpFeatureEllipse::selectMu20() & select )
192  {
193  vpMatrix H(1,6) ; H = 0;
194 
195  H[0][0] = -2*(A*mu20+B*mu11);
196  H[0][1] = 0 ;
197  H[0][2] = 2*((1/Z+A*xc)*mu20+B*xc*mu11) ;
198  H[0][3] = 2*(yc*mu20+xc*mu11);
199  H[0][4] = -4*mu20*xc;
200  H[0][5] = 2*mu11;
201 
202  L = vpMatrix::stack(L,H) ;
203  }
204 
205  if (vpFeatureEllipse::selectMu11() & select )
206  {
207  vpMatrix H(1,6) ; H = 0;
208 
209  H[0][0] = -A*mu11-B*mu02;
210  H[0][1] = -A*mu20-B*mu11;
211  H[0][2] = A*yc*mu20+(3/Z-C)*mu11+B*xc*mu02;
212  H[0][3] = 3*yc*mu11+xc*mu02;
213  H[0][4] = -yc*mu20-3*xc*mu11;
214  H[0][5] = mu02-mu20;
215 
216  L = vpMatrix::stack(L,H) ;
217  }
218 
219  if (vpFeatureEllipse::selectMu02() & select )
220  {
221  vpMatrix H(1,6) ; H = 0;
222 
223  H[0][0] = 0 ;
224  H[0][1] = -2*(A*mu11+B*mu02);
225  H[0][2] = 2*((1/Z+B*yc)*mu02+A*yc*mu11);
226  H[0][3] = 4*yc*mu02;
227  H[0][4] = -2*(yc*mu11 +xc*mu02) ;
228  H[0][5] = -2*mu11 ;
229  L = vpMatrix::stack(L,H) ;
230  }
231 
232 
233  return L ;
234 }
235 
240  const unsigned int select)
241 {
242  vpColVector e(0) ;
243 
244  try{
245  if (vpFeatureEllipse::selectX() & select )
246  {
247  vpColVector ex(1) ;
248  ex[0] = s[0] - s_star[0] ;
249 
250  e = vpColVector::stack(e,ex) ;
251  }
252 
253  if (vpFeatureEllipse::selectY() & select )
254  {
255  vpColVector ey(1) ;
256  ey[0] = s[1] - s_star[1] ;
257  e = vpColVector::stack(e,ey) ;
258  }
259 
260  if (vpFeatureEllipse::selectMu20() & select )
261  {
262  vpColVector ex(1) ;
263  ex[0] = s[2] - s_star[2] ;
264 
265  e = vpColVector::stack(e,ex) ;
266  }
267 
268  if (vpFeatureEllipse::selectMu11() & select )
269  {
270  vpColVector ey(1) ;
271  ey[0] = s[3] - s_star[3] ;
272  e = vpColVector::stack(e,ey) ;
273  }
274 
275  if (vpFeatureEllipse::selectMu02() & select )
276  {
277  vpColVector ey(1) ;
278  ey[0] = s[4] - s_star[4] ;
279  e = vpColVector::stack(e,ey) ;
280  }
281 
282  }
283  catch(...) {
284  throw;
285  }
286 
287  return e ;
288 
289 }
290 
291 
292 void
293 vpFeatureEllipse::print(const unsigned int select ) const
294 {
295 
296  std::cout <<"Ellipse: " << std::endl ;
297  if (vpFeatureEllipse::selectX() & select )
298  std::cout << " x=" << s[0] <<std::endl ;;
299  if (vpFeatureEllipse::selectY() & select )
300  std::cout << " y=" << s[1] <<std::endl ;
301  if (vpFeatureEllipse::selectMu20() & select )
302  std::cout << " mu20=" << s[2] <<std::endl ;
303  if (vpFeatureEllipse::selectMu11() & select )
304  std::cout << " mu11=" << s[3] <<std::endl ;
305  if (vpFeatureEllipse::selectMu02() & select )
306  std::cout << " mu02=" << s[4] <<std::endl ;
307  std::cout << "A = "<<A <<" B = "<<B <<" C = "<<C << std::endl ;
308 }
309 
310 
311 void
312 vpFeatureEllipse::buildFrom(const double x, const double y,
313  const double mu20, const double mu11,
314  const double mu02)
315 {
316 
317  s[0] = x ;
318  s[1] = y ;
319  s[2] = mu20 ;
320  s[3] = mu11 ;
321  s[4] = mu02 ;
322 
323  for( int i = 0; i < 5; i++) flags[i] = true;
324 
325 }
326 
327 void
328 vpFeatureEllipse::buildFrom(const double x, const double y,
329  const double mu20, const double mu11,
330  const double mu02,
331  const double a, const double b, const double c)
332 {
333 
334  s[0] = x ;
335  s[1] = y ;
336  s[2] = mu20 ;
337  s[3] = mu11 ;
338  s[4] = mu02 ;
339 
340  this->A = a ;
341  this->B = b ;
342  this->C = c ;
343 
344  for( unsigned int i = 0; i < nbParameters; i++) flags[i] = true;
345 }
346 
347 void
348 vpFeatureEllipse::set_x(const double x)
349 {
350  s[0] = x ;
351  flags[0] = true;
352 }
353 
354 void
355 vpFeatureEllipse::set_y(const double y)
356 {
357  s[1] = y ;
358  flags[1] = true;
359 }
360 
361 void
362 vpFeatureEllipse::set_xy(const double x,const double y)
363 {
364  s[0] = x ;
365  s[1] = y ;
366  for( int i = 0; i < 2; i++) flags[i] = true;
367 }
368 
369 void
370 vpFeatureEllipse::setABC(const double a, const double b, const double c)
371 {
372  this->A = a ;
373  this->B = b ;
374  this->C = c ;
375  for( unsigned int i = 5; i < nbParameters; i++) flags[i] = true;
376 }
377 
378 
379 void
380 vpFeatureEllipse::setMu(const double mu20, const double mu11,
381  const double mu02)
382 {
383 
384  s[2] = mu20 ;
385  s[3] = mu11 ;
386  s[4] = mu02 ;
387  for( int i = 2; i < 5; i++) flags[i] = true;
388 
389 }
390 
391 
392 
402 void
404  const vpImage<unsigned char> &I,
405  const vpColor &color,
406  unsigned int thickness ) const
407 {
408  try{
409  double x = s[0] ;
410  double y = s[1] ;
411 
412  double mu20 = s[2] ;
413  double mu11 = s[3] ;
414  double mu02 = s[4] ;
415 
416  vpFeatureDisplay::displayEllipse(x, y, mu20, mu11, mu02,
417  cam, I, color, thickness) ;
418 
419  }
420  catch(...)
421  {
422  vpERROR_TRACE("Error caught") ;
423  throw ;
424  }
425 }
426 
436 void
438  const vpImage<vpRGBa> &I,
439  const vpColor &color,
440  unsigned int thickness ) const
441 {
442  try{
443  double x = s[0] ;
444  double y = s[1] ;
445 
446  double mu20 = s[2] ;
447  double mu11 = s[3] ;
448  double mu02 = s[4] ;
449 
450  vpFeatureDisplay::displayEllipse(x, y, mu20, mu11, mu02,
451  cam, I, color, thickness) ;
452 
453  }
454  catch(...)
455  {
456  vpERROR_TRACE("Error caught") ;
457  throw ;
458  }
459 }
460 
461 
464 {
465  vpFeatureEllipse *feature = new vpFeatureEllipse ;
466  return feature ;
467 }
468 
469 unsigned int vpFeatureEllipse::selectX() { return FEATURE_LINE[0] ; }
470 unsigned int vpFeatureEllipse::selectY() { return FEATURE_LINE[1] ; }
471 unsigned int vpFeatureEllipse::selectMu20() { return FEATURE_LINE[2] ; }
472 unsigned int vpFeatureEllipse::selectMu11() { return FEATURE_LINE[3] ; }
473 unsigned int vpFeatureEllipse::selectMu02() { return FEATURE_LINE[4] ; }
void init()
Default initialization.
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:97
bool * flags
Ensure that all the parameters needed to compute the iteraction matrix are set.
void set_x(const double x)
static void displayEllipse(double x, double y, double mu20, double mu11, double m02, const vpCameraParameters &cam, const vpImage< unsigned char > &I, const vpColor &color=vpColor::green, unsigned int thickness=1)
void stack(const double &d)
void resize(const unsigned int nrows, const unsigned int ncols, const bool flagNullify=true)
Definition: vpArray2D.h:167
#define vpERROR_TRACE
Definition: vpDebug.h:391
Class to define colors available for display functionnalities.
Definition: vpColor.h:121
void stack(const vpMatrix &A)
Definition: vpMatrix.cpp:2981
void setMu(const double mu20, const double mu11, const double mu02)
unsigned int dim_s
Dimension of the visual feature.
static unsigned int selectY()
static unsigned int selectMu11()
vpFeatureEllipse()
Default constructor.
class that defines what is a visual feature
void set_xy(const double x, const double y)
vpFeatureEllipse * duplicate() const
Feature duplication.
#define vpTRACE
Definition: vpDebug.h:414
static double sqr(double x)
Definition: vpMath.h:110
void print(const unsigned int select=FEATURE_ALL) const
print the name of the feature
static unsigned int selectMu20()
Generic class defining intrinsic camera parameters.
void setABC(const double A, const double B, const double C)
static const unsigned int FEATURE_LINE[32]
vpBasicFeatureDeallocatorType deallocate
void display(const vpCameraParameters &cam, const vpImage< unsigned char > &I, const vpColor &color=vpColor::green, unsigned int thickness=1) const
void set_y(const double y)
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
vpMatrix interaction(const unsigned int select=FEATURE_ALL)
compute the interaction matrix from a subset a the possible features
void buildFrom(const double x, const double y, const double mu20, const double mu11, const double mu02)
static unsigned int selectX()
Class that defines 2D ellipse visual feature.
vpColVector error(const vpBasicFeature &s_star, const unsigned int select=FEATURE_ALL)
unsigned int nbParameters
Number of parameters needed to compute the interaction matrix.
vpColVector s
State of the visual feature.
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:225
static unsigned int selectMu02()