Visual Servoing Platform  version 3.0.0
vpFeatureLuminance.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 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  * Luminance feature.
32  *
33  * Authors:
34  * Eric Marchand
35  *
36  *****************************************************************************/
37 
38 
39 #include <visp3/core/vpMatrix.h>
40 #include <visp3/core/vpHomogeneousMatrix.h>
41 #include <visp3/core/vpDisplay.h>
42 #include <visp3/core/vpPixelMeterConversion.h>
43 #include <visp3/core/vpImageConvert.h>
44 #include <visp3/core/vpImageFilter.h>
45 #include <visp3/core/vpException.h>
46 
47 #include <visp3/visual_features/vpFeatureLuminance.h>
48 
49 
62 void
64 {
65  if (flags == NULL)
66  flags = new bool[nbParameters];
67  for (unsigned int i = 0; i < nbParameters; i++) flags[i] = false;
68 
69  //default value Z (1 meters)
70  Z = 1;
71 
72  firstTimeIn =0 ;
73 
74  nbr = nbc = 0;
75 }
76 
77 
78 void
79 vpFeatureLuminance::init(unsigned int _nbr, unsigned int _nbc, double _Z)
80 {
81  init() ;
82 
83  nbr = _nbr ;
84  nbc = _nbc ;
85 
86  if((nbr < 2*bord) || (nbc < 2*bord)){
87  throw vpException(vpException::dimensionError, "border is too important compared to number of row or column.");
88  }
89 
90  // number of feature = nb column x nb lines in the images
91  dim_s = (nbr-2*bord)*(nbc-2*bord) ;
92 
93  s.resize(dim_s) ;
94 
95  if (pixInfo != NULL)
96  delete [] pixInfo;
97 
98  pixInfo = new vpLuminance[dim_s] ;
99 
100  Z = _Z ;
101 }
102 
107  : Z(1), nbr(0), nbc(0), bord(10), pixInfo(NULL), firstTimeIn(0), cam()
108 {
109  nbParameters = 1;
110  dim_s = 0 ;
111  flags = NULL;
112 
113  init() ;
114 }
115 
120  : vpBasicFeature(f), Z(1), nbr(0), nbc(0), bord(10), pixInfo(NULL), firstTimeIn(0), cam()
121 {
122  *this = f;
123 }
124 
129 {
130  Z = f.Z;
131  nbr = f.nbr;
132  nbc = f.nbc;
133  bord = f.bord;
135  cam = f.cam;
136  if (pixInfo)
137  delete [] pixInfo;
138  pixInfo = new vpLuminance[dim_s] ;
139  for(unsigned int i=0; i< dim_s; i++)
140  pixInfo[i] = f.pixInfo[i];
141  return (*this);
142 }
143 
148 {
149  if (pixInfo != NULL) delete [] pixInfo ;
150 }
151 
157 void
159 {
160  this->Z = Z_ ;
161  flags[0] = true;
162 }
163 
164 
170 double
172 {
173  return Z ;
174 }
175 
176 
177 void
179 {
180  cam = _cam ;
181 }
182 
183 
189 void
191 {
192  unsigned int l = 0;
193  double Ix,Iy ;
194 
195  double px = cam.get_px() ;
196  double py = cam.get_py() ;
197 
198 
199  if (firstTimeIn==0)
200  {
201  firstTimeIn=1 ;
202  l =0 ;
203  for (unsigned int i=bord; i < nbr-bord ; i++)
204  {
205  // cout << i << endl ;
206  for (unsigned int j = bord ; j < nbc-bord; j++)
207  { double x=0,y=0;
209  j,i,
210  x,y) ;
211 
212  pixInfo[l].x = x;
213  pixInfo[l].y = y;
214 
215  pixInfo[l].Z = Z ;
216 
217  l++;
218  }
219  }
220  }
221 
222  l= 0 ;
223  for (unsigned int i=bord; i < nbr-bord ; i++)
224  {
225  // cout << i << endl ;
226  for (unsigned int j = bord ; j < nbc-bord; j++)
227  {
228  // cout << dim_s <<" " <<l <<" " <<i << " " << j <<endl ;
229  Ix = px * vpImageFilter::derivativeFilterX(I,i,j) ;
230  Iy = py * vpImageFilter::derivativeFilterY(I,i,j) ;
231 
232  // Calcul de Z
233 
234  pixInfo[l].I = I[i][j] ;
235  s[l] = I[i][j] ;
236  pixInfo[l].Ix = Ix;
237  pixInfo[l].Iy = Iy;
238 
239  l++;
240  }
241  }
242 
243 }
244 
245 
246 
247 
253 void
255 {
256  double x,y,Ix,Iy,Zinv;
257 
258  L.resize(dim_s,6) ;
259 
260  for(unsigned int m = 0; m< L.getRows(); m++)
261  {
262  Ix = pixInfo[m].Ix;
263  Iy = pixInfo[m].Iy;
264 
265  x = pixInfo[m].x ;
266  y = pixInfo[m].y ;
267  Zinv = 1 / pixInfo[m].Z;
268 
269  {
270  L[m][0] = Ix * Zinv;
271  L[m][1] = Iy * Zinv;
272  L[m][2] = -(x*Ix+y*Iy)*Zinv;
273  L[m][3] = -Ix*x*y-(1+y*y)*Iy;
274  L[m][4] = (1+x*x)*Ix + Iy*x*y;
275  L[m][5] = Iy*x-Ix*y;
276  }
277  }
278 }
279 
284 vpMatrix vpFeatureLuminance::interaction(const unsigned int /* select */)
285 {
286  /* static */ vpMatrix L ; // warning C4640: 'L' : construction of local static object is not thread-safe
287  interaction(L) ;
288  return L ;
289 }
290 
291 
299 void
301  vpColVector &e)
302 {
303  e.resize(dim_s) ;
304 
305  for (unsigned int i =0 ; i < dim_s ; i++)
306  {
307  e[i] = s[i] - s_star[i] ;
308  }
309 }
310 
311 
312 
322  const unsigned int /* select */)
323 {
324  /* static */ vpColVector e ; // warning C4640: 'e' : construction of local static object is not thread-safe
325 
326  error(s_star, e) ;
327 
328  return e ;
329 
330 }
331 
332 
333 
334 
340 void
341 vpFeatureLuminance::print(const unsigned int /* select */) const
342 {
343  static int firsttime =0 ;
344 
345  if (firsttime==0)
346  {
347  firsttime=1 ;
348  vpERROR_TRACE("not implemented") ;
349  // Do not throw and error since it is not subject
350  // to produce a failure
351  }
352  }
353 
354 
355 
361 void
363  const vpImage<unsigned char> & /* I */,
364  const vpColor &/* color */,
365  unsigned int /* thickness */) const
366 {
367  static int firsttime =0 ;
368 
369  if (firsttime==0)
370  {
371  firsttime=1 ;
372  vpERROR_TRACE("not implemented") ;
373  // Do not throw and error since it is not subject
374  // to produce a failure
375  }
376 }
377 
383 void
385  const vpImage<vpRGBa> & /* I */,
386  const vpColor &/* color */,
387  unsigned int /* thickness */) const
388 {
389  static int firsttime =0 ;
390 
391  if (firsttime==0)
392  {
393  firsttime=1 ;
394  vpERROR_TRACE("not implemented") ;
395  // Do not throw and error since it is not subject
396  // to produce a failure
397  }
398 }
399 
400 
412 {
413  vpFeatureLuminance *feature = new vpFeatureLuminance ;
414  return feature ;
415 }
416 
417 
418 /*
419  * Local variables:
420  * c-basic-offset: 2
421  * End:
422  */
Class that defines the luminance and gradient of a point.
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:92
virtual ~vpFeatureLuminance()
Destructor.
bool * flags
Ensure that all the parameters needed to compute the iteraction matrix are set.
vpLuminance * pixInfo
Store the image (as a vector with intensity and gradient I, Ix, Iy)
void print(const unsigned int select=FEATURE_ALL) const
unsigned int bord
Border size.
void resize(const unsigned int nrows, const unsigned int ncols, const bool flagNullify=true)
Definition: vpArray2D.h:167
static double derivativeFilterY(const vpImage< T > &I, const unsigned int r, const unsigned int c)
void buildFrom(vpImage< unsigned char > &I)
#define vpERROR_TRACE
Definition: vpDebug.h:391
Class to define colors available for display functionnalities.
Definition: vpColor.h:121
void setCameraParameters(vpCameraParameters &_cam)
unsigned int dim_s
Dimension of the visual feature.
error that can be emited by ViSP classes.
Definition: vpException.h:73
static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)
Point coordinates conversion from pixel coordinates to normalized coordinates in meter...
double get_py() const
unsigned int nbr
Number of rows.
class that defines what is a visual feature
vpFeatureLuminance * duplicate() const
void set_Z(const double Z)
Class that defines the image luminance visual feature.
vpMatrix interaction(const unsigned int select=FEATURE_ALL)
vpFeatureLuminance & operator=(const vpFeatureLuminance &f)
Generic class defining intrinsic camera parameters.
vpColVector error(const vpBasicFeature &s_star, const unsigned int select=FEATURE_ALL)
unsigned int getRows() const
Return the number of rows of the 2D array.
Definition: vpArray2D.h:152
double get_px() const
vpCameraParameters cam
unsigned int nbc
Number of column.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
void display(const vpCameraParameters &cam, const vpImage< unsigned char > &I, const vpColor &color=vpColor::green, unsigned int thickness=1) const
static double derivativeFilterX(const vpImage< T > &I, const unsigned int r, const unsigned int c)
Definition: vpImageFilter.h:87
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:217