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
11 changes: 10 additions & 1 deletion include/tracking.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@
#include <memory>
#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_;
};
56 changes: 56 additions & 0 deletions samples/tracking_demo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <iostream>
#include <string>

#include "opencv2/core.hpp"
#include "opencv2/opencv.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;
}

// Do something cool.
VideoCapture cap;
if (parser.has("video"))
cap = VideoCapture(parser.get<string>("video"));
else
cap = VideoCapture(0);

if (!cap.isOpened())
{
cout << "Failed to open video capture." << endl;
return 1;
}

for (;;)
{
Mat frame;
cap >> frame;
MedianFlowTracker track;
track.Init(frame,Rect(25,25,50,50));
track.Track(frame);
rectangle(frame, track.Track(frame), CV_RGB(255, 0, 255));
if (frame.empty()) break;
imshow("x", frame);
if (cv::waitKey(30) >= 0) break;
}
return 0;
}
58 changes: 57 additions & 1 deletion src/tracking.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,69 @@
#include "tracking.hpp"

#include <iostream>
#include <algorithm>

using std::string;
using std::shared_ptr;
using namespace cv;
using namespace std;
int compX( Point a, Point b)
{
return a.x - b.x;
}
int compY(Point a, Point b)
{
return a.y - b.y;
}

shared_ptr<Tracker> Tracker::CreateTracker(const string &name) {
shared_ptr<Tracker> Tracker::CreateTracker(const string &name)
{
if (name == "median_flow")
{
return std::make_shared<MedianFlowTracker>();
}
std::cerr << "Failed to create tracker with name '" << name << "'"
<< std::endl;
return nullptr;
}

bool MedianFlowTracker::Init(const Mat & frame, const Rect & roi)
{
Mat frame_ = frame;
Rect position_ = roi;
return true;
}

Rect MedianFlowTracker::Track(const Mat & frame)
{
vector<Point> corners;
vector<Point> corners_out;
vector<Point> Shift;
vector<bool> status;
vector<bool> err;
goodFeaturesToTrack(frame, corners, 100, 0.3, 7);
calcOpticalFlowPyrLK(frame_, frame, corners, corners_out, status, err);
int erased = 0;

for (int i = 0; i < status.size(); i++)
if (!status[i])
{
corners_out.erase(corners_out.begin() + i - erased);
corners.erase(corners.begin() + i - erased);
erased++;
}

for(int i = 0; i < corners.size(); i++)
{
Shift[i].x = corners[i].x - corners_out[i].x;
Shift[i].y = corners[i].y - corners_out[i].y;
}
sort(Shift.begin(), Shift.end(), compX);
sort(Shift.begin(), Shift.end(), compY);


position_.x += Shift[Shift.size() / 2].x;
position_.y += Shift[Shift.size() / 2].y;

return position_;
}