include/opencv2/contrib/hybridtracker.hpp
Go to the documentation of this file.
00001 //*M///////////////////////////////////////////////////////////////////////////////////////
00002 //
00003 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
00004 //
00005 //  By downloading, copying, installing or using the software you agree to this license.
00006 //  If you do not agree to this license, do not download, install,
00007 //  copy or use the software.
00008 //
00009 //
00010 //                                License Agreement
00011 //                       For Open Source Computer Vision Library
00012 //
00013 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
00014 // Copyright (C) 2008-2011, Willow Garage Inc., all rights reserved.
00015 // Third party copyrights are property of their respective owners.
00016 //
00017 // Redistribution and use in source and binary forms, with or without modification,
00018 // are permitted provided that the following conditions are met:
00019 //
00020 //   * Redistribution's of source code must retain the above copyright notice,
00021 //     this list of conditions and the following disclaimer.
00022 //
00023 //   * Redistribution's in binary form must reproduce the above copyright notice,
00024 //     this list of conditions and the following disclaimer in the documentation
00025 //     and/or other materials provided with the distribution.
00026 //
00027 //   * The name of Intel Corporation may not be used to endorse or promote products
00028 //     derived from this software without specific prior written permission.
00029 //
00030 // This software is provided by the copyright holders and contributors "as is" and
00031 // any express or implied warranties, including, but not limited to, the implied
00032 // warranties of merchantability and fitness for a particular purpose are disclaimed.
00033 // In no event shall the Intel Corporation or contributors be liable for any direct,
00034 // indirect, incidental, special, exemplary, or consequential damages
00035 // (including, but not limited to, procurement of substitute goods or services;
00036 // loss of use, data, or profits; or business interruption) however caused
00037 // and on any theory of liability, whether in contract, strict liability,
00038 // or tort (including negligence or otherwise) arising in any way out of
00039 // the use of this software, even if advised of the possibility of such damage.
00040 //
00041 //M*/
00042 
00043 #ifndef __OPENCV_HYBRIDTRACKER_H_
00044 #define __OPENCV_HYBRIDTRACKER_H_
00045 
00046 #include "opencv2/core/core.hpp"
00047 #include "opencv2/core/operations.hpp"
00048 #include "opencv2/imgproc/imgproc.hpp"
00049 #include "opencv2/features2d/features2d.hpp"
00050 #include "opencv2/video/tracking.hpp"
00051 #include "opencv2/ml/ml.hpp"
00052 
00053 #ifdef __cplusplus
00054 
00055 namespace cv
00056 {
00057 
00058 // Motion model for tracking algorithm. Currently supports objects that do not move much.
00059 // To add Kalman filter
00060 struct CV_EXPORTS CvMotionModel
00061 {
00062     enum {LOW_PASS_FILTER = 0, KALMAN_FILTER = 1, EM = 2};
00063 
00064     CvMotionModel()
00065     {
00066     }
00067 
00068     float low_pass_gain;    // low pass gain
00069 };
00070 
00071 // Mean Shift Tracker parameters for specifying use of HSV channel and CamShift parameters.
00072 struct CV_EXPORTS CvMeanShiftTrackerParams
00073 {
00074     enum {  H = 0, HS = 1, HSV = 2  };
00075     CvMeanShiftTrackerParams(int tracking_type = CvMeanShiftTrackerParams::HS,
00076             CvTermCriteria term_crit = CvTermCriteria());
00077 
00078     int tracking_type;
00079     vector<float> h_range;
00080     vector<float> s_range;
00081     vector<float> v_range;
00082     CvTermCriteria term_crit;
00083 };
00084 
00085 // Feature tracking parameters
00086 struct CV_EXPORTS CvFeatureTrackerParams
00087 {
00088     enum {  SIFT = 0, SURF = 1, OPTICAL_FLOW = 2 };
00089     CvFeatureTrackerParams(int featureType = 0, int windowSize = 0)
00090     {
00091         feature_type = featureType;
00092         window_size = windowSize;
00093     }
00094 
00095     int feature_type; // Feature type to use
00096     int window_size; // Window size in pixels around which to search for new window
00097 };
00098 
00099 // Hybrid Tracking parameters for specifying weights of individual trackers and motion model.
00100 struct CV_EXPORTS CvHybridTrackerParams
00101 {
00102     CvHybridTrackerParams(float ft_tracker_weight = 0.5, float ms_tracker_weight = 0.5,
00103             CvFeatureTrackerParams ft_params = CvFeatureTrackerParams(),
00104             CvMeanShiftTrackerParams ms_params = CvMeanShiftTrackerParams(),
00105             CvMotionModel model = CvMotionModel());
00106 
00107     float ft_tracker_weight;
00108     float ms_tracker_weight;
00109     CvFeatureTrackerParams ft_params;
00110     CvMeanShiftTrackerParams ms_params;
00111     int motion_model;
00112     float low_pass_gain;
00113 };
00114 
00115 // Performs Camshift using parameters from MeanShiftTrackerParams
00116 class CV_EXPORTS CvMeanShiftTracker
00117 {
00118 private:
00119     Mat hsv, hue;
00120     Mat backproj;
00121     Mat mask, maskroi;
00122     MatND hist;
00123     Rect prev_trackwindow;
00124     RotatedRect prev_trackbox;
00125     Point2f prev_center;
00126 
00127 public:
00128     CvMeanShiftTrackerParams params;
00129 
00130     CvMeanShiftTracker();
00131     explicit CvMeanShiftTracker(CvMeanShiftTrackerParams _params);
00132     ~CvMeanShiftTracker();
00133     void newTrackingWindow(Mat image, Rect selection);
00134     RotatedRect updateTrackingWindow(Mat image);
00135     Mat getHistogramProjection(int type);
00136     void setTrackingWindow(Rect _window);
00137     Rect getTrackingWindow();
00138     RotatedRect getTrackingEllipse();
00139     Point2f getTrackingCenter();
00140 };
00141 
00142 // Performs SIFT/SURF feature tracking using parameters from FeatureTrackerParams
00143 class CV_EXPORTS CvFeatureTracker
00144 {
00145 private:
00146     Ptr<Feature2D> dd;
00147     Ptr<DescriptorMatcher> matcher;
00148     vector<DMatch> matches;
00149 
00150     Mat prev_image;
00151     Mat prev_image_bw;
00152     Rect prev_trackwindow;
00153     Point2d prev_center;
00154 
00155     int ittr;
00156     vector<Point2f> features[2];
00157 
00158 public:
00159     Mat disp_matches;
00160     CvFeatureTrackerParams params;
00161 
00162     CvFeatureTracker();
00163     explicit CvFeatureTracker(CvFeatureTrackerParams params);
00164     ~CvFeatureTracker();
00165     void newTrackingWindow(Mat image, Rect selection);
00166     Rect updateTrackingWindow(Mat image);
00167     Rect updateTrackingWindowWithSIFT(Mat image);
00168     Rect updateTrackingWindowWithFlow(Mat image);
00169     void setTrackingWindow(Rect _window);
00170     Rect getTrackingWindow();
00171     Point2f getTrackingCenter();
00172 };
00173 
00174 // Performs Hybrid Tracking and combines individual trackers using EM or filters
00175 class CV_EXPORTS CvHybridTracker
00176 {
00177 private:
00178     CvMeanShiftTracker* mstracker;
00179     CvFeatureTracker* fttracker;
00180 
00181     CvMat* samples;
00182     CvMat* labels;
00183 
00184     Rect prev_window;
00185     Point2f prev_center;
00186     Mat prev_proj;
00187     RotatedRect trackbox;
00188 
00189     int ittr;
00190     Point2f curr_center;
00191 
00192     inline float getL2Norm(Point2f p1, Point2f p2);
00193     Mat getDistanceProjection(Mat image, Point2f center);
00194     Mat getGaussianProjection(Mat image, int ksize, double sigma, Point2f center);
00195     void updateTrackerWithEM(Mat image);
00196     void updateTrackerWithLowPassFilter(Mat image);
00197 
00198 public:
00199     CvHybridTrackerParams params;
00200     CvHybridTracker();
00201     explicit CvHybridTracker(CvHybridTrackerParams params);
00202     ~CvHybridTracker();
00203 
00204     void newTracker(Mat image, Rect selection);
00205     void updateTracker(Mat image);
00206     Rect getTrackingWindow();
00207 };
00208 
00209 typedef CvMotionModel MotionModel;
00210 typedef CvMeanShiftTrackerParams MeanShiftTrackerParams;
00211 typedef CvFeatureTrackerParams FeatureTrackerParams;
00212 typedef CvHybridTrackerParams HybridTrackerParams;
00213 typedef CvMeanShiftTracker MeanShiftTracker;
00214 typedef CvFeatureTracker FeatureTracker;
00215 typedef CvHybridTracker HybridTracker;
00216 }
00217 
00218 #endif
00219 
00220 #endif