Skip to content

Commit fc1337d

Browse files
committed
Small code style refactoring
1 parent 7fae3ed commit fc1337d

5 files changed

Lines changed: 62 additions & 131 deletions

File tree

cars_counting/CarsCounting.cpp

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -207,50 +207,50 @@ void CarsCounting::DrawTrack(cv::Mat frame,
207207
cv::rectangle(frame, ResizeRect(track.m_rrect.boundingRect()), cv::Scalar(0, 255, 0), 1, CV_AA);
208208
#endif
209209

210-
if (!m_geoParams.Empty())
211-
{
212-
size_t period = 2 * cvRound(m_fps);
213-
if (period >= track.m_trace.size())
214-
period = track.m_trace.size();
215-
const auto& from = m_geoParams.Pix2Geo(track.m_trace[track.m_trace.size() - period]);
216-
const auto& to = m_geoParams.Pix2Geo(track.m_trace[track.m_trace.size() - 1]);
217-
auto dist = DistanceInMeters(from, to);
218-
219-
std::stringstream label;
220-
if (period >= cvRound(m_fps) / 4)
221-
{
222-
auto velocity = (3.6f * dist * m_fps) / period;
223-
//std::cout << track.m_type << ": distance " << std::fixed << std::setw(2) << std::setprecision(2) << dist << " on time " << (period / m_fps) << " with velocity " << velocity << " km/h: " << track.m_confidence << std::endl;
224-
if (velocity < 1.f || std::isnan(velocity))
225-
velocity = 0;
226-
label << track.m_type << " " << std::fixed << std::setw(2) << std::setprecision(2) << velocity << " km/h";
227-
228-
int baseLine = 0;
229-
double fontScale = 0.5;
230-
cv::Size labelSize = cv::getTextSize(label.str(), cv::FONT_HERSHEY_SIMPLEX, fontScale, 1, &baseLine);
231-
232-
cv::Rect brect = track.m_rrect.boundingRect();
233-
if (brect.x < 0)
234-
{
235-
brect.width = std::min(brect.width, frame.cols - 1);
236-
brect.x = 0;
237-
}
238-
else if (brect.x + brect.width >= frame.cols)
239-
{
240-
brect.x = std::max(0, frame.cols - brect.width - 1);
241-
brect.width = std::min(brect.width, frame.cols - 1);
242-
}
243-
if (brect.y - labelSize.height < 0)
244-
{
245-
brect.height = std::min(brect.height, frame.rows - 1);
246-
brect.y = labelSize.height;
247-
}
248-
else if (brect.y + brect.height >= frame.rows)
249-
{
250-
brect.y = std::max(0, frame.rows - brect.height - 1);
251-
brect.height = std::min(brect.height, frame.rows - 1);
252-
}
253-
cv::rectangle(frame, cv::Rect(cv::Point(brect.x, brect.y - labelSize.height), cv::Size(labelSize.width, labelSize.height + baseLine)), cv::Scalar(200, 200, 200), cv::FILLED);
210+
if (!m_geoParams.Empty())
211+
{
212+
size_t period = 2 * cvRound(m_fps);
213+
if (period >= track.m_trace.size())
214+
period = track.m_trace.size();
215+
const auto& from = m_geoParams.Pix2Geo(track.m_trace[track.m_trace.size() - period]);
216+
const auto& to = m_geoParams.Pix2Geo(track.m_trace[track.m_trace.size() - 1]);
217+
auto dist = DistanceInMeters(from, to);
218+
219+
std::stringstream label;
220+
if (period >= cvRound(m_fps) / 4)
221+
{
222+
auto velocity = (3.6f * dist * m_fps) / period;
223+
//std::cout << track.m_type << ": distance " << std::fixed << std::setw(2) << std::setprecision(2) << dist << " on time " << (period / m_fps) << " with velocity " << velocity << " km/h: " << track.m_confidence << std::endl;
224+
if (velocity < 1.f || std::isnan(velocity))
225+
velocity = 0;
226+
label << track.m_type << " " << std::fixed << std::setw(2) << std::setprecision(2) << velocity << " km/h";
227+
228+
int baseLine = 0;
229+
double fontScale = 0.5;
230+
cv::Size labelSize = cv::getTextSize(label.str(), cv::FONT_HERSHEY_SIMPLEX, fontScale, 1, &baseLine);
231+
232+
cv::Rect brect = track.m_rrect.boundingRect();
233+
if (brect.x < 0)
234+
{
235+
brect.width = std::min(brect.width, frame.cols - 1);
236+
brect.x = 0;
237+
}
238+
else if (brect.x + brect.width >= frame.cols)
239+
{
240+
brect.x = std::max(0, frame.cols - brect.width - 1);
241+
brect.width = std::min(brect.width, frame.cols - 1);
242+
}
243+
if (brect.y - labelSize.height < 0)
244+
{
245+
brect.height = std::min(brect.height, frame.rows - 1);
246+
brect.y = labelSize.height;
247+
}
248+
else if (brect.y + brect.height >= frame.rows)
249+
{
250+
brect.y = std::max(0, frame.rows - brect.height - 1);
251+
brect.height = std::min(brect.height, frame.rows - 1);
252+
}
253+
cv::rectangle(frame, cv::Rect(cv::Point(brect.x, brect.y - labelSize.height), cv::Size(labelSize.width, labelSize.height + baseLine)), cv::Scalar(200, 200, 200), cv::FILLED);
254254
cv::putText(frame, label.str(), brect.tl(), cv::FONT_HERSHEY_SIMPLEX, fontScale, cv::Scalar(0, 0, 0));
255255
}
256256
}
@@ -380,9 +380,9 @@ bool CarsCounting::InitTracker(cv::UMat frame)
380380
}
381381

382382
#if 0
383-
std::vector<cv::Point> framePoints{ cv::Point(420, 348), cv::Point(509, 283), cv::Point(731, 281), cv::Point(840, 343) };
384-
std::vector<cv::Point2f> geoPoints{ cv::Point2f(45.526646, 5.974535), cv::Point2f(45.527566, 5.973849), cv::Point2f(45.527904, 5.974135), cv::Point2f(45.526867, 5.974826) };
385-
m_geoParams.SetKeyPoints(framePoints, geoPoints);
383+
std::vector<cv::Point> framePoints{ cv::Point(420, 348), cv::Point(509, 283), cv::Point(731, 281), cv::Point(840, 343) };
384+
std::vector<cv::Point2f> geoPoints{ cv::Point2f(45.526646, 5.974535), cv::Point2f(45.527566, 5.973849), cv::Point2f(45.527904, 5.974135), cv::Point2f(45.526867, 5.974826) };
385+
m_geoParams.SetKeyPoints(framePoints, geoPoints);
386386
#endif
387387

388388
return res;
@@ -401,13 +401,13 @@ void CarsCounting::DrawData(cv::Mat frame, int framesCounter, int currTime)
401401
std::cout << "Frame " << framesCounter << ": tracks = " << tracks.size() << ", time = " << currTime << std::endl;
402402
}
403403

404-
if (!m_geoParams.Empty())
405-
{
406-
std::vector<cv::Point> points = m_geoParams.GetFramePoints();
407-
for (size_t i = 0; i < points.size(); ++i)
408-
{
409-
cv::line(frame, points[i % points.size()], points[(i + 1) % points.size()], cv::Scalar(255, 255, 255), 1, cv::LINE_AA);
410-
}
404+
if (!m_geoParams.Empty())
405+
{
406+
std::vector<cv::Point> points = m_geoParams.GetFramePoints();
407+
for (size_t i = 0; i < points.size(); ++i)
408+
{
409+
cv::line(frame, points[i % points.size()], points[(i + 1) % points.size()], cv::Scalar(255, 255, 255), 1, cv::LINE_AA);
410+
}
411411
}
412412

413413
for (const auto& track : tracks)

src/Tracker/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ else(HAVE_OPENCV_CONTRIB)
157157

158158
endif(HAVE_OPENCV_CONTRIB)
159159

160-
option(USE_OCV_UKF "Should use the Unscented Kalman Filterr from opencv_contrib?" OFF)
160+
option(USE_OCV_UKF "Should use the Unscented Kalman Filter from opencv_contrib?" OFF)
161161

162162
if(USE_OCV_UKF)
163163
add_definitions(-DUSE_OCV_UKF)

src/Tracker/Ctracker.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,24 +226,18 @@ void CTracker::CreateDistaceMatrix(const regions_t& regions, distMatrix_t& costM
226226
++ind;
227227

228228
if (m_settings.m_distType[ind] > 0.0f && ind == tracking::DistJaccard)
229-
{
230229
dist += m_settings.m_distType[ind] * track->CalcDistJaccard(reg);
231-
}
232230
++ind;
233231

234232
if (m_settings.m_distType[ind] > 0.0f && ind == tracking::DistHist)
235-
{
236233
dist += m_settings.m_distType[ind] * track->CalcDistHist(reg, currFrame);
237-
}
238234
++ind;
239235
assert(ind == tracking::DistsCount);
240236
}
241237

242238
costMatrix[i + j * N] = dist;
243239
if (dist > maxCost)
244-
{
245240
maxCost = dist;
246-
}
247241
}
248242
}
249243
}

src/Tracker/track.cpp

Lines changed: 4 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,9 @@ void CTrack::Update(
151151
)
152152
{
153153
if (m_filterObjectSize) // Kalman filter for object coordinates and size
154-
{
155154
RectUpdate(region, dataCorrect, prevFrame, currFrame);
156-
}
157155
else // Kalman filter only for object center
158-
{
159156
PointUpdate(region.m_rrect.center, region.m_rrect.size, dataCorrect, currFrame.size());
160-
}
161157

162158
if (dataCorrect)
163159
{
@@ -174,9 +170,7 @@ void CTrack::Update(
174170
}
175171

176172
if (m_trace.size() > max_trace_length)
177-
{
178173
m_trace.pop_front(m_trace.size() - max_trace_length);
179-
}
180174
}
181175

182176
///
@@ -469,13 +463,11 @@ void CTrack::RectUpdate(
469463

470464
cv::Size roiSize(std::max(2 * brect.width, currFrame.cols / 4), std::max(2 * brect.height, currFrame.rows / 4));
471465
if (roiSize.width > currFrame.cols)
472-
{
473466
roiSize.width = currFrame.cols;
474-
}
467+
475468
if (roiSize.height > currFrame.rows)
476-
{
477469
roiSize.height = currFrame.rows;
478-
}
470+
479471
cv::Point roiTL(brect.x + brect.width / 2 - roiSize.width / 2, brect.y + brect.height / 2 - roiSize.height / 2);
480472
cv::Rect roiRect(roiTL, roiSize);
481473
Clamp(roiRect.x, roiRect.width, currFrame.cols);
@@ -505,13 +497,9 @@ void CTrack::RectUpdate(
505497
lastRect.area() > 0)
506498
{
507499
if (m_staticFrame.empty())
508-
{
509500
m_tracker->init(cv::UMat(prevFrame, roiRect), lastRect);
510-
}
511501
else
512-
{
513502
m_tracker->init(cv::UMat(m_staticFrame, roiRect), lastRect);
514-
}
515503
#if 0
516504
#ifndef SILENT_WORK
517505
cv::Mat tmp = cv::UMat(prevFrame, roiRect).getMat(cv::ACCESS_READ).clone();
@@ -550,9 +538,7 @@ void CTrack::RectUpdate(
550538
else
551539
{
552540
if (m_tracker && !m_tracker.empty())
553-
{
554541
m_tracker.release();
555-
}
556542
}
557543
#else
558544
std::cerr << "KCF tracker was disabled in CMAKE! Set lostTrackType = TrackNone in constructor." << std::endl;
@@ -572,9 +558,7 @@ void CTrack::RectUpdate(
572558

573559
cv::Rect2d lastRect(brect.x, brect.y, brect.width, brect.height);
574560
if (!m_staticFrame.empty())
575-
{
576561
lastRect = cv::Rect2d(m_staticRect.x, m_staticRect.y, m_staticRect.width, m_staticRect.height);
577-
}
578562

579563
if (lastRect.x >= 0 &&
580564
lastRect.y >= 0 &&
@@ -623,25 +607,20 @@ void CTrack::RectUpdate(
623607
{
624608
UpdateRRect(brect, m_kalman.Update(newRect.boundingRect(), true));
625609
}
626-
627610
recalcPrediction = false;
628611
}
629612
}
630613
}
631614
else
632615
{
633616
if (m_VOTTracker)
634-
{
635617
m_VOTTracker = nullptr;
636-
}
637618
}
638619
break;
639620
}
640621

641622
if (recalcPrediction)
642-
{
643623
UpdateRRect(m_predictionRect.boundingRect(), m_kalman.Update(region.m_brect, dataCorrect));
644-
}
645624

646625
cv::Rect brect = m_predictionRect.boundingRect();
647626
int dx = Clamp(brect.x, brect.width, currFrame.cols);
@@ -666,14 +645,11 @@ void CTrack::CreateExternalTracker(int channels)
666645
{
667646
case tracking::TrackNone:
668647
if (m_VOTTracker)
669-
{
670648
m_VOTTracker = nullptr;
671-
}
649+
672650
#ifdef USE_OCV_KCF
673651
if (m_tracker && !m_tracker.empty())
674-
{
675652
m_tracker.release();
676-
}
677653
#endif
678654
break;
679655

@@ -704,9 +680,7 @@ void CTrack::CreateExternalTracker(int channels)
704680
}
705681
#endif
706682
if (m_VOTTracker)
707-
{
708683
m_VOTTracker = nullptr;
709-
}
710684
break;
711685

712686
case tracking::TrackMIL:
@@ -723,9 +697,7 @@ void CTrack::CreateExternalTracker(int channels)
723697
}
724698
#endif
725699
if (m_VOTTracker)
726-
{
727700
m_VOTTracker = nullptr;
728-
}
729701
break;
730702

731703
case tracking::TrackMedianFlow:
@@ -742,9 +714,7 @@ void CTrack::CreateExternalTracker(int channels)
742714
}
743715
#endif
744716
if (m_VOTTracker)
745-
{
746717
m_VOTTracker = nullptr;
747-
}
748718
break;
749719

750720
case tracking::TrackGOTURN:
@@ -761,9 +731,7 @@ void CTrack::CreateExternalTracker(int channels)
761731
}
762732
#endif
763733
if (m_VOTTracker)
764-
{
765734
m_VOTTracker = nullptr;
766-
}
767735
break;
768736

769737
case tracking::TrackMOSSE:
@@ -778,9 +746,7 @@ void CTrack::CreateExternalTracker(int channels)
778746
}
779747
#endif
780748
if (m_VOTTracker)
781-
{
782749
m_VOTTracker = nullptr;
783-
}
784750
break;
785751

786752
case tracking::TrackCSRT:
@@ -805,36 +771,26 @@ void CTrack::CreateExternalTracker(int channels)
805771
}
806772
#endif
807773
if (m_VOTTracker)
808-
{
809774
m_VOTTracker = nullptr;
810-
}
811775
break;
812776

813777
case tracking::TrackDAT:
814778
#ifdef USE_OCV_KCF
815779
if (m_tracker && !m_tracker.empty())
816-
{
817-
m_tracker.release();
818-
}
780+
m_tracker.release();
819781
#endif
820782
if (!m_VOTTracker)
821-
{
822783
m_VOTTracker = std::unique_ptr<DAT_TRACKER>(new DAT_TRACKER());
823-
}
824784
break;
825785

826786
case tracking::TrackSTAPLE:
827787
#ifdef USE_OCV_KCF
828788
if (m_tracker && !m_tracker.empty())
829-
{
830789
m_tracker.release();
831-
}
832790
#endif
833791
#ifdef USE_STAPLE_TRACKER
834792
if (!m_VOTTracker)
835-
{
836793
m_VOTTracker = std::unique_ptr<STAPLE_TRACKER>(new STAPLE_TRACKER());
837-
}
838794
#else
839795
std::cerr << "Project was compiled without STAPLE tracking!" << std::endl;
840796
#endif
@@ -843,15 +799,11 @@ void CTrack::CreateExternalTracker(int channels)
843799
case tracking::TrackLDES:
844800
#ifdef USE_OCV_KCF
845801
if (m_tracker && !m_tracker.empty())
846-
{
847802
m_tracker.release();
848-
}
849803
#endif
850804
#ifdef USE_STAPLE_TRACKER
851805
if (!m_VOTTracker)
852-
{
853806
m_VOTTracker = std::unique_ptr<LDESTracker>(new LDESTracker());
854-
}
855807
#else
856808
std::cerr << "Project was compiled without STAPLE tracking!" << std::endl;
857809
#endif

0 commit comments

Comments
 (0)