Visual Servoing Platform  version 3.6.1 under development (2024-05-27)
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((static_cast<int>(m_direction) + 1) % directionSize);
145 
146  return direction;
147  }
148 
153  vpDirection counterClockwise()
154  {
155  vpDirection direction;
156  int directionSize = static_cast<int>(LAST_DIRECTION);
157  int idx = vpMath::modulo(static_cast<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 = static_cast<int>(point.get_i() + m_diry[static_cast<int>(m_direction)]);
172  int xx = static_cast<int>(point.get_j() + m_dirx[static_cast<int>(m_direction)]);
173 
174  if ((xx < 0) || (xx >= static_cast<int>(I.getWidth())) || (yy < 0) || (yy >= static_cast<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  explicit 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  std::vector<vpContour *>::const_iterator contour_m_children_end = contour.m_children.end();
239  for (std::vector<vpContour *>::const_iterator it = contour.m_children.begin(); it != contour_m_children_end;
240  ++it) {
241  vpContour *copy = new vpContour(**it);
242  copy->m_parent = this;
243  m_children.push_back(copy);
244  }
245  }
246 
250  virtual ~vpContour()
251  {
252  std::vector<vpContour *>::iterator m_children_end = m_children.end();
253  for (std::vector<vpContour *>::iterator it = m_children.begin(); it != m_children_end; ++it) {
254  (*it)->m_parent = nullptr;
255  if (*it != nullptr) {
256  delete *it;
257  *it = nullptr;
258  }
259  }
260  }
261 
266  {
268 
269  if (m_parent == nullptr) {
270  // We are a root or an uninitialized contour so delete everything
271  std::vector<vpContour *>::iterator m_children_end = m_children.end();
272  for (std::vector<vpContour *>::iterator it = m_children.begin(); it != m_children_end; ++it) {
273  (*it)->m_parent = nullptr;
274  if (*it != nullptr) {
275  delete *it;
276  *it = nullptr;
277  }
278  }
279  }
280  else {
281  // Make the current contour the root contour
282  // to avoid problem when deleting
283  m_parent = nullptr;
284  }
285 
286  m_children.clear();
287  std::vector<vpContour *>::const_iterator other_m_children_end = other.m_children.end();
288  for (std::vector<vpContour *>::const_iterator it = other.m_children.begin(); it != other_m_children_end; ++it) {
289  vpContour *copy = new vpContour(**it);
290  copy->m_parent = this;
291  m_children.push_back(copy);
292  }
293 
294  return *this;
295  }
296 
300  void setParent(vpContour *parent)
301  {
302  m_parent = parent;
303 
304  if (parent != nullptr) {
305  parent->m_children.push_back(this);
306  }
307  }
308 };
309 
319 VISP_EXPORT void drawContours(vpImage<unsigned char> &I, const std::vector<std::vector<vpImagePoint> > &contours,
320  unsigned char grayValue = 255);
321 
331 VISP_EXPORT void drawContours(vpImage<vpRGBa> &I, const std::vector<std::vector<vpImagePoint> > &contours,
332  const vpColor &color);
333 
345 VISP_EXPORT void findContours(const vpImage<unsigned char> &I_original, vpContour &contours,
346  std::vector<std::vector<vpImagePoint> > &contourPts,
347  const vpContourRetrievalType &retrievalMode = vp::CONTOUR_RETR_TREE);
348 } // namespace vp
349 
350 #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:290
VISP_EXPORT void drawContours(vpImage< unsigned char > &I, const std::vector< std::vector< vpImagePoint > > &contours, unsigned char grayValue=255)
Definition: vpContours.cpp:256
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:265
vpContourType m_contourType
Contour type.
Definition: vpContours.h:214
virtual ~vpContour()
Definition: vpContours.h:250
void setParent(vpContour *parent)
Definition: vpContours.h:300
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