Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpMomentObject.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  * Object input structure used by moments.
32  *
33  * Authors:
34  * Filip Novotny
35  *
36  *****************************************************************************/
37 
38 #include <visp3/core/vpMomentBasic.h>
39 #include <visp3/core/vpMomentObject.h>
40 #include <visp3/core/vpCameraParameters.h>
41 #include <visp3/core/vpPixelMeterConversion.h>
42 #include <visp3/core/vpConfig.h>
43 #include <stdexcept>
44 
45 #include <cmath>
46 #include <limits>
47 
48 #ifdef VISP_HAVE_OPENMP
49 #include <omp.h>
50 #endif
51 #include <cassert>
52 
62 double vpMomentObject::calc_mom_polygon(unsigned int p, unsigned int q, const std::vector<vpPoint>& points)
63 {
64  unsigned int i,k,l;
65  double den,mom;
66  double x_p_k;
67  double y_l;
68  double y_q_l;
69 
70  den = static_cast<double>( (p+q+2)*(p+q+1)*vpMath::comb(p+q,p) );
71 
72  mom = 0.0;
73  for (i=1;i<=points.size()-1;i++)
74  {
75  double s = 0.0;
76  double x_k=1;
77  for (k=0;k<=p;k++)
78  {
79  y_l=1;
80  x_p_k = pow(points[i-1].get_x(), (int)(p-k));
81  for (l=0;l<=q;l++)
82  {
83  y_q_l=pow(points[i-1].get_y(), (int)(q-l));
84 
85  s += static_cast<double>(
86  vpMath::comb(k+l,l)
87  *vpMath::comb(p+q-k-l,q-l)
88  *x_k
89  *x_p_k
90  *y_l
91  *y_q_l );
92 
93  y_l*=points[i].get_y();
94 
95  }
96  x_k*=points[i].get_x();
97 
98  }
99 
100  s *= ((points[i-1].get_x())*(points[i].get_y())-(points[i].get_x())*(points[i-1].get_y()));
101  mom += s;
102  }
103  mom /= den;
104  return(mom);
105 }
106 
120 void vpMomentObject::cacheValues(std::vector<double>& cache,double x, double y){
121  cache[0]=1;
122 
123  for(unsigned int i=1;i<order;i++)
124  cache[i]=cache[i-1]*x;
125 
126  for(unsigned int j=order;j<order*order;j+=order)
127  cache[j]=cache[j-order]*y;
128 
129  for(unsigned int j=1;j<order;j++){
130  for(unsigned int i=1;i<order-j;i++){
131  cache[j*order+i] = cache[j*order]*cache[i];
132  }
133  }
134 }
135 
140 void vpMomentObject::cacheValues(std::vector<double>& cache,double x, double y, double IntensityNormalized) {
141 
142  cache[0]=IntensityNormalized;
143 
144  double invIntensityNormalized = 0.;
145  if (std::fabs(IntensityNormalized)>=std::numeric_limits<double>::epsilon())
146  invIntensityNormalized = 1.0/IntensityNormalized;
147 
148  for(unsigned int i=1;i<order;i++)
149  cache[i]=cache[i-1]*x;
150 
151  for(unsigned int j=order;j<order*order;j+=order)
152  cache[j]=cache[j-order]*y;
153 
154  for(unsigned int j=1;j<order;j++){
155  for(unsigned int i=1;i<order-j;i++){
156  cache[j*order+i] = cache[j*order]*cache[i]*invIntensityNormalized;
157  }
158  }
159 }
160 
161 
234 void vpMomentObject::fromVector(std::vector<vpPoint>& points){
236  if(std::fabs(points.rbegin()->get_x()-points.begin()->get_x())>std::numeric_limits<double>::epsilon() ||
237  std::fabs(points.rbegin()->get_y()-points.begin()->get_y())>std::numeric_limits<double>::epsilon()){
238  points.resize(points.size()+1);
239  points[points.size()-1] = points[0];
240  }
241  for(unsigned int j=0;j<order*order;j++){
242  values[j]=calc_mom_polygon(j%order,j/order,points);
243  }
244  } else {
245  std::vector<double> cache(order*order,0.);
246  values.assign(order*order,0);
247  for(unsigned int i=0;i<points.size();i++){
248  cacheValues(cache,points[i].get_x(),points[i].get_y());
249  for(unsigned int j=0;j<order;j++){
250  for(unsigned int k=0;k<order-j;k++){
251  values[j*order+k]+=cache[j*order+k];
252  }
253  }
254  }
255  }
256 }
257 
287 void vpMomentObject::fromImage(const vpImage<unsigned char>& image, unsigned char threshold, const vpCameraParameters& cam){
288 #ifdef VISP_HAVE_OPENMP
289  #pragma omp parallel shared(threshold)
290  {
291  std::vector<double> curvals(order*order);
292  curvals.assign(order*order,0.);
293 
294  #pragma omp for nowait//automatically organize loop counter between threads
295  for(int i=0;i<(int)image.getCols();i++){
296  for(int j=0;j<(int)image.getRows();j++){
297  unsigned int i_ = static_cast<unsigned int>(i);
298  unsigned int j_ = static_cast<unsigned int>(j);
299  if(image[j_][i_]>threshold){
300  double x=0;
301  double y=0;
303 
304  double yval=1.;
305  for(unsigned int k=0;k<order;k++){
306  double xval=1.;
307  for(unsigned int l=0;l<order-k;l++){
308  curvals[(k*order+l)]+=(xval*yval);
309  xval*=x;
310  }
311  yval*=y;
312  }
313  }
314  }
315  }
316 
317  #pragma omp master //only set this variable in master thread
318  {
319  values.assign(order*order, 0.);
320  }
321 
322  #pragma omp barrier
323  for(unsigned int k=0;k<order;k++){
324  for(unsigned int l=0;l<order-k;l++){
325  #pragma omp atomic
326  values[k*order+l]+= curvals[k*order+l];
327  }
328  }
329 
330  }
331 #else
332  std::vector<double> cache(order*order,0.);
333  values.assign(order*order,0);
334  for(unsigned int i=0;i<image.getCols();i++){
335  for(unsigned int j=0;j<image.getRows();j++){
336  if(image[j][i]>threshold){
337  double x=0;
338  double y=0;
340  cacheValues(cache,x,y);
341  for(unsigned int k=0;k<order;k++){
342  for(unsigned int l=0;l<order-k;l++){
343  values[k*order+l]+=cache[k*order+l];
344  }
345  }
346  }
347  }
348  }
349 #endif
350 
351  //Normalisation equivalent to sampling interval/pixel size delX x delY
352  double norm_factor = 1./(cam.get_px()*cam.get_py());
353  for (std::vector<double>::iterator it = values.begin(); it!=values.end(); ++it) {
354  *it = (*it) * norm_factor;
355  }
356 }
357 
369  vpCameraImgBckGrndType bg_type, bool normalize_with_pix_size)
370 {
371  std::vector<double> cache(order*order,0.);
372  values.assign(order*order,0);
373 
374  // (x,y) - Pixel co-ordinates in metres
375  double x=0;
376  double y=0;
377  //for indexing into cache[] and values[]
378  unsigned int idx = 0;
379  unsigned int kidx = 0;
380 
381  double intensity = 0;
382 
383  //double Imax = static_cast<double>(image.getMaxValue());
384 
385  double iscale = 1.0;
386  if (flg_normalize_intensity) { // This makes the image a probability density function
387  double Imax = 255.; // To check the effect of gray level change. ISR Coimbra
388  iscale = 1.0/Imax;
389  }
390 
391  if (bg_type == vpMomentObject::WHITE) {
393  for(unsigned int j=0;j<image.getRows();j++){
394  for(unsigned int i=0;i<image.getCols();i++){
395  x = 0;
396  y = 0;
397  intensity = (double)(image[j][i])*iscale;
398  double intensity_white = 1. - intensity;
399 
401  cacheValues(cache,x,y, intensity_white); // Modify 'cache' which has x^p*y^q to x^p*y^q*(1 - I(x,y))
402 
403  // Copy to "values"
404  for(unsigned int k=0;k<order;k++){
405  kidx = k*order;
406  for(unsigned int l=0;l<order-k;l++){
407  idx = kidx+l;
408  values[idx]+= cache[idx];
409  }
410  }
411  }
412  }
413  }
414  else {
416  for(unsigned int j=0;j<image.getRows();j++){
417  for(unsigned int i=0;i<image.getCols();i++){
418  x = 0;
419  y = 0;
420  intensity = (double)(image[j][i])*iscale;
422 
423  // Cache values for fast moment calculation
424  cacheValues(cache,x,y, intensity); // Modify 'cache' which has x^p*y^q to x^p*y^q*I(x,y)
425 
426  // Copy to moments array 'values'
427  for(unsigned int k=0;k<order;k++){
428  kidx = k*order;
429  for(unsigned int l=0;l<order-k;l++){
430  idx = kidx+l;
431  values[idx]+= cache[idx];
432  }
433  }
434 
435  }
436  }
437  }
438 
439  if (normalize_with_pix_size){
440  // Normalisation equivalent to sampling interval/pixel size delX x delY
441  double norm_factor = 1./(cam.get_px()*cam.get_py());
442  for (std::vector<double>::iterator it = values.begin(); it!=values.end(); ++it) {
443  *it = (*it) * norm_factor;
444  }
445  }
446 }
447 
452 void
453 vpMomentObject::init(unsigned int orderinp) {
454  order = orderinp + 1;
456  flg_normalize_intensity = true; // By default, the intensity values are normalized
457  values.resize((order+1)*(order+1));
458  values.assign((order+1)*(order+1),0);
459 }
460 
464 void
466  order = objin.getOrder()+1;
467  type = objin.getType();
469  values.resize(objin.values.size());
470  values = objin.values;
471 }
472 
486 vpMomentObject::vpMomentObject(unsigned int max_order)
487  : flg_normalize_intensity(true), order(max_order+1), type(vpMomentObject::DENSE_FULL_OBJECT),
488  values()
489 {
490  init(max_order);
491 }
492 
497  : flg_normalize_intensity(true), order(1), type(vpMomentObject::DENSE_FULL_OBJECT),
498  values()
499 {
500  init(srcobj);
501 }
502 
522 const std::vector<double>& vpMomentObject::get() const {
523  return values;
524 
525 }
526 
533 double vpMomentObject::get(unsigned int i, unsigned int j) const {
534  assert(i+j<=getOrder());
535  if(i+j>=order) throw vpException(vpException::badValue,"The requested value has not been computed, you should specify a higher order.");
536 
537  return values[j*order+i];
538 }
539 
546 void vpMomentObject::set(unsigned int i, unsigned int j, const double& value_ij){
547  assert(i+j<=getOrder());
548  if(i+j>=order) throw vpException(vpException::badValue,"The requested value cannot be set, you should specify a higher order for the moment object.");
549  values[j*order+i] = value_ij;
550 }
551 
567 VISP_EXPORT std::ostream & operator<<(std::ostream & os, const vpMomentObject& m){
568  for(unsigned int i = 0;i<m.values.size();i++){
569 
570  if(i%(m.order)==0)
571  os << std::endl;
572 
573  if((i%(m.order)+i/(m.order))<m.order)
574  os << m.values[i] ;
575  else
576  os << "x";
577 
578  os << "\t";
579 
580  }
581 
582  return os;
583 }
584 
589 void
590 vpMomentObject::printWithIndices(const vpMomentObject& momobj, std::ostream& os) {
591  std::vector<double> moment = momobj.get();
592  os << std::endl <<"Order of vpMomentObject: "<<momobj.getOrder()<<std::endl;
593  // Print out values. This is same as printing using operator <<
594  for(unsigned int k=0; k<=momobj.getOrder(); k++) {
595  for(unsigned int l=0; l<(momobj.getOrder()+1)-k; l++){
596  os << "m[" << l << "," << k << "] = " << moment[k*(momobj.getOrder()+1)+ l] << "\t";
597  }
598  os << std::endl;
599  }
600  os <<std::endl;
601 }
602 
629 vpMatrix
631  std::vector<double> moment = momobj.get();
632  unsigned int order = momobj.getOrder();
633  vpMatrix M(order+1, order+1);
634  for(unsigned int k=0; k<=order; k++) {
635  for(unsigned int l=0; l<(order+1)-k; l++){
636  M[l][k] = moment[k*(order+1)+ l];
637  }
638  }
639  return M;
640 }
641 
650 // deliberate empty
651 }
unsigned int getCols() const
Definition: vpImage.h:166
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:97
vpMomentObject(unsigned int order)
static vpMatrix convertTovpMatrix(const vpMomentObject &momobj)
static void printWithIndices(const vpMomentObject &momobj, std::ostream &os)
error that can be emited by ViSP classes.
Definition: vpException.h:73
Class for generic objects.
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...
const std::vector< double > & get() const
double get_py() const
virtual ~vpMomentObject()
void fromImage(const vpImage< unsigned char > &image, unsigned char threshold, const vpCameraParameters &cam)
void init(unsigned int orderinp)
unsigned int getRows() const
Definition: vpImage.h:204
void cacheValues(std::vector< double > &cache, double x, double y)
void set(unsigned int i, unsigned int j, const double &value_ij)
Generic class defining intrinsic camera parameters.
bool flg_normalize_intensity
unsigned int order
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:267
void fromVector(std::vector< vpPoint > &points)
double get_px() const
std::vector< double > values
vpObjectType getType() const
vpObjectType type
static long double comb(unsigned int n, unsigned int p)
Definition: vpMath.h:234
unsigned int getOrder() const