13
13
#include < opencv2/highgui.hpp>
14
14
#include < iostream>
15
15
#include < cstring>
16
+ #include " samples_utility.hpp"
16
17
17
18
using namespace std ;
18
19
using namespace cv ;
19
20
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
-
39
21
int main ( int argc, char ** argv ){
40
22
// show help
41
23
if (argc<2 ){
@@ -48,9 +30,6 @@ int main( int argc, char** argv ){
48
30
return 0 ;
49
31
}
50
32
51
- // ROI selector
52
- BoxExtractor box;
53
-
54
33
// create the tracker
55
34
Ptr<Tracker> tracker = TrackerKCF::create ();
56
35
@@ -62,7 +41,7 @@ int main( int argc, char** argv ){
62
41
63
42
// get bounding box
64
43
cap >> frame;
65
- Rect2d roi=box. extract (" tracker" ,frame);
44
+ Rect2d roi= selectROI (" tracker" , frame, true , false );
66
45
67
46
// quit if ROI was not selected
68
47
if (roi.width ==0 || roi.height ==0 )
@@ -82,7 +61,13 @@ int main( int argc, char** argv ){
82
61
break ;
83
62
84
63
// 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
+ }
86
71
87
72
// draw the tracked object
88
73
rectangle ( frame, roi, Scalar ( 255 , 0 , 0 ), 2 , 1 );
@@ -95,100 +80,3 @@ int main( int argc, char** argv ){
95
80
}
96
81
97
82
}
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 *)¶ms );
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
- }
0 commit comments