ViSP  2.8.0
vpMomentCInvariant.cpp
1 /****************************************************************************
2  *
3  * $Id: vpMomentCInvariant.cpp 4056 2013-01-05 13:04:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 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  * Descriptor for various invariants used to drive space roations around X and Y axis.
36  *
37  * Authors:
38  * Filip Novotny
39  *
40  *****************************************************************************/
41 
42 #include <visp/vpMomentCInvariant.h>
43 #include <visp/vpMomentCentered.h>
44 #include <visp/vpMomentObject.h>
45 
50  values.resize(14);
51 }
52 
58 void vpMomentCInvariant::computeI(vpMomentCentered& momentCentered, std::vector<double>& I){
59  double mu30 = momentCentered.get(3,0);
60  double mu30_2 = mu30*mu30;
61 
62  double mu03 = momentCentered.get(0,3);
63  double mu03_2 = mu03*mu03;
64  double mu03_3 = mu03*mu03_2;
65 
66  double mu20 = momentCentered.get(2,0);
67  double mu02 = momentCentered.get(0,2);
68  double mu50 = momentCentered.get(5,0);
69  double mu32 = momentCentered.get(3,2);
70  double mu14 = momentCentered.get(1,4);
71  double mu05 = momentCentered.get(0,5);
72  double mu23 = momentCentered.get(2,3);
73  double mu41 = momentCentered.get(4,1);
74  double mu40 = momentCentered.get(4,0);
75  double mu04 = momentCentered.get(0,4);
76  double mu31 = momentCentered.get(3,1);
77  double mu13 = momentCentered.get(1,3);
78  double mu22 = momentCentered.get(2,2);
79  double mu21 = momentCentered.get(2,1);
80  double mu12 = momentCentered.get(1,2);
81  double mu11 = momentCentered.get(1,1);
82 
83 
84  double mu11_2 = mu11*mu11;
85  double mu12_2 = mu12*mu12;
86  double mu21_2 = mu21*mu21;
87  double mu22_2 = mu22*mu22;
88  double mu13_2 = mu13*mu13;
89  double mu31_2 = mu31*mu31;
90  double mu04_2 = mu04*mu04;
91  double mu40_2 = mu40*mu40;
92  double mu21_3 = mu21*mu21_2;
93  double mu12_3 = mu12_2*mu12;
94  double mu12_4 = mu12_3*mu12;
95  double mu21_4 = mu21_2*mu21_2;
96 
97 
98  double kappa = mu30_2+mu03_2-3*mu21_2+6*mu21*mu03;
99  double zeta = mu20-mu02;
100  double zeta_2 = zeta * zeta;
101  double omicron = (mu03_2+3*mu03*mu21+mu30*(mu30+3*mu12));
102  double omega = mu50+2*mu32+mu14;
103  double nu = mu05+2*mu23+mu41;
104  double ro = mu50-2*mu32-3*mu14;
105  double gamma = mu05-2*mu23-3*mu41;
106 
107  double delta = mu50-10*mu32+5*mu14;
108  double phi = mu05-10*mu23+5*mu41;
109  double omega_2 = omega*omega;
110  double nu_2 = nu*nu;
111  double ro_2 = ro*ro;
112  double gamma_2 = gamma*gamma;
113  double delta_2 = delta*delta;
114  double phi_2 = phi*phi;
115 
116  I[1]=-mu20*mu02+mu11_2;
117  I[2]=zeta_2+4*mu11_2;
118  I[3]=(mu30-3*mu12)*(mu30-3*mu12)+(mu03-3*mu21)*(mu03-3*mu21);
119  I[4]=(mu30+mu12)*(mu30+mu12)+(mu21+mu03)*(mu21+mu03);
120  I[5]=-mu30_2*mu03_2+(-4*mu12_3+6*mu21*mu12*mu03)*mu30-4*mu21_3*mu03+3*mu21_2*mu12_2;
121  I[6]=3*mu12_4+2*mu30*mu12_3+(3*mu30_2-6*mu03*mu21)*mu12_2-6*mu30*mu21*(mu21+mu03)*mu12+2*mu30_2*mu03_2+2*mu21_3*mu03+3*mu21_2*mu03_2+3*mu21_4;
122 
123  I[7]=(3*mu21+2*mu03)*mu12_3+3*mu30*(mu03+2*mu21)*mu12_2-3*mu21*(mu30+mu03+mu21)*(-mu30+mu03+mu21)*mu12+mu30*(-mu30_2*mu03-2*mu21_3-3*mu03*mu21_2+mu03_3);
124  I[8]=3*mu21_4-3*mu21_3*mu03+(3*mu03_2+kappa-6*mu12_2)*mu21_2-mu03*(-15*mu12_2+kappa)*mu21-(-3*mu12_2*mu30+(2*kappa-3*mu03_2)*mu12+kappa*mu30)*mu12;
125  I[9]=omicron*omicron;
126 
127  I[10]=mu40*mu04-4*mu31*mu13+3*mu22_2;
128  I[11]=3*mu13_2+2*mu31*mu13+(-3*mu40-3*mu04)*mu22-2*mu40*mu04+3*mu31_2;
129  I[12]=3*mu04_2+(2*mu40+12*mu22)*mu04+3*mu40_2+12*mu40*mu22+16*mu31*mu13;
130  I[13]=omega_2+nu_2;
131  I[14]=ro_2+gamma_2;
132  I[15]=delta_2+phi_2;
133 
134  double a;
136  a = momentCentered.get(2,0)+momentCentered.get(0,2);
137  else
138  a = getObject().get(0,0);
139 
140  c[1]=momentCentered.get(2,0)-momentCentered.get(0,2);
141  s[1]=2*momentCentered.get(1,1);
142  c[2]=momentCentered.get(0,3)-3*momentCentered.get(2,1);
143  s[2]=momentCentered.get(3,0)-3*momentCentered.get(1,2);
144  c[3]=c[1]*c[1]-s[1]*s[1];
145  s[3]=2*s[1]*c[1];
146 
147  II[1]=c[1]*c[1]+s[1]*s[1];
148  II[2]=c[2]*c[2]+s[2]*s[2];
149  II[3]=momentCentered.get(2,0)+momentCentered.get(0,2);
150 
151  K=(II[1]*(II[3]*sqrt(II[3])))/sqrt(a);
152 
153 
154 
155 }
156 
163  if(getObject().getOrder()<5) throw vpException(vpException::notInitialized,"Order is not high enough for vpMomentCInvariant. Specify at least order 5.");
164  bool found_moment_centered;
165  vpMomentCentered& momentCentered = (static_cast<vpMomentCentered&>(getMoments().get("vpMomentCentered",found_moment_centered)));
166 
167  if(!found_moment_centered) throw vpException(vpException::notInitialized,"vpMomentCentered not found");
168 
169  computeI(momentCentered,I);
170  double II3_2 = II[3]*II[3];
171  double II3_3 = II3_2*II[3];
172 
173  double a;
174  if(getObject().getType()==vpMomentObject::DISCRETE)
175  a = momentCentered.get(2,0)+momentCentered.get(0,2);
176  else
177  a = getObject().get(0,0);
178 
179  values[0] = I[1]/I[2];
180  values[1] = I[3]/I[4];
181 
182  values[2] = I[5]/I[6];
183 
184  values[3] = I[7]/I[6];
185 
186  values[4] = I[8]/I[6];
187 
188  values[5] = I[9]/I[6];
189 
190  values[6] = I[11]/I[10];
191 
192  values[7] = I[12]/I[10];
193 
194  values[8] = I[13]/I[15];
195 
196  values[9] = I[14]/I[15];
197 
198  values[10] = (c[2]*c[3]+s[2]*s[3])/K;
199  values[11] = (s[2]*c[3]-c[2]*s[3])/K;
200 
201  values[12] = II[1]/(II3_2);
202  values[13] = a*II[2]/(II3_3);
203 }
204 
209 void vpMomentCInvariant::printI(unsigned int index){
210  std::cout << "I("<< index << ")=" << I[index] << std::endl;
211 }
212 
216 std::ostream & operator<<(std::ostream & os, const vpMomentCInvariant& c){
217  for(unsigned int i = 0;i<c.values.size();i++){
218  os << c.values[i] << "," << std::endl;
219  }
220 
221  return os;
222 }
223 
224 
225 
226 
vpMomentObject & getObject() const
Definition: vpMoment.h:119
vpMomentDatabase & getMoments()
Definition: vpMoment.h:115
double get(unsigned int i, unsigned int j)
error that can be emited by ViSP classes.
Definition: vpException.h:75
vpMoment & get(const char *type, bool &found)
This class defines shared methods/attributes for 2D moments.
Definition: vpMoment.h:104
std::vector< double > & get()
This class defines the double-indexed centered moment descriptor .
VISP_EXPORT std::ostream & operator<<(std::ostream &os, const vpImagePoint &ip)
Definition: vpImagePoint.h:529
vpObjectType getType() const
This class defines several 2D (translation+rotation+scale) invariants for both symmetric and non-symm...
std::vector< double > values
Definition: vpMoment.h:110
void printI(unsigned int index)