CLAHE Python bindings
[profile/ivi/opencv.git] / modules / contrib / src / featuretracker.cpp
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 #include "precomp.hpp"
43 #include <stdio.h>
44 #include <iostream>
45 #include "opencv2/calib3d/calib3d.hpp"
46 #include "opencv2/contrib/hybridtracker.hpp"
47
48 using namespace cv;
49
50 CvFeatureTracker::CvFeatureTracker(CvFeatureTrackerParams _params) :
51     params(_params)
52 {
53     switch (params.feature_type)
54     {
55     case CvFeatureTrackerParams::SIFT:
56         dd = Algorithm::create<Feature2D>("Feature2D.SIFT");
57         if( dd.empty() )
58             CV_Error(CV_StsNotImplemented, "OpenCV has been compiled without SIFT support");
59         dd->set("nOctaveLayers", 5);
60         dd->set("contrastThreshold", 0.04);
61         dd->set("edgeThreshold", 10.7);
62     case CvFeatureTrackerParams::SURF:
63         dd = Algorithm::create<Feature2D>("Feature2D.SURF");
64         if( dd.empty() )
65             CV_Error(CV_StsNotImplemented, "OpenCV has been compiled without SURF support");
66         dd->set("hessianThreshold", 400);
67         dd->set("nOctaves", 3);
68         dd->set("nOctaveLayers", 4);
69     default:
70         CV_Error(CV_StsBadArg, "Unknown feature type");
71     }
72
73     matcher = new BFMatcher(NORM_L2);
74 }
75
76 CvFeatureTracker::~CvFeatureTracker()
77 {
78 }
79
80 void CvFeatureTracker::newTrackingWindow(Mat image, Rect selection)
81 {
82     image.copyTo(prev_image);
83     cvtColor(prev_image, prev_image_bw, CV_BGR2GRAY);
84     prev_trackwindow = selection;
85     prev_center.x = selection.x;
86     prev_center.y = selection.y;
87     ittr = 0;
88 }
89
90 Rect CvFeatureTracker::updateTrackingWindow(Mat image)
91 {
92     if(params.feature_type == CvFeatureTrackerParams::OPTICAL_FLOW)
93         return updateTrackingWindowWithFlow(image);
94     else
95         return updateTrackingWindowWithSIFT(image);
96 }
97
98 Rect CvFeatureTracker::updateTrackingWindowWithSIFT(Mat image)
99 {
100     ittr++;
101     vector<KeyPoint> prev_keypoints, curr_keypoints;
102     vector<Point2f> prev_keys, curr_keys;
103     Mat prev_desc, curr_desc;
104
105     Rect window = prev_trackwindow;
106     Mat mask = Mat::zeros(image.size(), CV_8UC1);
107     rectangle(mask, Point(window.x, window.y), Point(window.x + window.width,
108             window.y + window.height), Scalar(255), CV_FILLED);
109
110     dd->operator()(prev_image, mask, prev_keypoints, prev_desc);
111
112     window.x -= params.window_size;
113     window.y -= params.window_size;
114     window.width += params.window_size;
115     window.height += params.window_size;
116     rectangle(mask, Point(window.x, window.y), Point(window.x + window.width,
117             window.y + window.height), Scalar(255), CV_FILLED);
118
119     dd->operator()(image, mask, curr_keypoints, curr_desc);
120
121     if (prev_keypoints.size() > 4 && curr_keypoints.size() > 4)
122     {
123         //descriptor->compute(prev_image, prev_keypoints, prev_desc);
124         //descriptor->compute(image, curr_keypoints, curr_desc);
125
126         matcher->match(prev_desc, curr_desc, matches);
127
128         for (int i = 0; i < (int)matches.size(); i++)
129         {
130             prev_keys.push_back(prev_keypoints[matches[i].queryIdx].pt);
131             curr_keys.push_back(curr_keypoints[matches[i].trainIdx].pt);
132         }
133
134         Mat T = findHomography(prev_keys, curr_keys, CV_LMEDS);
135
136         prev_trackwindow.x += cvRound(T.at<double> (0, 2));
137         prev_trackwindow.y += cvRound(T.at<double> (1, 2));
138     }
139
140     prev_center.x = prev_trackwindow.x;
141     prev_center.y = prev_trackwindow.y;
142     prev_image = image;
143     return prev_trackwindow;
144 }
145
146 Rect CvFeatureTracker::updateTrackingWindowWithFlow(Mat image)
147 {
148     ittr++;
149     Size subPixWinSize(10,10), winSize(31,31);
150     Mat image_bw;
151     TermCriteria termcrit(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 20, 0.03);
152     vector<uchar> status;
153     vector<float> err;
154
155     cvtColor(image, image_bw, CV_BGR2GRAY);
156     cvtColor(prev_image, prev_image_bw, CV_BGR2GRAY);
157
158     if (ittr == 1)
159     {
160         Mat mask = Mat::zeros(image.size(), CV_8UC1);
161         rectangle(mask, Point(prev_trackwindow.x, prev_trackwindow.y), Point(
162                 prev_trackwindow.x + prev_trackwindow.width, prev_trackwindow.y
163                         + prev_trackwindow.height), Scalar(255), CV_FILLED);
164         goodFeaturesToTrack(image_bw, features[1], 500, 0.01, 20, mask, 3, 0, 0.04);
165         cornerSubPix(image_bw, features[1], subPixWinSize, Size(-1, -1), termcrit);
166     }
167     else
168     {
169         calcOpticalFlowPyrLK(prev_image_bw, image_bw, features[0], features[1],
170                 status, err, winSize, 3, termcrit);
171
172         Point2f feature0_center(0, 0);
173         Point2f feature1_center(0, 0);
174         int goodtracks = 0;
175         for (int i = 0; i < (int)features[1].size(); i++)
176         {
177             if (status[i] == 1)
178             {
179                 feature0_center.x += features[0][i].x;
180                 feature0_center.y += features[0][i].y;
181                 feature1_center.x += features[1][i].x;
182                 feature1_center.y += features[1][i].y;
183                 goodtracks++;
184             }
185         }
186
187         feature0_center.x /= goodtracks;
188         feature0_center.y /= goodtracks;
189         feature1_center.x /= goodtracks;
190         feature1_center.y /= goodtracks;
191
192         prev_center.x += (feature1_center.x - feature0_center.x);
193         prev_center.y += (feature1_center.y - feature0_center.y);
194
195         prev_trackwindow.x = (int)prev_center.x;
196         prev_trackwindow.y = (int)prev_center.y;
197     }
198
199     swap(features[0], features[1]);
200     image.copyTo(prev_image);
201     return prev_trackwindow;
202 }
203
204 void CvFeatureTracker::setTrackingWindow(Rect _window)
205 {
206     prev_trackwindow = _window;
207 }
208
209 Rect CvFeatureTracker::getTrackingWindow()
210 {
211     return prev_trackwindow;
212 }
213
214 Point2f CvFeatureTracker::getTrackingCenter()
215 {
216     Point2f center(0, 0);
217     center.x = (float)(prev_center.x + prev_trackwindow.width/2.0);
218     center.y = (float)(prev_center.y + prev_trackwindow.height/2.0);
219     return center;
220 }
221