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
12 changes: 11 additions & 1 deletion include/tracking.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@
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 bool Init(const cv::Mat &frame, const cv::Mat &next_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::Mat &next_frame, const cv::Rect &roi);
virtual cv::Rect Track(const cv::Mat &frame);

protected:
cv::Rect position_;
cv::Mat frame_;
};
43 changes: 43 additions & 0 deletions samples/template_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
#include <string>

#include "opencv2/core.hpp"
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

#include "tracking.hpp"


using namespace std;
using namespace cv;
Expand All @@ -26,6 +31,44 @@ int main(int argc, const char** argv) {
return 0;
}


string filename = "pedestrians.mpg";

VideoCapture capture(filename);
Mat frame;
Mat next_frame;

if (!capture.isOpened())
throw "Error when reading steam_avi";


MedianFlowTracker medianflowtraacker;

Rect ROI(70, 70, 90, 90);

const cv::Rect &roi = ROI;

namedWindow("video", 1);
for (; ; )
{
capture >> frame;

if (frame.empty()) {
continue;
}

//getting next frame
capture.read(next_frame);

medianflowtraacker.Init(frame, next_frame, roi);

imshow("video", frame);
waitKey(20);
}
waitKey(0);



// Do something cool.
cout << "This is empty template sample." << endl;

Expand Down
96 changes: 96 additions & 0 deletions src/tracking.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,109 @@
#include "tracking.hpp"

#include <iostream>
#include <opencv2/video/tracking.hpp>


#include "opencv2/core.hpp"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>


using namespace std;
using namespace cv;


using std::string;
using std::shared_ptr;
using namespace cv;

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 cv::Mat &frame, const cv::Mat &next_frame, const cv::Rect &roi)
{
vector<Point2f> corners;
vector<Point2f> next_corners;

std::vector<uchar> status;
std::vector<float> err;

int maxCorners = 150;
double qualityLevel = 0.01;
double minDistance = 10;
int maxTrackbar = 100;

RNG rng(12345);

Mat frame_gray;
frame.copyTo(frame_gray);

//convert to gray
cvtColor(frame, frame_gray, COLOR_BGR2GRAY);

//set 8-bit, single-channel
frame_gray.convertTo(frame_gray, CV_8UC1);

//cut roi
Mat cropped_frame = frame_gray(roi);
imshow("Croped_frame", cropped_frame);

// get features from roi
goodFeaturesToTrack(cropped_frame, corners, maxCorners, qualityLevel, minDistance);

/// corners detected
cout << "** Number of corners detected: " << corners.size() << endl;

waitKey(0);

//////////////////////////////////
///for next_frame
/////////////////////////////////

Mat next_frame_gray;
next_frame.copyTo(next_frame_gray);

//convert to gray
cvtColor(next_frame, next_frame_gray, COLOR_BGR2GRAY);

//set 8-bit, single-channel
next_frame_gray.convertTo(next_frame_gray, CV_8UC1);

//cut roi
Mat next_cropped_frame = next_frame_gray(roi);

// get features from roi
goodFeaturesToTrack(next_cropped_frame, next_corners, maxCorners, qualityLevel, minDistance);

/// corners detected
cout << "** Number of corners on next frame detected: " << next_corners.size() << endl;

waitKey(0);

//////


//count optical flow
calcOpticalFlowPyrLK(cropped_frame, next_cropped_frame, corners, next_corners, status, err);

for (int i = 0; i < status.size(); i++) {
//if (!status[i])
//cout << "** status: " << status[i] << endl;
}

return true;
}

cv::Rect MedianFlowTracker::Track(const cv::Mat &frame)
{
cv::Rect objects;
return objects;
}