Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpContours.h
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2024 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 */
64
69
70#ifndef VP_CONTOURS_H
71#define VP_CONTOURS_H
72
73#include <visp3/core/vpColor.h>
74#include <visp3/core/vpImage.h>
75#include <visp3/core/vpPolygon.h>
76
77namespace VISP_NAMESPACE_NAME
78{
79
95
100{
101public:
104
106 int m_dirx[8];
107
109 int m_diry[8];
110
115 {
116 const unsigned int dir0 = 0, dir1 = 1, dir2 = 2, dir3 = 3;
117 const unsigned int dir4 = 4, dir5 = 5, dir6 = 6, dir7 = 7;
119
120 m_dirx[dir0] = 0;
121 m_dirx[dir1] = 1;
122 m_dirx[dir2] = 1;
123 m_dirx[dir3] = 1;
124 m_dirx[dir4] = 0;
125 m_dirx[dir5] = -1;
126 m_dirx[dir6] = -1;
127 m_dirx[dir7] = -1;
128
129 m_diry[dir0] = -1;
130 m_diry[dir1] = -1;
131 m_diry[dir2] = 0;
132 m_diry[dir3] = 1;
133 m_diry[dir4] = 1;
134 m_diry[dir5] = 1;
135 m_diry[dir6] = 0;
136 m_diry[dir7] = -1;
137 }
138
144 {
145 vpDirection direction;
146 int directionSize = LAST_DIRECTION;
147 direction.m_direction = static_cast<vpDirectionType>((static_cast<int>(m_direction) + 1) % directionSize);
148
149 return direction;
150 }
151
157 {
158 vpDirection direction;
159 int directionSize = static_cast<int>(LAST_DIRECTION);
160 int idx = VISP_NAMESPACE_ADDRESSING vpMath::modulo(static_cast<int>(m_direction) - 1, directionSize);
161 direction.m_direction = static_cast<vpDirectionType>(idx);
162
163 return direction;
164 }
165
172 VISP_NAMESPACE_ADDRESSING vpImagePoint active(const VISP_NAMESPACE_ADDRESSING vpImage<int> &I, const VISP_NAMESPACE_ADDRESSING vpImagePoint &point)
173 {
174 int yy = static_cast<int>(point.get_i() + m_diry[static_cast<int>(m_direction)]);
175 int xx = static_cast<int>(point.get_j() + m_dirx[static_cast<int>(m_direction)]);
176
177 if ((xx < 0) || (xx >= static_cast<int>(I.getWidth())) || (yy < 0) || (yy >= static_cast<int>(I.getHeight()))) {
178 return VISP_NAMESPACE_ADDRESSING vpImagePoint(-1, -1);
179 }
180
181 int pixel = I[yy][xx];
182 return pixel != 0 ? VISP_NAMESPACE_ADDRESSING vpImagePoint(yy, xx) : VISP_NAMESPACE_ADDRESSING vpImagePoint(-1, -1);
183 }
184};
185
194
205
210{
212 std::vector<vpContour *> m_children;
218 std::vector<VISP_NAMESPACE_ADDRESSING vpImagePoint> m_points;
219
224
228 VP_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
319VISP_EXPORT void drawContours(VISP_NAMESPACE_ADDRESSING vpImage<unsigned char> &I, const std::vector<std::vector<VISP_NAMESPACE_ADDRESSING vpImagePoint> > &contours,
320 unsigned char grayValue = 255);
321
331VISP_EXPORT void drawContours(VISP_NAMESPACE_ADDRESSING vpImage<VISP_NAMESPACE_ADDRESSING vpRGBa> &I, const std::vector<std::vector<VISP_NAMESPACE_ADDRESSING vpImagePoint> > &contours,
332 const VISP_NAMESPACE_ADDRESSING vpColor &color);
333
345VISP_EXPORT void findContours(const VISP_NAMESPACE_ADDRESSING vpImage<unsigned char> &I_original, vpContour &contours,
346 std::vector<std::vector<VISP_NAMESPACE_ADDRESSING vpImagePoint> > &contourPts,
347 const vpContourRetrievalType &retrievalMode = CONTOUR_RETR_TREE);
348
349} // namespace
350
351#endif
VISP_NAMESPACE_ADDRESSING vpImagePoint active(const VISP_NAMESPACE_ADDRESSING vpImage< int > &I, const VISP_NAMESPACE_ADDRESSING vpImagePoint &point)
Definition vpContours.h:172
vpDirectionType m_direction
Direction.
Definition vpContours.h:103
int m_diry[8]
Pixel increment along y to reach a given direction.
Definition vpContours.h:109
int m_dirx[8]
Pixel increment along x to reach a given direction.
Definition vpContours.h:106
Class to define RGB colors available for display functionalities.
Definition vpColor.h:157
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition of the vpImage class member functions.
Definition vpImage.h:131
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:177
VISP_EXPORT void findContours(const VISP_NAMESPACE_ADDRESSING vpImage< unsigned char > &I_original, vpContour &contours, std::vector< std::vector< VISP_NAMESPACE_ADDRESSING vpImagePoint > > &contourPts, const vpContourRetrievalType &retrievalMode=CONTOUR_RETR_TREE)
VISP_EXPORT void drawContours(VISP_NAMESPACE_ADDRESSING vpImage< unsigned char > &I, const std::vector< std::vector< VISP_NAMESPACE_ADDRESSING vpImagePoint > > &contours, unsigned char grayValue=255)
@ SOUTH_EAST
South-East direction.
Definition vpContours.h:88
@ EAST
East direction.
Definition vpContours.h:87
@ SOUTH_WEST
South-West direction.
Definition vpContours.h:90
@ NORTH_EAST
North-East direction.
Definition vpContours.h:86
@ SOUTH
South direction.
Definition vpContours.h:89
@ NORTH
North direction.
Definition vpContours.h:85
@ LAST_DIRECTION
Number of possible directions.
Definition vpContours.h:93
@ NORTH_WEST
North-West direction.
Definition vpContours.h:92
@ WEST
West direction.
Definition vpContours.h:91
std::vector< vpContour * > m_children
Children contour.
Definition vpContours.h:212
VP_EXPLICIT vpContour(const vpContourType &type)
Definition vpContours.h:228
std::vector< VISP_NAMESPACE_ADDRESSING vpImagePoint > m_points
Vector of points belonging to the contour.
Definition vpContours.h:218
vpContourType m_contourType
Contour type.
Definition vpContours.h:214
vpContour * m_parent
Parent contour.
Definition vpContours.h:216
vpContour(const vpContour &contour)
Definition vpContours.h:233
void setParent(vpContour *parent)
Definition vpContours.h:300
vpContour & operator=(const vpContour &other)
Definition vpContours.h:265