From 780ee074e0c51c497331511e35bed7ba51b8e156 Mon Sep 17 00:00:00 2001 From: ben Date: Tue, 29 Oct 2024 17:39:43 +0200 Subject: [PATCH] AM-78 Add depth of BFS search Add proper initialisation of ThreadPool. Previously it was totally HW dependent parameter in code. Added initilisation based on configuration multiplier provided. --- analyze/algorithm/DiffObjectDetector.cpp | 2 +- analyze/algorithm/ObjectDetector.cpp | 21 ++++++++++++------- analyze/algorithm/ObjectDetector.h | 4 ++-- .../algorithm/movement/MovementDetector.cpp | 2 +- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/analyze/algorithm/DiffObjectDetector.cpp b/analyze/algorithm/DiffObjectDetector.cpp index 2035005..7420217 100644 --- a/analyze/algorithm/DiffObjectDetector.cpp +++ b/analyze/algorithm/DiffObjectDetector.cpp @@ -14,7 +14,7 @@ namespace am::analyze::algorithm { // optimized bfs depending to left/right borders for threads, // every thread will search in defined area(column) of image - ObjectRectangle bfs(MatrixU16 &changes, Pixels &toCheck, ObjectRectangle &object, ImageRowSegment row) + ObjectRectangle bfs(MatrixU16 &changes, const Pixels &toCheck, ObjectRectangle &object, ImageRowSegment row) { Pixels nextCheck; diff --git a/analyze/algorithm/ObjectDetector.cpp b/analyze/algorithm/ObjectDetector.cpp index 16f5f0c..6425924 100644 --- a/analyze/algorithm/ObjectDetector.cpp +++ b/analyze/algorithm/ObjectDetector.cpp @@ -18,11 +18,17 @@ namespace am::analyze::algorithm // Time dependent execution, if max calculation time exceeded calculation should // finalize processing, and show up collected objects(if found). - ObjectRectangle bfs(const ImagePair &pair, MatrixU16 &visited, Pixels &toCheck, + ObjectRectangle bfs(const ImagePair &pair, MatrixU16 &visited, const Pixels &toCheck, ObjectRectangle &object, ImageRowSegment row, std::chrono::steady_clock::time_point &startTime, - const Configuration &conf) + const Configuration &conf, size_t depth) { + if(depth == 120 * 2) { + // limit depth of search, low step value can affect stack consuming on images with noise. + // depth can be modified or be configurable depending on HW. + printf("Depth limit exceeded max_depth = %zd.\n", depth); + return object; + } auto timeNow = std::chrono::steady_clock::now(); std::chrono::duration calcDuration = timeNow - startTime; if (calcDuration.count() >= conf.CalculationTimeLimit) @@ -34,7 +40,7 @@ namespace am::analyze::algorithm } Pixels nextCheck; - for (auto &position : toCheck) + for (const auto &position : toCheck) { if (visited(position.rowId, position.colId) != common::CHANGE && pair.getAbsoluteDiff(position.rowId, position.colId) > @@ -47,8 +53,9 @@ namespace am::analyze::algorithm object.addPixel(position.rowId, position.colId); } } - if (nextCheck.size()) - bfs(pair, visited, nextCheck, object, row, startTime, conf); + if (nextCheck.size()){ + bfs(pair, visited, nextCheck, object, row, startTime, conf, depth +1); + } return object; } @@ -83,7 +90,7 @@ namespace am::analyze::algorithm ObjectRectangle obj(rowId, colId); auto conns = checkConnections(rowId, colId, pair.getWidth(), row, conf.PixelStep); - resultList.emplace_back(bfs(pair, changes, conns, obj, row, startTime, conf)); + resultList.emplace_back(bfs(pair, changes, conns, obj, row, startTime, conf, 1)); } } } @@ -98,7 +105,7 @@ namespace am::analyze::algorithm mLogger->info("ObjectDetector::getObjectsRects pair threads:%d", mThreadsCount); // threadpool could be replaced with std::async calls - am::common::ThreadPool pool; + am::common::ThreadPool pool(mThreadsCount); for (size_t rowId = 0; rowId < mThreadsCount - 1; ++rowId) { ImageRowSegment row{rowId * rowHeight, rowId * rowHeight + rowHeight}; diff --git a/analyze/algorithm/ObjectDetector.h b/analyze/algorithm/ObjectDetector.h index 0b0391e..a05c939 100644 --- a/analyze/algorithm/ObjectDetector.h +++ b/analyze/algorithm/ObjectDetector.h @@ -7,9 +7,9 @@ namespace am::analyze::algorithm { ObjectRectangle bfs(const ImagePair &pair, common::types::MatrixU16 &visited, - std::vector &toCheck, ObjectRectangle &object, + const std::vector &toCheck, ObjectRectangle &object, ImageRowSegment row, std::chrono::steady_clock::time_point &startTime, - const Configuration &conf); + const Configuration &conf, size_t depth); class ObjectDetector : public BfsObjectDetector { diff --git a/analyze/algorithm/movement/MovementDetector.cpp b/analyze/algorithm/movement/MovementDetector.cpp index b9ebdf4..aed9b62 100644 --- a/analyze/algorithm/movement/MovementDetector.cpp +++ b/analyze/algorithm/movement/MovementDetector.cpp @@ -64,7 +64,7 @@ namespace am::analyze::algorithm::movement { ObjectRectangle pxs(rowId, colId); auto conns = checkConnections(rowId, colId, pair->getHeight(), {0u, pair->getWidth()}, mConfiguration.PixelStep); - auto objFound = bfs(*pair, changes, conns, pxs, {0u, pair->getWidth()}, startTime, mConfiguration); + auto objFound = bfs(*pair, changes, conns, pxs, {0u, pair->getWidth()}, startTime, mConfiguration, 1); found.emplace_back(objFound); } }