Skip to content

Commit 188ca4a

Browse files
committed
Merge pull request #1423 from berak:mosse_tracker
2 parents 3f8e435 + ea6f3d1 commit 188ca4a

File tree

6 files changed

+295
-1
lines changed

6 files changed

+295
-1
lines changed

modules/tracking/doc/tracking.bib

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,10 @@ @inproceedings{GOTURN
100100
booktitle = {European Conference Computer Vision (ECCV)},
101101
year = {2016}
102102
}
103+
104+
@inproceedings{MOSSE,
105+
title={Visual Object Tracking using Adaptive Correlation Filters},
106+
author={Bolme, David S. and Beveridge, J. Ross and Draper, Bruce A. and Lui Yui, Man},
107+
booktitle = {Conference on Computer Vision and Pattern Recognition (CVPR)},
108+
year = {2010}
109+
}

modules/tracking/include/opencv2/tracking/tracker.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,6 +1297,22 @@ class CV_EXPORTS_W TrackerGOTURN : public Tracker
12971297
virtual ~TrackerGOTURN() {}
12981298
};
12991299

1300+
/** @brief the MOSSE tracker
1301+
note, that this tracker works with grayscale images, if passed bgr ones, they will get converted internally.
1302+
@cite MOSSE Visual Object Tracking using Adaptive Correlation Filters
1303+
*/
1304+
1305+
class CV_EXPORTS_W TrackerMOSSE : public Tracker
1306+
{
1307+
public:
1308+
/** @brief Constructor
1309+
*/
1310+
CV_WRAP static Ptr<TrackerMOSSE> create();
1311+
1312+
virtual ~TrackerMOSSE() {}
1313+
};
1314+
1315+
13001316
/************************************ MultiTracker Class ---By Laksono Kurnianggoro---) ************************************/
13011317
/** @brief This class is used to track multiple objects using the specified tracker algorithm.
13021318
* The MultiTracker is naive implementation of multiple object tracking.

modules/tracking/samples/samples_utility.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ inline cv::Ptr<cv::Tracker> createTrackerByName(cv::String name)
1919
tracker = cv::TrackerMIL::create();
2020
else if (name == "GOTURN")
2121
tracker = cv::TrackerGOTURN::create();
22+
else if (name == "MOSSE")
23+
tracker = cv::TrackerMOSSE::create();
2224
else
2325
CV_Error(cv::Error::StsBadArg, "Invalid tracking algorithm name\n");
2426

modules/tracking/samples/tracker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ static void help()
2222
"Example of <video_name> is in opencv_extra/testdata/cv/tracking/\n"
2323
"Call:\n"
2424
"./tracker <tracker_algorithm> <video_name> <start_frame> [<bounding_frame>]\n"
25-
"tracker_algorithm can be: MIL, BOOSTING, MEDIANFLOW, TLD\n"
25+
"tracker_algorithm can be: MIL, BOOSTING, MEDIANFLOW, TLD, KCF, GOTURN, MOSSE.\n"
2626
<< endl;
2727

2828
cout << "\n\nHot keys: \n"

modules/tracking/src/mosseTracker.cpp

Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
// This file is part of the OpenCV project.
2+
// It is subject to the license terms in the LICENSE file found in the top-level directory
3+
// of this distribution and at http://opencv.org/license.html.
4+
5+
//
6+
//[1] David S. Bolme et al. "Visual Object Tracking using Adaptive Correlation Filters"
7+
// http://www.cs.colostate.edu/~draper/papers/bolme_cvpr10.pdf
8+
//
9+
10+
//
11+
// credits:
12+
// Kun-Hsin Chen: for initial c++ code
13+
// Cracki: for the idea of only converting the used patch to gray
14+
//
15+
16+
#include "opencv2/tracking.hpp"
17+
18+
namespace cv {
19+
namespace tracking {
20+
21+
struct DummyModel : TrackerModel
22+
{
23+
virtual void modelUpdateImpl(){}
24+
virtual void modelEstimationImpl( const std::vector<Mat>& ){}
25+
};
26+
27+
28+
const double eps=0.00001; // for normalization
29+
const double rate=0.2; // learning rate
30+
const double psrThreshold=5.7; // no detection, if PSR is smaller than this
31+
32+
33+
struct MosseImpl : TrackerMOSSE
34+
{
35+
protected:
36+
37+
Point2d center; //center of the bounding box
38+
Size size; //size of the bounding box
39+
Mat hanWin;
40+
Mat G; //goal
41+
Mat H, A, B; //state
42+
43+
// Element-wise division of complex numbers in src1 and src2
44+
Mat divDFTs( const Mat &src1, const Mat &src2 ) const
45+
{
46+
Mat c1[2],c2[2],a1,a2,s1,s2,denom,re,im;
47+
48+
// split into re and im per src
49+
cv::split(src1, c1);
50+
cv::split(src2, c2);
51+
52+
// (Re2*Re2 + Im2*Im2) = denom
53+
// denom is same for both channels
54+
cv::multiply(c2[0], c2[0], s1);
55+
cv::multiply(c2[1], c2[1], s2);
56+
cv::add(s1, s2, denom);
57+
58+
// (Re1*Re2 + Im1*Im1)/(Re2*Re2 + Im2*Im2) = Re
59+
cv::multiply(c1[0], c2[0], a1);
60+
cv::multiply(c1[1], c2[1], a2);
61+
cv::divide(a1+a2, denom, re, 1.0 );
62+
63+
// (Im1*Re2 - Re1*Im2)/(Re2*Re2 + Im2*Im2) = Im
64+
cv::multiply(c1[1], c2[0], a1);
65+
cv::multiply(c1[0], c2[1], a2);
66+
cv::divide(a1+a2, denom, im, -1.0);
67+
68+
// Merge Re and Im back into a complex matrix
69+
Mat dst, chn[] = {re,im};
70+
cv::merge(chn, 2, dst);
71+
return dst;
72+
}
73+
74+
75+
void preProcess( Mat &window ) const
76+
{
77+
window.convertTo(window, CV_32F);
78+
log(window + 1.0f, window);
79+
80+
//normalize
81+
Scalar mean,StdDev;
82+
meanStdDev(window, mean, StdDev);
83+
window = (window-mean[0]) / (StdDev[0]+eps);
84+
85+
//Gaussain weighting
86+
window = window.mul(hanWin);
87+
}
88+
89+
90+
double correlate( const Mat &image_sub, Point &delta_xy ) const
91+
{
92+
Mat IMAGE_SUB, RESPONSE, response;
93+
// filter in dft space
94+
dft(image_sub, IMAGE_SUB, DFT_COMPLEX_OUTPUT);
95+
mulSpectrums(IMAGE_SUB, H, RESPONSE, 0, true );
96+
idft(RESPONSE, response, DFT_SCALE|DFT_REAL_OUTPUT);
97+
// update center position
98+
double maxVal; Point maxLoc;
99+
minMaxLoc(response, 0, &maxVal, 0, &maxLoc);
100+
delta_xy.x = maxLoc.x - int(response.size().width/2);
101+
delta_xy.y = maxLoc.y - int(response.size().height/2);
102+
// normalize response
103+
Scalar mean,std;
104+
meanStdDev(response, mean, std);
105+
return (maxVal-mean[0]) / (std[0]+eps); // PSR
106+
}
107+
108+
109+
Mat randWarp( const Mat& a ) const
110+
{
111+
cv::RNG rng(8031965);
112+
113+
// random rotation
114+
double C=0.1;
115+
double ang = rng.uniform(-C,C);
116+
double c=cos(ang), s=sin(ang);
117+
// affine warp matrix
118+
Mat_<float> W(2,3);
119+
W << c + rng.uniform(-C,C), -s + rng.uniform(-C,C), 0,
120+
s + rng.uniform(-C,C), c + rng.uniform(-C,C), 0;
121+
122+
// random translation
123+
Mat_<float> center_warp(2, 1);
124+
center_warp << a.cols/2, a.rows/2;
125+
W.col(2) = center_warp - (W.colRange(0, 2))*center_warp;
126+
127+
Mat warped;
128+
warpAffine(a, warped, W, a.size(), BORDER_REFLECT);
129+
return warped;
130+
}
131+
132+
133+
virtual bool initImpl( const Mat& image, const Rect2d& boundingBox )
134+
{
135+
model = makePtr<DummyModel>();
136+
137+
Mat img;
138+
if (image.channels() == 1)
139+
img = image;
140+
else
141+
cvtColor(image, img, COLOR_BGR2GRAY);
142+
143+
int w = getOptimalDFTSize(int(boundingBox.width));
144+
int h = getOptimalDFTSize(int(boundingBox.height));
145+
146+
//Get the center position
147+
int x1 = int(floor((2*boundingBox.x+boundingBox.width-w)/2));
148+
int y1 = int(floor((2*boundingBox.y+boundingBox.height-h)/2));
149+
center.x = x1 + (w)/2;
150+
center.y = y1 + (h)/2;
151+
size.width = w;
152+
size.height = h;
153+
154+
Mat window;
155+
getRectSubPix(img, size, center, window);
156+
createHanningWindow(hanWin, size, CV_32F);
157+
158+
// goal
159+
Mat g=Mat::zeros(size,CV_32F);
160+
g.at<float>(h/2, w/2) = 1;
161+
GaussianBlur(g, g, Size(-1,-1), 2.0);
162+
double maxVal;
163+
minMaxLoc(g, 0, &maxVal);
164+
g = g / maxVal;
165+
dft(g, G, DFT_COMPLEX_OUTPUT);
166+
167+
// initial A,B and H
168+
A = Mat::zeros(G.size(), G.type());
169+
B = Mat::zeros(G.size(), G.type());
170+
for(int i=0; i<8; i++)
171+
{
172+
Mat window_warp = randWarp(window);
173+
preProcess(window_warp);
174+
175+
Mat WINDOW_WARP, A_i, B_i;
176+
dft(window_warp, WINDOW_WARP, DFT_COMPLEX_OUTPUT);
177+
mulSpectrums(G , WINDOW_WARP, A_i, 0, true);
178+
mulSpectrums(WINDOW_WARP, WINDOW_WARP, B_i, 0, true);
179+
A+=A_i;
180+
B+=B_i;
181+
}
182+
H = divDFTs(A,B);
183+
return true;
184+
}
185+
186+
virtual bool updateImpl( const Mat& image, Rect2d& boundingBox )
187+
{
188+
if (H.empty()) // not initialized
189+
return false;
190+
191+
Mat image_sub;
192+
getRectSubPix(image, size, center, image_sub);
193+
194+
if (image_sub.channels() != 1)
195+
cvtColor(image_sub, image_sub, COLOR_BGR2GRAY);
196+
preProcess(image_sub);
197+
198+
Point delta_xy;
199+
double PSR = correlate(image_sub, delta_xy);
200+
if (PSR < psrThreshold)
201+
return false;
202+
203+
//update location
204+
center.x += delta_xy.x;
205+
center.y += delta_xy.y;
206+
207+
Mat img_sub_new;
208+
getRectSubPix(image, size, center, img_sub_new);
209+
if (img_sub_new.channels() != 1)
210+
cvtColor(img_sub_new, img_sub_new, COLOR_BGR2GRAY);
211+
preProcess(img_sub_new);
212+
213+
// new state for A and B
214+
Mat F, A_new, B_new;
215+
dft(img_sub_new, F, DFT_COMPLEX_OUTPUT);
216+
mulSpectrums(G, F, A_new, 0, true );
217+
mulSpectrums(F, F, B_new, 0, true );
218+
219+
// update A ,B, and H
220+
A = A*(1-rate) + A_new*rate;
221+
B = B*(1-rate) + B_new*rate;
222+
H = divDFTs(A, B);
223+
224+
// return tracked rect
225+
double x=center.x, y=center.y;
226+
int w = size.width, h=size.height;
227+
boundingBox = Rect2d(Point2d(x-0.5*w, y-0.5*h), Point2d(x+0.5*w, y+0.5*h));
228+
return true;
229+
}
230+
231+
public:
232+
MosseImpl() { isInit = 0; }
233+
234+
// dummy implementation.
235+
virtual void read( const FileNode& ){}
236+
virtual void write( FileStorage& ) const{}
237+
238+
}; // MosseImpl
239+
240+
} // tracking
241+
242+
243+
Ptr<TrackerMOSSE> TrackerMOSSE::create()
244+
{
245+
return makePtr<tracking::MosseImpl>();
246+
}
247+
248+
249+
} // cv

modules/tracking/test/test_trackers.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,13 @@ TEST_P(DistanceAndOverlap, DISABLED_TLD)
464464
TrackerTest test( TrackerTLD::create(), dataset, 60, .4f, NoTransform);
465465
test.run();
466466
}
467+
468+
TEST_P(DistanceAndOverlap, MOSSE)
469+
{
470+
TrackerTest test( TrackerMOSSE::create(), dataset, 22, .7f, NoTransform);
471+
test.run();
472+
}
473+
467474
/***************************************************************************************/
468475
//Tests with shifted initial window
469476
TEST_P(DistanceAndOverlap, Shifted_Data_MedianFlow)
@@ -495,6 +502,12 @@ TEST_P(DistanceAndOverlap, DISABLED_Shifted_Data_TLD)
495502
TrackerTest test( TrackerTLD::create(), dataset, 120, .2f, CenterShiftLeft);
496503
test.run();
497504
}
505+
506+
TEST_P(DistanceAndOverlap, Shifted_Data_MOSSE)
507+
{
508+
TrackerTest test( TrackerMOSSE::create(), dataset, 13, .69f, CenterShiftLeft);
509+
test.run();
510+
}
498511
/***************************************************************************************/
499512
//Tests with scaled initial window
500513
TEST_P(DistanceAndOverlap, Scaled_Data_MedianFlow)
@@ -534,6 +547,13 @@ TEST_P(DistanceAndOverlap, DISABLED_GOTURN)
534547
test.run();
535548
}
536549

550+
TEST_P(DistanceAndOverlap, Scaled_Data_MOSSE)
551+
{
552+
TrackerTest test( TrackerMOSSE::create(), dataset, 22, 0.69f, Scale_1_1, 1);
553+
test.run();
554+
}
555+
556+
537557
INSTANTIATE_TEST_CASE_P( Tracking, DistanceAndOverlap, TESTSET_NAMES);
538558

539559
/* End of file. */

0 commit comments

Comments
 (0)