Skip to content

Commit 98ba77e

Browse files
Nuzhny007Smorodov
authored andcommitted
Added new CSRT tracker from opencv_contrib (Smorodov#106)
* Added hough3d sources * Calculate 3D hough trajectory lines * Added fps * Some warnings removing * Fixed warning * Added small crops in DNN detector for High resolution videos and small objects * Small refactoring * Some fix for single tracking * Added openmp flags to the compiler * Draw trajectory option * Update Readme * Fixed link * Hough3D disabled now in CMake * Added Yolo (and Tiny Yolo) detector * Update Readme * Remove unused Hybrid face detector * Extract Capture+Detection into the separate thread * Fixed potential deadlock * Points visualization * Control out the frame moving * Remove hough3d from master * Fixed NMS for object with different types * Fix in Readme and TODO * OpenCV 4.0 ready * Added YOLO v3 tiny * Added compute target for DNN examples (default is OpenCL_FP16) * Fixed compatible bug with OpenCV version < 4.0 * Fixed project name in CMake * Simple implementation of the Abandoned detection * Increased timeout * Fixed help message * Added Abandoned to the Readme * Fixed Readme * Switch travis to the latest OpenCV 3.4.1 version * Set timeouts to 60 sec * Fixed travis script for OpenCV 3.4.1 * Fixed travis for gcc6 * Fixed linker error * Fixed compiler in travis * Fixed travis and CMakeLists for C++14 support * Capture for webcams * Fix bug with ROI for local tracking * Automatic inertia correction in linear Kalman filter * Update TODO * Yolo eample refactoring * Refactoring: split project to share library and example * Added cars counting example * Fix for OpenCV 4.0 * Single threaded cars counting example * Added lines class * Added API for road lines * Added cars counting code * Changed road line caption * Fixed bug with roi rect clamping * To Linear Kalman filter added speed for rectangle size * Improved quality of line intersection algorithm * Fixed bug with Kalman width and height correction * Return MOSSE tracker * Temporal correction object size when selected only points filter * Some small improvements * Compilation on Windows MSVC 2017 with OpenCV 4.0 * Added new CSRT tracker from opencv_contrib * Fixed some warnings * Fixed type conversion warnings * In CMake added options for enable|disable building cars counting and all other examples
1 parent b2a1ea6 commit 98ba77e

12 files changed

Lines changed: 153 additions & 67 deletions

File tree

CMakeLists.txt

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
cmake_minimum_required(VERSION 2.8)
1+
cmake_minimum_required(VERSION 3.5)
22

3-
project(MultitargetTracking)
3+
project(MTTracking)
44

55
unset(CMAKE_CXX_FLAGS CACHE)
66

@@ -18,14 +18,21 @@ elseif (MSVC)
1818
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc /W4 -DGTL_STATIC" CACHE STRING COMPILE_FLAGS FORCE)
1919
endif()
2020

21-
2221
set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/build)
2322
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
2423
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})
2524

2625
FIND_PACKAGE(OpenCV)
2726

2827
add_subdirectory(src)
29-
add_subdirectory(example)
30-
add_subdirectory(cars_counting)
3128

29+
option(BUILD_EXAMPLES "Should compiled examples (motion detection, pedestrians, faces, DNNs etc)?" ON)
30+
if (BUILD_EXAMPLES)
31+
add_subdirectory(example)
32+
endif(BUILD_EXAMPLES)
33+
34+
option(BUILD_CARS_COUNTING "Should compiled Cars counting example?" OFF)
35+
if (BUILD_CARS_COUNTING)
36+
add_subdirectory(cars_counting)
37+
endif(BUILD_CARS_COUNTING)
38+

cars_counting/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required (VERSION 2.8)
1+
cmake_minimum_required (VERSION 3.5)
22

33
project(CarsCounting)
44

cars_counting/CarsCounting.cpp

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ void CarsCounting::Process()
7676

7777
m_fps = std::max(1.f, (float)capture.get(cv::CAP_PROP_FPS));
7878

79+
m_fps = std::max(1.f, (float)capture.get(cv::CAP_PROP_FPS));
80+
7981
cv::Mat colorFrame;
8082
cv::UMat grayFrame;
8183
for (;;)
@@ -189,11 +191,19 @@ void CarsCounting::DrawTrack(cv::Mat frame,
189191

190192
if (isStatic)
191193
{
192-
cv::rectangle(frame, ResizeRect(track.GetLastRect()), cv::Scalar(255, 0, 255), 2, CV_AA);
194+
#if (CV_VERSION_MAJOR >= 4)
195+
cv::rectangle(frame, ResizeRect(track.GetLastRect()), cv::Scalar(255, 0, 255), 2, cv::LINE_AA);
196+
#else
197+
cv::rectangle(frame, ResizeRect(track.GetLastRect()), cv::Scalar(255, 0, 255), 2, CV_AA);
198+
#endif
193199
}
194200
else
195201
{
196-
cv::rectangle(frame, ResizeRect(track.GetLastRect()), cv::Scalar(0, 255, 0), 1, CV_AA);
202+
#if (CV_VERSION_MAJOR >= 4)
203+
cv::rectangle(frame, ResizeRect(track.GetLastRect()), cv::Scalar(0, 255, 0), 1, cv::LINE_AA);
204+
#else
205+
cv::rectangle(frame, ResizeRect(track.GetLastRect()), cv::Scalar(0, 255, 0), 1, CV_AA);
206+
#endif
197207
}
198208

199209
if (drawTrajectory)
@@ -204,11 +214,18 @@ void CarsCounting::DrawTrack(cv::Mat frame,
204214
{
205215
const TrajectoryPoint& pt1 = track.m_trace.at(j);
206216
const TrajectoryPoint& pt2 = track.m_trace.at(j + 1);
207-
208-
cv::line(frame, ResizePoint(pt1.m_prediction), ResizePoint(pt2.m_prediction), cl, 1, CV_AA);
217+
#if (CV_VERSION_MAJOR >= 4)
218+
cv::line(frame, ResizePoint(pt1.m_prediction), ResizePoint(pt2.m_prediction), cl, 1, cv::LINE_AA);
219+
#else
220+
cv::line(frame, ResizePoint(pt1.m_prediction), ResizePoint(pt2.m_prediction), cl, 1, CV_AA);
221+
#endif
209222
if (!pt2.m_hasRaw)
210223
{
211-
cv::circle(frame, ResizePoint(pt2.m_prediction), 4, cl, 1, CV_AA);
224+
#if (CV_VERSION_MAJOR >= 4)
225+
cv::circle(frame, ResizePoint(pt2.m_prediction), 4, cl, 1, cv::LINE_AA);
226+
#else
227+
cv::circle(frame, ResizePoint(pt2.m_prediction), 4, cl, 1, CV_AA);
228+
#endif
212229
}
213230
}
214231
}
@@ -219,7 +236,11 @@ void CarsCounting::DrawTrack(cv::Mat frame,
219236

220237
for (auto pt : track.m_lastRegion.m_points)
221238
{
222-
cv::circle(frame, cv::Point(cvRound(pt.x), cvRound(pt.y)), 1, cl, -1, CV_AA);
239+
#if (CV_VERSION_MAJOR >= 4)
240+
cv::circle(frame, cv::Point(cvRound(pt.x), cvRound(pt.y)), 1, cl, -1, cv::LINE_AA);
241+
#else
242+
cv::circle(frame, cv::Point(cvRound(pt.x), cvRound(pt.y)), 1, cl, -1, CV_AA);
243+
#endif
223244
}
224245
}
225246
}
@@ -254,24 +275,24 @@ bool CarsCounting::InitTracker(cv::UMat frame)
254275
settings.m_distType = tracking::DistCenters;
255276
settings.m_kalmanType = tracking::KalmanLinear;
256277
settings.m_filterGoal = tracking::FilterRect;
257-
settings.m_lostTrackType = tracking::TrackKCF; // Use KCF tracker for collisions resolving
278+
settings.m_lostTrackType = tracking::TrackerCSRT; // Use KCF tracker for collisions resolving
258279
settings.m_matchType = tracking::MatchHungrian;
259280
settings.m_dt = 0.5f; // Delta time for Kalman filter
260281
settings.m_accelNoiseMag = 0.5f; // Accel noise magnitude for Kalman filter
261-
settings.m_distThres = frame.rows / 15; // Distance threshold between region and object on two frames
282+
settings.m_distThres = frame.rows / 15.f; // Distance threshold between region and object on two frames
262283

263284
settings.m_useAbandonedDetection = false;
264285
if (settings.m_useAbandonedDetection)
265286
{
266287
settings.m_minStaticTime = minStaticTime;
267288
settings.m_maxStaticTime = 60;
268-
settings.m_maximumAllowedSkippedFrames = settings.m_minStaticTime * m_fps; // Maximum allowed skipped frames
289+
settings.m_maximumAllowedSkippedFrames = cvRound(settings.m_minStaticTime * m_fps); // Maximum allowed skipped frames
269290
settings.m_maxTraceLength = 2 * settings.m_maximumAllowedSkippedFrames; // Maximum trace length
270291
}
271292
else
272293
{
273-
settings.m_maximumAllowedSkippedFrames = 2 * m_fps; // Maximum allowed skipped frames
274-
settings.m_maxTraceLength = 4 * m_fps; // Maximum trace length
294+
settings.m_maximumAllowedSkippedFrames = cvRound(2 * m_fps); // Maximum allowed skipped frames
295+
settings.m_maxTraceLength = cvRound(4 * m_fps); // Maximum trace length
275296
}
276297

277298
m_tracker = std::make_unique<CTracker>(settings);

example/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required (VERSION 2.8)
1+
cmake_minimum_required (VERSION 3.5)
22

33
project(MultitargetTracker)
44

example/MouseExample.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@ void MouseTracking(cv::CommandLineParser parser)
8686

8787
for (size_t i = 0; i < pts.size(); i++)
8888
{
89-
cv::circle(frame, pts[i], 3, cv::Scalar(0, 255, 0), 1, CV_AA);
89+
#if (CV_VERSION_MAJOR >= 4)
90+
cv::circle(frame, pts[i], 3, cv::Scalar(0, 255, 0), 1, cv::LINE_AA);
91+
#else
92+
cv::circle(frame, pts[i], 3, cv::Scalar(0, 255, 0), 1, CV_AA);
93+
#endif
9094
}
9195

9296
tracker.Update(regions, cv::UMat(), 100);
@@ -101,7 +105,11 @@ void MouseTracking(cv::CommandLineParser parser)
101105
{
102106
for (size_t j = 0; j < track->m_trace.size() - 1; j++)
103107
{
104-
cv::line(frame, track->m_trace[j], track->m_trace[j + 1], colors[i % colors.size()], 2, CV_AA);
108+
#if (CV_VERSION_MAJOR >= 4)
109+
cv::line(frame, track->m_trace[j], track->m_trace[j + 1], colors[i % colors.size()], 2, cv::LINE_AA);
110+
#else
111+
cv::line(frame, track->m_trace[j], track->m_trace[j + 1], colors[i % colors.size()], 2, CV_AA);
112+
#endif
105113
}
106114
}
107115
}

example/VideoExample.cpp

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -322,11 +322,19 @@ void VideoExample::DrawTrack(cv::Mat frame,
322322

323323
if (isStatic)
324324
{
325-
cv::rectangle(frame, ResizeRect(track.GetLastRect()), cv::Scalar(255, 0, 255), 2, CV_AA);
325+
#if (CV_VERSION_MAJOR >= 4)
326+
cv::rectangle(frame, ResizeRect(track.GetLastRect()), cv::Scalar(255, 0, 255), 2, cv::LINE_AA);
327+
#else
328+
cv::rectangle(frame, ResizeRect(track.GetLastRect()), cv::Scalar(255, 0, 255), 2, CV_AA);
329+
#endif
326330
}
327331
else
328332
{
329-
cv::rectangle(frame, ResizeRect(track.GetLastRect()), cv::Scalar(0, 255, 0), 1, CV_AA);
333+
#if (CV_VERSION_MAJOR >= 4)
334+
cv::rectangle(frame, ResizeRect(track.GetLastRect()), cv::Scalar(0, 255, 0), 1, cv::LINE_AA);
335+
#else
336+
cv::rectangle(frame, ResizeRect(track.GetLastRect()), cv::Scalar(0, 255, 0), 1, CV_AA);
337+
#endif
330338
}
331339

332340
if (drawTrajectory)
@@ -337,11 +345,18 @@ void VideoExample::DrawTrack(cv::Mat frame,
337345
{
338346
const TrajectoryPoint& pt1 = track.m_trace.at(j);
339347
const TrajectoryPoint& pt2 = track.m_trace.at(j + 1);
340-
341-
cv::line(frame, ResizePoint(pt1.m_prediction), ResizePoint(pt2.m_prediction), cl, 1, CV_AA);
348+
#if (CV_VERSION_MAJOR >= 4)
349+
cv::line(frame, ResizePoint(pt1.m_prediction), ResizePoint(pt2.m_prediction), cl, 1, cv::LINE_AA);
350+
#else
351+
cv::line(frame, ResizePoint(pt1.m_prediction), ResizePoint(pt2.m_prediction), cl, 1, CV_AA);
352+
#endif
342353
if (!pt2.m_hasRaw)
343354
{
344-
cv::circle(frame, ResizePoint(pt2.m_prediction), 4, cl, 1, CV_AA);
355+
#if (CV_VERSION_MAJOR >= 4)
356+
cv::circle(frame, ResizePoint(pt2.m_prediction), 4, cl, 1, cv::LINE_AA);
357+
#else
358+
cv::circle(frame, ResizePoint(pt2.m_prediction), 4, cl, 1, CV_AA);
359+
#endif
345360
}
346361
}
347362
}
@@ -352,7 +367,11 @@ void VideoExample::DrawTrack(cv::Mat frame,
352367

353368
for (auto pt : track.m_lastRegion.m_points)
354369
{
355-
cv::circle(frame, cv::Point(cvRound(pt.x), cvRound(pt.y)), 1, cl, -1, CV_AA);
370+
#if (CV_VERSION_MAJOR >= 4)
371+
cv::circle(frame, cv::Point(cvRound(pt.x), cvRound(pt.y)), 1, cl, -1, cv::LINE_AA);
372+
#else
373+
cv::circle(frame, cv::Point(cvRound(pt.x), cvRound(pt.y)), 1, cl, -1, CV_AA);
374+
#endif
356375
}
357376
}
358377
}

example/VideoExample.h

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -138,20 +138,20 @@ class MotionDetectorExample : public VideoExample
138138
settings.m_matchType = tracking::MatchHungrian;
139139
settings.m_dt = 0.4f; // Delta time for Kalman filter
140140
settings.m_accelNoiseMag = 0.5f; // Accel noise magnitude for Kalman filter
141-
settings.m_distThres = frame.rows / 20; // Distance threshold between region and object on two frames
141+
settings.m_distThres = frame.rows / 20.f; // Distance threshold between region and object on two frames
142142

143143
settings.m_useAbandonedDetection = true;
144144
if (settings.m_useAbandonedDetection)
145145
{
146146
settings.m_minStaticTime = minStaticTime;
147147
settings.m_maxStaticTime = 60;
148-
settings.m_maximumAllowedSkippedFrames = settings.m_minStaticTime * m_fps; // Maximum allowed skipped frames
148+
settings.m_maximumAllowedSkippedFrames = cvRound(settings.m_minStaticTime * m_fps); // Maximum allowed skipped frames
149149
settings.m_maxTraceLength = 2 * settings.m_maximumAllowedSkippedFrames; // Maximum trace length
150150
}
151151
else
152152
{
153-
settings.m_maximumAllowedSkippedFrames = 2 * m_fps; // Maximum allowed skipped frames
154-
settings.m_maxTraceLength = 4 * m_fps; // Maximum trace length
153+
settings.m_maximumAllowedSkippedFrames = cvRound(2 * m_fps); // Maximum allowed skipped frames
154+
settings.m_maxTraceLength = cvRound(4 * m_fps); // Maximum trace length
155155
}
156156

157157
m_tracker = std::make_unique<CTracker>(settings);
@@ -235,8 +235,8 @@ class FaceDetectorExample : public VideoExample
235235
settings.m_dt = 0.3f; // Delta time for Kalman filter
236236
settings.m_accelNoiseMag = 0.1f; // Accel noise magnitude for Kalman filter
237237
settings.m_distThres = 0.8f; // Distance threshold between region and object on two frames
238-
settings.m_maximumAllowedSkippedFrames = m_fps / 2; // Maximum allowed skipped frames
239-
settings.m_maxTraceLength = 5 * m_fps; // Maximum trace length
238+
settings.m_maximumAllowedSkippedFrames = cvRound(m_fps / 2); // Maximum allowed skipped frames
239+
settings.m_maxTraceLength = cvRound(5 * m_fps); // Maximum trace length
240240

241241
m_tracker = std::make_unique<CTracker>(settings);
242242

@@ -313,9 +313,9 @@ class PedestrianDetectorExample : public VideoExample
313313
settings.m_matchType = tracking::MatchHungrian;
314314
settings.m_dt = 0.3f; // Delta time for Kalman filter
315315
settings.m_accelNoiseMag = 0.1f; // Accel noise magnitude for Kalman filter
316-
settings.m_distThres = frame.rows / 10; // Distance threshold between region and object on two frames
317-
settings.m_maximumAllowedSkippedFrames = m_fps; // Maximum allowed skipped frames
318-
settings.m_maxTraceLength = 5 * m_fps; // Maximum trace length
316+
settings.m_distThres = frame.rows / 10.f; // Distance threshold between region and object on two frames
317+
settings.m_maximumAllowedSkippedFrames = cvRound(m_fps); // Maximum allowed skipped frames
318+
settings.m_maxTraceLength = cvRound(5 * m_fps); // Maximum trace length
319319

320320
m_tracker = std::make_unique<CTracker>(settings);
321321

@@ -391,9 +391,9 @@ class SSDMobileNetExample : public VideoExample
391391
settings.m_matchType = tracking::MatchHungrian;
392392
settings.m_dt = 0.3f; // Delta time for Kalman filter
393393
settings.m_accelNoiseMag = 0.1f; // Accel noise magnitude for Kalman filter
394-
settings.m_distThres = frame.rows / 10; // Distance threshold between region and object on two frames
395-
settings.m_maximumAllowedSkippedFrames = 2 * m_fps; // Maximum allowed skipped frames
396-
settings.m_maxTraceLength = 5 * m_fps; // Maximum trace length
394+
settings.m_distThres = frame.rows / 10.f; // Distance threshold between region and object on two frames
395+
settings.m_maximumAllowedSkippedFrames = cvRound(2 * m_fps); // Maximum allowed skipped frames
396+
settings.m_maxTraceLength = cvRound(5 * m_fps); // Maximum trace length
397397

398398
m_tracker = std::make_unique<CTracker>(settings);
399399

@@ -424,7 +424,11 @@ class SSDMobileNetExample : public VideoExample
424424
int baseLine = 0;
425425
cv::Size labelSize = cv::getTextSize(label, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
426426
auto rect(track->GetLastRect());
427-
cv::rectangle(frame, cv::Rect(cv::Point(rect.x, rect.y - labelSize.height), cv::Size(labelSize.width, labelSize.height + baseLine)), cv::Scalar(255, 255, 255), CV_FILLED);
427+
#if (CV_VERSION_MAJOR >= 4)
428+
cv::rectangle(frame, cv::Rect(cv::Point(rect.x, rect.y - labelSize.height), cv::Size(labelSize.width, labelSize.height + baseLine)), cv::Scalar(255, 255, 255), cv::FILLED);
429+
#else
430+
cv::rectangle(frame, cv::Rect(cv::Point(rect.x, rect.y - labelSize.height), cv::Size(labelSize.width, labelSize.height + baseLine)), cv::Scalar(255, 255, 255), CV_FILLED);
431+
#endif
428432
cv::putText(frame, label, cv::Point(rect.x, rect.y), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));
429433
}
430434
}
@@ -502,9 +506,9 @@ class YoloExample : public VideoExample
502506
settings.m_matchType = tracking::MatchHungrian;
503507
settings.m_dt = 0.3f; // Delta time for Kalman filter
504508
settings.m_accelNoiseMag = 0.2f; // Accel noise magnitude for Kalman filter
505-
settings.m_distThres = frame.rows / 10; // Distance threshold between region and object on two frames
506-
settings.m_maximumAllowedSkippedFrames = 2 * m_fps; // Maximum allowed skipped frames
507-
settings.m_maxTraceLength = 5 * m_fps; // Maximum trace length
509+
settings.m_distThres = frame.rows / 10.f; // Distance threshold between region and object on two frames
510+
settings.m_maximumAllowedSkippedFrames = cvRound(2 * m_fps); // Maximum allowed skipped frames
511+
settings.m_maxTraceLength = cvRound(5 * m_fps); // Maximum trace length
508512

509513
m_tracker = std::make_unique<CTracker>(settings);
510514

@@ -535,7 +539,11 @@ class YoloExample : public VideoExample
535539
int baseLine = 0;
536540
cv::Size labelSize = cv::getTextSize(label, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
537541
auto rect(track->GetLastRect());
538-
cv::rectangle(frame, cv::Rect(cv::Point(rect.x, rect.y - labelSize.height), cv::Size(labelSize.width, labelSize.height + baseLine)), cv::Scalar(255, 255, 255), CV_FILLED);
542+
#if (CV_VERSION_MAJOR >= 4)
543+
cv::rectangle(frame, cv::Rect(cv::Point(rect.x, rect.y - labelSize.height), cv::Size(labelSize.width, labelSize.height + baseLine)), cv::Scalar(255, 255, 255), cv::FILLED);
544+
#else
545+
cv::rectangle(frame, cv::Rect(cv::Point(rect.x, rect.y - labelSize.height), cv::Size(labelSize.width, labelSize.height + baseLine)), cv::Scalar(255, 255, 255), CV_FILLED);
546+
#endif
539547
cv::putText(frame, label, cv::Point(rect.x, rect.y), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));
540548
}
541549
}

0 commit comments

Comments
 (0)