#include <opencv2/core.hpp> #include <opencv2/highgui.hpp> #include <opencv2/imgproc.hpp> #include <opencv2/tracking/tracker.hpp> #include <opencv2/core/utility.hpp> #include <opencv2/tracking.hpp
> #include <opencv2/videoio.hpp> #include <iostream> #include <vector> using namespace CV; Move the mouse to select a rectangular box void Mouseclickcallback (int event, int x, int y, int flags, void* UserData) {//Rectangle data Return CV::RECT2D * pRe
ct = reinterpret_cast<cv::rect2d*> (userdata); Mouse Down action if (event = = Cv::event_lbuttondown) {std::cout << "Lbuttondown (" << x << "," << ;
Y << ")" << Std::endl;
Gets the x, y coordinates prect->x = x;
Prect->y = y; }//Mouse up action else if (event = = Cv::event_lbuttonup) {std::cout << "lbuttonup (" << x << "," &L
t;< y << ")" << Std::endl;
Get rectangle width High prect->width = Std::abs (x-prect->x); Prect->height = Std::abs (y-prect->y);
}} int main (int argc, char** argv) {//Read video stream cv::videocapture cap ("...
\\images\\car.avi ");
if (!cap.isopened ()) {std::cout << "on data!" << Std::endl;
return-1;
} cap.set (Cv_cap_prop_pos_msec, 2*1000);
Cv::mat frame;
Initializes the TLD tracker cv::rect2d *rect (new cv::rect2d);
CV::P tr<cv::trackertld> tracker = Cv::trackertld::createtracker ();
Read First frame initialize rectangle box cap >> frame;
Cv::resize (frame, frame, cv::size (), 0.25,0.25);
Cv::imshow ("Trackertld", frame);
Mouse movement gets the rectangular area cv::setmousecallback ("Trackertld", Mouseclickcallback, reinterpret_cast<void*> (rect));
Cv::waitkey ();
if (rect->area () = = 0.0) return-1;
Tracker initial Tracker->init (frame, *rect);
Double fps = 1.0;
while (true) {cap >> frame;
Cv::resize (frame, frame, cv::size (), 0.25,0.25);
if (Frame.empty ()) break; Tracker Update if (Tracker->update (frame, *rect))//Draw trace results Cv::rectangle (frame, *rect, cv::scalar (255, 0, 0),
2, 1); Cv::imshow ("TrackerTLD ", frame);
char C = cv::waitkey (10);
if (= = c) break;
} cap.release ();
return 0;
}
Reprint: http://blog.csdn.net/zhuwei1988