Visual Servoing Platform  version 3.6.1 under development (2024-04-20)
vpContours.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 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  * Description:
31  * Basic contours extraction based on the orignal work of
32  * Sina Samangooei (ss@ecs.soton.ac.uk).
33  */
70 #ifndef _vpContours_h_
71 #define _vpContours_h_
72 
73 #include <visp3/core/vpColor.h>
74 #include <visp3/core/vpImage.h>
75 #include <visp3/core/vpPolygon.h>
76 
77 
78 namespace
79 {
83 typedef enum
84 {
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 
99 struct vpDirection
100 {
102  vpDirectionType m_direction;
103 
105  int m_dirx[8];
106 
108  int m_diry[8];
109 
113  vpDirection()
114  {
115  m_direction = NORTH;
116 
117  m_dirx[0] = 0;
118  m_dirx[1] = 1;
119  m_dirx[2] = 1;
120  m_dirx[3] = 1;
121  m_dirx[4] = 0;
122  m_dirx[5] = -1;
123  m_dirx[6] = -1;
124  m_dirx[7] = -1;
125 
126  m_diry[0] = -1;
127  m_diry[1] = -1;
128  m_diry[2] = 0;
129  m_diry[3] = 1;
130  m_diry[4] = 1;
131  m_diry[5] = 1;
132  m_diry[6] = 0;
133  m_diry[7] = -1;
134  }
135 
140  vpDirection clockwise()
141  {
142  vpDirection direction;
143  int directionSize = LAST_DIRECTION;
144  direction.m_direction = vpDirectionType(((int)m_direction + 1) % directionSize);
145 
146  return direction;
147  }
148 
153  vpDirection counterClockwise()
154  {
155  vpDirection direction;
156  int directionSize = (int)LAST_DIRECTION;
157  int idx = vpMath::modulo((int)m_direction - 1, directionSize);
158  direction.m_direction = vpDirectionType(idx);
159 
160  return direction;
161  }
162 
169  vpImagePoint active(const vpImage<int> &I, const vpImagePoint &point)
170  {
171  int yy = (int)(point.get_i() + m_diry[(int)m_direction]);
172  int xx = (int)(point.get_j() + m_dirx[(int)m_direction]);
173 
174  if (xx < 0 || xx >= (int)I.getWidth() || yy < 0 || yy >= (int)I.getHeight()) {
175  return vpImagePoint(-1, -1);
176  }
177 
178  int pixel = I[yy][xx];
179  return pixel != 0 ? vpImagePoint(yy, xx) : vpImagePoint(-1, -1);
180  }
181 };
182 } // namespace
183 
184 namespace vp
185 {
189 typedef enum
190 {
192  CONTOUR_HOLE
194 
198 typedef enum
199 {
205 
209 struct vpContour
210 {
212  std::vector<vpContour *> m_children;
218  std::vector<vpImagePoint> m_points;
219 
224 
228  vpContour(const vpContourType &type) : m_children(), m_contourType(type), m_parent(nullptr), m_points() { }
229 
233  vpContour(const vpContour &contour)
234  : m_children(), m_contourType(contour.m_contourType), m_parent(nullptr), m_points(contour.m_points)
235  {
236 
237  // Copy the underlying contours
238  for (std::vector<vpContour *>::const_iterator it = contour.m_children.begin(); it != contour.m_children.end();
239  ++it) {
240  vpContour *copy = new vpContour(**it);
241  copy->m_parent = this;
242  m_children.push_back(copy);
243  }
244  }
245 
249  virtual ~vpContour()
250  {
251  for (std::vector<vpContour *>::iterator it = m_children.begin(); it != m_children.end(); ++it) {
252  (*it)->m_parent = nullptr;
253  if (*it != nullptr) {
254  delete *it;
255  *it = nullptr;
256  }
257  }
258  }
259 
264  {
266 
267  if (m_parent == nullptr) {
268  // We are a root or an uninitialized contour so delete everything
269  for (std::vector<vpContour *>::iterator it = m_children.begin(); it != m_children.end(); ++it) {
270  (*it)->m_parent = nullptr;
271  if (*it != nullptr) {
272  delete *it;
273  *it = nullptr;
274  }
275  }
276  }
277  else {
278  // Make the current contour the root contour
279  // to avoid problem when deleting
280  m_parent = nullptr;
281  }
282 
283  m_children.clear();
284  for (std::vector<vpContour *>::const_iterator it = other.m_children.begin(); it != other.m_children.end(); ++it) {
285  vpContour *copy = new vpContour(**it);
286  copy->m_parent = this;
287  m_children.push_back(copy);
288  }
289 
290  return *this;
291  }
292 
296  void setParent(vpContour *parent)
297  {
298  m_parent = parent;
299 
300  if (parent != nullptr) {
301  parent->m_children.push_back(this);
302  }
303  }
304 };
305 
315 VISP_EXPORT void drawContours(vpImage<unsigned char> &I, const std::vector<std::vector<vpImagePoint> > &contours,
316  unsigned char grayValue = 255);
317 
327 VISP_EXPORT void drawContours(vpImage<vpRGBa> &I, const std::vector<std::vector<vpImagePoint> > &contours,
328  const vpColor &color);
329 
341 VISP_EXPORT void findContours(const vpImage<unsigned char> &I_original, vpContour &contours,
342  std::vector<std::vector<vpImagePoint> > &contourPts,
343  const vpContourRetrievalType &retrievalMode = vp::CONTOUR_RETR_TREE);
344 } // namespace vp
345 
346 #endif
Class to define RGB colors available for display functionalities.
Definition: vpColor.h:152
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
double get_j() const
Definition: vpImagePoint.h:125
double get_i() const
Definition: vpImagePoint.h:114
Definition of the vpImage class member functions.
Definition: vpImage.h:69
unsigned int getWidth() const
Definition: vpImage.h:245
unsigned int getHeight() const
Definition: vpImage.h:184
static float modulo(const float &value, const float &modulo)
Gives the rest of value divided by modulo when the quotient can only be an integer.
Definition: vpMath.h:175
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:281
VISP_EXPORT void drawContours(vpImage< unsigned char > &I, const std::vector< std::vector< vpImagePoint > > &contours, unsigned char grayValue=255)
Definition: vpContours.cpp:247
vpContourRetrievalType
Definition: vpContours.h:199
@ CONTOUR_RETR_LIST
Definition: vpContours.h:202
@ CONTOUR_RETR_TREE
Definition: vpContours.h:200
@ CONTOUR_RETR_EXTERNAL
Definition: vpContours.h:203
vpContourType
Definition: vpContours.h:190
@ CONTOUR_HOLE
Definition: vpContours.h:192
@ CONTOUR_OUTER
Definition: vpContours.h:191
vpContour & operator=(const vpContour &other)
Definition: vpContours.h:263
vpContourType m_contourType
Contour type.
Definition: vpContours.h:214
virtual ~vpContour()
Definition: vpContours.h:249
void setParent(vpContour *parent)
Definition: vpContours.h:296
vpContour * m_parent
Parent contour.
Definition: vpContours.h:216
vpContour(const vpContourType &type)
Definition: vpContours.h:228
std::vector< vpImagePoint > m_points
Vector of points belonging to the contour.
Definition: vpContours.h:218
std::vector< vpContour * > m_children
Children contour.
Definition: vpContours.h:212
vpContour(const vpContour &contour)
Definition: vpContours.h:233