Skip to content

Commit

Permalink
Add guards for openmp usage, add guards for vpRBKltTracker
Browse files Browse the repository at this point in the history
  • Loading branch information
SamFlt committed Sep 18, 2024
1 parent b15541c commit 6affb4b
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 22 deletions.
10 changes: 8 additions & 2 deletions modules/tracker/rbt/include/visp3/rbt/vpRBKltTracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@
#ifndef VP_RB_KLT_TRACKER_H
#define VP_RB_KLT_TRACKER_H

#include <visp3/core/vpConfig.h>

#if (defined(VISP_HAVE_MODULE_KLT) && defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO))
#define VP_HAVE_RB_KLT_TRACKER
#endif

#if defined(VP_HAVE_RB_KLT_TRACKER)
#include <visp3/rbt/vpRBFeatureTracker.h>
#include <visp3/core/vpPoint.h>
#include <visp3/core/vpTrackingException.h>
Expand All @@ -45,8 +52,6 @@

#include <opencv2/core/mat.hpp>



class VISP_EXPORT vpRBKltTracker : public vpRBFeatureTracker
{
public:
Expand Down Expand Up @@ -195,3 +200,4 @@ class VISP_EXPORT vpRBKltTracker : public vpRBFeatureTracker

};
#endif
#endif
32 changes: 31 additions & 1 deletion modules/tracker/rbt/src/core/vpRBSilhouetteControlPoint.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,34 @@

/****************************************************************************
*
* ViSP, open source Visual Servoing Platform software.
* Copyright (C) 2005 - 2024 by Inria. All rights reserved.
*
* This software is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* See the file LICENSE.txt at the root directory of this source
* distribution for additional information about the GNU GPL.
*
* For using ViSP with software that can not be combined with the GNU
* GPL, please contact Inria about acquiring a ViSP Professional
* Edition License.
*
* See https://visp.inria.fr for more information.
*
* This software was developed at:
* Inria Rennes - Bretagne Atlantique
* Campus Universitaire de Beaulieu
* 35042 Rennes Cedex
* France
*
* If you have questions regarding the use of this file, please contact
* Inria at visp@inria.fr
*
* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
* WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
*****************************************************************************/

#include <visp3/rbt/vpRBSilhouetteControlPoint.h>
#include <visp3/core/vpTrackingException.h>
Expand Down
9 changes: 8 additions & 1 deletion modules/tracker/rbt/src/core/vpRBTracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,13 +361,20 @@ void vpRBTracker::updateRender(vpRBFeatureTrackerInput &frame)
frame.renders.boundingBox = m_renderer.getBoundingBox();

// Extract data from Panda textures
#pragma omp sections
#ifdef VISP_HAVE_OPENMP
#pragma omp parallel sections
#endif

{
#ifdef VISP_HAVE_OPENMP
#pragma omp section
#endif
{
m_renderer.getRenderer<vpPanda3DGeometryRenderer>()->getRender(frame.renders.normals, frame.renders.depth, frame.renders.boundingBox, m_imageHeight, m_imageWidth);
}
#ifdef VISP_HAVE_OPENMP
#pragma omp section
#endif
{
m_renderer.getRenderer<vpPanda3DDepthCannyFilter>()->getRender(frame.renders.silhouetteCanny, frame.renders.isSilhouette, frame.renders.boundingBox, m_imageHeight, m_imageWidth);
// m_renderer.placeRenderInto(m_tempRenders.renders.silhouetteCanny, frame.renders.silhouetteCanny, vpRGBf(0.f));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ void vpRBProbabilistic3DDriftDetector::update(const vpRBFeatureTrackerInput &pre
if (m_points.size() > 0) {

// Step 0: project all points
#ifdef VISP_HAVE_OPENMP
#pragma omp parallel for
#endif
for (vpStored3DSurfaceColorPoint &p : m_points) {
p.update(cTo, cprevTo, frame.cam);
}
Expand Down
2 changes: 2 additions & 0 deletions modules/tracker/rbt/src/features/vpRBDenseDepthTracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ void vpRBDenseDepthTracker::computeVVSIter(const vpRBFeatureTrackerInput &frame,
}
double t1 = vpTime::measureTimeMs();
vpRotationMatrix cRo = cMo.getRotationMatrix();
#ifdef VISP_HAVE_OPENMP
#pragma omp parallel for
#endif
for (unsigned int i = 0; i < m_depthPoints.size(); ++i) {
vpDepthPoint &depthPoint = m_depthPoints[i];
depthPoint.update(cMo, cRo);
Expand Down
10 changes: 6 additions & 4 deletions modules/tracker/rbt/src/features/vpRBFeatureTrackerFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,16 @@ vpRBFeatureTrackerFactory::vpRBFeatureTrackerFactory()
p->loadJsonConfiguration(j);
return p;
});
registerType("klt", [](const nlohmann::json &j) {
std::shared_ptr<vpRBKltTracker> p(new vpRBKltTracker());
registerType("depth", [](const nlohmann::json &j) {
std::shared_ptr<vpRBDenseDepthTracker> p(new vpRBDenseDepthTracker());
p->loadJsonConfiguration(j);
return p;
});
registerType("depth", [](const nlohmann::json &j) {
std::shared_ptr<vpRBDenseDepthTracker> p(new vpRBDenseDepthTracker());
#if defined(VP_HAVE_RB_KLT_TRACKER)
registerType("klt", [](const nlohmann::json &j) {
std::shared_ptr<vpRBKltTracker> p(new vpRBKltTracker());
p->loadJsonConfiguration(j);
return p;
});
#endif
}
5 changes: 5 additions & 0 deletions modules/tracker/rbt/src/features/vpRBKltTracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
*****************************************************************************/

#include <visp3/rbt/vpRBKltTracker.h>

#if defined(VP_HAVE_RB_KLT_TRACKER)

#include <visp3/core/vpImageConvert.h>
#include <visp3/core/vpPixelMeterConversion.h>
#include <visp3/core/vpMeterPixelConversion.h>
Expand Down Expand Up @@ -286,3 +289,5 @@ void vpRBKltTracker::display(const vpCameraParameters &cam, const vpImage<unsign
vpDisplay::displayPoint(I, v, u, vpColor::red, 2);
}
}

#endif
31 changes: 19 additions & 12 deletions modules/tracker/rbt/src/features/vpRBSilhouetteCCDTracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@
*****************************************************************************/

#include <visp3/rbt/vpRBSilhouetteCCDTracker.h>

#ifdef VISP_HAVE_OPENMP
#include <omp.h>
#endif

#define VISP_DEBUG_CCD_TRACKER 0

Expand Down Expand Up @@ -243,7 +246,9 @@ void vpRBSilhouetteCCDTracker::display(const vpCameraParameters &cam, const vpIm

void vpRBSilhouetteCCDTracker::updateCCDPoints(const vpHomogeneousMatrix &cMo)
{
#ifdef VISP_HAVE_OPENMP
#pragma omp parallel for
#endif
for (vpRBSilhouetteControlPoint &p : m_controlPoints) {
p.updateSilhouettePoint(cMo);
}
Expand All @@ -270,7 +275,9 @@ void vpRBSilhouetteCCDTracker::computeLocalStatistics(const vpImage<vpRGBa> &I,
// the second column store the one inside the curve
vpMatrix normalized_param = vpMatrix(resolution, 2, 0.0);

#ifdef VISP_HAVE_OPENMP
#pragma omp parallel for
#endif
for (unsigned int kk = 0; kk < m_controlPoints.size(); kk++) {
// temporary points used to store those points in the
// normal direction as well as negative normal direction
Expand Down Expand Up @@ -376,7 +383,7 @@ void vpRBSilhouetteCCDTracker::computeLocalStatistics(const vpImage<vpRGBa> &I,
normalized_param[kk][1] += vic_ptr[10 * negative_normal + 7];
}

}
}

#pragma omp parallel for
for (unsigned int i = 0; i < resolution; ++i) {
Expand Down Expand Up @@ -492,7 +499,7 @@ void vpRBSilhouetteCCDTracker::computeLocalStatistics(const vpImage<vpRGBa> &I,
}

}
}
}

void vpRBSilhouetteCCDTracker::computeErrorAndInteractionMatrix()
{
Expand All @@ -503,7 +510,9 @@ void vpRBSilhouetteCCDTracker::computeErrorAndInteractionMatrix()
m_weighted_error.resize(nerror_ccd, false);
m_L.resize(nerror_ccd, 6, false, false);
double beforeParallel = vpTime::measureTimeMs();
#ifdef VISP_HAVE_OPENMP
#pragma omp parallel
#endif
{

// vpMatrix tmp_cov(3, 3);
Expand All @@ -515,7 +524,9 @@ void vpRBSilhouetteCCDTracker::computeErrorAndInteractionMatrix()
double Lnvp[6];
unsigned int normal_points_number = static_cast<unsigned int>(floor(m_ccdParameters.h / m_ccdParameters.delta_h));

#ifdef VISP_HAVE_OPENMP
#pragma omp for
#endif
for (unsigned int kk = 0; kk < m_controlPoints.size(); kk++) {
const int i = kk;
const vpRBSilhouetteControlPoint &p = m_controlPoints[kk];
Expand Down Expand Up @@ -610,17 +621,22 @@ void vpRBSilhouetteCCDTracker::computeErrorAndInteractionMatrix()

std::vector<vpColVector> localGradients; // Store all the gradients and hessians and then sum them up after the parallel region. This ensures that computation is determinist
std::vector<vpMatrix> localHessians;
#ifdef VISP_HAVE_OPENMP
#pragma omp parallel
#endif
{
vpColVector localGradient(nabla_E.getRows(), 0.0);
vpMatrix localHessian(hessian_E.getRows(), hessian_E.getCols(), 0.0);
#ifdef VISP_HAVE_OPENMP
#pragma omp single
#endif
{
localGradients.resize(omp_get_num_threads(), localGradient);
localHessians.resize(omp_get_num_threads(), localHessian);
}

#ifdef VISP_HAVE_OPENMP
#pragma omp for schedule(static)
#endif
for (unsigned int i = 0; i < m_gradients.size(); ++i) {
m_gradients[i] *= m_weights[i];
m_hessians[i] *= m_weights[i];
Expand All @@ -635,22 +651,13 @@ void vpRBSilhouetteCCDTracker::computeErrorAndInteractionMatrix()
hessian_E += localHessians[i];
}
double afterWeight = vpTime::measureTimeMs();
//std::cout << "Weighting and sum took " << afterWeight - beforeWeightAndSum << std::endl;

//sigmaF = 0.2*sigmaF + 0.8*computeCovarianceMatrix(m_L,v,error_ccd);
//std::cout << " sigmaF " << sigmaF << std::endl;

m_LTL = hessian_E;
m_LTR = -nabla_E;

// m_LTL = m_L.AtA();
// std::cout << m_LTL - hessian_E << std::endl;
// computeJTR(m_L, -m_weighted_error, m_LTR);

vpMatrix hessian_E_inv = hessian_E.inverseByCholesky();
//Sigma_Phi = /*Sigma_Phi +*/ 2*hessian_E_inv;
Sigma_Phi = m_ccdParameters.covarianceIterDecreaseFactor * Sigma_Phi + 2 * (1 - m_ccdParameters.covarianceIterDecreaseFactor) * hessian_E_inv;

m_cov = Sigma_Phi;
//std::cout << "Rest took: " << vpTime::measureTimeMs() - afterWeight << std::endl;
}
6 changes: 6 additions & 0 deletions tutorial/tracking/render-based/render-based-tutorial-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ class vpRBExperimentLogger
vpDisplay::getImage(IRGB, IColOverlay);
vpDisplay::getImage(Idepth, IdepthOverlay);
vpDisplay::getImage(Imask, ImaskOverlay);
#ifdef VISP_HAVE_OPENMP
#pragma omp parallel for
#endif
for (unsigned int i = 0; i < IRGB.getHeight(); ++i) {
memcpy(Iout[i], IgrayOverlay[i], IRGB.getWidth() * sizeof(vpRGBa));
memcpy(Iout[i] + IRGB.getWidth(), IColOverlay[i], IRGB.getWidth() * sizeof(vpRGBa));
Expand Down Expand Up @@ -342,7 +344,9 @@ void enableRendererProfiling()

void displayNormals(const vpImage<vpRGBf> &normalsImage, vpImage<vpRGBa> &normalDisplayImage)
{
#ifdef VISP_HAVE_OPENMP
#pragma omp parallel for
#endif
for (int i = 0; i < normalsImage.getSize(); ++i) {
normalDisplayImage.bitmap[i].R = static_cast<unsigned char>((normalsImage.bitmap[i].R + 1.0) * 127.5f);
normalDisplayImage.bitmap[i].G = static_cast<unsigned char>((normalsImage.bitmap[i].G + 1.0) * 127.5f);
Expand All @@ -356,7 +360,9 @@ void displayNormals(const vpImage<vpRGBf> &normalsImage, vpImage<vpRGBa> &normal
void displayCanny(const vpImage<vpRGBf> &cannyRawData,
vpImage<unsigned char> &canny, const vpImage<unsigned char> &valid)
{
#ifdef VISP_HAVE_OPENMP
#pragma omp parallel for
#endif
for (int i = 0; i < cannyRawData.getSize(); ++i) {
vpRGBf &px = cannyRawData.bitmap[i];
canny.bitmap[i] = valid.bitmap[i] * 255;
Expand Down
4 changes: 3 additions & 1 deletion tutorial/tracking/render-based/tutorial-rbt-realsense.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ struct CmdArguments
void updateDepth(const vpImage<uint16_t> &depthRaw, float depthScale, float maxZDisplay, vpImage<float> &depth, vpImage<unsigned char> &IdepthDisplay)
{
depth.resize(depthRaw.getHeight(), depthRaw.getWidth());
#ifdef VISP_HAVE_OPENMP
#pragma omp parallel for
#endif
for (unsigned int i = 0; i < depthRaw.getSize(); ++i) {
depth.bitmap[i] = depthScale * static_cast<float>(depthRaw.bitmap[i]);
IdepthDisplay.bitmap[i] = depth.bitmap[i] > maxZDisplay ? 0 : static_cast<unsigned int>((depth.bitmap[i] / maxZDisplay) * 255.f);
Expand All @@ -53,7 +55,7 @@ int main(int argc, const char **argv)
vpRBTrackerTutorial::vpRBExperimentPlotter plotter;

vpJsonArgumentParser parser(
"Tutorial showing the usage of the Render-Based tracker with a realsense camera",
"Tutorial showing the usage of the Render-Based tracker with a RealSense camera",
"--config", "/"
);

Expand Down
4 changes: 3 additions & 1 deletion tutorial/tracking/render-based/tutorial-rbt-sequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ int main(int argc, const char **argv)
float scale = 9.999999747378752e-05;
depth.resize(dataArray.getHeight(), dataArray.getWidth());
depthDisplay.resize(dataArray.getHeight(), dataArray.getWidth());
#pragma omp simd
#ifdef VISP_HAVE_OPENMP
#pragma omp parallel for
#endif
for (unsigned int i = 0; i < dataArray.getSize(); ++i) {
float value = static_cast<float>(dataArray.bitmap[i]) * scale;
depth.bitmap[i] = value;
Expand Down

0 comments on commit 6affb4b

Please sign in to comment.