Visual Servoing Platform  version 3.1.0
vpDetectorBase.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 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 http://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  * Authors:
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 #include <visp3/core/vpConfig.h>
39 
40 #include <visp3/detection/vpDetectorBase.h>
41 
45 vpDetectorBase::vpDetectorBase() : m_polygon(), m_message(), m_nb_objects(0) {}
46 
50 std::vector<vpImagePoint> &vpDetectorBase::getPolygon(size_t i)
51 {
52  if (i < m_polygon.size())
53  return m_polygon[i];
54  else
55  throw(vpException(vpException::badValue, "Bad index to retrieve object %d. Only %d objects are detected.", i,
56  m_polygon.size()));
57 }
58 
62 std::string &vpDetectorBase::getMessage(size_t i)
63 {
64  if (i < m_polygon.size())
65  return m_message[i];
66  else
67  throw(vpException(vpException::badValue, "Bad index to retrieve object %d . Only %d objects are detected.", i,
68  m_polygon.size()));
69 }
70 
75 {
76  vpImagePoint cog(0, 0);
77  for (size_t j = 0; j < m_polygon[i].size(); j++) {
78  cog += m_polygon[i][j];
79  }
80  cog /= (double)m_polygon[i].size();
81  return cog;
82 }
83 
88 {
89  assert(m_polygon[i].size() > 2);
90 
91  double left, right;
92  double top, bottom;
93  left = right = m_polygon[i][0].get_u();
94  top = bottom = m_polygon[i][0].get_v();
95  for (size_t j = 0; j < m_polygon[i].size(); j++) {
96  double u = m_polygon[i][j].get_u();
97  double v = m_polygon[i][j].get_v();
98  if (u < left)
99  left = u;
100  if (u > right)
101  right = u;
102  if (v < top)
103  top = v;
104  if (v > bottom)
105  bottom = v;
106  }
107  vpRect roi(vpImagePoint(top, left), vpImagePoint(bottom, right));
108  return roi;
109 }
vpRect getBBox(size_t i) const
error that can be emited by ViSP classes.
Definition: vpException.h:71
vpImagePoint getCog(size_t i) const
std::vector< std::vector< vpImagePoint > > m_polygon
Defines a rectangle in the plane.
Definition: vpRect.h:78
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
std::vector< std::string > & getMessage()
std::vector< std::string > m_message
Message attached to each object.
std::vector< std::vector< vpImagePoint > > & getPolygon()