Skip to content

Commit

Permalink
Improve histogram mask, compute only on bb if asked
Browse files Browse the repository at this point in the history
  • Loading branch information
SamFlt committed Oct 31, 2024
1 parent d15369d commit 5f27e95
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 15 deletions.
2 changes: 2 additions & 0 deletions modules/tracker/rbt/include/visp3/rbt/vpColorHistogram.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ class VISP_EXPORT vpColorHistogram
void merge(const vpColorHistogram &other, float alpha);

void computeProbas(const vpImage<vpRGBa> &image, vpImage<float> &proba) const;
void computeProbas(const vpImage<vpRGBa> &image, vpImage<float> &proba, const vpRect &bb) const;


inline unsigned int colorToIndex(const vpRGBa &p) const
{
Expand Down
8 changes: 8 additions & 0 deletions modules/tracker/rbt/include/visp3/rbt/vpColorHistogramMask.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
/**
* @}
*/
Expand All @@ -116,6 +122,8 @@ class VISP_EXPORT vpColorHistogramMask : public vpObjectMask

vpImage<bool> m_mask;
vpImage<float> m_probaObject, m_probaBackground;

bool m_computeOnBBOnly;
};

END_VISP_NAMESPACE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<unsigned int>(ii) >= frame.I.getHeight() - 4 || static_cast<unsigned int>(jj) >= frame.I.getWidth() - 4) {
if (ii <= m_ccdParameters.h || jj <= m_ccdParameters.h || static_cast<unsigned int>(ii) >= frame.I.getHeight() - m_ccdParameters.h || static_cast<unsigned int>(jj) >= frame.I.getWidth() - m_ccdParameters.h) {
continue;
}
vpRBSilhouetteControlPoint pccd;
Expand Down
25 changes: 22 additions & 3 deletions modules/tracker/rbt/src/mask/vpColorHistogram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,25 @@ void vpColorHistogram::computeProbas(const vpImage<vpRGBa> &image, vpImage<float
}
}

void vpColorHistogram::computeProbas(const vpImage<vpRGBa> &image, vpImage<float> &proba, const vpRect &bb) const
{
proba.resize(image.getHeight(), image.getWidth(), 0.f);
const int h = static_cast<int>(image.getHeight()), w = static_cast<int>(image.getWidth());
const int top = static_cast<int>(bb.getTop());
const int left = static_cast<int>(bb.getLeft());
const int bottom = std::min(h- 1, static_cast<int>(bb.getBottom()));
const int right = std::min(w - 1, static_cast<int>(bb.getRight()));
#pragma omp parallel for
for (unsigned int i = top; i <= static_cast<unsigned int>(bottom); ++i) {
const vpRGBa *colorRow = image[i];
float *probaRow = proba[i];
for (unsigned int j = left; j <= static_cast<unsigned int>(right); ++j) {
probaRow[j] = m_probas[colorToIndex(colorRow[j])];
}
}
}



double vpColorHistogram::kl(const vpColorHistogram &other) const
{
Expand Down Expand Up @@ -175,16 +194,16 @@ void vpColorHistogram::computeSplitHistograms(const vpImage<vpRGBa> &image, cons

std::vector<unsigned int> countsIn(bins, 0), countsOut(bins, 0);

#pragma omp parallel
//#pragma omp parallel
{
std::vector<unsigned int>localCountsIn(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];
Expand Down
49 changes: 38 additions & 11 deletions modules/tracker/rbt/src/mask/vpColorHistogramMask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

BEGIN_VISP_NAMESPACE

vpColorHistogramMask::vpColorHistogramMask() { }
vpColorHistogramMask::vpColorHistogramMask() : m_computeOnBBOnly(false) { }

void vpColorHistogramMask::updateMask(const vpRBFeatureTrackerInput &frame,
const vpRBFeatureTrackerInput &previousFrame,
Expand Down Expand Up @@ -74,33 +74,59 @@ void vpColorHistogramMask::updateMask(const vpRBFeatureTrackerInput &frame,
}
vpColorHistogram::computeSplitHistograms(rgb, m_mask, renderBB, m_histObjectFrame, m_histBackgroundFrame);



const float pObject = static_cast<float>(m_histObjectFrame.getNumPixels()) / static_cast<float>(m_mask.getSize());
const float pBackground = 1.f - pObject;
{
{
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<unsigned int>(bottom); ++i) {
for (unsigned int j = left; j <= static_cast<unsigned int>(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)));
}

}
Expand All @@ -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

Expand Down

0 comments on commit 5f27e95

Please sign in to comment.