Visual Servoing Platform
version 3.7.0
Toggle main menu visibility
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
77
namespace
VISP_NAMESPACE_NAME
78
{
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
class
vpDirection
100
{
101
public
:
103
vpDirectionType
m_direction
;
104
106
int
m_dirx
[8];
107
109
int
m_diry
[8];
110
114
vpDirection
()
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;
118
m_direction
=
NORTH
;
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
143
vpDirection
clockwise
()
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
156
vpDirection
counterClockwise
()
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
189
typedef
enum
190
{
191
CONTOUR_OUTER
,
192
CONTOUR_HOLE
193
}
vpContourType
;
194
198
typedef
enum
199
{
200
CONTOUR_RETR_TREE
,
202
CONTOUR_RETR_LIST
,
203
CONTOUR_RETR_EXTERNAL
204
}
vpContourRetrievalType
;
205
209
struct
vpContour
210
{
212
std::vector<vpContour *>
m_children
;
214
vpContourType
m_contourType
;
216
vpContour
*
m_parent
;
218
std::vector<VISP_NAMESPACE_ADDRESSING vpImagePoint>
m_points
;
219
223
vpContour
() :
m_children
(),
m_contourType
(
CONTOUR_HOLE
),
m_parent
(nullptr),
m_points
() { }
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
265
vpContour
&
operator=
(
const
vpContour
&other)
266
{
267
m_contourType
= other.
m_contourType
;
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
(VISP_NAMESPACE_ADDRESSING
vpImage<unsigned char>
&I,
const
std::vector<std::vector<VISP_NAMESPACE_ADDRESSING vpImagePoint> > &contours,
320
unsigned
char
grayValue = 255);
321
331
VISP_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
345
VISP_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_NAME::vpDirection::clockwise
vpDirection clockwise()
Definition
vpContours.h:143
VISP_NAMESPACE_NAME::vpDirection::active
VISP_NAMESPACE_ADDRESSING vpImagePoint active(const VISP_NAMESPACE_ADDRESSING vpImage< int > &I, const VISP_NAMESPACE_ADDRESSING vpImagePoint &point)
Definition
vpContours.h:172
VISP_NAMESPACE_NAME::vpDirection::vpDirection
vpDirection()
Definition
vpContours.h:114
VISP_NAMESPACE_NAME::vpDirection::counterClockwise
vpDirection counterClockwise()
Definition
vpContours.h:156
VISP_NAMESPACE_NAME::vpDirection::m_direction
vpDirectionType m_direction
Direction.
Definition
vpContours.h:103
VISP_NAMESPACE_NAME::vpDirection::m_diry
int m_diry[8]
Pixel increment along y to reach a given direction.
Definition
vpContours.h:109
VISP_NAMESPACE_NAME::vpDirection::m_dirx
int m_dirx[8]
Pixel increment along x to reach a given direction.
Definition
vpContours.h:106
vpColor
Class to define RGB colors available for display functionalities.
Definition
vpColor.h:157
vpImagePoint
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition
vpImagePoint.h:82
vpImage
Definition of the vpImage class member functions.
Definition
vpImage.h:131
vpMath::modulo
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_NAMESPACE_NAME::findContours
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_NAMESPACE_NAME::drawContours
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)
VISP_NAMESPACE_NAME
Definition
vpEigenConversion.h:44
VISP_NAMESPACE_NAME::vpDirectionType
vpDirectionType
Definition
vpContours.h:84
VISP_NAMESPACE_NAME::SOUTH_EAST
@ SOUTH_EAST
South-East direction.
Definition
vpContours.h:88
VISP_NAMESPACE_NAME::EAST
@ EAST
East direction.
Definition
vpContours.h:87
VISP_NAMESPACE_NAME::SOUTH_WEST
@ SOUTH_WEST
South-West direction.
Definition
vpContours.h:90
VISP_NAMESPACE_NAME::NORTH_EAST
@ NORTH_EAST
North-East direction.
Definition
vpContours.h:86
VISP_NAMESPACE_NAME::SOUTH
@ SOUTH
South direction.
Definition
vpContours.h:89
VISP_NAMESPACE_NAME::NORTH
@ NORTH
North direction.
Definition
vpContours.h:85
VISP_NAMESPACE_NAME::LAST_DIRECTION
@ LAST_DIRECTION
Number of possible directions.
Definition
vpContours.h:93
VISP_NAMESPACE_NAME::NORTH_WEST
@ NORTH_WEST
North-West direction.
Definition
vpContours.h:92
VISP_NAMESPACE_NAME::WEST
@ WEST
West direction.
Definition
vpContours.h:91
VISP_NAMESPACE_NAME::vpContourType
vpContourType
Definition
vpContours.h:190
VISP_NAMESPACE_NAME::CONTOUR_OUTER
@ CONTOUR_OUTER
Definition
vpContours.h:191
VISP_NAMESPACE_NAME::CONTOUR_HOLE
@ CONTOUR_HOLE
Definition
vpContours.h:192
VISP_NAMESPACE_NAME::vpContourRetrievalType
vpContourRetrievalType
Definition
vpContours.h:199
VISP_NAMESPACE_NAME::CONTOUR_RETR_LIST
@ CONTOUR_RETR_LIST
Definition
vpContours.h:202
VISP_NAMESPACE_NAME::CONTOUR_RETR_EXTERNAL
@ CONTOUR_RETR_EXTERNAL
Definition
vpContours.h:203
VISP_NAMESPACE_NAME::CONTOUR_RETR_TREE
@ CONTOUR_RETR_TREE
Definition
vpContours.h:200
VISP_NAMESPACE_NAME::vpContour
Definition
vpContours.h:210
VISP_NAMESPACE_NAME::vpContour::m_children
std::vector< vpContour * > m_children
Children contour.
Definition
vpContours.h:212
VISP_NAMESPACE_NAME::vpContour::~vpContour
virtual ~vpContour()
Definition
vpContours.h:250
VISP_NAMESPACE_NAME::vpContour::vpContour
VP_EXPLICIT vpContour(const vpContourType &type)
Definition
vpContours.h:228
VISP_NAMESPACE_NAME::vpContour::m_points
std::vector< VISP_NAMESPACE_ADDRESSING vpImagePoint > m_points
Vector of points belonging to the contour.
Definition
vpContours.h:218
VISP_NAMESPACE_NAME::vpContour::m_contourType
vpContourType m_contourType
Contour type.
Definition
vpContours.h:214
VISP_NAMESPACE_NAME::vpContour::vpContour
vpContour()
Definition
vpContours.h:223
VISP_NAMESPACE_NAME::vpContour::m_parent
vpContour * m_parent
Parent contour.
Definition
vpContours.h:216
VISP_NAMESPACE_NAME::vpContour::vpContour
vpContour(const vpContour &contour)
Definition
vpContours.h:233
VISP_NAMESPACE_NAME::vpContour::setParent
void setParent(vpContour *parent)
Definition
vpContours.h:300
VISP_NAMESPACE_NAME::vpContour::operator=
vpContour & operator=(const vpContour &other)
Definition
vpContours.h:265
modules
imgproc
include
visp3
imgproc
vpContours.h
Generated by
1.17.0