1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
#include <opencv2/opencv.hpp> #include <opencv2/tracking.hpp> #include <opencv2/tracking/tracking_legacy.hpp> #include <iostream> #include <vector> using namespace cv;
void mouseClickCallback(int event, int x, int y, int flags, void* userdata) { cv::Rect2d * pRect = reinterpret_cast<cv::Rect2d*>(userdata); if (event == cv::EVENT_LBUTTONDOWN) { std::cout << "LBUTTONDOWN (" << x << ", " << y << ")" << std::endl; pRect->x = x; pRect->y = y; } else if (event == cv::EVENT_LBUTTONUP) { std::cout << "LBUTTONUP (" << x << ", " << y << ")" << std::endl; pRect->width = std::abs(x - pRect->x); pRect->height = std::abs(y - pRect->y); } } int main(int argc, char** argv) { cv::VideoCapture cap(0); if (!cap.isOpened()) { std::cout << " on data! " << std::endl; return -1; } cap.set(cv::CAP_PROP_POS_MSEC, 2*1000); cv::Mat frame; cv::Rect2d *rect(new cv::Rect2d); cv::Ptr<cv::legacy::TrackerTLD> tracker = cv::legacy::TrackerTLD::create(); cap >> frame; cv::resize(frame, frame, cv::Size(), 0.25,0.25); cv::imshow("TrackerTLD", frame); cv::setMouseCallback("TrackerTLD", mouseClickCallback, reinterpret_cast<void*>(rect)); cv::waitKey(); if (rect->area() == 0.0) return -1; 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; if (tracker->update(frame, *rect)) cv::rectangle(frame, *rect, cv::Scalar(255, 0, 0 ), 2, 1); cv::imshow("TrackerTLD", frame); char c = cv::waitKey(10); if (27 == c) break; } cap.release(); return 0; }
|