OpenCV 2.4.8 components for OpenCVgrabber.
[mmanager-3rdparty.git] / OpenCV2.4.8 / build / include / opencv2 / contrib / hybridtracker.hpp
1 //*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                                License Agreement
11 //                       For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2008-2011, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 //   * Redistribution's of source code must retain the above copyright notice,
21 //     this list of conditions and the following disclaimer.
22 //
23 //   * Redistribution's in binary form must reproduce the above copyright notice,
24 //     this list of conditions and the following disclaimer in the documentation
25 //     and/or other materials provided with the distribution.
26 //
27 //   * The name of Intel Corporation may not be used to endorse or promote products
28 //     derived from this software without specific prior written permission.
29 //
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
40 //
41 //M*/
42
43 #ifndef __OPENCV_HYBRIDTRACKER_H_
44 #define __OPENCV_HYBRIDTRACKER_H_
45
46 #include "opencv2/core/core.hpp"
47 #include "opencv2/core/operations.hpp"
48 #include "opencv2/imgproc/imgproc.hpp"
49 #include "opencv2/features2d/features2d.hpp"
50 #include "opencv2/video/tracking.hpp"
51 #include "opencv2/ml/ml.hpp"
52
53 #ifdef __cplusplus
54
55 namespace cv
56 {
57
58 // Motion model for tracking algorithm. Currently supports objects that do not move much.
59 // To add Kalman filter
60 struct CV_EXPORTS CvMotionModel
61 {
62     enum {LOW_PASS_FILTER = 0, KALMAN_FILTER = 1, EM = 2};
63
64     CvMotionModel()
65     {
66     }
67
68     float low_pass_gain;    // low pass gain
69 };
70
71 // Mean Shift Tracker parameters for specifying use of HSV channel and CamShift parameters.
72 struct CV_EXPORTS CvMeanShiftTrackerParams
73 {
74     enum {  H = 0, HS = 1, HSV = 2  };
75     CvMeanShiftTrackerParams(int tracking_type = CvMeanShiftTrackerParams::HS,
76             CvTermCriteria term_crit = CvTermCriteria());
77
78     int tracking_type;
79     vector<float> h_range;
80     vector<float> s_range;
81     vector<float> v_range;
82     CvTermCriteria term_crit;
83 };
84
85 // Feature tracking parameters
86 struct CV_EXPORTS CvFeatureTrackerParams
87 {
88     enum {  SIFT = 0, SURF = 1, OPTICAL_FLOW = 2 };
89     CvFeatureTrackerParams(int featureType = 0, int windowSize = 0)
90     {
91         feature_type = featureType;
92         window_size = windowSize;
93     }
94
95     int feature_type; // Feature type to use
96     int window_size; // Window size in pixels around which to search for new window
97 };
98
99 // Hybrid Tracking parameters for specifying weights of individual trackers and motion model.
100 struct CV_EXPORTS CvHybridTrackerParams
101 {
102     CvHybridTrackerParams(float ft_tracker_weight = 0.5, float ms_tracker_weight = 0.5,
103             CvFeatureTrackerParams ft_params = CvFeatureTrackerParams(),
104             CvMeanShiftTrackerParams ms_params = CvMeanShiftTrackerParams(),
105             CvMotionModel model = CvMotionModel());
106
107     float ft_tracker_weight;
108     float ms_tracker_weight;
109     CvFeatureTrackerParams ft_params;
110     CvMeanShiftTrackerParams ms_params;
111     int motion_model;
112     float low_pass_gain;
113 };
114
115 // Performs Camshift using parameters from MeanShiftTrackerParams
116 class CV_EXPORTS CvMeanShiftTracker
117 {
118 private:
119     Mat hsv, hue;
120     Mat backproj;
121     Mat mask, maskroi;
122     MatND hist;
123     Rect prev_trackwindow;
124     RotatedRect prev_trackbox;
125     Point2f prev_center;
126
127 public:
128     CvMeanShiftTrackerParams params;
129
130     CvMeanShiftTracker();
131     explicit CvMeanShiftTracker(CvMeanShiftTrackerParams _params);
132     ~CvMeanShiftTracker();
133     void newTrackingWindow(Mat image, Rect selection);
134     RotatedRect updateTrackingWindow(Mat image);
135     Mat getHistogramProjection(int type);
136     void setTrackingWindow(Rect _window);
137     Rect getTrackingWindow();
138     RotatedRect getTrackingEllipse();
139     Point2f getTrackingCenter();
140 };
141
142 // Performs SIFT/SURF feature tracking using parameters from FeatureTrackerParams
143 class CV_EXPORTS CvFeatureTracker
144 {
145 private:
146     Ptr<Feature2D> dd;
147     Ptr<DescriptorMatcher> matcher;
148     vector<DMatch> matches;
149
150     Mat prev_image;
151     Mat prev_image_bw;
152     Rect prev_trackwindow;
153     Point2d prev_center;
154
155     int ittr;
156     vector<Point2f> features[2];
157
158 public:
159     Mat disp_matches;
160     CvFeatureTrackerParams params;
161
162     CvFeatureTracker();
163     explicit CvFeatureTracker(CvFeatureTrackerParams params);
164     ~CvFeatureTracker();
165     void newTrackingWindow(Mat image, Rect selection);
166     Rect updateTrackingWindow(Mat image);
167     Rect updateTrackingWindowWithSIFT(Mat image);
168     Rect updateTrackingWindowWithFlow(Mat image);
169     void setTrackingWindow(Rect _window);
170     Rect getTrackingWindow();
171     Point2f getTrackingCenter();
172 };
173
174 // Performs Hybrid Tracking and combines individual trackers using EM or filters
175 class CV_EXPORTS CvHybridTracker
176 {
177 private:
178     CvMeanShiftTracker* mstracker;
179     CvFeatureTracker* fttracker;
180
181     CvMat* samples;
182     CvMat* labels;
183
184     Rect prev_window;
185     Point2f prev_center;
186     Mat prev_proj;
187     RotatedRect trackbox;
188
189     int ittr;
190     Point2f curr_center;
191
192     inline float getL2Norm(Point2f p1, Point2f p2);
193     Mat getDistanceProjection(Mat image, Point2f center);
194     Mat getGaussianProjection(Mat image, int ksize, double sigma, Point2f center);
195     void updateTrackerWithEM(Mat image);
196     void updateTrackerWithLowPassFilter(Mat image);
197
198 public:
199     CvHybridTrackerParams params;
200     CvHybridTracker();
201     explicit CvHybridTracker(CvHybridTrackerParams params);
202     ~CvHybridTracker();
203
204     void newTracker(Mat image, Rect selection);
205     void updateTracker(Mat image);
206     Rect getTrackingWindow();
207 };
208
209 typedef CvMotionModel MotionModel;
210 typedef CvMeanShiftTrackerParams MeanShiftTrackerParams;
211 typedef CvFeatureTrackerParams FeatureTrackerParams;
212 typedef CvHybridTrackerParams HybridTrackerParams;
213 typedef CvMeanShiftTracker MeanShiftTracker;
214 typedef CvFeatureTracker FeatureTracker;
215 typedef CvHybridTracker HybridTracker;
216 }
217
218 #endif
219
220 #endif