Skip to content

Commit

Permalink
Fixed Tracking using SparsePyrLKOpticalFlow on CPU
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexeyAB committed Feb 19, 2018
1 parent 5f74aeb commit 2972f8d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 20 deletions.
6 changes: 4 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ jobs:
working_directory: ~/work
steps:
- checkout
- run: make LIBSO=1 GPU=0 CUDNN=0 OPENCV=0 -j 8
- run: make clean
- run: make LIBSO=1 GPU=0 CUDNN=0 OPENCV=0 -j 8
- run: make clean
- run: make LIBSO=1 GPU=0 CUDNN=0 OPENCV=1 -j 8
- run: make clean
- run: make LIBSO=1 GPU=1 CUDNN=1 OPENCV=1 -j 8
1 change: 1 addition & 0 deletions src/yolo_console_dll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#ifdef _WIN32
#define OPENCV
#define GPU
#endif

// To use tracking - uncomment the following line. Tracking is supported only by OpenCV 3.x
Expand Down
29 changes: 11 additions & 18 deletions src/yolo_v2_class.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,8 @@ class Tracker_optflow {

#elif defined(TRACK_OPTFLOW) && defined(OPENCV)

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

class Tracker_optflow {
public:
Expand All @@ -340,8 +341,7 @@ class Tracker_optflow {
}

// just to avoid extra allocations
cv::Mat src_mat;
cv::Mat dst_mat, dst_grey;
cv::Mat dst_grey;
cv::Mat prev_pts_flow, cur_pts_flow;
cv::Mat status, err;

Expand Down Expand Up @@ -373,15 +373,10 @@ class Tracker_optflow {
void update_tracking_flow(cv::Mat new_src_mat, std::vector<bbox_t> _cur_bbox_vec)
{
if (new_src_mat.channels() == 3) {
if (src_mat.cols == 0) {
src_mat = cv::Mat(new_src_mat.size(), new_src_mat.type());
src_grey = cv::Mat(new_src_mat.size(), CV_8UC1);
}

update_cur_bbox_vec(_cur_bbox_vec);

src_mat = new_src_mat;
cv::cvtColor(src_mat, src_grey, CV_BGR2GRAY, 1);
cv::cvtColor(new_src_mat, src_grey, CV_BGR2GRAY, 1);
}
}

Expand All @@ -393,27 +388,25 @@ class Tracker_optflow {
return cur_bbox_vec;
}

if (dst_mat.cols == 0) {
dst_mat = cv::Mat(new_dst_mat.size(), new_dst_mat.type());
dst_grey = cv::Mat(new_dst_mat.size(), CV_8UC1);
}

dst_mat = new_dst_mat;
cv::cvtColor(dst_mat, dst_grey, CV_BGR2GRAY, 1);
cv::cvtColor(new_dst_mat, dst_grey, CV_BGR2GRAY, 1);

if (src_grey.rows != dst_grey.rows || src_grey.cols != dst_grey.cols) {
src_grey = dst_grey.clone();
return cur_bbox_vec;
}

if (prev_pts_flow.cols < 1) {
return cur_bbox_vec;
}

////sync_PyrLKOpticalFlow_gpu.sparse(src_grey_gpu, dst_grey_gpu, prev_pts_flow_gpu, cur_pts_flow_gpu, status_gpu, &err_gpu); // OpenCV 2.4.x
sync_PyrLKOpticalFlow->calc(src_grey, dst_grey, prev_pts_flow, cur_pts_flow, status, err); // OpenCV 3.x

dst_grey.copyTo(src_grey);

std::vector<bbox_t> result_bbox_vec;

if (err.cols == cur_bbox_vec.size() && status.cols == cur_bbox_vec.size())
if (err.rows == cur_bbox_vec.size() && status.rows == cur_bbox_vec.size())
{
for (size_t i = 0; i < cur_bbox_vec.size(); ++i)
{
Expand All @@ -438,7 +431,7 @@ class Tracker_optflow {
}
}

prev_pts_flow = cur_pts_flow;
prev_pts_flow = cur_pts_flow.clone();

return result_bbox_vec;
}
Expand Down

0 comments on commit 2972f8d

Please sign in to comment.