Skip to content

Commit 3e9b1f6

Browse files
committed
Merge pull request #1123 from sovrasov:tracker_kcf_conf
2 parents 6ffdd0f + 4ae95b6 commit 3e9b1f6

File tree

3 files changed

+19
-121
lines changed

3 files changed

+19
-121
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,7 @@ class CV_EXPORTS_W TrackerKCF : public Tracker
12361236
*/
12371237
void write(FileStorage& /*fs*/) const;
12381238

1239+
double detect_thresh; //!< detection confidence threshold
12391240
double sigma; //!< gaussian kernel bandwidth
12401241
double lambda; //!< regularization
12411242
double interp_factor; //!< linear interpolation factor for adaptation

modules/tracking/samples/kcf.cpp

Lines changed: 9 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,11 @@
1313
#include <opencv2/highgui.hpp>
1414
#include <iostream>
1515
#include <cstring>
16+
#include "samples_utility.hpp"
1617

1718
using namespace std;
1819
using namespace cv;
1920

20-
class BoxExtractor {
21-
public:
22-
Rect2d extract(Mat img);
23-
Rect2d extract(const std::string& windowName, Mat img, bool showCrossair = true);
24-
25-
struct handlerT{
26-
bool isDrawing;
27-
Rect2d box;
28-
Mat image;
29-
30-
// initializer list
31-
handlerT(): isDrawing(false) {};
32-
}params;
33-
34-
private:
35-
static void mouseHandler(int event, int x, int y, int flags, void *param);
36-
void opencv_mouse_callback( int event, int x, int y, int , void *param );
37-
};
38-
3921
int main( int argc, char** argv ){
4022
// show help
4123
if(argc<2){
@@ -48,9 +30,6 @@ int main( int argc, char** argv ){
4830
return 0;
4931
}
5032

51-
// ROI selector
52-
BoxExtractor box;
53-
5433
// create the tracker
5534
Ptr<Tracker> tracker = TrackerKCF::create();
5635

@@ -62,7 +41,7 @@ int main( int argc, char** argv ){
6241

6342
// get bounding box
6443
cap >> frame;
65-
Rect2d roi=box.extract("tracker",frame);
44+
Rect2d roi= selectROI("tracker", frame, true, false);
6645

6746
//quit if ROI was not selected
6847
if(roi.width==0 || roi.height==0)
@@ -82,7 +61,13 @@ int main( int argc, char** argv ){
8261
break;
8362

8463
// update the tracking result
85-
tracker->update(frame,roi);
64+
bool isfound = tracker->update(frame,roi);
65+
if(!isfound)
66+
{
67+
cout << "The target has been lost...\n";
68+
waitKey(0);
69+
return 0;
70+
}
8671

8772
// draw the tracked object
8873
rectangle( frame, roi, Scalar( 255, 0, 0 ), 2, 1 );
@@ -95,100 +80,3 @@ int main( int argc, char** argv ){
9580
}
9681

9782
}
98-
99-
void BoxExtractor::mouseHandler(int event, int x, int y, int flags, void *param){
100-
BoxExtractor *self =static_cast<BoxExtractor*>(param);
101-
self->opencv_mouse_callback(event,x,y,flags,param);
102-
}
103-
104-
void BoxExtractor::opencv_mouse_callback( int event, int x, int y, int , void *param ){
105-
handlerT * data = (handlerT*)param;
106-
switch( event ){
107-
// update the selected bounding box
108-
case EVENT_MOUSEMOVE:
109-
if( data->isDrawing ){
110-
data->box.width = x-data->box.x;
111-
data->box.height = y-data->box.y;
112-
}
113-
break;
114-
115-
// start to select the bounding box
116-
case EVENT_LBUTTONDOWN:
117-
data->isDrawing = true;
118-
data->box = cvRect( x, y, 0, 0 );
119-
break;
120-
121-
// cleaning up the selected bounding box
122-
case EVENT_LBUTTONUP:
123-
data->isDrawing = false;
124-
if( data->box.width < 0 ){
125-
data->box.x += data->box.width;
126-
data->box.width *= -1;
127-
}
128-
if( data->box.height < 0 ){
129-
data->box.y += data->box.height;
130-
data->box.height *= -1;
131-
}
132-
break;
133-
}
134-
}
135-
136-
Rect2d BoxExtractor::extract(Mat img){
137-
return extract("Bounding Box Extractor", img);
138-
}
139-
140-
Rect2d BoxExtractor::extract(const std::string& windowName, Mat img, bool showCrossair){
141-
142-
int key=0;
143-
144-
// show the image and give feedback to user
145-
imshow(windowName,img);
146-
printf("Select an object to track and then press SPACE/BACKSPACE/ENTER button!\n");
147-
148-
// copy the data, rectangle should be drawn in the fresh image
149-
params.image=img.clone();
150-
151-
// select the object
152-
setMouseCallback( windowName, mouseHandler, (void *)&params );
153-
154-
// end selection process on SPACE (32) BACKSPACE (27) or ENTER (13)
155-
while(!(key==32 || key==27 || key==13)){
156-
// draw the selected object
157-
rectangle(
158-
params.image,
159-
params.box,
160-
Scalar(255,0,0),2,1
161-
);
162-
163-
// draw cross air in the middle of bounding box
164-
if(showCrossair){
165-
// horizontal line
166-
line(
167-
params.image,
168-
Point((int)params.box.x,(int)(params.box.y+params.box.height/2)),
169-
Point((int)(params.box.x+params.box.width),(int)(params.box.y+params.box.height/2)),
170-
Scalar(255,0,0),2,1
171-
);
172-
173-
// vertical line
174-
line(
175-
params.image,
176-
Point((int)(params.box.x+params.box.width/2),(int)params.box.y),
177-
Point((int)(params.box.x+params.box.width/2),(int)(params.box.y+params.box.height)),
178-
Scalar(255,0,0),2,1
179-
);
180-
}
181-
182-
// show the image bouding box
183-
imshow(windowName,params.image);
184-
185-
// reset the image
186-
params.image=img.clone();
187-
188-
//get keyboard event
189-
key=waitKey(1);
190-
}
191-
192-
193-
return params.box;
194-
}

modules/tracking/src/trackerKCF.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,10 @@ namespace cv{
341341

342342
// extract the maximum response
343343
minMaxLoc( response, &minVal, &maxVal, &minLoc, &maxLoc );
344+
if (maxVal < params.detect_thresh)
345+
{
346+
return false;
347+
}
344348
roi.x+=(maxLoc.x-roi.width/2+1);
345349
roi.y+=(maxLoc.y-roi.height/2+1);
346350
}
@@ -821,6 +825,7 @@ namespace cv{
821825
* Parameters
822826
*/
823827
TrackerKCF::Params::Params(){
828+
detect_thresh = 0.5;
824829
sigma=0.2;
825830
lambda=0.01;
826831
interp_factor=0.075;
@@ -841,6 +846,9 @@ namespace cv{
841846
void TrackerKCF::Params::read( const cv::FileNode& fn ){
842847
*this = TrackerKCF::Params();
843848

849+
if (!fn["detect_thresh"].empty())
850+
fn["detect_thresh"] >> detect_thresh;
851+
844852
if (!fn["sigma"].empty())
845853
fn["sigma"] >> sigma;
846854

@@ -883,6 +891,7 @@ namespace cv{
883891
}
884892

885893
void TrackerKCF::Params::write( cv::FileStorage& fs ) const{
894+
fs << "detect_thresh" << detect_thresh;
886895
fs << "sigma" << sigma;
887896
fs << "lambda" << lambda;
888897
fs << "interp_factor" << interp_factor;

0 commit comments

Comments
 (0)