Skip to content

Commit e61b98d

Browse files
author
Sergey Nuzhny
committed
Using fps and frame size for setting detector params
1 parent 05f1c93 commit e61b98d

4 files changed

Lines changed: 51 additions & 33 deletions

File tree

Tracker/Ctracker.cpp

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "Ctracker.h"
2+
#include "HungarianAlg.h"
23

34
// ---------------------------------------------------------------------------
45
// Tracker. Manage tracks. Create, remove, update.
@@ -102,27 +103,27 @@ void CTracker::Update(
102103
AssignmentProblemSolver APS;
103104
APS.Solve(Cost, N, M, assignment, AssignmentProblemSolver::optimal);
104105

105-
// -----------------------------------
106-
// clean assignment from pairs with large distance
107-
// -----------------------------------
108-
for (size_t i = 0; i < assignment.size(); i++)
109-
{
110-
if (assignment[i] != -1)
111-
{
112-
if (Cost[i + assignment[i] * N] > dist_thres)
113-
{
114-
assignment[i] = -1;
115-
tracks[i]->skipped_frames++;
116-
}
117-
}
118-
else
119-
{
120-
// If track have no assigned detect, then increment skipped frames counter.
121-
tracks[i]->skipped_frames++;
122-
}
123-
}
124-
125-
// -----------------------------------
106+
// -----------------------------------
107+
// clean assignment from pairs with large distance
108+
// -----------------------------------
109+
for (size_t i = 0; i < assignment.size(); i++)
110+
{
111+
if (assignment[i] != -1)
112+
{
113+
if (Cost[i + assignment[i] * N] > dist_thres)
114+
{
115+
assignment[i] = -1;
116+
tracks[i]->skipped_frames++;
117+
}
118+
}
119+
else
120+
{
121+
// If track have no assigned detect, then increment skipped frames counter.
122+
tracks[i]->skipped_frames++;
123+
}
124+
}
125+
126+
// -----------------------------------
126127
// If track didn't get detects long time, remove it.
127128
// -----------------------------------
128129
for (int i = 0; i < static_cast<int>(tracks.size()); i++)

Tracker/Ctracker.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
#include "defines.h"
88
#include "track.h"
9-
#include "HungarianAlg.h"
109
#include "LocalTracker.h"
1110

1211
// --------------------------------------------------------------------------

main.cpp

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,31 @@ int main(int argc, char** argv)
5656
cv::Mat frame;
5757
cv::Mat gray;
5858

59+
const int StartFrame = 0;
60+
capture.set(cv::CAP_PROP_POS_FRAMES, StartFrame);
61+
62+
const int fps = std::max(1, static_cast<int>(capture.get(cv::CAP_PROP_FPS) + 0.5));
63+
5964
capture >> frame;
6065
cv::cvtColor(frame, gray, cv::COLOR_BGR2GRAY);
6166

6267
// If true then trajectories will be more smooth and accurate
6368
// But on high resolution videos with many objects may be to slow
64-
bool useLocalTracking = true;
69+
bool useLocalTracking = false;
6570

6671
CDetector detector(BackgroundSubtract::ALG_MOG, useLocalTracking, gray);
6772
detector.SetMinObjectSize(cv::Size(gray.cols / 50, gray.rows / 50));
68-
//detector.SetMinObjectSize(cv::Size(2, 2));
69-
70-
CTracker tracker(useLocalTracking, CTracker::RectsDist, CTracker::FilterRect, 0.2f, 0.1f, gray.cols / 10.0f, 10, 50);
73+
detector.SetMinObjectSize(cv::Size(4, 2));
74+
75+
CTracker tracker(useLocalTracking,
76+
CTracker::RectsDist,
77+
CTracker::FilterRect,
78+
0.2f, // Delta time for Kalman filter
79+
0.1f, // Accel noise magnitude for Kalman filter
80+
gray.cols / 100.0f, // Distance threshold between two frames
81+
fps, // Maximum allowed skipped frames
82+
5 * fps // Maximum trace length
83+
);
7184

7285
int k = 0;
7386

@@ -82,8 +95,6 @@ int main(int argc, char** argv)
8295
capture >> frame;
8396
if (frame.empty())
8497
{
85-
//capture.set(cv::CAP_PROP_POS_FRAMES, 0);
86-
//continue;
8798
break;
8899
}
89100
cv::cvtColor(frame, gray, cv::COLOR_BGR2GRAY);
@@ -103,12 +114,13 @@ int main(int argc, char** argv)
103114
int64 t2 = cv::getTickCount();
104115

105116
allTime += t2 - t1;
117+
int currTime = static_cast<int>((t2 - t1) / freq + 0.5);
106118

107-
std::cout << "Frame " << framesCounter << ": tracks = " << tracker.tracks.size() << ", time = " << ((t2 - t1) / freq) << std::endl;
119+
std::cout << "Frame " << framesCounter << ": tracks = " << tracker.tracks.size() << ", time = " << currTime << std::endl;
108120

109121
for (size_t i = 0; i < tracker.tracks.size(); i++)
110122
{
111-
if (tracker.tracks[i]->trace.size() > 10)
123+
if (tracker.tracks[i]->trace.size() > static_cast<size_t>(fps))
112124
{
113125
cv::rectangle(frame, tracker.tracks[i]->GetLastRect(), cv::Scalar(0, 255, 0), 1, CV_AA);
114126

@@ -121,7 +133,7 @@ int main(int argc, char** argv)
121133

122134
cv::imshow("Video", frame);
123135

124-
int waitTime = manualMode ? 0 : 20;
136+
int waitTime = manualMode ? 0 : std::max<int>(1, 1000 / fps - currTime);
125137
k = cv::waitKey(waitTime);
126138

127139
if (k == 'm' || k == 'M')
@@ -133,10 +145,16 @@ int main(int argc, char** argv)
133145
{
134146
writer << frame;
135147
}
148+
149+
++framesCounter;
150+
if (framesCounter > 500)
151+
{
152+
//break;
153+
}
136154
}
137155

138156
std::cout << "work time = " << (allTime / freq) << std::endl;
139-
//cv::waitKey(0);
157+
cv::waitKey(0);
140158

141159
#else
142160

vibe_src/BackgroundSubtract.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,5 @@ void BackgroundSubtract::subtract(const cv::Mat& image, cv::Mat& foreground)
8484
cv::Mat dilateElement = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3, 3), cv::Point(-1, -1));
8585
cv::dilate(foreground, foreground, dilateElement, cv::Point(-1, -1), 1);
8686

87-
//cv::imshow("after", foreground);
87+
cv::imshow("after", foreground);
8888
}

0 commit comments

Comments
 (0)