Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
CondensImg.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 
26 #include "CondensImg.hh"
27 #include <Base/ImageUtils/ImageDraw.hh>
28 
29 using namespace BIAS;
30 using namespace std;
31 
33 {
34  // the process mean is the image center
35  processMean_=areaMax_ * 0.5;
36 
37  // this draws the sample to the image center a little
38  // its 99% the old position, 1% the mean
39  processFirstOrderScale_.Set(0.99);
40 
41  // a second Order ARP, set scale to zero to have just a first order
42  // this takes 90% of the last displacements (velocity)
43  processSecondOrderScale_.Set(.9);
44 
45  // and a diffusion
46  diffusionSigma_=0.01 * areaMax_;
47 
48  importanceFraction_ = 0.0;
49 
50  defaultInitFraction_=0.05;
51 }
52 
53 
54 void CondensImg::InitPriorPositions(unsigned int nrOfInitSamples)
55 {
56  // uniform distribution
57  for (unsigned int n=0; n<nrOfInitSamples; n++)
58  for (unsigned int d=0;d<stateDim_;d++) {
59  samplePosNew_[n][d]=
60  random_.GetUniformDistributed(areaMin_[d],areaMax_[d]);
61  }
62 }
63 
64 
66 {
67  // this function has to assign a weight (probability) to each sample
68  for (unsigned int n=0;n<Nsamples_;n++) {
69  // the observation is set by SetObservation
70  // just set the image value as weight
71  if ((samplePosNew_[n][0]>0) &&
72  (samplePosNew_[n][0]<= (double)obsImg_.GetWidth()) &&
73  (samplePosNew_[n][1]>0) &&
74  (samplePosNew_[n][1]<= (double)obsImg_.GetHeight()) ) {
75  sampleWeights_[n]=
76  (double)(obsImg_.GetImageDataArray()[(int)samplePosNew_[n][1]][(int)samplePosNew_[n][0]]);
77  }
78  }
79 }
80 
82 {
83  // assume we know, that the 'real' object is in the left image half
84  // assign importanceWeights accordingly
85  for (unsigned int n=0; n<Nsamples_; n++)
86  if ( (samplePosOld_[n][0]>areaMin_[0]) &&
87  (samplePosOld_[n][1]>areaMin_[1]) &&
88  (samplePosOld_[n][0]<areaMax_[0]/2) &&
89  (samplePosOld_[n][1]<areaMax_[1]) )
90  sampleImportanceWeights_[n]=255;
91  else sampleImportanceWeights_[n]=0;
92 }
93 
95 {
96  areaMin_=min;
97  areaMax_=max;
98 }
99 
100 
102 {
103  obsImg_=obsImg;
104 }
105 
106 void CondensImg::DrawSamples(Image<unsigned char> &img, unsigned char value)
107 {
108  if (!img.IsEmpty()) img.Release();
109  img.Init(obsImg_.GetWidth(),obsImg_.GetHeight(),1);
111  img.FillImageWithConstValue((unsigned char)0);
112  unsigned int radius;
113  unsigned char values[1];
114  for (unsigned int i=0;i<Nsamples_;i++) {
115  values[0] = value;
116  radius = (unsigned int)(sampleWeights_[i]* Nsamples_ /
117  sumOfWeights_ + 0.5);
118  // cerr<<"Drawing circle with radius:"<<radius<<
119  // "at pos:"<<samplePosOld_[i]<<endl;
120  if ( (samplePosOld_[i][0]-radius>0) && (samplePosOld_[i][1]-radius>0) &&
121  (samplePosOld_[i][0]+radius<img.GetWidth()) &&
122  (samplePosOld_[i][1]+radius<img.GetHeight()) )
124  (unsigned int)samplePosOld_[i][0],
125  (unsigned int)samplePosOld_[i][1],
126  radius, &values[0]);
127  }
128 }
129 
130 
132 {
133  if (fImg_.IsEmpty())
134  fImg_.Init(img.GetWidth(), img.GetHeight(),1);
135 
136  double max = DrawPosteriorDistribution(fImg_);
137 
138  float *pixel;
139  // now copy values into result img.
140  pixel = fImg_.GetImageData();
141  unsigned char *data= img.GetImageData();
142  unsigned int size=img.GetPixelCount();
143  for (unsigned int i=0;i<size;i++) {
144  *data = (unsigned char)(*pixel / max * 255.0);
145  data++;
146  pixel++;
147  }
148 }
149 
150 
151 
152 
154 {
155  float max=0;
156  float *pixel;
157  double x,y,dx,dy;
158  int width=img.GetWidth() ,height=img.GetHeight();
159  img.FillImageWithConstValue((float)0);
160  for (unsigned int i=0;i<Nsamples_;i++) {
161  x = samplePosOld_[i][0];
162  y = samplePosOld_[i][1];
163 
164  if ( (x>0) && (y>0) &&
165  (x<width-1) && (y<height-1) ) {
166  pixel= (img.GetImageDataArray()[(int)y])+ (int)x;
167  // inverse bilinear interpolation
168  dx = x - (double)floor(x); dy = y - (double)floor(y);
169  // top left
170  *pixel+= float((1-dx) * (1-dy) * sampleWeights_[i]);
171  if (*pixel>max) max=*pixel;
172  // top right
173  pixel++;
174  *pixel+= float((1-dy) * dx * sampleWeights_[i]);
175  if (*pixel>max) max=*pixel;
176  // bottom right
177  pixel+=width;
178  *pixel+= float(dx * dy * sampleWeights_[i]);
179  if (*pixel>max) max=*pixel;
180  // bottom left
181  pixel--;
182  *pixel+= float((1-dx) * dy * sampleWeights_[i]);
183  if (*pixel>max) max=*pixel;
184  }
185  }
186  return max;
187 }
188 
189 
191 {
192  double var=0;
194  for (unsigned int n=0;n<Nsamples_;n++) {
195  diff = samplePosOld_[n] - mean_;
196  var += diff.ScalarProduct(diff) * sampleWeights_[n];
197  }
198  var /= sumOfWeights_;
199  var = sqrt(var);
200  ImageDraw<unsigned char>::LineGrey(img, (int)(mean_[0]-var),
201  (int)(mean_[1]), (int)(mean_[0]+var),
202  (int)(mean_[1]),255);
203  ImageDraw<unsigned char>::LineGrey(img, (int)mean_[0], (int)(mean_[1]-var),
204  (int)(mean_[0]), (int)(mean_[1] + var), 255);
205 }
206 
T ScalarProduct(const Vector< T > &argvec) const
scalar product (inner product) of two vectors returning a scalr
Definition: Vector.hh:355
void Release()
reimplemented from ImageBase
Definition: Image.cpp:1579
void DrawSamples(Image< unsigned char > &img, unsigned char value=255)
this draws the sample weights as circles
Definition: CondensImg.cpp:106
gray values, 1 channel
Definition: ImageBase.hh:130
virtual void InitPriorPositions(unsigned int nrOfInitSamples)
The Prior condition is used to init the first sample positions.
Definition: CondensImg.cpp:54
bool IsEmpty() const
check if ImageData_ points to allocated image buffer or not
Definition: ImageBase.hh:245
virtual void EvaluateObservationDensities()
most important function, here u have to calculate a probability for each sample position based on you...
Definition: CondensImg.cpp:65
void SetColorModel(EColorModel Model)
Definition: ImageBase.hh:561
unsigned int GetWidth() const
Definition: ImageBase.hh:312
virtual void InitModelDefaults()
Here u can specify the model and process defaults.
Definition: CondensImg.cpp:32
static int CircleCenter(Image< StorageType > &im, unsigned int CenterX, unsigned int CenterY, unsigned int Radius, const StorageType Value[]=NULL)
draws a circular line, either using Value or a good contrast value
Definition: ImageDraw.cpp:977
void DrawPosteriorDistribution(BIAS::Image< unsigned char > &img)
Draws the samples with their weight as greyvalue, the weight is distributed to the 4 pixels around th...
Definition: CondensImg.cpp:131
void SetArea(Vector2< double > min, Vector2< double > max)
The area for the initial uniform distribution.
Definition: CondensImg.cpp:94
void SetObservation(Image< unsigned char > &osbImg)
converts to Image&lt;float&gt; and calls the function above
Definition: CondensImg.cpp:101
unsigned int GetHeight() const
Definition: ImageBase.hh:319
void DrawMean(BIAS::Image< unsigned char > &img)
Draws the mean as a cross, which size is proportional to the variance.
Definition: CondensImg.cpp:190
static int LineGrey(Image< StorageType > &im, const unsigned int start[2], const unsigned int end[2], const StorageType value)
faster algorithm for 1 channel images
Definition: ImageDraw.cpp:500
void FillImageWithConstValue(StorageType Value)
fill grey images
Definition: Image.cpp:456
virtual void EvaluateImportanceWeights()
If you want to use importance sampling, then overload this function, which is the importance function...
Definition: CondensImg.cpp:81
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
const StorageType * GetImageData() const
overloaded GetImageData() from ImageBase
Definition: Image.hh:137
unsigned long int GetPixelCount() const
returns number of pixels in image
Definition: ImageBase.hh:422
const StorageType ** GetImageDataArray() const
overloaded GetImageDataArray() from ImageBase
Definition: Image.hh:153