Visual Servoing Platform  version 3.6.1 under development (2024-04-25)
vpDetectorBase.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See https://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Base class for object detection.
33  *
34 *****************************************************************************/
35 #include <visp3/core/vpConfig.h>
36 
37 #include <visp3/detection/vpDetectorBase.h>
38 
42 vpDetectorBase::vpDetectorBase() : m_polygon(), m_message(), m_nb_objects(0), m_timeout_ms(0) { }
43 
45  : m_polygon(o.m_polygon), m_message(o.m_message), m_nb_objects(o.m_nb_objects), m_timeout_ms(o.m_timeout_ms)
46 { }
47 
51 std::vector<vpImagePoint> &vpDetectorBase::getPolygon(size_t i)
52 {
53  if (i < m_polygon.size()) {
54  return m_polygon[i];
55  }
56  else {
57  throw(vpException(vpException::badValue, "Bad index to retrieve object %d. Only %d objects are detected.", i,
58  m_polygon.size()));
59  }
60 }
61 
65 std::string &vpDetectorBase::getMessage(size_t i)
66 {
67  if (i < m_polygon.size()) {
68  return m_message[i];
69  }
70  else {
71  throw(vpException(vpException::badValue, "Bad index to retrieve object %d . Only %d objects are detected.", i,
72  m_polygon.size()));
73  }
74 }
75 
80 {
81  vpImagePoint cog(0, 0);
82  size_t m_polygon_i_size = m_polygon[i].size();
83  for (size_t j = 0; j < m_polygon_i_size; ++j) {
84  cog += m_polygon[i][j];
85  }
86  cog /= static_cast<double>(m_polygon[i].size());
87  return cog;
88 }
89 
94 {
95  assert(m_polygon[i].size() > 2);
96 
97  double left, right;
98  double top, bottom;
99  left = m_polygon[i][0].get_u();
100  right = m_polygon[i][0].get_u();
101  top = m_polygon[i][0].get_v();
102  bottom = m_polygon[i][0].get_v();
103  size_t m_polygon_i_size = m_polygon[i].size();
104  for (size_t j = 0; j < m_polygon_i_size; ++j) {
105  double u = m_polygon[i][j].get_u();
106  double v = m_polygon[i][j].get_v();
107  if (u < left) {
108  left = u;
109  }
110  if (u > right) {
111  right = u;
112  }
113  if (v < top) {
114  top = v;
115  }
116  if (v > bottom) {
117  bottom = v;
118  }
119  }
120  vpRect roi(vpImagePoint(top, left), vpImagePoint(bottom, right));
121  return roi;
122 }
std::vector< std::vector< vpImagePoint > > & getPolygon()
std::vector< std::string > m_message
Message attached to each object.
std::vector< std::vector< vpImagePoint > > m_polygon
For each object, defines the polygon that contains the object.
vpRect getBBox(size_t i) const
vpImagePoint getCog(size_t i) const
std::vector< std::string > & getMessage()
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
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
Defines a rectangle in the plane.
Definition: vpRect.h:76