ViSP  2.10.0
vpDetectorBase.h
1 /****************************************************************************
2  *
3  * $Id$
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  * Description:
34  * Base class for object detection.
35  *
36  * Authors:
37  * Fabien Spindler
38  *
39  *****************************************************************************/
40 
41 #ifndef __vpDetectorBase_h__
42 #define __vpDetectorBase_h__
43 
44 #include <vector>
45 #include <utility>
46 #include <string>
47 #include <assert.h>
48 
49 #include <visp/vpImage.h>
50 #include <visp/vpImagePoint.h>
51 #include <visp/vpRect.h>
52 
64 class VISP_EXPORT vpDetectorBase
65 {
66 protected:
67  std::vector< std::vector<vpImagePoint> > m_polygon;
68  std::vector< std::string > m_message;
69  size_t m_nb_objects;
70 
71 public:
75  vpDetectorBase() : m_polygon(), m_message(), m_nb_objects(0) {}
79  virtual ~vpDetectorBase() {};
80 
86  virtual bool detect(const vpImage<unsigned char> &I) = 0;
90  std::vector< std::vector<vpImagePoint> > & getPolygon()
91  {
92  return m_polygon;
93  }
94 
98  std::vector<vpImagePoint> & getPolygon(size_t i)
99  {
100  if (i < m_polygon.size())
101  return m_polygon[i];
102  else
103  throw(vpException(vpException::badValue, "Bad index to retrieve object %d. Only %d objects are detected.", i, m_polygon.size()));
104  }
108  std::string & getMessage(size_t i)
109  {
110  if (i < m_polygon.size())
111  return m_message[i];
112  else
113  throw(vpException(vpException::badValue, "Bad index to retrieve object %d . Only %d objects are detected.", i, m_polygon.size()));
114  }
118  std::vector< std::string > & getMessage()
119  {
120  return m_message;
121  }
125  size_t getNbObjects() const {return m_nb_objects; }
129  vpImagePoint getCog(size_t i) const
130  {
131  vpImagePoint cog(0,0);
132  for(size_t j=0; j < m_polygon[i].size(); j++) {
133  cog += m_polygon[i][j];
134  }
135  cog /= (double)m_polygon[i].size();
136  return cog;
137  }
141  vpRect getBBox(size_t i) const
142  {
143  assert(m_polygon[i].size() > 2);
144 
145  double left, right;
146  double top, bottom;
147  left = right = m_polygon[i][0].get_u();
148  top = bottom = m_polygon[i][0].get_v();
149  for(size_t j=0; j < m_polygon[i].size(); j++) {
150  double u = m_polygon[i][j].get_u();
151  double v = m_polygon[i][j].get_v();
152  if (u < left) left = u;
153  if (u > right) right = u;
154  if (v < top) top = v;
155  if (v > bottom) bottom = v;
156  }
157  vpRect roi(vpImagePoint(top, left), vpImagePoint(bottom, right));
158  return roi;
159  }
160 };
161 
162 #endif
virtual ~vpDetectorBase()
std::vector< vpImagePoint > & getPolygon(size_t i)
error that can be emited by ViSP classes.
Definition: vpException.h:76
size_t getNbObjects() const
vpImagePoint getCog(size_t i) const
size_t m_nb_objects
Number of detected objects.
std::vector< std::vector< vpImagePoint > > & getPolygon()
std::vector< std::string > & getMessage()
std::vector< std::string > m_message
Message attached to each object.
std::string & getMessage(size_t i)
Defines a rectangle in the plane.
Definition: vpRect.h:85
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:93
vpRect getBBox(size_t i) const
std::vector< std::vector< vpImagePoint > > m_polygon
For each object, defines the polygon that contains the object.