Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleParabola.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003, 2004 (see file CONTACTS 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 
26 /**
27  * @example ExampleParabola.cpp
28  @brief This little example demonstrates the usage of parabola
29  @ingroup g_examples
30  @author woelk
31 */
32 
33 #include <Matcher2D/CornerMatcher.hh>
34 #include <Base/Image/Image.hh>
35 #include <Base/ImageUtils/ImageDraw.hh>
36 #include <Base/Image/ImageIO.hh>
37 #include <Base/Geometry/HomgPoint2D.hh>
38 #include <Base/Debug/TimeMeasure.hh>
39 #include <Filter/Gradient.hh>
40 
41 #include <iostream>
42 #include <iomanip>
43 
44 using namespace BIAS;
45 using namespace std;
46 
47 int main(int argc, char*argv[])
48 {
49  Image<unsigned char> im[2], drawim;
50  HomgPoint2D p[2], realpoint;
51 
52  double correlation, mincorrelation=1.0, error, errorsum=0.0;
53  unsigned int halfsearchwinsize=2;
54  unsigned int uip[2], rectsize=7;
55  unsigned int halfnccwinsize=3;
56  unsigned int newim, oldim, res;
57  int counter;
58  bool debug=false;
59  string file;
60  bool draw=false;
61  bool UseLeastSquaresParabola=false;
62  MetaData *md;
63  AppData appdata;
64 
65  CornerMatcher matcher;
66  TimeMeasure timer, tc;
67 
68  if (debug){
69  matcher.SetDebugLevel(matcher.GetDebugLevel() |D_CORNER_MATCHER_SEARCH);
70  matcher.SetDebugLevel(matcher.GetDebugLevel() |D_REGION_MATCHER_SEARCHNCC);
71  matcher.SetDebugLevel(matcher.GetDebugLevel() |D_REGION_MATCHER_PARABOLA);
72  // matcher.SetDebugLevel(matcher.GetDebugLevel()|D_REGION_MATCHER_SEARCH_ODD);
73  // matcher.SetDebugLevel(matcher.GetDebugLevel()|D_REGION_MATCHER_SEARCH_EVEN);
74  }
75 
76  if (argc<4){
77  cerr << argv[0] << " : <x-coo> <y-coo> <image> <image> ..."<< endl;
78  return -5;
79  }
80 
81  p[0][0]=atof(argv[1]);
82  p[0][1]=atof(argv[2]);
83  p[0][2]=p[1][2]=1.0;
84 
85  if (ImageIO::Load(argv[3], im[0])!=0){
86  BIASERR("error loading image "<<argv[3]);
87  return -1;
88  }
89 
90  counter=3;
91  newim=0;
92  while (counter < argc-1){
93  counter++;
94  newim=(newim==0)?(1):(0);
95  oldim=(newim==0)?(1):(0);
96 
97  if (ImageIO::Load(argv[counter], im[newim])!=0){
98  BIASERR("error loading image "<<argv[counter]);
99  return -1;
100  } else {
101  if (debug)
102  cout << "--------------- newim: " << argv[counter]
103  << " --- oldim: " << argv[counter-1] << endl;
104  md=im[newim].GetMetaData();
105  if (md->Find(AppData::MD_HomgPoint2D, "#[HomgPoint2D]", appdata)>=0){
106  if (appdata.tag==AppData::MD_USE_ASCII){
107  if (debug) cerr << appdata.stag <<" : "<<appdata.sdata<<endl;
108  sscanf(appdata.sdata.c_str(), "[ %lf %lf %lf ]",
109  &realpoint[0], &realpoint[1], &realpoint[2]);
110  } else {
111  memcpy(realpoint.GetData(), appdata.data, appdata.length);
112  }
113  if (debug) cerr <<"realpoint is at "<<realpoint<<endl;
114  } else {
115  BIASERR("cannot find HomgPoint2D in meta data");
116  }
117  }
118 
119  if (p[oldim][0]<=halfnccwinsize+3 ||
120  p[oldim][1]<=halfnccwinsize+3 ||
121  p[oldim][0]+halfnccwinsize+3>=im[oldim].GetWidth()-1 ||
122  p[oldim][1]+halfnccwinsize+3>=im[oldim].GetHeight()-1){
123  cout << "point ("<<p[oldim][0]<<", "<<p[oldim][1]<<") too close to "
124  <<"image border, stopping"<<endl;
125  break;
126  }
127 
128  timer.Start();
129  tc.Start();
130  if ((res=matcher.NCCSearch(p[oldim], p[oldim], im[oldim], im[newim],
131  halfnccwinsize, halfsearchwinsize, p[newim],
132  mincorrelation, correlation))!=0){
133  BIASERR("error in NCCSearch");
134  }
135 
136  if (res!=0){
137  BIASERR("error matching");
138  break;
139  }
140 
141  if (debug)
142  cerr << p[oldim] << " NCC -> " << p[newim]
143  << " ("<<correlation<<") Parabola -> ";
144 
145  if (UseLeastSquaresParabola){
146  if ((res=matcher.ParabolaNCC5(p[oldim], p[newim], im[oldim], im[newim],
147  halfnccwinsize, p[newim]))!=0){
148  BIASERR("error in ParabolaNCC");
149  break;
150  }
151  } else {
152  if ((res=matcher.ParabolaNCC(p[oldim], p[newim], im[oldim], im[newim],
153  halfnccwinsize, p[newim]))!=0){
154  BIASERR("error in ParabolaNCC");
155  break;
156  }
157  }
158  tc.Stop();
159 
160  timer.Stop();
161 
162  tc.Print();
163  tc.Reset();
164  if (debug)
165  cerr << p[newim] << endl;
166 
167  if (res!=0){
168  BIASERR("error matching");
169  break;
170  }
171 
172  error=p[newim].Distance(realpoint);
173  errorsum+=error;
174 
175  cout << setw(2) << counter-3 << " : (" << p[oldim][0]<<", "<<p[oldim][1]
176  << ") -> ("<< p[newim][0]<<", "<<p[newim][1]<<") corr: "<<correlation
177  <<" real: ("<<realpoint[0]<<", "<<realpoint[1]
178  <<") error: "<<error<<endl;
179 
180  if (draw){
181  uip[0]=(unsigned int)rint(p[newim][0]);
182  uip[1]=(unsigned int)rint(p[newim][1]);
183  drawim=im[newim];
184  ImageDraw<unsigned char>::RectangleCenter(drawim, uip, rectsize);
185 
186  file=argv[counter];
187  file+=".tracked";
188  //ImageIO::Save(file, drawim);
189  ImageIO::Save(file, drawim);
190  }
191  }
192 
193  cerr <<"errorsum over "<<counter-3<<" images: "<<errorsum<<endl
194  <<" mean error "<<errorsum/(double)(counter-3)<<endl;
195 
196  cerr << "Parabola approximation over "<<argc-3<<" images took "
197  <<timer.GetUserTime()<<" ms"
198  << "\n this is equal to "<<timer.GetUserTime()/double(argc-3)
199  <<" ms per image and corner" << endl;
200 
201  return 0;
202 }
203 
void Print(std::ostream &os=std::cout) const
std::string stag
the tag as given in ascii meta data
Definition: MetaData.hh:100
const T * GetData() const
get the data pointer the member function itself is const (before {..}) because it doesn&#39;t change the ...
Definition: Vector3.hh:202
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
HOMGPOINT2D_TYPE Distance(const HomgPoint2D &point) const
Definition: HomgPoint2D.hh:367
MetaData * GetMetaData()
Definition: ImageBase.hh:456
int length
number of bytes used by the data block
Definition: MetaData.hh:96
std::string sdata
the data as given in ascii meta data
Definition: MetaData.hh:102
int GetDebugLevel() const
Definition: Debug.hh:332
char * data
pointer to block of data
Definition: MetaData.hh:98
int NCCSearch(const HomgPoint2D &p1, const HomgPoint2D &p2, const Image< StorageType > &im1, const Image< StorageType > &im2, const unsigned int halfnccwinsizeconst, const unsigned int halfsearchwinsize, HomgPoint2D &resultpoint, const double mincorrelation, double &result)
wrapper for RegionMatcher::NCCSearchOdd/NCCSearchEven, does boundary checks and casts ...
void SetDebugLevel(const long int lv)
Definition: Debug.hh:318
int Find(const enum AppData::TAppData tag, AppData &data) const
searches for tag in binary coded AppDatas.
Definition: MetaData.cpp:363
static int Save(const std::string &filename, const ImageBase &img, const enum TFileFormat FileFormat=FF_auto, const bool sync=BIAS_DEFAULT_SYNC, const int c_jpeg_quality=BIAS_DEFAULT_IMAGE_QUALITY, const bool forceNewID=BIAS_DEFAULT_FORCENEWID, const bool &writeMetaData=true)
Export image as file using extrnal libs.
Definition: ImageIO.cpp:725
enum TAppData tag
The tag defines the type of data, e.g.
Definition: MetaData.hh:94
static int RectangleCenter(Image< StorageType > &im, const int x, const int y, const int size, const StorageType value[])
Draws the rectangle around X, Y with Size and Value[i] in channel i.
Definition: ImageDraw.cpp:284
static int Load(const std::string &FileName, ImageBase &img)
first tries a call to Read MIP image and if that fails, tries to Import Image with all other availabl...
Definition: ImageIO.cpp:141
this class collects all additional data chunks of type AppData to be written into/read from an image ...
Definition: MetaData.hh:121
int ParabolaNCC(const HomgPoint2D &p1, const HomgPoint2D &p2, const Image< StorageType > &im1, const Image< StorageType > &im2, const unsigned int halfwinsize, HomgPoint2D &result) const
wrapper with range checking for RegionMatcher::KLT for a description see RegionMatcher::KLT gradientb...
int ParabolaNCC5(const HomgPoint2D &p1, const HomgPoint2D &p2, const Image< StorageType > &im1, const Image< StorageType > &im2, const unsigned int halfwinsize, HomgPoint2D &result) const
wrapper for RegionMatcher::ParabolaNCC5
class with various functions to detect corresponding image points.
double GetUserTime() const
return user time (=system usage time) in msec JW For Win32: user-time is the sum over all processes o...
class TimeMeasure contains functions for timing real time and cpu time.
Definition: TimeMeasure.hh:111
this is a chunk of metadata, also see MetaData
Definition: MetaData.hh:49