Basic Image AlgorithmS Library 2.8.0

CondensHisto.hh

00001 /* 
00002 This file is part of the BIAS library (Basic ImageAlgorithmS).
00003 
00004 Copyright (C) 2003-2009    (see file CONTACT for details)
00005   Multimediale Systeme der Informationsverarbeitung
00006   Institut fuer Informatik
00007   Christian-Albrechts-Universitaet Kiel
00008 
00009 
00010 BIAS is free software; you can Sredistribute it and/or modify
00011 it under the terms of the GNU Lesser General Public License as published by
00012 the Free Software Foundation; either version 2.1 of the License, or
00013 (at your option) any later version.
00014 
00015 BIAS is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU Lesser General Public License for more details.
00019 
00020 You should have received a copy of the GNU Lesser General Public License
00021 along with BIAS; if not, write to the Free Software
00022 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023 */
00024 
00025 
00026 #ifndef _HISTO_CONDENSATION__HH_
00027 #define _HISTO_CONDENSATION__HH_
00028 
00029 #include <bias_config.h>
00030 
00031 // BIAS
00032 #include <StateEstimator/Condensation.hh>
00033 #include <Base/Image/Image.hh>
00034 #include <Base/Math/Vector2.hh>
00035 #include <Base/Math/Vector3.hh>
00036 #include <Image/ColorHistogram.hh>
00037 #include <Image/IntegralHistogram.hh>
00038 
00039 #define D_COND_INT      (1<<6)
00040 
00041 namespace BIAS{
00042 
00043   /** 
00044       @class CondensHisto
00045       @ingroup g_stateestimation
00046       @brief class for tracking an object in an image
00047 
00048       This class is able to handle "normal" ColorHistograms, and
00049       MultiColorHistograms.
00050       Just Set a MultiColorHistogram in SetReferenceHistogram() and 
00051       the whole process switches to multiHistogram processing
00052       
00053       You have to call:
00054       - SetActiveImage(imgHSL) with an HSL image;
00055       - SetReferenceHistogram(chist); (colorHistogram or ColorMultiHistogram)
00056       - SetArea(Vector3<double>(0,0,0.5), Vector3<double>(width,height,1.5)); 
00057       
00058       - Init(*nsamples); 
00059       - InitPrior(); //to set samples for first frame
00060      
00061       then just call these functions in every turn
00062       - SetActiveImage(imgHSL)
00063       - PredictNewSamplePositions()
00064       - Process();   
00065       @author Ingo Schiller, March 2003, edited by grest
00066   */
00067   class BIASStateEstimator_EXPORT CondensHisto : public Condensation{
00068   public:
00069     /**
00070        Set the state dimension in the constructor
00071     */
00072     CondensHisto();
00073 
00074     virtual void InitModelDefaults();
00075     
00076     virtual void InitPriorPositions(unsigned int nrOfInitSamples);
00077     
00078     virtual void EvaluateObservationDensities();
00079     
00080     // virtual void EvaluateImportanceWeights();
00081    
00082     void UseIntegralHistogram(bool bIntHisto);
00083 
00084     /**
00085        Checks the image Quality by checking the luminance
00086        of the image.
00087        the shutter and gain of the camera has to be set
00088        by the returned shutter and gain in params
00089        @param percent = the allowed percentage of pixels, below minsat in
00090                         light and dark regions
00091        @param shutter = the shutter the camera has to be set to
00092        @param gain    = the gain    ------------  ------------
00093     */
00094     void CheckImageQuality(double percent, int &shutter, int &gain);
00095 
00096     /** The area for the initial uniform distribution.
00097         3-dim case
00098     */     
00099     void SetArea(Vector3<double> min, Vector3<double> max);
00100 
00101     /**
00102        Sets the minimum weight each sample is assigned,
00103        when < lower minWeight_
00104     */
00105     inline void SetMinWeight(double min)
00106     {minWeight_ = min;}
00107     
00108     /**
00109        Sets the scale for the diffusion,
00110        diffusi0nSigma = diffScale_*areaMax_
00111        example value would be 0.05
00112     */
00113     inline void SetDiffScale(double diff)
00114     {
00115       diffScale_ = diff;
00116       diffusionSigma_[0] = diffScale_*areaMax_[0];
00117       diffusionSigma_[1] = diffScale_*areaMax_[1];
00118     }
00119     
00120     /**
00121        Sets the scale for the diffusion for the size
00122        of the histogram ,
00123        diffusionSigma[stateDim_-1] = diffScaleSize_*areaMax_[stateDim-1]
00124        example value would be 0.05
00125     */
00126     inline void SetDiffScaleSize(double diff)
00127     {
00128       diffScaleSize_= diff;
00129       diffusionSigma_[stateDim_-1]= diffScaleSize_*areaMax_[stateDim_-1];
00130     }
00131 
00132 
00133     /**
00134        Sets the scale for the second order prediction,
00135        valid values are 0.0000..1 - 1.0
00136     */
00137     inline void SetSecondOrderScale(double scale)
00138     {
00139       if(scale > 1.0)
00140         scale = 1.0;
00141       else if(scale < 0)
00142         scale = 0.0;
00143       secondOrderScale_ = scale;
00144       processSecondOrderScale_.Set(secondOrderScale_); 
00145     }
00146 
00147   /**
00148        Sets the scale for the first order prediction,
00149        valid values are 0.0000..1 - 1.0
00150     */
00151     inline void SetFirstOrderScale(double scale)
00152     {
00153       if(scale > 1.0)
00154         scale = 1.0;
00155       else if(scale < 0)
00156         scale = 0.0;
00157       firstOrderScale_ = scale;
00158       processFirstOrderScale_.Set(firstOrderScale_); 
00159     }
00160 
00161 
00162 
00163     /** sets the present picture to compare and create new observation */
00164     inline void SetActiveImage(Image<unsigned char> &img)
00165     {img_ = &img;}
00166 
00167     /** sets the reference colorhistogram */
00168     inline
00169     void SetReferenceHistogram(ColorHistogram<unsigned char> &refHisto)
00170     {
00171         if(img_ != NULL){
00172       refHisto_=refHisto;
00173       m_dMaxRefHistoSize = double(img_->GetWidth() * img_->GetHeight())/
00174         double(refHisto.GetHeightROI() * refHisto.GetWidthROI());
00175       chist_.SetBinSize(refHisto_.GetBinSize(), refHisto_.GetBinSizeSat());
00176       useMultiHistogram_ = false;
00177       //  cout<<"Using Normal Histogram"<<endl;
00178         }
00179       else{
00180         BIASERR("No actual image, can not net reference histogram.")
00181       }
00182     }
00183  
00184     /** sets if Multihistogram is used or not, default is false */
00185     inline
00186     void SetMultiHistogram(bool multi)
00187     {useMultiHistogram_ = multi;}
00188 
00189     
00190 
00191 
00192     /* at every (rounded) sample position, the sampleImg is set
00193        to UCHAR_MAX */
00194     void GetSamplePositions(Image<float> &sampleImg);
00195 
00196     /* 
00197      @returns the actual position of the mean value 
00198      @author Ingo Schiller
00199      @modified 03. August 2004
00200     */
00201     void GetMeanValue(Vector<double> &mean);
00202 
00203 
00204     /* 
00205      @returns the actual position of the sample, with max weight 
00206      @author Ingo Schiller
00207      @modified 29.11.04
00208     */
00209     void GetMaxValue(Vector<double> &max);
00210 
00211     /**
00212        @param meanVar= MeanVariance is a vector of struct:
00213        [varPosx, varPosy, varSize, varWeight]
00214     */
00215     void GetMeanVariance(Vector<double>  &meanVar);
00216 
00217     /**
00218        @param maxVar= MaxVariance is a vector of struct:
00219        [varPosx, varPosy, varSize, varWeight]
00220     */
00221     void GetMaxVariance(Vector<double>  &maxVar);
00222 
00223 
00224     /**
00225        @return = the maximum weight of all samples;
00226        @param index = the index of which maxWeight is found
00227     */
00228     double GetMaxWeight(int &index);
00229 
00230   
00231     /**
00232        @returns the aktual image on which the condensation is 
00233        doing it's job
00234        @author Ingo Schiller
00235     */
00236     Image<unsigned char> GetAktImage();
00237 
00238   
00239     /**
00240        this draws the Sizes of the Histogram
00241        as arrows at their x position
00242        @author Ingo Schiler
00243      */
00244     void DrawHistoSizes(Image<unsigned char> &img);
00245 
00246     /** this draws the sample weights as circles
00247      */
00248     virtual void DrawSamples(Image<unsigned char> &img);
00249 
00250     /** 
00251         this draws the sample weights as retangeles with greyvalues
00252         on white base
00253      */
00254     virtual void DrawSamplesWhite(Image<unsigned char> &img);
00255 
00256     /** this draws the samples,
00257         histogram sizes as rects and weights as grey values
00258         @author grest / schiller
00259      */
00260     void DrawSamplesExt(Image<unsigned char> &img);
00261 
00262     /** this draws the samples as circles and the color indicates the 
00263         size of the histogram (the z-coordinate)
00264     */
00265     void DrawWeightedSamples(Image<float> &img);
00266 
00267     /** Draws the samples with their weight as greyvalue,
00268         the weight is distributed to the 4 pixels around the sample postion.
00269         Image has to be initialized with correct size and channelcount !
00270     */
00271     void DrawPosteriorDistribution(BIAS::Image<unsigned char> &img);
00272     
00273     /** same as above but float precision and 
00274         returns the maximum value also.
00275     */
00276     double DrawPosteriorDistribution(BIAS::Image<float> &img);
00277     
00278     
00279     /** Draws the mean as a rectangle
00280         which size is proportional to the histogram size
00281      */
00282     virtual void DrawMean(BIAS::Image<unsigned char> &img);
00283     
00284   protected:
00285     // for better readablity of EvaluateObservationDensities()
00286     void EvaluateFromIntegralHistoImage_();
00287 
00288     BIAS::Vector3<double> areaMin_, areaMax_;
00289     Image<unsigned char> *img_;
00290 
00291     // the current histogram for comparision with the old one
00292     ColorHistogram<unsigned char> chist_;     
00293      
00294    //the integral Histogram image
00295     IntegralHistogram intHisto_;
00296 
00297     //reference ColorHistogram 
00298     ColorHistogram<unsigned char> refHisto_;
00299     ColorHistogram<unsigned char> refHisto2_;
00300 
00301     // needed for DrawPosteriorDistribution()
00302     BIAS::Image<float> fImg_;
00303     //basic weight of each sample to provide a wider spread of the samples
00304     double minWeight_; 
00305     double diffScale_;
00306     double diffScaleSize_;
00307     double secondOrderScale_;
00308     double firstOrderScale_;
00309     bool useMultiHistogram_;
00310     double   m_dMaxRefHistoSize;
00311 
00312     //relative to first hist, unit is first hist size
00313     BIAS::Vector2<double> secondHistPos_; 
00314 
00315     bool bIntegralHisto_;
00316   };
00317 
00318 
00319 
00320 }
00321 #endif
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends