Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions include/tracking.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,27 @@
#include <string>

#include "opencv2/core/core.hpp"
#include "opencv2\opencv.hpp"

class Tracker {
public:
static std::shared_ptr<Tracker> CreateTracker(const std::string &name);
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;
};*/
55 changes: 55 additions & 0 deletions samples/tracking_demo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <iostream>
#include <string>

#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<bool>("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;
}
43 changes: 43 additions & 0 deletions src/tracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,48 @@ using namespace cv;
shared_ptr<Tracker> Tracker::CreateTracker(const string &name) {
std::cerr << "Failed to create tracker with name '" << name << "'"
<< std::endl;
if (name == "median_flow") {
return std::make_shared<MedianFlowTracker>();
}
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<Point2f> features;
std::vector<Point2f> features_;
std::vector<uchar> status;
std::vector<float> 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::vector<float>dx, 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_;
}