From 548e3a01654ba0c5386fdfe03d45cfd46f5a4cfe Mon Sep 17 00:00:00 2001 From: okondratieva Date: Thu, 7 Jul 2016 18:53:05 +0400 Subject: [PATCH] day_4 --- include/tracking.hpp | 17 ++++++++++++ samples/tracking_demo.cpp | 55 +++++++++++++++++++++++++++++++++++++++ src/tracking.cpp | 43 ++++++++++++++++++++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 samples/tracking_demo.cpp diff --git a/include/tracking.hpp b/include/tracking.hpp index f80004c..e0b84e1 100644 --- a/include/tracking.hpp +++ b/include/tracking.hpp @@ -4,6 +4,7 @@ #include #include "opencv2/core/core.hpp" +#include "opencv2\opencv.hpp" class Tracker { public: @@ -11,3 +12,19 @@ class Tracker { virtual bool Init(const cv::Mat &frame, const cv::Rect &roi) = 0; virtual cv::Rect Track(const cv::Mat &frame) = 0; }; +class MedianFlowTracker : public Tracker { +public: + virtual bool Init(const cv::Mat &frame, const cv::Rect &roi); + virtual cv::Rect Track(const cv::Mat &frame); + +protected: + cv::Rect position_; + cv::Mat frame_; +}; +/* +struct MouseCallbackState { + bool is_selection_started; + bool is_selection_finished; + Point point_first; + Point point_second; +};*/ \ No newline at end of file diff --git a/samples/tracking_demo.cpp b/samples/tracking_demo.cpp new file mode 100644 index 0000000..6b0e6fe --- /dev/null +++ b/samples/tracking_demo.cpp @@ -0,0 +1,55 @@ +#include +#include + +#include "opencv2/core.hpp" +#include "tracking.hpp" +using namespace std; +using namespace cv; + +const char* kAbout = + "This is an empty application that can be treated as a template for your " + "own doing-something-cool applications."; + +const char* kOptions = + "{ v video | | video to process }" + "{ h ? help usage | | print help message }"; + + +int main(int argc, const char** argv) { + // Parse command line arguments. + CommandLineParser parser(argc, argv, kOptions); + parser.about(kAbout); + + // If help option is given, print help message and exit. + if (parser.get("help")) { + parser.printMessage(); + return 0; + } + MedianFlowTracker tracker; + //VideoCapture cap("Users/iss2016/Documents/itseez-ss-2016-practice/test/test_data/video/logo.mp4"); + Mat frame_prev,frame_next; + VideoCapture cap(0); + Rect roi(10, 20, 50, 100); + + cap >> frame_prev; + tracker.Init(frame_prev, roi); + + for (;;) { + cap >> frame_next; + + rectangle(frame_next, tracker.Track(frame_next),CV_RGB(0,240,0)); + + tracker.Init(frame_next, roi); + + + imshow("Tracking", frame_next); + if(waitKey(30)>=0)break; + } + + + + // Do something cool. + cout << "This is empty template sample." << endl; + + return 0; +} diff --git a/src/tracking.cpp b/src/tracking.cpp index 26cbe06..a20cd32 100644 --- a/src/tracking.cpp +++ b/src/tracking.cpp @@ -9,5 +9,48 @@ using namespace cv; shared_ptr Tracker::CreateTracker(const string &name) { std::cerr << "Failed to create tracker with name '" << name << "'" << std::endl; + if (name == "median_flow") { + return std::make_shared(); + } return nullptr; } +bool MedianFlowTracker::Init(const cv::Mat &frame, const cv::Rect &roi) { + position_ = roi; + frame_ = frame.clone(); + return true; +} +cv::Rect MedianFlowTracker::Track(const cv::Mat &frame) { + std::vector features; + std::vector features_; + std::vector status; + std::vector err; + + Mat frame_prev_roi(frame_, position_); + + goodFeaturesToTrack(frame_prev_roi, features, 100, 0.3, 5); + goodFeaturesToTrack(frame, features_, 100, 0.3, 5); + + calcOpticalFlowPyrLK(frame_, frame, features_, features, status, err); + + int k = 0; + for (int i = 0; i < status.size();i++) + if (status[i] == 0) + { + features_.erase(features_.begin() + i - k); + features.erase(features.begin() + i - k); + k++; + } + + std::vectordx, dy; + for (int i = 0; i < features_.size(); i++) { + dx.push_back(features[i].x-features_[i].x); + dy.push_back(features[i].y - features_[i].y); + } + + std::sort(dx.begin(), dx.end()); + std::sort(dy.begin(), dy.end()); + position_.x += dx.size() / 2; + position_.y += dy.size() / 2; + + return position_; +}