Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Commit_Not_Ready #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
9 changes: 3 additions & 6 deletions samples/trackers_factory.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
#include "tracker.hpp"

cv::Ptr<Tracker> createTrackerDummy();
// TODO: Declare your implementation here
// cv::Ptr<Tracker> createTrackerYourName();
cv::Ptr<Tracker> createTrackerMoskalenko();

cv::Ptr<Tracker> createTracker(const std::string &impl_name)
{
if (impl_name == "dummy")
return createTrackerDummy();
// TODO: Add case for your implementation
// else if (impl_name == "your_name"):
// return createTrackerYourName();

else if (impl_name == "moskalenko")
return createTrackerMoskalenko();
return 0;
}
107 changes: 107 additions & 0 deletions samples/tracking_Moskalenko.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#include <iostream>
#include <string>
#include <vector>
#include <iostream>

#include <tracker.hpp>

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

using namespace cv;
using namespace std;

class TrackerMoskalenko : public Tracker
{
public:
cv::Mat image_prev, image_next;
std::vector<cv::Point2f> features_prev, features_next;
std::vector<uchar> status;
std::vector<float> err;
std::vector<uchar> status_back;
std::vector<float> err_back;
public:
virtual ~TrackerMoskalenko()
{
}

virtual bool init( const cv::Mat& frame, const cv::Rect& initial_position );
virtual bool track( const cv::Mat& frame, cv::Rect& new_position );

private:
cv::Rect position_;
};

bool TrackerMoskalenko::init( const cv::Mat& frame, const cv::Rect& initial_position )
{
Mat image_gray;
cv::cvtColor(frame,image_gray,CV_BGR2GRAY);
image_next = image_gray.clone();
cv::goodFeaturesToTrack(image_next(initial_position),
features_next,
1000, 0.5, 7);
position_ = initial_position;
return true;
}

bool TrackerMoskalenko::track( const cv::Mat& frame, cv::Rect& new_position )
{
image_prev = image_next.clone();
features_prev = features_next;

Mat image_gray;
cv::cvtColor(frame,image_gray,CV_BGR2GRAY);
image_next = image_gray.clone();

cv::calcOpticalFlowPyrLK(
image_prev, image_next,
features_prev,
features_next,
status,
err
);

Mat a = frame.clone();
for (int i = 0 ; i < features_next.size(); i++)
{
cv::circle(a,features_next[i],2,Scalar(0,255,0));
}
imshow("features_next",a);
waitKey(20);

vector<float> x,y,err_copy;
err_copy = err;
sort(err_copy.begin(),err_copy.end());
float p = err_copy[err_copy.size()/2];
vector<Point2f> res;


for (int i = 0 ; i < features_next.size(); i++)
{
if (err[i]<p)
{
x.push_back(features_next[i].x - features_prev[i].x);
y.push_back(features_next[i].y - features_prev[i].y);
}
}


if (x.size() > 0)
{
sort(x.begin(),x.end());
sort(y.begin(),y.end());
position_.x += x[x.size()/2];
position_.y += y[y.size()/2];

new_position = position_;
}
return true;
}

cv::Ptr<Tracker> createTrackerMoskalenko()
{
return cv::Ptr<Tracker>(new TrackerMoskalenko());
}
1 change: 1 addition & 0 deletions samples/tracking_sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ void help(const char *argv0)
"$ ./bin/tracking_sample dummy ../dataset/car.mp4\n\n"
"$ ./bin/tracking_sample dummy ../dataset/car.mp4 142,125,232,164\n\n"
"$ ./bin/tracking_sample dummy ../dataset/car.mp4 ../dataset/car.txt\n\n"
"$ ./bin/tracking_sample moskalenko ../dataset/car.mp4 ../dataset/car.txt\n\n"
<< std::endl;
}

Expand Down