From 99fb0282ba5cdfdb2b889df8916b36207d21ded0 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 6 Mar 2016 09:11:19 +0100 Subject: tracker/pt: less malloc/free each frame --- tracker-pt/point_extractor.cpp | 29 ++++++----------------------- tracker-pt/point_extractor.h | 19 ++++++++++++++++++- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/tracker-pt/point_extractor.cpp b/tracker-pt/point_extractor.cpp index da05feb3..a1294c1e 100644 --- a/tracker-pt/point_extractor.cpp +++ b/tracker-pt/point_extractor.cpp @@ -15,6 +15,8 @@ PointExtractor::PointExtractor() { + blobs.reserve(max_blobs); + points.reserve(max_blobs); } const std::vector& PointExtractor::extract_points(cv::Mat& frame) @@ -34,23 +36,10 @@ const std::vector& PointExtractor::extract_points(cv::Mat& frame) const double region_size_min = s.min_point_size; const double region_size_max = s.max_point_size; - struct blob - { - double radius; - cv::Vec2d pos; - double confid; - blob(double radius, const cv::Vec2d& pos, double confid) : radius(radius), pos(pos), confid(confid) - { - //qDebug() << "radius" << radius << "pos" << pos[0] << pos[1] << "confid" << confid; - } - }; - - // mask for everything that passes the threshold (or: the upper threshold of the hysteresis) - - std::vector blobs; - std::vector> contours; - const int thres = s.threshold; + + contours.clear(); + if (!s.auto_threshold) { cv::threshold(frame_gray, frame_bin, thres, 255, cv::THRESH_BINARY); @@ -88,13 +77,10 @@ const std::vector& PointExtractor::extract_points(cv::Mat& frame) cv::findContours(frame_bin, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE); } - int cnt = 0; + blobs.clear(); for (auto& c : contours) { - if (cnt++ > 30) - break; - const auto m = cv::moments(cv::Mat(c)); const cv::Vec2d pos(m.m10 / m.m00, m.m01 / m.m00); @@ -139,8 +125,6 @@ const std::vector& PointExtractor::extract_points(cv::Mat& frame) blobs.push_back(blob(radius, pos, confid)); - enum { max_blobs = 16 }; - if (blobs.size() == max_blobs) break; } @@ -149,7 +133,6 @@ const std::vector& PointExtractor::extract_points(cv::Mat& frame) std::sort(blobs.begin(), blobs.end(), [](b& b1, b& b2) {return b1.confid > b2.confid;}); QMutexLocker l(&mtx); - points.reserve(blobs.size()); points.clear(); for (auto& b : blobs) diff --git a/tracker-pt/point_extractor.h b/tracker-pt/point_extractor.h index ab51762d..3e4661f9 100644 --- a/tracker-pt/point_extractor.h +++ b/tracker-pt/point_extractor.h @@ -12,9 +12,10 @@ #include #include "ftnoir_tracker_pt_settings.h" - #include +#include + class PointExtractor { public: @@ -33,6 +34,22 @@ private: cv::Mat frame_gray; cv::Mat frame_bin; cv::Mat hist; + + enum { max_blobs = 16 }; + + struct blob + { + double radius; + cv::Vec2d pos; + double confid; + blob(double radius, const cv::Vec2d& pos, double confid) : radius(radius), pos(pos), confid(confid) + { + //qDebug() << "radius" << radius << "pos" << pos[0] << pos[1] << "confid" << confid; + } + }; + + std::vector blobs; + std::vector> contours; }; #endif //POINTEXTRACTOR_H -- cgit v1.2.3