Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
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
78namespace
79{
83typedef 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
99struct vpDirection
100{
102 vpDirectionType m_direction;
103
105 int m_dirx[8];
106
108 int m_diry[8];
109
113 vpDirection()
114 {
115 m_dirx[0] = 0;
116 m_dirx[1] = 1;
117 m_dirx[2] = 1;
118 m_dirx[3] = 1;
119 m_dirx[4] = 0;
120 m_dirx[5] = -1;
121 m_dirx[6] = -1;
122 m_dirx[7] = -1;
123
124 m_diry[0] = -1;
125 m_diry[1] = -1;
126 m_diry[2] = 0;
127 m_diry[3] = 1;
128 m_diry[4] = 1;
129 m_diry[5] = 1;
130 m_diry[6] = 0;
131 m_diry[7] = -1;
132 }
133
138 vpDirection clockwise()
139 {
140 vpDirection direction;
141 int directionSize = LAST_DIRECTION;
142 direction.m_direction = vpDirectionType(((int)m_direction + 1) % directionSize);
143
144 return direction;
145 }
146
151 vpDirection counterClockwise()
152 {
153 vpDirection direction;
154 int directionSize = (int)LAST_DIRECTION;
155 int idx = vpMath::modulo((int)m_direction - 1, directionSize);
156 direction.m_direction = vpDirectionType(idx);
157
158 return direction;
159 }
160
167 vpImagePoint active(const vpImage<int> &I, const vpImagePoint &point)
168 {
169 int yy = (int)(point.get_i() + m_diry[(int)m_direction]);
170 int xx = (int)(point.get_j() + m_dirx[(int)m_direction]);
171
172 if (xx < 0 || xx >= (int)I.getWidth() || yy < 0 || yy >= (int)I.getHeight()) {
173 return vpImagePoint(-1, -1);
174 }
175
176 int pixel = I[yy][xx];
177 return pixel != 0 ? vpImagePoint(yy, xx) : vpImagePoint(-1, -1);
178 }
179};
180} // namespace
181
182namespace vp
183{
192
203
208{
210 std::vector<vpContour *> m_children;
216 std::vector<vpImagePoint> m_points;
217
222
226 vpContour(const vpContourType &type) : m_children(), m_contourType(type), m_parent(NULL), m_points() { }
227
231 vpContour(const vpContour &contour)
232 : m_children(), m_contourType(contour.m_contourType), m_parent(NULL), m_points(contour.m_points)
233 {
234
235 // Copy the underlying contours
236 for (std::vector<vpContour *>::const_iterator it = contour.m_children.begin(); it != contour.m_children.end();
237 ++it) {
238 vpContour *copy = new vpContour(**it);
239 copy->m_parent = this;
240 m_children.push_back(copy);
241 }
242 }
243
247 virtual ~vpContour()
248 {
249 for (std::vector<vpContour *>::iterator it = m_children.begin(); it != m_children.end(); ++it) {
250 (*it)->m_parent = NULL;
251 if (*it != NULL) {
252 delete *it;
253 *it = NULL;
254 }
255 }
256 }
257
262 {
264
265 if (m_parent == NULL) {
266 // We are a root or an uninitialized contour so delete everything
267 for (std::vector<vpContour *>::iterator it = m_children.begin(); it != m_children.end(); ++it) {
268 (*it)->m_parent = NULL;
269 if (*it != NULL) {
270 delete *it;
271 *it = NULL;
272 }
273 }
274 }
275 else {
276 // Make the current contour the root contour
277 // to avoid problem when deleting
278 m_parent = NULL;
279 }
280
281 m_children.clear();
282 for (std::vector<vpContour *>::const_iterator it = other.m_children.begin(); it != other.m_children.end(); ++it) {
283 vpContour *copy = new vpContour(**it);
284 copy->m_parent = this;
285 m_children.push_back(copy);
286 }
287
288 return *this;
289 }
290
294 void setParent(vpContour *parent)
295 {
296 m_parent = parent;
297
298 if (parent != NULL) {
299 parent->m_children.push_back(this);
300 }
301 }
302};
303
313VISP_EXPORT void drawContours(vpImage<unsigned char> &I, const std::vector<std::vector<vpImagePoint> > &contours,
314 unsigned char grayValue = 255);
315
325VISP_EXPORT void drawContours(vpImage<vpRGBa> &I, const std::vector<std::vector<vpImagePoint> > &contours,
326 const vpColor &color);
327
339VISP_EXPORT void findContours(const vpImage<unsigned char> &I_original, vpContour &contours,
340 std::vector<std::vector<vpImagePoint> > &contourPts,
341 const vpContourRetrievalType &retrievalMode = vp::CONTOUR_RETR_TREE);
342} // namespace vp
343
344#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 ...
double get_j() const
double get_i() const
Definition of the vpImage class member functions.
Definition vpImage.h:135
unsigned int getWidth() const
Definition vpImage.h:242
unsigned int getHeight() const
Definition vpImage.h:184
static int modulo(int a, int n)
Definition vpMath.cpp:435
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)
VISP_EXPORT void drawContours(vpImage< unsigned char > &I, const std::vector< std::vector< vpImagePoint > > &contours, unsigned char grayValue=255)
vpContourRetrievalType
Definition vpContours.h:197
@ CONTOUR_RETR_LIST
Definition vpContours.h:200
@ CONTOUR_RETR_TREE
Definition vpContours.h:198
@ CONTOUR_RETR_EXTERNAL
Definition vpContours.h:201
vpContourType
Definition vpContours.h:188
@ CONTOUR_HOLE
Definition vpContours.h:190
@ CONTOUR_OUTER
Definition vpContours.h:189
vpContourType m_contourType
Contour type.
Definition vpContours.h:212
virtual ~vpContour()
Definition vpContours.h:247
void setParent(vpContour *parent)
Definition vpContours.h:294
vpContour * m_parent
Parent contour.
Definition vpContours.h:214
vpContour(const vpContourType &type)
Definition vpContours.h:226
std::vector< vpImagePoint > m_points
Vector of points belonging to the contour.
Definition vpContours.h:216
vpContour & operator=(const vpContour &other)
Definition vpContours.h:261
std::vector< vpContour * > m_children
Children contour.
Definition vpContours.h:210
vpContour(const vpContour &contour)
Definition vpContours.h:231