From 5f27e958759c3127980d1f8594fe9f2b591d804f Mon Sep 17 00:00:00 2001 From: Samuel Felton Date: Thu, 31 Oct 2024 16:59:41 +0100 Subject: [PATCH] Improve histogram mask, compute only on bb if asked --- .../rbt/include/visp3/rbt/vpColorHistogram.h | 2 + .../include/visp3/rbt/vpColorHistogramMask.h | 8 +++ .../src/features/vpRBSilhouetteCCDTracker.cpp | 2 +- .../tracker/rbt/src/mask/vpColorHistogram.cpp | 25 ++++++++-- .../rbt/src/mask/vpColorHistogramMask.cpp | 49 ++++++++++++++----- 5 files changed, 71 insertions(+), 15 deletions(-) diff --git a/modules/tracker/rbt/include/visp3/rbt/vpColorHistogram.h b/modules/tracker/rbt/include/visp3/rbt/vpColorHistogram.h index d2e1bc9668..a390b5ad0d 100644 --- a/modules/tracker/rbt/include/visp3/rbt/vpColorHistogram.h +++ b/modules/tracker/rbt/include/visp3/rbt/vpColorHistogram.h @@ -89,6 +89,8 @@ class VISP_EXPORT vpColorHistogram void merge(const vpColorHistogram &other, float alpha); void computeProbas(const vpImage &image, vpImage &proba) const; + void computeProbas(const vpImage &image, vpImage &proba, const vpRect &bb) const; + inline unsigned int colorToIndex(const vpRGBa &p) const { diff --git a/modules/tracker/rbt/include/visp3/rbt/vpColorHistogramMask.h b/modules/tracker/rbt/include/visp3/rbt/vpColorHistogramMask.h index d431f393b4..6240c7c74b 100644 --- a/modules/tracker/rbt/include/visp3/rbt/vpColorHistogramMask.h +++ b/modules/tracker/rbt/include/visp3/rbt/vpColorHistogramMask.h @@ -101,6 +101,12 @@ class VISP_EXPORT vpColorHistogramMask : public vpObjectMask } m_backgroundUpdateRate = updateRate; } + + bool isComputedOnlyOnBoundingBox() const { return m_computeOnBBOnly; } + void setComputeOnlyOnBoundingBox(bool bbOnly) + { + m_computeOnBBOnly = bbOnly; + } /** * @} */ @@ -116,6 +122,8 @@ class VISP_EXPORT vpColorHistogramMask : public vpObjectMask vpImage m_mask; vpImage m_probaObject, m_probaBackground; + + bool m_computeOnBBOnly; }; END_VISP_NAMESPACE diff --git a/modules/tracker/rbt/src/features/vpRBSilhouetteCCDTracker.cpp b/modules/tracker/rbt/src/features/vpRBSilhouetteCCDTracker.cpp index 457792efd0..4b56e4eed1 100644 --- a/modules/tracker/rbt/src/features/vpRBSilhouetteCCDTracker.cpp +++ b/modules/tracker/rbt/src/features/vpRBSilhouetteCCDTracker.cpp @@ -93,7 +93,7 @@ void vpRBSilhouetteCCDTracker::extractFeatures(const vpRBFeatureTrackerInput &fr // std::cout << sp.j << ", " << sp.i << std::endl; int ii = sp.i, jj = sp.j; - if (ii <= 4 || jj <= 4 || static_cast(ii) >= frame.I.getHeight() - 4 || static_cast(jj) >= frame.I.getWidth() - 4) { + if (ii <= m_ccdParameters.h || jj <= m_ccdParameters.h || static_cast(ii) >= frame.I.getHeight() - m_ccdParameters.h || static_cast(jj) >= frame.I.getWidth() - m_ccdParameters.h) { continue; } vpRBSilhouetteControlPoint pccd; diff --git a/modules/tracker/rbt/src/mask/vpColorHistogram.cpp b/modules/tracker/rbt/src/mask/vpColorHistogram.cpp index db1b975647..2fe9294451 100644 --- a/modules/tracker/rbt/src/mask/vpColorHistogram.cpp +++ b/modules/tracker/rbt/src/mask/vpColorHistogram.cpp @@ -128,6 +128,25 @@ void vpColorHistogram::computeProbas(const vpImage &image, vpImage &image, vpImage &proba, const vpRect &bb) const +{ + proba.resize(image.getHeight(), image.getWidth(), 0.f); + const int h = static_cast(image.getHeight()), w = static_cast(image.getWidth()); + const int top = static_cast(bb.getTop()); + const int left = static_cast(bb.getLeft()); + const int bottom = std::min(h- 1, static_cast(bb.getBottom())); + const int right = std::min(w - 1, static_cast(bb.getRight())); +#pragma omp parallel for + for (unsigned int i = top; i <= static_cast(bottom); ++i) { + const vpRGBa *colorRow = image[i]; + float *probaRow = proba[i]; + for (unsigned int j = left; j <= static_cast(right); ++j) { + probaRow[j] = m_probas[colorToIndex(colorRow[j])]; + } + } +} + + double vpColorHistogram::kl(const vpColorHistogram &other) const { @@ -175,16 +194,16 @@ void vpColorHistogram::computeSplitHistograms(const vpImage &image, cons std::vector countsIn(bins, 0), countsOut(bins, 0); -#pragma omp parallel +//#pragma omp parallel { std::vectorlocalCountsIn(bins, 0), localCountsOut(bins, 0); -#pragma omp for schedule(static, 1024) +//#pragma omp for schedule(static, 1024) for (unsigned int i = 0; i < image.getSize(); ++i) { unsigned int index = insideMask.colorToIndex(image.bitmap[i]); localCountsIn[index] += mask.bitmap[i] > 0; localCountsOut[index] += mask.bitmap[i] == 0; } -#pragma omp critical +//#pragma omp critical { for (unsigned int i = 0; i < bins; ++i) { countsIn[i] += localCountsIn[i]; diff --git a/modules/tracker/rbt/src/mask/vpColorHistogramMask.cpp b/modules/tracker/rbt/src/mask/vpColorHistogramMask.cpp index c83aa00acb..3b131de267 100644 --- a/modules/tracker/rbt/src/mask/vpColorHistogramMask.cpp +++ b/modules/tracker/rbt/src/mask/vpColorHistogramMask.cpp @@ -38,7 +38,7 @@ BEGIN_VISP_NAMESPACE -vpColorHistogramMask::vpColorHistogramMask() { } +vpColorHistogramMask::vpColorHistogramMask() : m_computeOnBBOnly(false) { } void vpColorHistogramMask::updateMask(const vpRBFeatureTrackerInput &frame, const vpRBFeatureTrackerInput &previousFrame, @@ -74,8 +74,6 @@ void vpColorHistogramMask::updateMask(const vpRBFeatureTrackerInput &frame, } vpColorHistogram::computeSplitHistograms(rgb, m_mask, renderBB, m_histObjectFrame, m_histBackgroundFrame); - - const float pObject = static_cast(m_histObjectFrame.getNumPixels()) / static_cast(m_mask.getSize()); const float pBackground = 1.f - pObject; { @@ -83,24 +81,52 @@ void vpColorHistogramMask::updateMask(const vpRBFeatureTrackerInput &frame, if (pObject != 0.f) { m_histObject.merge(m_histObjectFrame, m_objectUpdateRate); } - // m_histObject.computeProbas(frame.IRGB, m_probaObject); + if (m_computeOnBBOnly) { + m_histObject.computeProbas(frame.IRGB, m_probaObject, frame.renders.boundingBox); + } + else { + m_histObject.computeProbas(frame.IRGB, m_probaObject); + } } { if (pBackground != 0.f) { m_histBackground.merge(m_histBackgroundFrame, m_backgroundUpdateRate); } - // m_histBackground.computeProbas(frame.IRGB, m_probaBackground); + if (m_computeOnBBOnly) { + m_histBackground.computeProbas(frame.IRGB, m_probaBackground, frame.renders.boundingBox); + } + else { + m_histBackground.computeProbas(frame.IRGB, m_probaBackground); + } } } - mask.resize(height, width); + if (m_computeOnBBOnly) { + mask.resize(height, width, 0.f); #pragma omp parallel for - for (unsigned int i = 0; i < mask.getSize(); ++i) { - float poPix = m_histObject.probability(frame.IRGB.bitmap[i]); - float pbPix = m_histBackground.probability(frame.IRGB.bitmap[i]); + for (unsigned int i = top; i <= static_cast(bottom); ++i) { + for (unsigned int j = left; j <= static_cast(right); ++j) { + const float poPix = m_probaObject[i][j]; + const float pbPix = m_probaBackground[i][j]; + + float denom = (pObject * poPix + pBackground * pbPix); + mask[i][j] = (denom > 0.f) * std::max(0.f, std::min(1.f, (poPix / denom))); + m_mask[i][j] = renderDepth[i][j] > 0.f && fabs(renderDepth[i][j] - depth[i][j]) <= m_depthErrorTolerance; + } + } + } + else { + mask.resize(height, width); + for (unsigned int i = 0; i < mask.getSize(); ++i) { + // float poPix = m_histObject.probability(frame.IRGB.bitmap[i]); + // float pbPix = m_histBackground.probability(frame.IRGB.bitmap[i]); + const float poPix = m_probaObject.bitmap[i]; + const float pbPix = m_probaBackground.bitmap[i]; + + float denom = (pObject * poPix + pBackground * pbPix); + mask.bitmap[i] = (denom > 0.f) * std::max(0.f, std::min(1.f, (poPix / denom))); + } - float denom = (pObject * poPix + pBackground * pbPix); - mask.bitmap[i] = (denom > 0.f) * std::max(0.f, std::min(1.f, (poPix / denom))); } } @@ -112,6 +138,7 @@ void vpColorHistogramMask::loadJsonConfiguration(const nlohmann::json &json) m_backgroundUpdateRate = json.at("backgroundUpdateRate"); m_objectUpdateRate = json.at("objectUpdateRate"); m_depthErrorTolerance = json.at("maxDepthError"); + m_computeOnBBOnly = json.value("computeOnlyOnBoundingBox", m_computeOnBBOnly); } #endif