fgbgsegment.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011, Mårten Björkman (celle@csc.kth.se) 
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are
00007  * met:
00008  *
00009  *  1.Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  *  2.Redistributions in binary form must reproduce the above
00012  *    copyright notice, this list of conditions and the following
00013  *    disclaimer in the documentation and/or other materials provided
00014  *    with the distribution.  
00015  *  3.The name of Mårten Björkman may not be used to endorse or
00016  *    promote products derived from this software without specific
00017  *    prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00020  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00021  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00022  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00023  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00024  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00025  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00026  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00027  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00028  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00029  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030  */
00031 
00033 
00089 
00090 
00091 
00092 #ifndef FGBGSEGMENT_H
00093 #define FGBGSEGMENT_H
00094 
00095 #include <vector>
00096 #include <stdint.h>
00097 #include "pyra/tpimage.h"
00098 #include "matrix3.h"
00099 
00103 class CudaSegment;
00104 
00106 class FgBgSegment {
00107 
00108   friend class CudaSegment;
00109 
00110   //enum Labeling { BACKGROUND = 1, FLATSURFACE = 2, FOREGROUND = 3 };
00111   static const float eps = 1e-6f;
00112 public:
00114   static const int hist_size = 12;
00115 
00117   class ColorModel { 
00118   protected:
00120     FgBgSegment &segm;
00121     
00122   public: 
00123     static const float weight = 0.01;
00124   public:
00125     float histogram[hist_size*hist_size];
00126     float greyhist[hist_size];
00127     float colorcost[hist_size*hist_size];
00128     float greycost[hist_size];
00130     ColorModel *prior;
00132 
00133     ColorModel(FgBgSegment &segm);
00135 
00136     ColorModel(const ColorModel &model);
00138     ColorModel &operator=(const ColorModel &model);
00139     ~ColorModel();
00140 
00142 
00145     float CreateHistogram(Image<float> &probabilities, bool allPoints);
00147     void SmoothAndNormalizeHist( float const* hist, float const* phist,
00148                                  int size, 
00149                                  float* const histogram, float* const cost);
00150     void NormalizeHist( float* const histogram,
00151                         float* const cost,
00152                         int size);
00154 
00155     void CreateHistogram(Image<uint8_t> &mask, bool allPoints);
00157     virtual void Update() { };
00158   };  
00159   
00160 protected:
00161   
00163   class FlatSurface : public ColorModel {
00164   public:
00165     static const float weight_a  = 100.0f*100.0f; 
00166     static const float weight_b = 50.0f*50.0f; 
00167     static const float weight_d = 1.0f*1.0f;
00168     static const float strength = 10.0f;
00169   public:
00170     float alpha, beta, disp, spread_d;
00171     float min_x, min_y, max_x, max_y;
00173     Image<float> probabilities; 
00174   public:
00176 
00181     FlatSurface(FgBgSegment &segm, int width, int height);
00183     void Initialize();
00185     virtual void Update();
00186   };
00187   
00189   class Foreground : public ColorModel {
00190   public:
00191     static const float weight_p = 0.005f*0.005f;
00192     static const float weight_d = 0.5f*0.5f;
00193     static const float strength = 10.0f;
00194   public:
00195 
00197     float window_size;
00198     float ball_size;     
00199 
00201     bool combined; 
00202     Vector3 position3d; 
00203     Matrix3 spread3d;  
00205     Image<float> probabilities; 
00206   public:
00208 
00213     Foreground(FgBgSegment &segm, int width, int height);
00215 
00219     void Initialize(int startx, int starty);
00221     virtual void Update();
00222     
00225     void SetInitParams( float l_window_size, float l_ball_size);
00226     
00227   };
00228   
00230   class Background : public ColorModel {
00231   public:
00232     static const float weight_d = 0.1f*0.1f;
00233     static const float strength = 10.0f;
00234   public:
00235     float disp, spread_d;
00237     Image<float> probabilities;
00238   public:
00240 
00245     Background(FgBgSegment &segm, int width, int height);
00247     void Initialize();
00249     virtual void Update();
00250   };
00251   
00253   bool withSurface;
00255   bool withColors;     
00257   bool withDisparities;
00259   bool withColorHoles;
00260   /* If colour information has holes, use uniform probabilities or 
00261      histograms over luminance
00262   */
00263   bool uniform;
00265   int verbose;                
00266   
00268   Background ground;   
00270   FlatSurface surface;          
00272   std::vector<Foreground*> figures;  
00273 
00275   Image<uint8_t> hue;
00277   Image<uint8_t> saturation;
00279   Image<uint8_t> grey;  
00281   Image<float> *disparities;        
00283   std::vector<ColorModel> colorPriors;
00284   
00286   int width;
00288   int height;
00290   int drange;        
00292   float gradWeight;
00293 
00295   float windowSize;
00296   float ballSize;
00297   
00299   CudaSegment *cudaSegment;          
00301   bool gpuopt;                        
00302     
00304 
00306   void CreateHistograms(bool allPoints);
00308   void InitSegmentation();
00310 
00313   template <int numFigures> static void PixSegment(FgBgSegment &segm);
00315 
00318   void RGBToHSV(Image<uint8_t> &cimg);
00319 
00320 public:
00322 
00328   FgBgSegment(int width, int height, int drange, float gradWeight = 20.0, 
00329               float w_size = 0.20f, float b_size = 0.20f);
00330   ~FgBgSegment();
00332   void UseGPU(bool gpuopt = true);
00334 
00342   void Execute(Image<uint8_t> &image, Image<float> &disp, 
00343                bool initialize, int loops = 1, 
00344                int startx = -1, int starty = -1);
00346 
00352   void SetNewForeground(int startx, int starty, Image<float> &dimg, int drange_);
00354 
00359   void SetNewForeground(Image<uint8_t> &mask, Image<float> &dimg, 
00360                         int drange, bool reuseLast = false);
00361   
00363   void GetSurfaceParameters( float &alpha, float &beta, float &disp);
00364     
00365   
00366   void GetSurfaceMinMax( float &min_x,
00367                          float &min_y,
00368                          float &max_x,
00369                          float &max_y );
00370                          
00371 
00373   void SetWithColors( bool val);
00375   bool GetWithColors();
00376   
00378   void SetWithColorHoles( bool val);
00380   bool GetWithColorHoles();
00381   
00383   void SetUniform( bool val);
00385   bool GetUniform();
00386   
00388   void SetWithSurface( bool val);
00390   bool GetWithSurface();
00391   
00393   void SetWithDisparities( bool val);
00395   bool GetWithDisparities();
00396 
00398   void SetGradWeight( float val);
00400   float GetGradWeight();
00401   
00403 
00406   void MakeSegmentImage(Image<uint8_t> &image);
00408 
00413   void MakeMaskImage(Image<uint8_t> &image, int val = 255, int obj = 0);
00415 
00418   void MakeBorderImage(Image<uint8_t> &image);
00419 
00420 };
00421 
00422 #endif // FGBGSEGMENT_H


active_realtime_segmentation
Author(s): Mårten Björkman. Maintained by Jeannette Bohg
autogenerated on Fri Jan 3 2014 12:02:50