Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpMeEllipse.h
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 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
35
36#ifndef VP_ME_ELLIPSE_H
37#define VP_ME_ELLIPSE_H
38
39#include <visp3/core/vpColVector.h>
40#include <visp3/core/vpMatrix.h>
41
42#include <visp3/core/vpImagePoint.h>
43#include <visp3/me/vpMeSite.h>
44#include <visp3/me/vpMeTracker.h>
45
46#include <visp3/core/vpColor.h>
47#include <visp3/core/vpImage.h>
48
49#include <list>
50#include <math.h>
51
52#if ((__cplusplus >= 201703L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L))) // Check if cxx17 or higher
53#include <optional>
54#endif
55
57
99class VISP_EXPORT vpMeEllipse : public vpMeTracker
100{
101public:
105 vpMeEllipse();
106
110 vpMeEllipse(const vpMeEllipse &me_ellipse);
111
115 virtual ~vpMeEllipse() VP_OVERRIDE;
116
120 vpMeEllipse &operator=(const vpMeEllipse &me_ellipse);
121
131 void display(const vpImage<unsigned char> &I, const vpColor &col, unsigned int thickness = 1);
132
142 void display(const vpImage<vpRGBa> &I, const vpColor &col, unsigned int thickness = 1);
143
153 inline vpColVector get_nij() const
154 {
155 vpColVector nij(3);
156 const unsigned int index_0 = 0;
157 const unsigned int index_1 = 1;
158 const unsigned int index_2 = 2;
159 nij[index_0] = m_n20;
160 nij[index_1] = m_n11;
161 nij[index_2] = m_n02;
162
163 return nij;
164 }
165
175 inline vpColVector get_ABE() const
176 {
177 vpColVector ABE(3);
178 const unsigned int index_0 = 0;
179 const unsigned int index_1 = 1;
180 const unsigned int index_2 = 2;
181 ABE[index_0] = m_a;
182 ABE[index_1] = m_b;
183 ABE[index_2] = m_e;
184
185 return ABE;
186 }
187
196 inline double getArea() const { return m_m00; }
197
203 inline vpImagePoint getCenter() const { return m_iPc; }
204
208 unsigned int getExpectedDensity() const { return m_expectedDensity; }
209
213 unsigned int getNumberOfGoodPoints() const { return m_numberOfGoodPoints; }
214
221 inline vpImagePoint getFirstEndpoint() const { return m_iP1; }
222
229 inline double getHighestAngle() const { return m_alphamax; }
230
237 inline vpImagePoint getSecondEndpoint() const { return m_iP2; }
238
245 inline double getSmallestAngle() const { return m_alphamin; }
246
260 void initTracking(const vpImage<unsigned char> &I, bool trackCircle = false, bool trackArc = false);
261
281#if ((__cplusplus >= 201703L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L))) // Check if cxx17 or higher
282 inline void initTracking(const vpImage<unsigned char> &I, std::optional<std::vector<vpImagePoint>> &opt_ips,
283 bool trackCircle = false, bool trackArc = false)
284#else
285 inline void initTracking(const vpImage<unsigned char> &I, std::vector<vpImagePoint> *opt_ips,
286 bool trackCircle = false, bool trackArc = false)
287#endif
288 {
289#if ((__cplusplus >= 201703L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L))) // Check if cxx17 or higher
290 if (!opt_ips.has_value())
291#else
292 if (opt_ips == nullptr)
293#endif
294 {
295 initTracking(I, trackCircle, trackArc);
296 return;
297 }
298
299 if (opt_ips->size() != 0) {
300 initTracking(I, *opt_ips, trackCircle, trackArc);
301 return;
302 }
303
304 unsigned int n = 5; // by default, 5 points for an ellipse
305 const unsigned int nForCircle = 3;
306 m_trackCircle = trackCircle;
307 if (trackCircle) {
308 n = nForCircle;
309 }
310 opt_ips->resize(n);
311 m_trackArc = trackArc;
312
314
315 if (m_trackCircle) {
316 if (m_trackArc) {
317 std::cout << "First and third points specify the extremities of the arc of circle (clockwise)" << std::endl;
318 }
319 for (unsigned int k = 0; k < n; ++k) {
320 std::cout << "Click point " << (k + 1) << "/" << n << " on the circle " << std::endl;
321 vpDisplay::getClick(I, (*opt_ips)[k], true);
322 const unsigned int crossSize = 10;
323 vpDisplay::displayCross(I, (*opt_ips)[k], crossSize, vpColor::red);
325 std::cout << (*opt_ips)[k] << std::endl;
326 }
327 }
328 else {
329 if (m_trackArc) {
330 std::cout << "First and fifth points specify the extremities of the arc of ellipse (clockwise)" << std::endl;
331 }
332 for (unsigned int k = 0; k < n; ++k) {
333 std::cout << "Click point " << (k + 1) << "/" << n << " on the ellipse " << std::endl;
334 vpDisplay::getClick(I, (*opt_ips)[k], true);
335 const unsigned int crossSize = 10;
336 vpDisplay::displayCross(I, (*opt_ips)[k], crossSize, vpColor::red);
338 std::cout << (*opt_ips)[k] << std::endl;
339 }
340 }
341 initTracking(I, *opt_ips, trackCircle, trackArc);
342 }
343
361 void initTracking(const vpImage<unsigned char> &I, const std::vector<vpImagePoint> &iP, bool trackCircle = false,
362 bool trackArc = false);
363
378 void initTracking(const vpImage<unsigned char> &I, const vpColVector &param, vpImagePoint *pt1 = nullptr,
379 const vpImagePoint *pt2 = nullptr, bool trackCircle = false);
380
385 void printParameters() const;
386
390 void setEndpoints(const vpImagePoint &pt1, const vpImagePoint &pt2)
391 {
392 m_iP1 = pt1;
393 m_iP2 = pt2;
394 }
395
405 void setThresholdRobust(double threshold)
406 {
407 if (threshold < 0) {
409 }
410 else if (threshold > 1) {
412 }
413 else {
414 m_thresholdWeight = threshold;
415 }
416 }
417
422 void track(const vpImage<unsigned char> &I);
423
440 static void displayEllipse(const vpImage<unsigned char> &I, const vpImagePoint &center, const double &A, const double &B,
441 const double &E, const double &smallalpha, const double &highalpha,
442 const vpColor &color = vpColor::green, unsigned int thickness = 1);
443
460 static void displayEllipse(const vpImage<vpRGBa> &I, const vpImagePoint &center, const double &A, const double &B,
461 const double &E, const double &smallalpha, const double &highalpha,
462 const vpColor &color = vpColor::green, unsigned int thickness = 1);
463
464#ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
465 // Marked deprecated since they override vpMeTracker::display(). Warning detected by mingw64
469 VP_DEPRECATED static void display(const vpImage<unsigned char> &I, const vpImagePoint &center, const double &A, const double &B,
470 const double &E, const double &smallalpha, const double &highalpha,
471 const vpColor &color = vpColor::green, unsigned int thickness = 1);
472
476 VP_DEPRECATED static void display(const vpImage<vpRGBa> &I, const vpImagePoint &center, const double &A, const double &B,
477 const double &E, const double &smallalpha, const double &highalpha,
478 const vpColor &color = vpColor::green, unsigned int thickness = 1);
479#endif
480
481protected:
489 double m_a;
491 double m_b;
495 double m_e;
496
508 double m_alpha1;
513 double m_alpha2;
515 double m_ce;
517 double m_se;
519 std::list<double> m_angleList;
521 double m_m00;
522
525
535 double m_uc;
537 double m_vc;
539 double m_n20;
541 double m_n11;
543 double m_n02;
545 unsigned int m_expectedDensity;
554
561 void computeAbeFromNij();
562
568 double computeAngleOnEllipse(const vpImagePoint &pt) const;
569
576 void computeKiFromNij();
577
583 void computeNijFromAbe();
584
592 void computePointOnEllipse(const double angle, vpImagePoint &iP);
593
601 double computeTheta(const vpImagePoint &iP) const;
602
613 double computeTheta(double u, double v) const;
614
624 void getParameters();
625
633 void leastSquare(const vpImage<unsigned char> &I, const std::vector<vpImagePoint> &iP);
634
643 unsigned int leastSquareRobust(const vpImage<unsigned char> &I);
644
655 void leastSquareRobustCircle(const double &um, const double &vm, unsigned int &k, vpColVector &w);
656
667 void leastSquareRobustEllipse(const double &um, const double &vm, unsigned int &k, vpColVector &w);
668
680 unsigned int plugHoles(const vpImage<unsigned char> &I);
681
693 virtual void sample(const vpImage<unsigned char> &I, bool doNotTrack = false) VP_OVERRIDE;
694
700 void updateTheta();
701};
702
703END_VISP_NAMESPACE
704
705#endif
Implementation of column vector and the associated operations.
Class to define RGB colors available for display functionalities.
Definition vpColor.h:157
static const vpColor red
Definition vpColor.h:198
static const vpColor green
Definition vpColor.h:201
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
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)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition of the vpImage class member functions.
Definition vpImage.h:131
double m_n20
Second order centered and normalized moments .
double m_arcEpsilon
Epsilon value used to check if arc angles are the same.
void leastSquareRobustEllipse(const double &um, const double &vm, unsigned int &k, vpColVector &w)
void computePointOnEllipse(const double angle, vpImagePoint &iP)
double getHighestAngle() const
double m_alpha2
vpImagePoint m_iPc
The coordinates of the ellipse center.
double getSmallestAngle() const
void leastSquareRobustCircle(const double &um, const double &vm, unsigned int &k, vpColVector &w)
double m_vc
Value of v coordinate of iPc.
unsigned int m_numberOfGoodPoints
Number of correct points tracked along the ellipse.
void computeKiFromNij()
unsigned int plugHoles(const vpImage< unsigned char > &I)
void computeNijFromAbe()
void getParameters()
double m_uc
Value of u coordinate of iPc.
bool m_trackCircle
Track a circle (true) or an ellipse (false).
void computeAbeFromNij()
void setEndpoints(const vpImagePoint &pt1, const vpImagePoint &pt2)
vpColVector m_K
double m_m00
Ellipse area.
vpImagePoint getSecondEndpoint() const
vpImagePoint getFirstEndpoint() const
double m_alphamin
void setThresholdRobust(double threshold)
vpColVector get_ABE() const
unsigned int getNumberOfGoodPoints() const
std::list< double > m_angleList
Stores the value in increasing order of the angle on the ellipse for each vpMeSite.
bool m_trackArc
Track an arc of ellipse/circle (true) or a complete one (false).
double m_a
is the semi major axis of the ellipse.
void initTracking(const vpImage< unsigned char > &I, std::optional< std::vector< vpImagePoint > > &opt_ips, bool trackCircle=false, bool trackArc=false)
vpImagePoint getCenter() const
unsigned int leastSquareRobust(const vpImage< unsigned char > &I)
double m_alpha1
void leastSquare(const vpImage< unsigned char > &I, const std::vector< vpImagePoint > &iP)
double m_b
is the semi minor axis of the ellipse.
double m_alphamax
double computeTheta(const vpImagePoint &iP) const
vpColVector get_nij() const
double m_se
Value of sin(e).
double m_n02
Second order centered and normalized moments .
unsigned int getExpectedDensity() const
unsigned int m_expectedDensity
Expected number of points to track along the ellipse.
void updateTheta()
double getArea() const
vpImagePoint m_iP2
double m_n11
Second order centered and normalized moments .
vpImagePoint m_iP1
double m_ce
Value of cos(e).
double m_thresholdWeight
Threshold on the weights for the robust least square.
double computeAngleOnEllipse(const vpImagePoint &pt) const
void initTracking(const vpImage< unsigned char > &I)
virtual void sample(const vpImage< unsigned char > &image, bool doNotTrack=false)=0
void track(const vpImage< unsigned char > &I)
vpMeTracker & operator=(vpMeTracker &meTracker)
void display(const vpImage< unsigned char > &I)