33#include <visp3/core/vpConfig.h>
34#include <visp3/core/vpException.h>
35#include <visp3/core/vpMath.h>
36#include <visp3/core/vpMouseButton.h>
37#include <visp3/core/vpTime.h>
38#include <visp3/core/vpUniRand.h>
40#ifdef VISP_HAVE_DISPLAY
41#include <visp3/gui/vpPlot.h>
45#include <visp3/core/vpParticleFilter.h>
48#include "vpTutoCommonData.h"
49#include "vpTutoMeanSquareFitting.h"
50#include "vpTutoParabolaModel.h"
51#include "vpTutoSegmentation.h"
53#ifdef ENABLE_VISP_NAMESPACE
57#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11) && defined(VISP_HAVE_DISPLAY)
58#ifndef DOXYGEN_SHOULD_SKIP_THIS
69double evaluate(
const vpImagePoint &pt,
const vpTutoParabolaModel &model)
73 double v_model = model.eval(u);
74 double error =
v - v_model;
89double evaluate(
const vpColVector &coeffs,
const unsigned int &height,
const unsigned int &width,
const std::vector<vpImagePoint> &pts)
91 unsigned int nbPts =
static_cast<unsigned int>(pts.size());
92 vpColVector residuals(nbPts);
93 vpColVector weights(nbPts, 1.);
94 vpTutoParabolaModel model(coeffs, height, width);
96 for (
unsigned int i = 0;
i < nbPts; ++
i) {
97 double squareError = evaluate(pts[i], model);
98 residuals[
i] = squareError;
100 double meanSquareError = residuals.sum() /
static_cast<double>(nbPts);
101 return std::sqrt(meanSquareError);
115void display(
const vpColVector &coeffs,
const vpImage<T> &I,
const vpColor &color,
116 const unsigned int &vertPosLegend,
const unsigned int &horPosLegend)
118#if defined(VISP_HAVE_DISPLAY)
121 for (
unsigned int u = 0;
u <
width; ++
u) {
122 double v = model.eval(u);
144std::vector<vpImagePoint> automaticInitialization(tutorial::vpTutoCommonData &data)
147 const unsigned int minNbPts =
data.m_degree + 1;
148 const unsigned int nbPtsToUse = 10 * minNbPts;
149 std::vector<vpImagePoint> initPoints;
152 tutorial::performSegmentationHSV(data);
155 std::vector<vpImagePoint> edgePoints = tutorial::extractSkeleton(data);
156 unsigned int nbEdgePoints =
static_cast<unsigned int>(edgePoints.size());
158 if (nbEdgePoints < nbPtsToUse) {
163 auto ptHasLowerU = [](
const vpImagePoint &ptA,
const vpImagePoint &ptB) {
164 return ptA.
get_u() < ptB.get_u();
166 std::sort(edgePoints.begin(), edgePoints.end(), ptHasLowerU);
168 unsigned int idStart, idStop;
169 if (nbEdgePoints > nbPtsToUse + 20) {
172 idStop =
static_cast<unsigned int>(edgePoints.size()) - 10;
177 idStop =
static_cast<unsigned int>(edgePoints.size());
181 unsigned int sizeWindow = idStop - idStart + 1;
182 unsigned int step = sizeWindow / (nbPtsToUse - 1);
183 for (
unsigned int id = idStart;
id <= idStop;
id +=
step) {
184 initPoints.push_back(edgePoints[
id]);
195std::vector<vpImagePoint> manualInitialization(
const tutorial::vpTutoCommonData &data)
198 const bool waitForClick =
true;
199 vpImagePoint ipClick;
203 const unsigned int sizeCross = 10;
204 const unsigned int thicknessCross = 2;
208 const unsigned int minNbPts =
data.m_degree + 1;
209 std::vector<vpImagePoint> initPoints;
211 bool notEnoughPoints =
true;
212 while (notEnoughPoints) {
217 vpDisplay::displayText(
data.m_I_orig,
data.m_ipLegend,
"Left click to add init point (min.: " + std::to_string(minNbPts) +
"), right click to estimate the initial coefficients of the Particle Filter.",
data.m_colorLegend);
222 unsigned int nbInitPoints =
static_cast<unsigned int>(initPoints.size());
223 for (
unsigned int i = 0;
i < nbInitPoints; ++
i) {
236 initPoints.push_back(ipClick);
242 (initPoints.size() >= minNbPts ? notEnoughPoints =
false : notEnoughPoints =
true);
260vpColVector computeInitialGuess(tutorial::vpTutoCommonData &data)
263 std::vector<vpImagePoint> initPoints;
265#ifdef VISP_HAVE_DISPLAY
267 const bool waitForClick =
true;
268 vpImagePoint ipClick;
272 const unsigned int sizeCross = 10;
273 const unsigned int thicknessCross = 2;
276 bool automaticInit =
false;
280 vpDisplay::displayText(
data.m_I_orig,
data.m_ipLegend,
"Left click to manually select the init points, right click to automatically initialize the PF",
data.m_colorLegend);
291 automaticInit =
false;
294 automaticInit =
true;
302 initPoints = tutorial::automaticInitialization(data);
306 initPoints = tutorial::manualInitialization(data);
311 initPoints = tutorial::automaticInitialization(data);
315 tutorial::vpTutoMeanSquareFitting lmsFitter(
data.m_degree,
data.m_I_orig.getHeight(),
data.m_I_orig.getWidth());
316 lmsFitter.fit(initPoints);
317 vpColVector
X0 = lmsFitter.getCoeffs();
318 std::cout <<
"---[Initial fit]---" << std::endl;
319 std::cout << lmsFitter.getModel();
320 std::cout <<
"---[Initial fit]---" << std::endl;
325 unsigned int nbInitPoints =
static_cast<unsigned int>(initPoints.size());
326 for (
unsigned int i = 0;
i < nbInitPoints; ++
i) {
327 const vpImagePoint &ip = initPoints[
i];
332 lmsFitter.display(
data.m_I_orig,
vpColor::red,
static_cast<unsigned int>(
data.m_ipLegend.get_v() + 2 *
data.m_legendOffset.get_v()),
static_cast<unsigned int>(
data.m_ipLegend.get_u()));
342vpColVector fx(
const vpColVector &coeffs,
const double &)
344 vpColVector updatedCoeffs = coeffs;
345 return updatedCoeffs;
350class vpTutoAverageFunctor
353 vpTutoAverageFunctor(
const unsigned int °ree,
const unsigned int &height,
const unsigned int &width)
368 vpColVector averagePolynomials(
const std::vector<vpColVector> &particles,
const std::vector<double> &weights,
const vpParticleFilter<std::vector<vpImagePoint>>::vpStateAddFunction &)
370 const unsigned int nbParticles =
static_cast<unsigned int>(particles.size());
371 const double nbParticlesAsDOuble =
static_cast<double>(nbParticles);
373 const double sumWeight = std::accumulate(weights.begin(), weights.end(), 0.);
376 const double nbPointsForAverage = 10. * nbParticlesAsDOuble;
377 std::vector<vpImagePoint> initPoints;
380 for (
unsigned int i = 0;
i < nbParticles; ++
i) {
382 double nbPoints = std::floor(weights[i] * nbPointsForAverage / sumWeight);
385 vpTutoParabolaModel curve(particles[i], m_height, m_width);
386 double widthAsDouble =
static_cast<double>(m_width);
388 double step = widthAsDouble / (nbPoints - 1.);
389 for (
double u = 0.;
u < widthAsDouble;
u +=
step) {
390 double v = curve.eval(u);
391 vpImagePoint pt(v, u);
392 initPoints.push_back(pt);
398 vpTutoParabolaModel curve(particles[i], m_height, m_width);
399 double u =
static_cast<double>(m_width) / 2.;
400 double v = curve.eval(u);
401 vpImagePoint pt(v, u);
402 initPoints.push_back(pt);
406 vpTutoMeanSquareFitting lms(m_degree, m_height, m_width);
408 return lms.getCoeffs();
412 unsigned int m_degree;
413 unsigned int m_height;
414 unsigned int m_width;
419class vpTutoLikelihoodFunctor
429 vpTutoLikelihoodFunctor(
const double &stdev,
const unsigned int &height,
const unsigned int &width)
433 double sigmaDistanceSquared = stdev * stdev;
434 m_constantDenominator = 1. / std::sqrt(2. * M_PI * sigmaDistanceSquared);
435 m_constantExpDenominator = -1. / (2. * sigmaDistanceSquared);
451 double likelihood(
const vpColVector &coeffs,
const std::vector<vpImagePoint> &meas)
453 double likelihood = 0.;
454 unsigned int nbPoints =
static_cast<unsigned int>(meas.size());
457 vpTutoParabolaModel model(coeffs, m_height, m_width);
460 vpColVector residuals(nbPoints);
461 for (
unsigned int i = 0;
i < nbPoints; ++
i) {
462 double squareError = tutorial::evaluate(meas[i], model);
463 residuals[
i] = squareError;
468 vpColVector
w(nbPoints, 1.);
470 double sumError =
w.hadamard(residuals).sum();
473 likelihood = std::exp(m_constantExpDenominator * sumError /
w.sum()) * m_constantDenominator;
474 likelihood = std::min(likelihood, 1.0);
475 likelihood = std::max(likelihood, 0.);
480 double m_constantDenominator;
481 double m_constantExpDenominator;
482 unsigned int m_height;
483 unsigned int m_width;
489int main(
const int argc,
const char *argv[])
491 tutorial::vpTutoCommonData
data;
492 int returnCode =
data.init(argc, argv);
493 if (returnCode != tutorial::vpTutoCommonData::SOFTWARE_CONTINUE) {
496 const unsigned int vertOffset =
static_cast<unsigned int>(
data.m_legendOffset.get_i());
497 const unsigned int horOffset =
static_cast<unsigned int>(
data.m_ipLegend.get_j());
498 const unsigned int legendPFVert =
data.m_I_orig.getHeight() - 2 * vertOffset, legendPFHor = horOffset;
506 const double maxDistanceForLikelihood =
data.m_pfMaxDistanceForLikelihood;
507 const double sigmaLikelihood = maxDistanceForLikelihood / 3.;
508 const unsigned int nbParticles =
data.m_pfN;
509 std::vector<double> stdevsPF;
510 for (
unsigned int i = 0;
i <
data.m_degree + 1; ++
i) {
511 double ampliMax =
data.m_pfRatiosAmpliMax[
i] *
X0[
i];
512 stdevsPF.push_back(ampliMax / 3.);
514 unsigned long seedPF;
515 const float period = 33.3f;
516 if (
data.m_pfSeed < 0) {
520 seedPF =
data.m_pfSeed;
522 const int nbThread =
data.m_pfNbThreads;
527 tutorial::vpTutoLikelihoodFunctor likelihoodFtor(sigmaLikelihood,
data.m_I_orig.getHeight(),
data.m_I_orig.getWidth());
528 using std::placeholders::_1;
529 using std::placeholders::_2;
533 tutorial::vpTutoAverageFunctor averageCpter(
data.m_degree,
data.m_I_orig.getHeight(),
data.m_I_orig.getWidth());
534 using std::placeholders::_3;
541 filter.init(X0, processFunc, likelihoodFunc, checkResamplingFunc, resamplingFunc, meanFunc);
545#ifdef VISP_HAVE_DISPLAY
546 unsigned int plotHeight = 350, plotWidth = 350;
547 int plotXpos =
static_cast<int>(
data.m_legendOffset.get_u());
548 int plotYpos =
static_cast<int>(
data.m_I_orig.getHeight() + 4. *
data.m_legendOffset.get_v());
549 vpPlot plot(1, plotHeight, plotWidth, plotXpos, plotYpos,
"Root mean-square error");
550 plot.initGraph(0, 1);
551 plot.setLegend(0, 0,
"PF estimator");
557 unsigned int nbIter = 0;
558 double meanDtPF = 0.;
559 double meanRootMeanSquareErrorPF = 0.;
560 while (!
data.m_grabber.end() && run) {
561 std::cout <<
"Iter " << nbIter << std::endl;
562 data.m_grabber.acquire(
data.m_I_orig);
564 tutorial::performSegmentationHSV(data);
567 std::vector<vpImagePoint> edgePoints = tutorial::extractSkeleton(data);
570 std::vector<vpImagePoint> noisyEdgePoints = tutorial::addSaltAndPepperNoise(edgePoints, data);
572#ifdef VISP_HAVE_DISPLAY
582 filter.filter(noisyEdgePoints, period);
591 double pfError = tutorial::evaluate(Xest,
data.m_I_orig.getHeight(),
data.m_I_orig.getWidth(), edgePoints);
593 std::cout <<
" [Particle Filter method] " << std::endl;
594 std::cout <<
" Coeffs = [" <<
Xest.transpose() <<
" ]" << std::endl;
595 std::cout <<
" Root Mean Square Error = " << pfError <<
" pixels" << std::endl;
596 std::cout <<
" Fitting duration = " << dtPF <<
" ms" << std::endl;
598 meanRootMeanSquareErrorPF += pfError;
600#ifdef VISP_HAVE_DISPLAY
602 tutorial::display(Xest,
data.m_IskeletonNoisy,
vpColor::red, legendPFVert, legendPFHor);
605 plot.plot(0, 0, nbIter, pfError);
611 run =
data.manageClicks(
data.m_I_orig,
data.m_stepbystep);
616 double iterAsDouble =
static_cast<double>(nbIter);
617 std::cout << std::endl << std::endl <<
"-----[Statistics summary]-----" << std::endl;
618 std::cout <<
" [Particle Filter method] " << std::endl;
619 std::cout <<
" Average Root Mean Square Error = " << meanRootMeanSquareErrorPF / iterAsDouble <<
" pixels" << std::endl;
620 std::cout <<
" Average fitting duration = " << meanDtPF / iterAsDouble <<
" ms" << std::endl;
622#ifdef VISP_HAVE_DISPLAY
623 if (
data.m_grabber.end() && (!
data.m_stepbystep)) {
640 std::cerr <<
"ViSP must be compiled with C++ standard >= C++11 to use this tutorial." << std::endl;
641 std::cerr <<
"ViSP must also have a 3rd party enabling display features, such as X11 or OpenCV." << std::endl;
Implementation of column vector and the associated operations.
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
static void flush(const vpImage< unsigned char > &I)
static void displayPoint(const vpImage< unsigned char > &I, const vpImagePoint &ip, const vpColor &color, unsigned int thickness=1)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
unsigned int getWidth() const
unsigned int getHeight() const
static bool equal(double x, double y, double threshold=0.001)
The class permits to use a Particle Filter.
This class enables real time drawing of 2D or 3D graphics. An instance of the class open a window whi...
@ TUKEY
Tukey influence function.
void MEstimator(const vpRobustEstimatorType method, const vpColVector &residues, vpColVector &weights)
VISP_EXPORT double measureTimeMs()
VISP_EXPORT double measureTimeMicros()