Visual Servoing Platform  version 3.6.1 under development (2024-04-26)
vpMe.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Moving edges.
32  */
33 
39 #ifndef _vpMe_h_
40 #define _vpMe_h_
41 
42 #include <visp3/core/vpImage.h>
43 #include <visp3/core/vpMath.h>
44 #include <visp3/core/vpMatrix.h>
45 
123 class VISP_EXPORT vpMe
124 {
125 public:
129  typedef enum
130  {
132  OLD_THRESHOLD = 0,
135  NORMALIZED_THRESHOLD = 1
136  } vpLikelihoodThresholdType;
137 
138 private:
139  vpLikelihoodThresholdType m_likelihood_threshold_type;
141  double m_threshold;
142  double m_thresholdMarginRatio;
143  double m_minThreshold;
144  bool m_useAutomaticThreshold;
145  double m_mu1;
146  double m_mu2;
147  double m_min_samplestep;
148  unsigned int m_anglestep;
149  int m_mask_sign;
150  unsigned int m_range;
151  double m_sample_step;
152  int m_ntotal_sample;
153  int m_points_to_track;
155  unsigned int m_mask_size;
157  unsigned int m_mask_number;
160  int m_strip;
161  vpMatrix *m_mask;
162 
163 public:
167  vpMe();
168 
172  vpMe(const vpMe &me);
173 
177  virtual ~vpMe();
178 
182  vpMe &operator=(const vpMe &me);
183 
184 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
188  vpMe &operator=(const vpMe &&me);
189 #endif
190 
196  void checkSamplestep(double &sample_step)
197  {
198  if (sample_step < m_min_samplestep)
199  sample_step = m_min_samplestep;
200  }
201 
207  inline unsigned int getAngleStep() const { return m_anglestep; }
208 
214  inline vpMatrix *getMask() const { return m_mask; }
215 
223  inline unsigned int getMaskNumber() const { return m_mask_number; }
224 
230  inline int getMaskSign() const { return m_mask_sign; }
231 
239  inline unsigned int getMaskSize() const { return m_mask_size; }
240 
247  inline double getMinSampleStep() const { return m_min_samplestep; }
248 
254  inline double getMu1() const { return m_mu1; }
255 
261  inline double getMu2() const { return m_mu2; }
262 
268  inline int getNbTotalSample() const { return m_ntotal_sample; }
269 
275  inline int getPointsToTrack() const { return m_points_to_track; }
276 
282  inline unsigned int getRange() const { return m_range; }
283 
289  inline double getSampleStep() const { return m_sample_step; }
290 
296  inline int getStrip() const { return m_strip; }
297 
305  inline double getThreshold() const { return m_threshold; }
306 
314  inline double getThresholdMarginRatio() const { return m_thresholdMarginRatio; }
315 
324  inline double getMinThreshold() const { return m_minThreshold; }
325 
332  inline bool getUseAutomaticThreshold() const { return m_useAutomaticThreshold; }
333 
341  inline vpLikelihoodThresholdType getLikelihoodThresholdType() const { return m_likelihood_threshold_type; }
342 
347  void initMask(); // convolution masks - offset computation
348 
352  void print();
353 
359  void setAngleStep(const unsigned int &anglestep) { m_anglestep = anglestep; }
360 
368  void setMaskNumber(const unsigned int &mask_number);
369 
375  void setMaskSign(const int &mask_sign) { m_mask_sign = mask_sign; }
376 
384  void setMaskSize(const unsigned int &mask_size);
385 
392  void setMinSampleStep(const double &min_samplestep) { m_min_samplestep = min_samplestep; }
393 
399  void setMu1(const double &mu_1) { this->m_mu1 = mu_1; }
400 
406  void setMu2(const double &mu_2) { this->m_mu2 = mu_2; }
407 
413  void setNbTotalSample(const int &ntotal_sample) { m_ntotal_sample = ntotal_sample; }
414 
422  void setPointsToTrack(const int &points_to_track) { m_points_to_track = points_to_track; }
423 
429  void setRange(const unsigned int &range) { m_range = range; }
430 
436  void setSampleStep(const double &sample_step) { m_sample_step = sample_step; }
437 
443  void setStrip(const int &strip) { m_strip = strip; }
444 
480  void setThreshold(const double &threshold) { m_threshold = threshold; }
481 
489  inline void setThresholdMarginRatio(const double &thresholdMarginRatio)
490  {
491  if (thresholdMarginRatio > 1.) {
492  throw(vpException(vpException::badValue, "Threshold margin ratio must be between 0 and 1 if you want to use automatic threshold computation, or negative otherwise"));
493  }
494  m_thresholdMarginRatio = thresholdMarginRatio;
495  m_useAutomaticThreshold = (m_thresholdMarginRatio > 0) && (m_minThreshold > 0);
496  }
497 
505  inline void setMinThreshold(const double &minThreshold)
506  {
507  m_minThreshold = minThreshold;
508  m_useAutomaticThreshold = (m_thresholdMarginRatio > 0) && (m_minThreshold > 0);
509  }
510 
519  void setLikelihoodThresholdType(const vpLikelihoodThresholdType likelihood_threshold_type) { m_likelihood_threshold_type = likelihood_threshold_type; }
520 
521 #ifdef VISP_HAVE_NLOHMANN_JSON
528  friend void to_json(nlohmann::json &j, const vpMe &me);
529 
577  friend void from_json(const nlohmann::json &j, vpMe &me);
578 #endif
579 };
580 #ifdef VISP_HAVE_NLOHMANN_JSON
581 #include <nlohmann/json.hpp>
582 
583 NLOHMANN_JSON_SERIALIZE_ENUM(vpMe::vpLikelihoodThresholdType, {
584  {vpMe::vpLikelihoodThresholdType::OLD_THRESHOLD, "old"},
585  {vpMe::vpLikelihoodThresholdType::NORMALIZED_THRESHOLD, "normalized"}
586 });
587 
588 inline void to_json(nlohmann::json &j, const vpMe &me)
589 {
590  j = {
591  {"thresholdType", me.getLikelihoodThresholdType()},
592  {"threshold", me.getThreshold()},
593  {"thresholdMarginRatio", me.getThresholdMarginRatio()},
594  {"minThreshold", me.getMinThreshold()},
595  {"mu", {me.getMu1(), me.getMu2()}},
596  {"minSampleStep", me.getMinSampleStep()},
597  {"sampleStep", me.getSampleStep()},
598  {"range", me.getRange()},
599  {"ntotalSample", me.getNbTotalSample()},
600  {"pointsToTrack", me.getPointsToTrack()},
601  {"maskSize", me.getMaskSize()},
602  {"nMask", me.getMaskNumber()},
603  {"maskSign", me.getMaskSign()},
604  {"strip", me.getStrip()}
605  };
606 }
607 
608 inline void from_json(const nlohmann::json &j, vpMe &me)
609 {
610  if (j.contains("thresholdType")) {
611  me.setLikelihoodThresholdType(j.value("thresholdType", me.getLikelihoodThresholdType()));
612  }
613  me.setThreshold(j.value("threshold", me.getThreshold()));
614  me.setThresholdMarginRatio(j.value("thresholdMarginRatio", me.getThresholdMarginRatio()));
615  me.setMinThreshold(j.value("minThreshold", me.getMinThreshold()));
616 
617  if (j.contains("mu")) {
618  std::vector<double> mus = j.at("mu").get<std::vector<double>>();
619  assert((mus.size() == 2));
620  me.setMu1(mus[0]);
621  me.setMu2(mus[1]);
622  }
623  me.setMinSampleStep(j.value("minSampleStep", me.getMinSampleStep()));
624  me.setSampleStep(j.value("sampleStep", me.getSampleStep()));
625  me.setRange(j.value("range", me.getRange()));
626  me.setNbTotalSample(j.value("ntotalSample", me.getNbTotalSample()));
627  me.setPointsToTrack(j.value("pointsToTrack", me.getPointsToTrack()));
628  me.setMaskSize(j.value("maskSize", me.getMaskSize()));
629  me.setMaskSign(j.value("maskSign", me.getMaskSign()));
630  me.setStrip(j.value("strip", me.getStrip()));
631  if (j.contains("angleStep") && j.contains("nMask")) {
632  std::cerr << "both angle step and number of masks are defined, number of masks will take precedence" << std::endl;
633  me.setMaskNumber(j["nMask"]);
634  }
635  else if (j.contains("angleStep")) {
636  me.setAngleStep(j["angleStep"]);
637  }
638  else if (j.contains("nMask")) {
639  me.setMaskNumber(j["nMask"]);
640  }
641  me.initMask();
642 }
643 
644 #endif
645 
646 #endif
error that can be emitted by ViSP classes.
Definition: vpException.h:59
@ badValue
Used to indicate that a value is not in the allowed range.
Definition: vpException.h:85
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:146
Definition: vpMe.h:124
bool getUseAutomaticThreshold() const
Indicates if the contrast threshold of the vpMeSite is automatically computed.
Definition: vpMe.h:332
int getMaskSign() const
Definition: vpMe.h:230
void setMu1(const double &mu_1)
Definition: vpMe.h:399
void setMinThreshold(const double &minThreshold)
Definition: vpMe.h:505
void setPointsToTrack(const int &points_to_track)
Definition: vpMe.h:422
vpLikelihoodThresholdType getLikelihoodThresholdType() const
Definition: vpMe.h:341
double getMinSampleStep() const
Definition: vpMe.h:247
void setMaskSign(const int &mask_sign)
Definition: vpMe.h:375
void setRange(const unsigned int &range)
Definition: vpMe.h:429
void setLikelihoodThresholdType(const vpLikelihoodThresholdType likelihood_threshold_type)
Definition: vpMe.h:519
void setNbTotalSample(const int &ntotal_sample)
Definition: vpMe.h:413
void setAngleStep(const unsigned int &anglestep)
Definition: vpMe.h:359
void setMaskNumber(const unsigned int &mask_number)
Definition: vpMe.cpp:488
int getNbTotalSample() const
Definition: vpMe.h:268
void setThreshold(const double &threshold)
Definition: vpMe.h:480
void setStrip(const int &strip)
Definition: vpMe.h:443
void checkSamplestep(double &sample_step)
Definition: vpMe.h:196
unsigned int getAngleStep() const
Definition: vpMe.h:207
double getThresholdMarginRatio() const
Definition: vpMe.h:314
void setMinSampleStep(const double &min_samplestep)
Definition: vpMe.h:392
double getMu1() const
Definition: vpMe.h:254
unsigned int getMaskNumber() const
Definition: vpMe.h:223
int getPointsToTrack() const
Definition: vpMe.h:275
int getStrip() const
Definition: vpMe.h:296
void setSampleStep(const double &sample_step)
Definition: vpMe.h:436
double getMu2() const
Definition: vpMe.h:261
double getThreshold() const
Definition: vpMe.h:305
unsigned int getMaskSize() const
Definition: vpMe.h:239
void setMaskSize(const unsigned int &mask_size)
Definition: vpMe.cpp:496
void initMask()
Definition: vpMe.cpp:354
void setThresholdMarginRatio(const double &thresholdMarginRatio)
Definition: vpMe.h:489
void setMu2(const double &mu_2)
Definition: vpMe.h:406
vpMatrix * getMask() const
Definition: vpMe.h:214
double getSampleStep() const
Definition: vpMe.h:289
unsigned int getRange() const
Definition: vpMe.h:282
vpLikelihoodThresholdType
Definition: vpMe.h:130
double getMinThreshold() const
Definition: vpMe.h:324