Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
MatchDataBase.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 #include <Base/Common/BIASpragma.hh>
26 
27 #include "MatchDataBase.hh"
28 
29 #include <fstream>
30 
31 using namespace BIAS;
32 using namespace std;
33 
34 
35 
37  : Debug()
38 {
40  _AddParameter(para);
41 }
42 
44 {}
45 
47 {
48 
49  // clear K, R, C and P
50  _K.SetZero();
51  _Ki.SetZero();
52  _R.clear();
53  _C.clear();
54  _P.clear();
55  // now clear 2D points
56  _Points.clear();
57  _NormalizedPoints.clear();
58 }
59 
61 { K = _K; }
62 
63 void MatchDataBase::GetR(int imNo, RMatrixBase& R) const
64 { R = _R[imNo]; }
65 void MatchDataBase::GetR(std::vector<RMatrixBase>& R) const
66 { R = _R; }
67 
68 
69 void MatchDataBase::GetC(int imNo, HomgPoint3D& C) const
70 { C = _C[imNo]; }
71 void MatchDataBase::GetC(std::vector<HomgPoint3D>& C) const
72 { C = _C; }
73 
74 void MatchDataBase::GetP(int imNo, PMatrixBase& P) const
75 { P = _P[imNo]; }
76 void MatchDataBase::GetP(std::vector<PMatrixBase>& P) const
77 { P = _P; }
78 
79 void MatchDataBase::GetPoints(int imNo, std::vector<HomgPoint2D>& p) const
80 { p = _Points[imNo]; }
81 void MatchDataBase::GetPoints(std::vector<std::vector<HomgPoint2D> >& p) const
82 { p = _Points; }
83 void MatchDataBase::GetCorrespondences(vector<vector<HomgPoint2D> >& p) const
84 { _Transpose(_Points, p); }
85 
86 void MatchDataBase::GetNormalizedPoints(int imNo, std::vector<HomgPoint2D>& p) const
87 { p = _NormalizedPoints[imNo]; }
88 void MatchDataBase::GetNormalizedPoints(vector<vector<HomgPoint2D> >& p) const
89 { p = _NormalizedPoints; }
90 void MatchDataBase::GetNormalizedCorrespondences(vector<vector<HomgPoint2D> >& p) const
92 
93 void MatchDataBase::SetData(KMatrix& K, std::vector<RMatrixBase>& R,
94  std::vector<HomgPoint3D>& C,
95  std::vector<std::vector<HomgPoint2D> >& m)
96 {
97  BIASASSERT(m.size()==R.size());
98  BIASASSERT(m.size()>0);
99  *_NumImages=R.size();
100  *_NumPoints=m[0].size();
101  _SetCamData(K, R, C);
102  _Points.resize(*_NumImages);
103  _NormalizedPoints.resize(*_NumImages);
104  for (int i=0; i<*_NumImages; i++){
105  _Points[i].resize(*_NumPoints);
106  _NormalizedPoints[i].resize(*_NumPoints);
107  for (int p=0; p<*_NumPoints; p++){
108  _Points[i][p]=m[i][p];
109  _Points[i][p].Homogenize();
110  _NormalizedPoints[i][p]=_Ki*_Points[i][p];
111  }
112  }
113 }
114 
115 
116 void MatchDataBase::SetNormalizedData(KMatrix& K, std::vector<RMatrixBase>& R,
117  std::vector<HomgPoint3D>& C,
118  std::vector<std::vector<HomgPoint2D> >& m)
119 {
120  BIASASSERT(m.size()==R.size());
121  BIASASSERT(m.size()>0);
122  *_NumImages=R.size();
123  *_NumPoints=m[0].size();
124  _SetCamData(K, R, C);
125  _Points.resize(*_NumImages);
126  _NormalizedPoints.resize(*_NumImages);
127  for (int i=0; i<*_NumImages; i++){
128  _Points[i].resize(*_NumPoints);
129  _NormalizedPoints[i].resize(*_NumPoints);
130  for (int p=0; p<*_NumPoints; p++){
131  _NormalizedPoints[i][p]=m[i][p];
132  _NormalizedPoints[i][p].Homogenize();
133  _Points[i][p]=_K*_NormalizedPoints[i][p];
134  }
135  }
136 }
137 
138 
139 void MatchDataBase::_SetCamData(KMatrix& K, std::vector<RMatrixBase>& R,
140  std::vector<HomgPoint3D>& C)
141 {
142  _K=K;
143  _Ki=_K.Invert();
144  _R=R;
145  _C=C;
146  BIASASSERT(_R.size() == _C.size());
147  _P.resize(_R.size());
148  for (unsigned i=0; i<_R.size(); i++){
149  _P[i].Compose(_K, _R[i], _C[i]);
150  }
151 }
152 
153 int MatchDataBase::Write(const string& fname) const
154 {
155  ofstream os(fname.c_str());
156  if (!os){
157  BIASERR("error opening "<<fname);
158  return -1;
159  }
160  int res=Write(os);
161 
162  os.close();
163  return res;
164 }
165 
166 
167 int MatchDataBase::Write(ostream &os) const
168 {
169  BIASCDOUT(D_MD_IO,"using MatchDataBase::Write(ostream)\n");
170  BIASCDOUT(D_MD_IO,"MatchDataBase::Write() writing "<<*_NumImages<<" images with "<<*_NumPoints<<" points in each\n");
171  os.write((char*)_NumImages, sizeof(int));
172 
173  os.write((char*)_NumPoints, sizeof(int));
174 
175  os.write((char*)_K.GetData(), sizeof(KMATRIX_TYPE)*9);
176 
177  for (int i=0; i<*_NumImages; i++){
178  os.write((char*)_R[i].GetData(), sizeof(ROTATION_MATRIX_TYPE)*9);
179  os.write((char*)_C[i].GetData(), sizeof(HOMGPOINT3D_TYPE)*4);
180  for (int k=0; k<*_NumPoints; k++){
181  os.write((char*)_Points[i][k].GetData(), sizeof(HOMGPOINT2D_TYPE)*3);
182  }
183  }
184 
185  if (!os){
186  BIASERR("error writing data");
187  return -2;
188  }
189  return 0;
190 }
191 
192 
193 int MatchDataBase::Read(const string& fname)
194 {
195  ifstream is(fname.c_str());
196  if (!is){
197  BIASERR("error opening "<<fname);
198  return -1;
199  }
200  int res=Read(is);
201 
202  is.close();
203  return res;
204 }
205 
206 int MatchDataBase::Read(istream& is)
207 {
208  BIASCDOUT(D_MD_IO, "using MatchDataBase::Read(ostream)\n");
209  is.read((char*)_NumImages, sizeof(int));
210  is.read((char*)_NumPoints, sizeof(int));
211 
212  is.read((char*)_K.GetData(), sizeof(KMATRIX_TYPE)*9);
213  _Ki=_K.Invert();
214 
215  _Points.resize(*_NumImages);
216  _NormalizedPoints.resize(*_NumImages);
217  _R.resize(*_NumImages);
218  _C.resize(*_NumImages);
219  _P.resize(*_NumImages);
220  for (int i=0; i<*_NumImages; i++){
221  is.read((char*)_R[i].GetData(), sizeof(ROTATION_MATRIX_TYPE)*9);
222  is.read((char*)_C[i].GetData(), sizeof(HOMGPOINT3D_TYPE)*4);
223  _P[i].Compose(_K, _R[i], _C[i]);
224  _Points[i].resize(*_NumPoints);
225  _NormalizedPoints[i].resize(*_NumPoints);
226  for (int k=0; k<*_NumPoints; k++){
227  is.read((char*)_Points[i][k].GetData(), sizeof(HOMGPOINT2D_TYPE)*3);
228  _NormalizedPoints[i][k]=_Ki*_Points[i][k];
229  }
230  }
231 
232  if (!is){
233  BIASERR("error reading data");
234  return -2;
235  }
236  BIASCDOUT(D_MD_IO,"MatchDataBase::Read() read "<<*_NumImages
237  <<" images with "<<*_NumPoints<<" points in each\n");
238  return 0;
239 }
240 
241 
243 {
244  bool res=(*_NumImages==*r._NumImages) && (*_NumPoints==*r._NumPoints);
245  res=res && (_K == r._K) && (_Ki == r._Ki);
246  if (res){
247  for (int i=0; i<*_NumImages; i++){
248  res = res && (_R[i] == r._R[i]) && (_C[i] == r._C[i]) &&
249  (_P[i] == r._P[i]);
250  if (!res) cerr <<" R/C/P\n";
251  for (int k=0; k<*_NumPoints; k++){
252  res = res && (_Points[i][k]==r._Points[i][k]) &&
253  (_NormalizedPoints[i][k]==r._NormalizedPoints[i][k]);
254  if (!res) {
255  cerr <<"_Points/_NormalizedPoints ["<<k<<"]\n";
256  cerr << _Points[i][k]<<" - "<<r._Points[i][k]<<" = "
257  <<_Points[i][k]-r._Points[i][k]<<endl;
258  cerr << _NormalizedPoints[i][k]<<" <-> "<<r._NormalizedPoints[i][k]
259  <<" = "<<_NormalizedPoints[i][k]-r._NormalizedPoints[i][k]
260  <<endl;
261  break;
262  }
263  }
264  if (!res) break;
265  }
266  } else {
267  cerr <<" Num/K/Ki\n";
268  }
269  return res;
270 }
271 
273 {
274  ////////////////////////////////////////////////////////////
275  // PGNum
276  ///////////////////////////////////////////////////////////
277 
278  int grp;
279  if (para.CheckParam("NumStaticPoints")){
280  grp=para.GetGroupID("NumStaticPoints");
281  } else {
282  grp=para.GetFreeGroupID();
283  }
284  // cerr << "MatchDataBase: using ID "<<grp<<" for group num\n";
285 
286  if (para.CheckParam("NumImages")){
287  _NumImages=para.GetParamInt("NumImages");
288  } else {
289  _NumImages=para.AddParamInt("NumImages", "number of images", 2, 2,
290  numeric_limits<int>::max(), 0, grp);
291  }
292 
293  if (para.CheckParam("NumPoints")){
294  _NumPoints=para.GetParamInt("NumPoints");
295  } else {
296  _NumPoints=para.AddParamInt("NumPoints", "number of points in every image",
297  30, 0, numeric_limits<int>::max(), 0, grp);
298  }
299 
300 }
301 
302 /////////////////////////////////////////////////////////////////////////////
303 // operators
304 /////////////////////////////////////////////////////////////////////////////
305 
306 ostream& BIAS::operator<<(ostream& os, const MatchDataBase& m)
307 {
308  os << "K: "<<m._K<<endl;
309  os << "P: \n";
310  for (int i=0; i<*m._NumImages; i++){
311  os << setw(3) << i << " : " << m._P[i]<<endl;
312  }
313  os << "_Points: \n";
314  for (int k=0; k<*m._NumPoints; k++){
315  os << setw(3) << k << " : ";
316  for (int i=0; i<*m._NumImages; i++){
317  os <<i<<":"<<m._Points[i][k] <<" ";
318  }
319  os << endl;
320  }
321  os << "_NormalizedPoints: \n";
322  for (int k=0; k<*m._NumPoints; k++){
323  os << setw(3) << k << " : ";
324  for (int i=0; i<*m._NumImages; i++){
325  os <<i<<":"<<m._NormalizedPoints[i][k] <<" ";
326  }
327  os << endl;
328  }
329  return os;
330 }
331 
332 istream& BIAS::operator>>(istream& is, MatchDataBase& m)
333 {
334  BIASERR("unfinished");
335  return is;
336 }
337 
338 
void _SetCamData(KMatrix &K, std::vector< RMatrixBase > &R, std::vector< HomgPoint3D > &C)
std::vector< std::vector< HomgPoint2D > > _NormalizedPoints
int Read(const std::string &fname)
binary read
void GetR(int imNo, RMatrixBase &R) const
std::vector< PMatrixBase > _P
void GetK(KMatrix &K) const
MatchDataBase(Param &para)
void SetNormalizedData(KMatrix &K, std::vector< RMatrixBase > &R, std::vector< HomgPoint3D > &C, std::vector< std::vector< HomgPoint2D > > &m)
void GetNormalizedCorrespondences(std::vector< std::vector< HomgPoint2D > > &p) const
same as GetNormalizedPoints with inverted indices : p[i][k] is point number i in image k ...
void GetPoints(int imNo, std::vector< HomgPoint2D > &p) const
void _Transpose(const std::vector< std::vector< HomgPoint2D > > &src, std::vector< std::vector< HomgPoint2D > > &dst) const
std::vector< RMatrixBase > _R
int Write(const std::string &fname) const
binary write
void GetCorrespondences(std::vector< std::vector< HomgPoint2D > > &p) const
same as GetPOints with inverted indices : p[i][k] is point number i in image k
void GetNormalizedPoints(int imNo, std::vector< HomgPoint2D > &p) const
bool CheckParam(const std::string &name)
Check if parameter has already been added.
Definition: Param.cpp:527
class for representing matches, used in GenSynthMatches and biasshowsm
int * GetParamInt(const std::string &name) const
Definition: Param.cpp:618
void GetC(int imNo, HomgPoint3D &C) const
int GetFreeGroupID()
returns unused group id
Definition: Param.cpp:1421
class HomgPoint3D describes a point with 3 degrees of freedom in projective coordinates.
Definition: HomgPoint3D.hh:61
describes a projective 3D -&gt; 2D mapping in homogenous coordinates
Definition: PMatrixBase.hh:74
std::ostream & operator<<(std::ostream &os, const Array2D< T > &arg)
Definition: Array2D.hh:260
virtual void _AddParameter(Param &para)
This class Param provides generic support for parameters.
Definition: Param.hh:231
std::vector< HomgPoint3D > _C
std::vector< std::vector< HomgPoint2D > > _Points
K describes the mapping from world coordinates (wcs) to pixel coordinates (pcs).
Definition: KMatrix.hh:48
int * AddParamInt(const std::string &name, const std::string &help, int deflt=0, int min=std::numeric_limits< int >::min(), int max=std::numeric_limits< int >::max(), char cmdshort=0, int Group=GRP_NOSHOW)
For all adding routines:
Definition: Param.cpp:276
Implements a 3D rotation matrix.
Definition: RMatrixBase.hh:44
KMatrix Invert() const
returns analyticaly inverted matrix
Definition: KMatrix.cpp:31
void GetP(int imNo, PMatrixBase &P) const
int GetGroupID(const std::string &name)
returns group id of parameter with name
Definition: Param.cpp:1430
void SetData(KMatrix &K, std::vector< RMatrixBase > &R, std::vector< HomgPoint3D > &C, std::vector< std::vector< HomgPoint2D > > &m)
bool operator==(MatchDataBase &l)
BIASCommon_EXPORT std::istream & operator>>(std::istream &is, BIAS::TimeStamp &ts)
Standard input operator for TimeStamps.
Definition: TimeStamp.cpp:157