ViSP  2.9.0
vpFeatureMomentAlpha.cpp
1 /****************************************************************************
2  *
3  * $Id: vpFeatureMomentImpl.cpp 3317 2011-09-06 14:14:47Z fnovotny $
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  * Implementation for alpha moment features.
36  *
37  * Authors:
38  * Filip Novotny
39  *
40  *****************************************************************************/
41 
42 #include <visp/vpMomentObject.h>
43 #include <visp/vpMomentGravityCenter.h>
44 #include <visp/vpFeatureMomentAlpha.h>
45 #include <visp/vpMomentCentered.h>
46 #include <visp/vpFeatureMomentCentered.h>
47 #include <visp/vpFeatureMomentDatabase.h>
48 
49 #include <vector>
50 #include <limits>
51 
52 #ifdef VISP_MOMENTS_COMBINE_MATRICES
53 
62  bool found_moment_centered;
63  bool found_FeatureMoment_centered;
64 
65  const vpMomentCentered& momentCentered = (static_cast<const vpMomentCentered&>(moments.get("vpMomentCentered",found_moment_centered)));
66  vpFeatureMomentCentered& featureMomentCentered = (static_cast<vpFeatureMomentCentered&>(featureMomentsDataBase->get("vpFeatureMomentCentered",found_FeatureMoment_centered)));
67 
68  if(!found_moment_centered) throw vpException(vpException::notInitialized,"vpMomentCentered not found");
69  if(!found_FeatureMoment_centered) throw vpException(vpException::notInitialized,"vpFeatureMomentCentered not found");
70 
71  double multiplier = -1./(momentCentered.get(2,0)*momentCentered.get(2,0)-2*momentCentered.get(0,2)*momentCentered.get(2,0)+4*momentCentered.get(1,1)*momentCentered.get(1,1)+momentCentered.get(0,2)*momentCentered.get(0,2));
72 
73  interaction_matrices[0].resize(1,6);
74  interaction_matrices[0] = multiplier*(momentCentered.get(1,1)*featureMomentCentered.interaction(2,0)+(momentCentered.get(0,2)-momentCentered.get(2,0))*featureMomentCentered.interaction(1,1)-momentCentered.get(1,1)*featureMomentCentered.interaction(0,2));
75 }
76 
77 #else
78 
87  bool found_moment_centered;
88  bool found_moment_gravity;
89 
90  const vpMomentCentered& momentCentered = static_cast<const vpMomentCentered&>(moments.get("vpMomentCentered",found_moment_centered));
91  const vpMomentGravityCenter& momentGravity = static_cast<const vpMomentGravityCenter&>(moments.get("vpMomentGravityCenter",found_moment_gravity));
92  const vpMomentObject& momentObject = moment->getObject();
93 
94 
95  if(!found_moment_centered) throw vpException(vpException::notInitialized,"vpMomentCentered not found");
96  if(!found_moment_gravity) throw vpException(vpException::notInitialized,"vpMomentGravityCenter not found");
97 
98  double mu11 = momentCentered.get(1,1);
99  double mu20 = momentCentered.get(2,0);
100  double mu02 = momentCentered.get(0,2);
101  double mu12 = momentCentered.get(1,2);
102  double mu21 = momentCentered.get(2,1);
103  double mu03 = momentCentered.get(0,3);
104  double mu30 = momentCentered.get(3,0);
105 
106  double Xg = momentGravity.getXg();
107  double Yg = momentGravity.getYg();
108 
109  double Avx,Avy,Avz,Awx,Awy;
110  double beta,gamma;
111 
112  if (momentObject.getType()==vpMomentObject::DISCRETE){
113  beta = 4;
114  gamma = 2;
115  } else {
116  beta = 5;
117  gamma = 1;
118  }
119 
120  double d = (mu20-mu02)*(mu20-mu02) + 4 * mu11*mu11;
121  double DA = mu20+mu02;
122  double DA_2 = DA*DA;
123  double mu11_2 = mu11*mu11;
124 
125  Avx = mu11*DA*A/d+(DA*mu02+(0.5)*d-(0.5)*DA_2)*B/d;
126  Avy = (DA*mu02-(0.5)*d-(.5)*DA_2)*A/d-B*mu11*DA/d;
127 
128  Awx = (beta*(mu12*(mu20-mu02)+mu11*(mu03-mu21))+gamma*Xg*(mu02*(mu20-mu02)-2*mu11_2)+gamma*Yg*mu11*(mu20+mu02))/d;
129  Awy = (beta*(mu21*(mu02-mu20)+mu11*(mu30-mu12))+gamma*Xg*mu11*(mu20+mu02)+gamma*Yg*(mu20*(mu02-mu20)-2*mu11_2))/d;
130 
131  Avz = B*Awx-A*Awy;
132  interaction_matrices.resize(1);
133  interaction_matrices[0].resize(1,6);
134 
135  int VX = 0;
136  int VY = 1;
137  int VZ = 2;
138  int WX = 3;
139  int WY = 4;
140  int WZ = 5;
141 
142  interaction_matrices[0][0][VX] = Avx;
143  interaction_matrices[0][0][VY] = Avy;
144  interaction_matrices[0][0][VZ] = Avz;
145 
146  interaction_matrices[0][0][WX] = Awx;
147  interaction_matrices[0][0][WY] = Awy;
148  interaction_matrices[0][0][WZ] = -1.;
149 }
150 
151 vpColVector vpFeatureMomentAlpha::error (const vpBasicFeature &s_star, const unsigned int /* select */){
152  vpColVector e(0) ;
153  double err = s[0] - s_star[0] ;
154 
155  if (err < -M_PI) err += 2*M_PI ;
156  if (err > M_PI) err -= 2*M_PI ;
157 
158  vpColVector ecv(1) ;
159  ecv[0] = err ;
160  e = vpMatrix::stackMatrices(e,ecv) ;
161 
162  return e;
163 }
164 #endif
const vpMoment * moment
Functionality computation for centered moment feature. Computes the interaction matrix associated wit...
error that can be emited by ViSP classes.
Definition: vpException.h:76
Class for generic objects.
std::vector< vpMatrix > interaction_matrices
vpColVector error(const vpBasicFeature &s_star, const unsigned int select=FEATURE_ALL)
Compute the error between two visual features from a subset of the possible features.
vpMatrix interaction(unsigned int select_one, unsigned int select_two) const
class that defines what is a visual feature
const vpMoment & get(const char *type, bool &found) const
vpMomentDatabase & moments
This class defines the double-indexed centered moment descriptor .
void stackMatrices(const vpMatrix &A)
Definition: vpMatrix.cpp:3003
double get(unsigned int i, unsigned int j) const
Class describing 2D gravity center moment.
vpObjectType getType() const
vpFeatureMomentDatabase * featureMomentsDataBase
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
const vpMomentObject & getObject() const
Definition: vpMoment.h:123
vpFeatureMoment & get(const char *type, bool &found)
vpColVector s
State of the visual feature.