Skip to content

Commit

Permalink
Added KCF tracker from opencv_contrib for collisions resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuzhny007 committed Apr 27, 2017
1 parent 665d6f5 commit ac73f44
Show file tree
Hide file tree
Showing 7 changed files with 318 additions and 145 deletions.
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,16 @@ set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})
SOURCE_GROUP("graph" FILES ${graph_source} ${graph_header})
SOURCE_GROUP("GTL" FILES ${gtl_source} ${gtl_header})

option(USE_OCV_BGFG "Should the bgfg algorithms from opencv_contrib?" ON)
option(USE_OCV_BGFG "Should use the bgfg algorithms from opencv_contrib?" ON)
if(USE_OCV_BGFG)
add_definitions(-DUSE_OCV_BGFG)
endif(USE_OCV_BGFG)

option(USE_OCV_KCF "Should use the KCF tracker from opencv_contrib for collisions resolving?" ON)
if(USE_OCV_KCF)
add_definitions(-DUSE_OCV_KCF)
endif(USE_OCV_KCF)

# ----------------------------------------------------------------------------
# создаем проект
# ----------------------------------------------------------------------------
Expand Down
29 changes: 18 additions & 11 deletions Tracker/Ctracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ CTracker::CTracker(
bool useLocalTracking,
DistType distType,
KalmanType kalmanType,
bool useExternalTrackerForLostObjects,
MatchType matchType,
track_t dt_,
track_t accelNoiseMag_,
Expand All @@ -24,6 +25,7 @@ CTracker::CTracker(
m_useLocalTracking(useLocalTracking),
m_distType(distType),
m_kalmanType(kalmanType),
m_useExternalTrackerForLostObjects(useExternalTrackerForLostObjects),
m_matchType(matchType),
dt(dt_),
accelNoiseMag(accelNoiseMag_),
Expand All @@ -47,14 +49,17 @@ CTracker::~CTracker(void)
void CTracker::Update(
const std::vector<Point_t>& detections,
const regions_t& regions,
cv::Mat gray_frame
cv::Mat grayFrame
)
{
assert(detections.size() == regions.size());

if (m_useLocalTracking)
if (m_prevFrame.size() == grayFrame.size())
{
localTracker.Update(tracks, gray_frame);
if (m_useLocalTracking)
{
m_localTracker.Update(tracks, m_prevFrame, grayFrame);
}
}

// -----------------------------------
Expand All @@ -65,7 +70,7 @@ void CTracker::Update(
// If no tracks yet
for (size_t i = 0; i < detections.size(); ++i)
{
tracks.push_back(std::make_unique<CTrack>(detections[i], regions[i], dt, accelNoiseMag, NextTrackID++, m_kalmanType == FilterRect));
tracks.push_back(std::make_unique<CTrack>(detections[i], regions[i], dt, accelNoiseMag, NextTrackID++, m_kalmanType == FilterRect, m_useExternalTrackerForLostObjects));
}
}

Expand Down Expand Up @@ -183,13 +188,13 @@ void CTracker::Update(
if (Cost[i + assignment[i] * N] > dist_thres)
{
assignment[i] = -1;
tracks[i]->skipped_frames++;
tracks[i]->m_skippedFrames++;
}
}
else
{
// If track have no assigned detect, then increment skipped frames counter.
tracks[i]->skipped_frames++;
tracks[i]->m_skippedFrames++;
}
}

Expand All @@ -198,7 +203,7 @@ void CTracker::Update(
// -----------------------------------
for (int i = 0; i < static_cast<int>(tracks.size()); i++)
{
if (tracks[i]->skipped_frames > maximum_allowed_skipped_frames)
if (tracks[i]->m_skippedFrames > maximum_allowed_skipped_frames)
{
tracks.erase(tracks.begin() + i);
assignment.erase(assignment.begin() + i);
Expand All @@ -214,7 +219,7 @@ void CTracker::Update(
{
if (find(assignment.begin(), assignment.end(), i) == assignment.end())
{
tracks.push_back(std::make_unique<CTrack>(detections[i], regions[i], dt, accelNoiseMag, NextTrackID++, m_kalmanType == FilterRect));
tracks.push_back(std::make_unique<CTrack>(detections[i], regions[i], dt, accelNoiseMag, NextTrackID++, m_kalmanType == FilterRect, m_useExternalTrackerForLostObjects));
}
}

Expand All @@ -226,12 +231,14 @@ void CTracker::Update(

if (assignment[i] != -1) // If we have assigned detect, then update using its coordinates,
{
tracks[i]->skipped_frames = 0;
tracks[i]->Update(detections[assignment[i]], regions[assignment[i]], true, max_trace_length);
tracks[i]->m_skippedFrames = 0;
tracks[i]->Update(detections[assignment[i]], regions[assignment[i]], true, max_trace_length, m_prevFrame, grayFrame);
}
else // if not continue using predictions
{
tracks[i]->Update(Point_t(), CRegion(), false, max_trace_length);
tracks[i]->Update(Point_t(), CRegion(), false, max_trace_length, m_prevFrame, grayFrame);
}
}

grayFrame.copyTo(m_prevFrame);
}
8 changes: 6 additions & 2 deletions Tracker/Ctracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class CTracker
CTracker(bool useLocalTracking,
DistType distType,
KalmanType kalmanType,
bool useExternalTrackerForLostObjects,
MatchType matchType,
track_t dt_,
track_t accelNoiseMag_,
Expand All @@ -40,14 +41,15 @@ class CTracker
~CTracker(void);

tracks_t tracks;
void Update(const std::vector<Point_t>& detections, const regions_t& regions, cv::Mat gray_frame);
void Update(const std::vector<Point_t>& detections, const regions_t& regions, cv::Mat grayFrame);

private:
// Use local tracking for regions between two frames
bool m_useLocalTracking;

DistType m_distType;
KalmanType m_kalmanType;
bool m_useExternalTrackerForLostObjects;
MatchType m_matchType;

// Шаг времени опроса фильтра
Expand All @@ -65,5 +67,7 @@ class CTracker

size_t NextTrackID;

LocalTracker localTracker;
LocalTracker m_localTracker;

cv::Mat m_prevFrame;
};
49 changes: 20 additions & 29 deletions Tracker/LocalTracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,82 +19,75 @@ LocalTracker::~LocalTracker(void)
// ---------------------------------------------------------------------------
void LocalTracker::Update(
tracks_t& tracks,
cv::Mat grayFrame
cv::Mat prevFrame,
cv::Mat currFrame
)
{
if (m_prevFrame.size() != grayFrame.size())
{
m_prevFrame = grayFrame;
return;
}

std::vector<cv::Point2f> points[2];

points[0].reserve(8 * tracks.size());
for (auto& track : tracks)
{
track->pointsCount = 0;
for (const auto& pt : track->lastRegion.m_points)
for (const auto& pt : track->m_lastRegion.m_points)
{
points[0].push_back(pt);
}
}

if (points[0].empty())
{
m_prevFrame = grayFrame;
return;
}

cv::TermCriteria termcrit(cv::TermCriteria::COUNT | cv::TermCriteria::EPS, 30, 0.01);
cv::Size subPixWinSize(3, 3);
cv::Size winSize(21, 21);

cv::cornerSubPix(m_prevFrame, points[0], subPixWinSize, cv::Size(-1,-1), termcrit);
cv::cornerSubPix(prevFrame, points[0], subPixWinSize, cv::Size(-1,-1), termcrit);

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

cv::calcOpticalFlowPyrLK(m_prevFrame, grayFrame, points[0], points[1], status, err, winSize, 3, termcrit, 0, 0.001);
cv::calcOpticalFlowPyrLK(prevFrame, currFrame, points[0], points[1], status, err, winSize, 3, termcrit, 0, 0.001);

size_t i = 0;
for (auto& track : tracks)
{
track->averagePoint = Point_t(0, 0);
track->boundidgRect = cv::Rect(0, 0, 0, 0);
track->m_averagePoint = Point_t(0, 0);
track->m_boundidgRect = cv::Rect(0, 0, 0, 0);

for (auto it = track->lastRegion.m_points.begin(); it != track->lastRegion.m_points.end();)
for (auto it = track->m_lastRegion.m_points.begin(); it != track->m_lastRegion.m_points.end();)
{
if (status[i])
{
*it = points[1][i];
track->averagePoint += *it;
track->m_averagePoint += *it;

++it;
}
else
{
it = track->lastRegion.m_points.erase(it);
it = track->m_lastRegion.m_points.erase(it);
}

++i;
}

if (!track->lastRegion.m_points.empty())
if (!track->m_lastRegion.m_points.empty())
{
track->averagePoint /= static_cast<track_t>(track->lastRegion.m_points.size());
track->m_averagePoint /= static_cast<track_t>(track->m_lastRegion.m_points.size());

cv::Rect br = cv::boundingRect(track->lastRegion.m_points);
cv::Rect br = cv::boundingRect(track->m_lastRegion.m_points);
#if 0
br.x -= subPixWinSize.width;
br.width += 2 * subPixWinSize.width;
if (br.x < 0)
{
br.width += br.x;
br.x = 0;
}
if (br.x + br.width >= grayFrame.cols)
if (br.x + br.width >= currFrame.cols)
{
br.x = grayFrame.cols - br.width - 1;
br.x = currFrame.cols - br.width - 1;
}

br.y -= subPixWinSize.height;
Expand All @@ -104,14 +97,12 @@ void LocalTracker::Update(
br.height += br.y;
br.y = 0;
}
if (br.y + br.height >= grayFrame.rows)
if (br.y + br.height >= currFrame.rows)
{
br.y = grayFrame.rows - br.height - 1;
br.y = currFrame.rows - br.height - 1;
}

track->boundidgRect = br;
#endif
track->m_boundidgRect = br;
}
}

m_prevFrame = grayFrame;
}
6 changes: 1 addition & 5 deletions Tracker/LocalTracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,5 @@ class LocalTracker
LocalTracker();
~LocalTracker(void);

void Update(tracks_t& tracks,
cv::Mat grayFrame);

private:
cv::Mat m_prevFrame;
void Update(tracks_t& tracks, cv::Mat prevFrame, cv::Mat currFrame);
};
Loading

0 comments on commit ac73f44

Please sign in to comment.