Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
vpContours.h
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 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  * Basic contours extraction based on the orignal work of
33  * Sina Samangooei (ss@ecs.soton.ac.uk).
34  *
35  * Authors:
36  * Souriya Trinh
37  *
38  *****************************************************************************/
75 #ifndef _vpContours_h_
76 #define _vpContours_h_
77 
78 #include <visp3/core/vpColor.h>
79 #include <visp3/core/vpImage.h>
80 #include <visp3/core/vpPolygon.h>
81 
82 namespace
83 {
84 typedef enum {
85  NORTH,
86  NORTH_EAST,
87  EAST,
88  SOUTH_EAST,
89  SOUTH,
90  SOUTH_WEST,
91  WEST,
92  NORTH_WEST,
93  LAST_DIRECTION
94 } vpDirectionType;
95 
96 struct vpDirection {
97  vpDirectionType m_direction;
98 
99  int m_dirx[8];
100  int m_diry[8];
101 
102  vpDirection()
103  {
104  m_dirx[0] = 0;
105  m_dirx[1] = 1;
106  m_dirx[2] = 1;
107  m_dirx[3] = 1;
108  m_dirx[4] = 0;
109  m_dirx[5] = -1;
110  m_dirx[6] = -1;
111  m_dirx[7] = -1;
112 
113  m_diry[0] = -1;
114  m_diry[1] = -1;
115  m_diry[2] = 0;
116  m_diry[3] = 1;
117  m_diry[4] = 1;
118  m_diry[5] = 1;
119  m_diry[6] = 0;
120  m_diry[7] = -1;
121  }
122 
123  vpDirection clockwise()
124  {
125  vpDirection direction;
126  int directionSize = LAST_DIRECTION;
127  direction.m_direction = vpDirectionType(((int)m_direction + 1) % directionSize);
128 
129  return direction;
130  }
131 
132  vpDirection counterClockwise()
133  {
134  vpDirection direction;
135  int directionSize = (int)LAST_DIRECTION;
136  int idx = vpMath::modulo((int)m_direction - 1, directionSize);
137  direction.m_direction = vpDirectionType(idx);
138 
139  return direction;
140  }
141 
142  vpImagePoint active(const vpImage<int> &I, const vpImagePoint &point)
143  {
144  int yy = (int)(point.get_i() + m_diry[(int)m_direction]);
145  int xx = (int)(point.get_j() + m_dirx[(int)m_direction]);
146 
147  if (xx < 0 || xx >= (int)I.getWidth() || yy < 0 || yy >= (int)I.getHeight()) {
148  return vpImagePoint(-1, -1);
149  }
150 
151  int pixel = I[yy][xx];
152  return pixel != 0 ? vpImagePoint(yy, xx) : vpImagePoint(-1, -1);
153  }
154 };
155 }
156 
157 namespace vp
158 {
159 typedef enum {
162 } vpContourType;
163 
164 typedef enum {
170 
171 struct vpContour {
172  std::vector<vpContour *> m_children;
173  vpContourType m_contourType;
175  std::vector<vpImagePoint> m_points;
176 
177  vpContour() : m_children(), m_contourType(vp::CONTOUR_HOLE), m_parent(NULL), m_points() {}
178 
179  vpContour(const vpContourType &type) : m_children(), m_contourType(type), m_parent(NULL), m_points() {}
180 
181  vpContour(const vpContour &contour)
182  : m_children(), m_contourType(contour.m_contourType), m_parent(NULL), m_points(contour.m_points)
183  {
184 
185  // Copy the underlying contours
186  for (std::vector<vpContour *>::const_iterator it = contour.m_children.begin(); it != contour.m_children.end();
187  ++it) {
188  vpContour *copy = new vpContour(**it);
189  copy->m_parent = this;
190  m_children.push_back(copy);
191  }
192  }
193 
194  virtual ~vpContour()
195  {
196  for (std::vector<vpContour *>::iterator it = m_children.begin(); it != m_children.end(); ++it) {
197  (*it)->m_parent = NULL;
198  if (*it != NULL) {
199  delete *it;
200  *it = NULL;
201  }
202  }
203  }
204 
206  {
207  m_contourType = other.m_contourType;
208 
209  if (m_parent == NULL) {
210  // We are a root or an unintialized contour so delete everything
211  for (std::vector<vpContour *>::iterator it = m_children.begin(); it != m_children.end(); ++it) {
212  (*it)->m_parent = NULL;
213  if (*it != NULL) {
214  delete *it;
215  *it = NULL;
216  }
217  }
218  } else {
219  // Make the current contour the root contour
220  // to avoid problem when deleting
221  m_parent = NULL;
222  }
223 
224  m_children.clear();
225  for (std::vector<vpContour *>::const_iterator it = other.m_children.begin(); it != other.m_children.end(); ++it) {
226  vpContour *copy = new vpContour(**it);
227  copy->m_parent = this;
228  m_children.push_back(copy);
229  }
230 
231  return *this;
232  }
233 
234  void setParent(vpContour *parent)
235  {
236  m_parent = parent;
237 
238  if (parent != NULL) {
239  parent->m_children.push_back(this);
240  }
241  }
242 };
243 
244 VISP_EXPORT void drawContours(vpImage<unsigned char> &I, const std::vector<std::vector<vpImagePoint> > &contours,
245  unsigned char grayValue = 255);
246 VISP_EXPORT void drawContours(vpImage<vpRGBa> &I, const std::vector<std::vector<vpImagePoint> > &contours,
247  const vpColor &color);
248 
249 VISP_EXPORT void findContours(const vpImage<unsigned char> &I_original, vpContour &contours,
250  std::vector<std::vector<vpImagePoint> > &contourPts,
251  const vpContourRetrievalType &retrievalMode = vp::CONTOUR_RETR_TREE);
252 }
253 
254 #endif
double get_i() const
Definition: vpImagePoint.h:204
vpContour(const vpContour &contour)
Definition: vpContours.h:181
vpContour * m_parent
Definition: vpContours.h:174
Class to define colors available for display functionnalities.
Definition: vpColor.h:119
std::vector< vpImagePoint > m_points
Definition: vpContours.h:175
std::vector< vpContour * > m_children
Definition: vpContours.h:172
virtual ~vpContour()
Definition: vpContours.h:194
vpContour & operator=(const vpContour &other)
Definition: vpContours.h:205
VISP_EXPORT void findContours(const vpImage< unsigned char > &I_original, vpContour &contours, std::vector< std::vector< vpImagePoint > > &contourPts, const vpContourRetrievalType &retrievalMode=vp::CONTOUR_RETR_TREE)
Definition: vpContours.cpp:300
VISP_EXPORT void drawContours(vpImage< vpRGBa > &I, const std::vector< std::vector< vpImagePoint > > &contours, const vpColor &color)
Definition: vpContours.cpp:275
double get_j() const
Definition: vpImagePoint.h:215
vpContourType
Definition: vpContours.h:159
vpContourType m_contourType
Definition: vpContours.h:173
unsigned int getHeight() const
Definition: vpImage.h:186
vpContour(const vpContourType &type)
Definition: vpContours.h:179
void setParent(vpContour *parent)
Definition: vpContours.h:234
vpContourRetrievalType
Definition: vpContours.h:164
static int modulo(int a, int n)
Definition: vpMath.cpp:286
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
unsigned int getWidth() const
Definition: vpImage.h:244
Definition of the vpImage class member functions.
Definition: vpImage.h:124