1 //*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
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.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
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.
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.
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.
42 #include "precomp.hpp"
45 #include "opencv2/calib3d.hpp"
46 #include "opencv2/contrib/hybridtracker.hpp"
48 #ifdef HAVE_OPENCV_NONFREE
49 #include "opencv2/nonfree/nonfree.hpp"
51 static bool makeUseOfNonfree = initModule_nonfree();
56 CvFeatureTracker::CvFeatureTracker(CvFeatureTrackerParams _params) :
59 switch (params.feature_type)
61 case CvFeatureTrackerParams::SIFT:
62 dd = Algorithm::create<Feature2D>("Feature2D.SIFT");
64 CV_Error(CV_StsNotImplemented, "OpenCV has been compiled without SIFT support");
65 dd->set("nOctaveLayers", 5);
66 dd->set("contrastThreshold", 0.04);
67 dd->set("edgeThreshold", 10.7);
69 case CvFeatureTrackerParams::SURF:
70 dd = Algorithm::create<Feature2D>("Feature2D.SURF");
72 CV_Error(CV_StsNotImplemented, "OpenCV has been compiled without SURF support");
73 dd->set("hessianThreshold", 400);
74 dd->set("nOctaves", 3);
75 dd->set("nOctaveLayers", 4);
78 CV_Error(CV_StsBadArg, "Unknown feature type");
82 matcher = makePtr<BFMatcher>(int(NORM_L2));
85 CvFeatureTracker::~CvFeatureTracker()
89 void CvFeatureTracker::newTrackingWindow(Mat image, Rect selection)
91 image.copyTo(prev_image);
92 cvtColor(prev_image, prev_image_bw, COLOR_BGR2GRAY);
93 prev_trackwindow = selection;
94 prev_center.x = selection.x;
95 prev_center.y = selection.y;
99 Rect CvFeatureTracker::updateTrackingWindow(Mat image)
101 if(params.feature_type == CvFeatureTrackerParams::OPTICAL_FLOW)
102 return updateTrackingWindowWithFlow(image);
104 return updateTrackingWindowWithSIFT(image);
107 Rect CvFeatureTracker::updateTrackingWindowWithSIFT(Mat image)
110 std::vector<KeyPoint> prev_keypoints, curr_keypoints;
111 std::vector<Point2f> prev_keys, curr_keys;
112 Mat prev_desc, curr_desc;
114 Rect window = prev_trackwindow;
115 Mat mask = Mat::zeros(image.size(), CV_8UC1);
116 rectangle(mask, Point(window.x, window.y), Point(window.x + window.width,
117 window.y + window.height), Scalar(255), CV_FILLED);
119 dd->operator()(prev_image, mask, prev_keypoints, prev_desc);
121 window.x -= params.window_size;
122 window.y -= params.window_size;
123 window.width += params.window_size;
124 window.height += params.window_size;
125 rectangle(mask, Point(window.x, window.y), Point(window.x + window.width,
126 window.y + window.height), Scalar(255), CV_FILLED);
128 dd->operator()(image, mask, curr_keypoints, curr_desc);
130 if (prev_keypoints.size() > 4 && curr_keypoints.size() > 4)
132 //descriptor->compute(prev_image, prev_keypoints, prev_desc);
133 //descriptor->compute(image, curr_keypoints, curr_desc);
135 matcher->match(prev_desc, curr_desc, matches);
137 for (int i = 0; i < (int)matches.size(); i++)
139 prev_keys.push_back(prev_keypoints[matches[i].queryIdx].pt);
140 curr_keys.push_back(curr_keypoints[matches[i].trainIdx].pt);
143 Mat T = findHomography(prev_keys, curr_keys, LMEDS);
145 prev_trackwindow.x += cvRound(T.at<double> (0, 2));
146 prev_trackwindow.y += cvRound(T.at<double> (1, 2));
149 prev_center.x = prev_trackwindow.x;
150 prev_center.y = prev_trackwindow.y;
152 return prev_trackwindow;
155 Rect CvFeatureTracker::updateTrackingWindowWithFlow(Mat image)
158 Size subPixWinSize(10,10), winSize(31,31);
160 TermCriteria termcrit(TermCriteria::COUNT | TermCriteria::EPS, 20, 0.03);
161 std::vector<uchar> status;
162 std::vector<float> err;
164 cvtColor(image, image_bw, COLOR_BGR2GRAY);
165 cvtColor(prev_image, prev_image_bw, COLOR_BGR2GRAY);
169 Mat mask = Mat::zeros(image.size(), CV_8UC1);
170 rectangle(mask, Point(prev_trackwindow.x, prev_trackwindow.y), Point(
171 prev_trackwindow.x + prev_trackwindow.width, prev_trackwindow.y
172 + prev_trackwindow.height), Scalar(255), CV_FILLED);
173 goodFeaturesToTrack(image_bw, features[1], 500, 0.01, 20, mask, 3, 0, 0.04);
174 cornerSubPix(image_bw, features[1], subPixWinSize, Size(-1, -1), termcrit);
178 calcOpticalFlowPyrLK(prev_image_bw, image_bw, features[0], features[1],
179 status, err, winSize, 3, termcrit);
181 Point2f feature0_center(0, 0);
182 Point2f feature1_center(0, 0);
184 for (int i = 0; i < (int)features[1].size(); i++)
188 feature0_center.x += features[0][i].x;
189 feature0_center.y += features[0][i].y;
190 feature1_center.x += features[1][i].x;
191 feature1_center.y += features[1][i].y;
196 feature0_center.x /= goodtracks;
197 feature0_center.y /= goodtracks;
198 feature1_center.x /= goodtracks;
199 feature1_center.y /= goodtracks;
201 prev_center.x += (feature1_center.x - feature0_center.x);
202 prev_center.y += (feature1_center.y - feature0_center.y);
204 prev_trackwindow.x = (int)prev_center.x;
205 prev_trackwindow.y = (int)prev_center.y;
208 swap(features[0], features[1]);
209 image.copyTo(prev_image);
210 return prev_trackwindow;
213 void CvFeatureTracker::setTrackingWindow(Rect _window)
215 prev_trackwindow = _window;
218 Rect CvFeatureTracker::getTrackingWindow()
220 return prev_trackwindow;
223 Point2f CvFeatureTracker::getTrackingCenter()
225 Point2f center(0, 0);
226 center.x = (float)(prev_center.x + prev_trackwindow.width/2.0);
227 center.y = (float)(prev_center.y + prev_trackwindow.height/2.0);