Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleCondensHisto.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 /** @example ExampleCondensHisto.cpp
26  @relates CondensHisto,ColorHistogram
27  @ingroup g_examples
28  @ingroup g_stateestimation
29  @brief Example for condensation algorithm using the color histogram class
30  @author MIP
31 */
32 
33 
34 #include "StateEstimator/CondensHisto.hh"
35 #include "Base/ImageUtils/ImageDraw.hh"
36 #include "Base/Image/ImageBase.hh"
37 #include "Base/Image/ImageIO.hh"
38 #include "Base/Image/ImageConvert.hh"
39 #include "Image/ColorHistogram.hh"
40 #include "Filter/Rescale.hh"
41 #include <Base/Common/FileHandling.hh>
42 #include "math.h"
43 
44 
45 using namespace BIAS;
46 using namespace std;
47 
48 
49 int main(int argc, char** argv)
50 {
51  stringstream fileName;
52  int res = -1;
53  int binsize=0;
54  int ux = 341;
55  int uy = 301;
56  int lx = 383;
57  int ly = 353;
58 
59  BIAS::Image<unsigned char> image,image2, down ,down2;
61  BIAS::CondensHisto condensation;
62  BIAS::Image<float> samples;
64 
65 
66  //condensation.SetDebugLevel(D_COND_WEIGHTS);
67  //condensation.SetDebugLevel(condensation.GetDebugLevel() | D_COND_INIT);
68  //condensation.SetDebugLevel(condensation.GetDebugLevel() | D_COND_PROCESS);
69  //condensation.SetDebugLevel(condensation.GetDebugLevel() | D_COND_DEBUG);
70 
71  if (argc<3)
72  {
73  cerr<<"Give a bin width and an image as arguments!"<<endl;
74  cerr<<"Usage:"<< argv[0] <<" binsize imagefile"<<endl;
75  return -1;
76  }
77 
78 
79  if(ImageIO::Load(argv[2], image) != 0)
80  {
81  cerr<<"Error reading image!"<<endl;
82  return -1;
83  }
84  //operate on half image sizes
85 
86  down.Init((int)floor((double)image.GetWidth()/2),
87  (int)floor((double)image.GetHeight()/2),3);
89  rescale.SetFactor(2.0);
90  rescale.Downsample(image,down);
91  //convert image to HSL
92  image.Release();
93  image.Init(down.GetWidth(),down.GetHeight(),3);
94 
95  res = ImageConvert::ToHSL(down,image);
96  cerr<<"Image is in HSL colorspace"<<endl;
97 
98  fileName<<"MeanImage.mip";
99  ImageIO::Save(fileName.str(),down);
100 
101  binsize = atoi(argv[1]);
102  chist.SetBinSize(binsize);
103 
104  chist.GenerateHist(image,ux/2,uy/2,lx/2,ly/2);
105  chist.Dump();
106 
107  condensation.SetArea(Vector3<double>(0,0,0.5),\
108  Vector3<double>(image.GetWidth(),image.GetHeight(),1.5));
109 
110  //initialise condensation with amount of initial pixels
111 
112  condensation.Init(250);
113  condensation.InitPrior();
114  condensation.SetMinWeight(0.0);
115  condensation.InitPriorPositions(250);
116  //now set the refHisto_ in condensation
117  condensation.SetActiveImage(image);
118  condensation.SetReferenceHistogram(chist);
119 
120  //init a picture for the DrawWeightedSamples(img)
121  samples.Init(image.GetWidth(),image.GetHeight(),1);
122 
123  int n =2;
124  while(n<argc){
125 
126  //read in next image
127  if ((res=ImageIO::ImportImage(argv[n], image2)) != 0)
128  {
129  cerr<<"Error reading image!"<<endl;
130  return -1;
131  }
132  //convert image to HSL
133 
134  down2.Init((int)floor((double)image2.GetWidth()/2),
135  (int)floor((double)image2.GetHeight()/2),3);
136 
137  rescale.Downsample(image2,down2);
138  cout<<"Image: "<<n<<" imported."<<endl;
139 
140  image2.Init(down2.GetWidth(),down2.GetHeight(),3);
141  ImageConvert::ToHSL(down2,image2);
142  cerr<<"Image is in HSL colorspace"<<endl;
143 
144  //set the image as active in the condensation algorithm
145  condensation.SetActiveImage(image2);
146 
147  //and now predict new positions(incl. Diffusion etc.
148  condensation.PredictNewSamplePositions();
149 
150  //now process next step
151  condensation.Process();
152 
153  condensation.DrawWeightedSamples(samples);
154  stringstream samplesName;
155  samplesName<<"SamplesImage_"<<FileHandling::LeadingZeroString(n,4)<<".mip";
156  ImageIO::Save(samplesName.str(),samples);
157 
158  condensation.DrawMean(down2);
159  stringstream meanName;
160  meanName<<"MeanImage2_"<<FileHandling::LeadingZeroString(n,4)<<".mip";
161  ImageIO::Save(meanName.str(),down2);
162  cout<<"after Process()"<<endl;
163 
164  n++;
165  }
166 }
167 
168 
void Release()
reimplemented from ImageBase
Definition: Image.cpp:1579
int InitPrior()
sets the sample positions for first time step
int Init(unsigned int nrSmaples)
Init the Condensation directly after constructor, with the desired amount of Samples, there more the better the approximation of the real densities.
static int ImportImage(const std::string &filename, ImageBase &result)
Try to load/import an image from a file using external libs.
Definition: ImageIO.cpp:378
void Process()
This really does one iteration of Condensation.
void SetColorModel(EColorModel Model)
Definition: ImageBase.hh:561
void SetReferenceHistogram(ColorHistogram< unsigned char > &refHisto)
sets the reference colorhistogram
void Dump(std::ostream &os=std::cout)
dumps information on histogram
static BIASImageBase_EXPORT int ToHSL(const Image< StorageType > &source, Image< StorageType > &dest)
Create a HSL converted copy of source image in this Release() and Init() are called if necessary...
Definition: ToHSL.cpp:36
int Downsample(const Image< InputStorageType > &src, Image< OutputStorageType > &dst)
generic downsample function.
Definition: Rescale.cpp:111
unsigned int GetWidth() const
Definition: ImageBase.hh:312
void DrawWeightedSamples(Image< float > &img)
this draws the samples as circles and the color indicates the size of the histogram (the z-coordinate...
int GenerateHist(BIAS::Image< StorageType > &image, int ux, int uy, int lx, int ly)
calcluates the histogram from data and adds them to the internal data structures
virtual void DrawMean(BIAS::Image< unsigned char > &img)
Draws the mean as a rectangle which size is proportional to the histogram size.
void SetMinWeight(double min)
Sets the minimum weight each sample is assigned, when &lt; lower minWeight_.
void SetArea(Vector3< double > min, Vector3< double > max)
The area for the initial uniform distribution.
color values, 3 channels, order: red,green,blue
Definition: ImageBase.hh:131
int SetBinSize(int binsize)
set the binsize (must be divideable by 2pown)
static std::string LeadingZeroString(const int &n, const unsigned int &digits=DEFAULT_LEADING_ZEROS)
Create a string with leading zeroes from number.
unsigned int GetHeight() const
Definition: ImageBase.hh:319
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
void SetActiveImage(Image< unsigned char > &img)
sets the present picture to compare and create new observation
void Init(unsigned int Width, unsigned int Height, unsigned int channels=1, enum EStorageType storageType=ST_unsignedchar, const bool interleaved=true)
calls Init from ImageBase storageType is ignored, just dummy argument
Definition: Image.cpp:421
class for tracking an object in an image
Definition: CondensHisto.hh:67
virtual void InitPriorPositions(unsigned int nrOfInitSamples)
The Prior condition is used to init the first sample positions.
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
void SetFactor(double factor)
the downsampling factor
Definition: Rescale.hh:361