OpticalFlow.h
上传用户:lijia5631
上传日期:2008-11-10
资源大小:1214k
文件大小:7k
源码类别:

视频捕捉/采集

开发平台:

MultiPlatform

  1. /**
  2.   * HandVu - a library for computer vision-based hand gesture
  3.   * recognition.
  4.   * Copyright (C) 2004 Mathias Kolsch, matz@cs.ucsb.edu
  5.   *
  6.   * This program is free software; you can redistribute it and/or
  7.   * modify it under the terms of the GNU General Public License
  8.   * as published by the Free Software Foundation; either version 2
  9.   * of the License, or (at your option) any later version.
  10.   *
  11.   * This program is distributed in the hope that it will be useful,
  12.   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.   * GNU General Public License for more details.
  15.   *
  16.   * You should have received a copy of the GNU General Public License
  17.   * along with this program; if not, write to the Free Software
  18.   * Foundation, Inc., 59 Temple Place - Suite 330, 
  19.   * Boston, MA  02111-1307, USA.
  20.   *
  21.   * $Id: OpticalFlow.h,v 1.9 2005/08/16 00:09:55 matz Exp $
  22. **/
  23. #if !defined(__OPTICAL_FLOW_H__INCLUDED_)
  24. #define __OPTICAL_FLOW_H__INCLUDED_
  25. #include <cv.h>
  26. #include <vector>
  27. using namespace std;
  28. #include "Mask.h"
  29. #include "ProbDistrProvider.h"
  30. typedef vector<CvPoint2D32f> CPointVector;
  31. typedef vector<char> CStatusVector;
  32. typedef vector<double> CDoubleVector;
  33. typedef vector<CDoubleVector> CDoubleMatrix;
  34. typedef vector<float> CFloatVector;
  35. typedef struct _CondensState {
  36.   double x, y;
  37.   double vx, vy;
  38.   double angle;
  39. } CondensState;
  40. #pragma warning (disable:4786)
  41. class OpticalFlow {
  42.  public:
  43.   OpticalFlow();
  44.   ~OpticalFlow();
  45.  public:
  46.   void Initialize(int width, int height);
  47.   void PrepareTracking(IplImage* rgbImage,
  48.                        IplImage* currGrayImage, int curr_indx,
  49.                        ProbDistrProvider* pProbProv,
  50.                        const CuScanMatch& match,
  51.                        ConstMaskIt mask,
  52.                        int target_num_features,
  53.                        int winsize_width,
  54.                        int winsize_height,
  55.                        double min_distance,
  56.                        double max_feature_error);
  57.   int Track(IplImage* rgbImage,
  58.             IplImage* prevImage, IplImage* currImage,
  59.             int prev_indx, int curr_indx,
  60.             int last_width, int last_height,
  61.             bool flock, bool use_prob_distr);
  62.   void DrawOverlay(IplImage* iplImage, int overlay_level);
  63.   void GetMeanFeaturePos(CvPoint2D32f& mean);
  64.  protected:
  65.   void LKPyramids(IplImage* prevImage, IplImage* currImage,
  66.                   int prev_indx, int curr_indx);
  67.   void DrawFeaturesStatistics(IplImage* pImage, int overlay_level);
  68.   void FindGoodFeatures(IplImage* grayImage, const CRect& bbox,
  69.                         CPointVector& corners);
  70.   void PickSkinColoredFeatures(IplImage* rgbImage,
  71.                                const CPointVector& corners,
  72.                                CPointVector& features,
  73.                                const CuScanMatch& match,
  74.                                ConstMaskIt mask);
  75.   int FindCentroid(const CDoubleMatrix& distances,
  76.                    int discard_num_furthest);
  77.   void GetAverage(const CPointVector& features, CvPoint2D32f& avg);
  78.   void DistanceMatrix(const CPointVector& features,
  79.                       CDoubleMatrix& distances);
  80.   void ConcentrateFeatures(IplImage* rgbImage,
  81.                            CPointVector& features,
  82.                            CStatusVector& status,
  83.                            int last_width, int last_height,
  84.                            bool use_prob_distr);
  85.   void AddNewFeatures(IplImage* rgbImage,
  86.                            CPointVector& features,
  87.                            CStatusVector& status,
  88.                            int last_width, int last_height,
  89.                            bool use_prob_distr);
  90.   bool TooCloseToOthers(const CPointVector& features, int indx, int len);
  91.   void GetPixel(IplImage* rgbImage, int x, int y, ColorBGR** color);
  92.   // Condens
  93. #define OF_CONDENS_DIMS 5
  94.   void InitCondensation(int condens_num_samples);
  95.   void UpdateCondensation(IplImage* rgbImage, 
  96.     int prev_indx, int curr_indx);
  97.   void FollowObservationForSmallDiffs(const CPointVector& pred, 
  98.                                                  const CPointVector& obs, 
  99.                                                  CPointVector& corrected,
  100.                                                  double diff);
  101.   void PreparePredictFeatureLocations(const CondensState& base_state,
  102.                                                  const CPointVector& base,
  103.                                                  CDoubleVector& old_lens,
  104.                                                  CDoubleVector& old_d_angles);
  105.   void PredictFeatureLocations(const CDoubleVector& old_lens,
  106.                                           const CDoubleVector& old_d_angles,
  107.                                           const CondensState& predicted_state,
  108.                                           CPointVector& prediction);
  109.   double EstimateProbability(const CPointVector& prediction, 
  110.                                         const CPointVector& observation,
  111.                                         int discard_num_furthest);
  112.   double EstimateProbability(const CPointVector& prediction,
  113.                                         IplImage* rgbImage);
  114.   
  115.  protected:
  116.   int                        m_num_features_tracked;
  117.   double                     m_recent_max_rdv;
  118.   int                        m_recent_max_rdv_decay;
  119.   int                        m_num_features_lost;
  120.   int                        m_winsize_width;
  121.   int                        m_winsize_height;
  122.   double                     m_min_distance;
  123.   double                     m_max_feature_error;
  124.   ProbDistrProvider*         m_pProbDistrProvider;
  125.   int                        m_num_pyramid_levels;
  126.   int                        m_target_num_features;
  127.   bool                       m_prev_buf_meaningful;
  128.   IplImage*                  m_pyramids[2];
  129.   IplImage*                  m_tmpEVImage[2];
  130.   CPointVector               m_features[2];
  131.   CStatusVector              m_feature_status;
  132.   CFloatVector               m_errors;
  133.   CvPoint2D32f               m_mean_feature_pos;
  134.   int                        m_saved_prev_indx;
  135.   int                        m_saved_curr_indx;
  136.   bool                       m_prepared;
  137.   // condensation tracking of features
  138.   bool                       m_condens_is_tracking;
  139.   CvConDensation*            m_pConDens;
  140.   CondensState               m_condens_state;
  141.   CPointVector               m_tmp_predicted;
  142.   CPointVector               m_features_observation;
  143.   CDoubleVector              m_sample_confidences;
  144.   CRect                      m_condens_init_rect;
  145. };
  146. #pragma warning (default:4786)
  147. #endif // __OPTICAL_FLOW_H__INCLUDED_