Skip to content

Commit

Permalink
AM-78 Add depth of BFS search
Browse files Browse the repository at this point in the history
Add proper initialisation of ThreadPool. Previously it was totally HW
dependent parameter in code. Added initilisation based on configuration multiplier provided.
  • Loading branch information
ben committed Oct 29, 2024
1 parent 9d6e8ee commit 780ee07
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion analyze/algorithm/DiffObjectDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
21 changes: 14 additions & 7 deletions analyze/algorithm/ObjectDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double> calcDuration = timeNow - startTime;
if (calcDuration.count() >= conf.CalculationTimeLimit)
Expand All @@ -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) >
Expand All @@ -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;
}
Expand Down Expand Up @@ -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));
}
}
}
Expand All @@ -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};
Expand Down
4 changes: 2 additions & 2 deletions analyze/algorithm/ObjectDetector.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ namespace am::analyze::algorithm
{

ObjectRectangle bfs(const ImagePair &pair, common::types::MatrixU16 &visited,
std::vector<Pixel> &toCheck, ObjectRectangle &object,
const std::vector<Pixel> &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
{
Expand Down
2 changes: 1 addition & 1 deletion analyze/algorithm/movement/MovementDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down

0 comments on commit 780ee07

Please sign in to comment.