71 #include <visp3/imgproc/vpImgproc.h>
81 if (std::fabs(from.
get_i() - to.
get_i()) < std::numeric_limits<double>::epsilon()) {
83 direction.m_direction = EAST;
86 direction.m_direction = WEST;
90 if (std::fabs(from.
get_j() - to.
get_j()) < std::numeric_limits<double>::epsilon()) {
91 direction.m_direction = SOUTH;
94 direction.m_direction = SOUTH_EAST;
97 direction.m_direction = SOUTH_WEST;
101 if (std::fabs(from.
get_j() - to.
get_j()) < std::numeric_limits<double>::epsilon()) {
102 direction.m_direction = NORTH;
105 direction.m_direction = NORTH_EAST;
108 direction.m_direction = NORTH_WEST;
117 vpDirection direction;
122 bool b = checked[
static_cast<int>(direction.m_direction)];
124 if ((point.
get_i() < 0) || (point.
get_j() < 0)) {
128 unsigned int i =
static_cast<unsigned int>(point.
get_i());
129 unsigned int j =
static_cast<unsigned int>(point.
get_j());
131 return (I[i][j] != 0) && ((
static_cast<unsigned int>(point.
get_j()) == (I.
getWidth() - 1)) || b);
138 unsigned int i =
static_cast<unsigned int>(point.
get_i());
139 unsigned int j =
static_cast<unsigned int>(point.
get_j());
141 if (crossesEastBorder(I, checked, point)) {
144 else if (I[i][j] == 1) {
153 if (!fromTo(ij, i2j2, dir)) {
157 vpDirection trace = dir.clockwise();
161 while (trace.m_direction != dir.m_direction) {
164 if ((activePixel.
get_i() >= 0) && (activePixel.
get_j() >= 0)) {
169 trace = trace.clockwise();
172 if ((i1j1.get_i() < 0) || (i1j1.get_j() < 0)) {
180 bool checked[8] = {
false,
false,
false,
false,
false,
false,
false,
false };
183 if (!fromTo(i3j3, i2j2, dir)) {
187 trace = dir.counterClockwise();
191 for (
int cpt = 0; cpt < 8; ++cpt) {
192 checked[cpt] =
false;
196 i4j4 = trace.active(I, i3j3);
197 if ((i4j4.get_i() >= 0) && (i4j4.get_j() >= 0)) {
201 checked[
static_cast<int>(trace.m_direction)] =
true;
202 trace = trace.counterClockwise();
205 addContourPoint(I, border, i3j3, checked, nbd);
207 if ((i4j4 == ij) && (i3j3 == i1j1)) {
218 bool isOuterBorderStart(
const vpImage<int> &I,
unsigned int i,
unsigned int j)
220 return ((I[i][j] == 1) && ((j == 0) || (I[i][j - 1] == 0)));
223 bool isHoleBorderStart(
const vpImage<int> &I,
unsigned int i,
unsigned int j)
225 return ((I[i][j] >= 1) && ((j == (I.
getWidth() - 1)) || (I[i][j + 1] == 0)));
230 if ((level > 0) && (level < std::numeric_limits<int>::max())) {
235 contour_list.
m_children.push_back(contour_node);
238 std::vector<vp::vpContour *>::const_iterator root_m_children_end = root.
m_children.end();
239 for (std::vector<vp::vpContour *>::const_iterator it = root.
m_children.begin(); it != root_m_children_end; ++it) {
240 getContoursList(**it, level + 1, contour_list);
253 std::vector<std::vector<vpImagePoint> >::const_iterator contours_end = contours.end();
254 for (std::vector<std::vector<vpImagePoint> >::const_iterator it1 = contours.begin(); it1 != contours_end; ++it1) {
255 std::vector<vpImagePoint>::const_iterator it1_end = it1->end();
256 for (std::vector<vpImagePoint>::const_iterator it2 = it1->begin(); it2 != it1_end; ++it2) {
257 unsigned int i =
static_cast<unsigned int>(it2->get_i());
258 unsigned int j =
static_cast<unsigned int>(it2->get_j());
270 std::vector<std::vector<vpImagePoint> >::const_iterator contours_end = contours.end();
271 for (std::vector<std::vector<vpImagePoint> >::const_iterator it1 = contours.begin(); it1 != contours_end; ++it1) {
272 std::vector<vpImagePoint>::const_iterator it1_end = it1->end();
273 for (std::vector<vpImagePoint>::const_iterator it2 = it1->begin(); it2 != it1_end; ++it2) {
274 unsigned int i =
static_cast<unsigned int>(it2->get_i());
275 unsigned int j =
static_cast<unsigned int>(it2->get_j());
276 I[i][j] =
vpRGBa(color.
R, color.
G, color.
B);
284 if (I_original.
getSize() == 0) {
295 unsigned int i_width = I.
getWidth();
296 unsigned int i_original_width = I_original.
getWidth();
298 for (
unsigned int i = 0; i < i_height; ++i) {
299 if ((i == 0) || (i == (i_height - 1))) {
300 memset(I.
bitmap, 0,
sizeof(
int) * i_width);
304 for (
unsigned int j = 0; j < i_original_width; ++j) {
305 I[i][j + 1] = I_original[i - 1][j];
307 I[i][i_width - 1] = 0;
321 std::map<int, vpContour *> borderMap;
322 borderMap[lnbd] = root;
324 for (
unsigned int i = 0; i < i_height; ++i) {
327 for (
unsigned int j = 0; j < i_width; ++j) {
330 bool isOuter = isOuterBorderStart(I, i, j);
331 bool isHole = isHoleBorderStart(I, i, j);
333 if (isOuter || isHole) {
343 borderPrime = borderMap[lnbd];
367 borderPrime = borderMap[lnbd];
387 followBorder(I, ij, from, border, nbd);
397 contourPts.push_back(border->
m_points);
400 borderMap[nbd] = border;
404 if ((fji != 0) && (fji != 1)) {
405 lnbd = std::abs(fji);
414 std::vector<vpContour *>::iterator contours_m_children_end = contours.
m_children.end();
415 for (std::vector<vpContour *>::iterator it = contours.
m_children.begin(); it != contours_m_children_end; ++it) {
416 (*it)->m_parent =
nullptr;
417 if (*it !=
nullptr) {
428 std::vector<vpContour *>::iterator root_m_children_end = root->
m_children.end();
429 for (std::vector<vpContour *>::const_iterator it = root->
m_children.begin(); it != root_m_children_end; ++it) {
431 std::vector<vpContour *> children_copy = (*it)->m_children;
433 (*it)->m_children.clear();
437 (*it)->m_children = children_copy;
439 size_t contours_m_children_size = contours.
m_children.size();
440 for (
size_t i = 0; i < contours_m_children_size; ++i) {
443 contourPts.push_back((*it)->m_points);
447 getContoursList(*root, 0, contours);
450 std::vector<vpContour *>::iterator contours_m_children_end = contours.
m_children.end();
451 for (std::vector<vpContour *>::iterator it = contours.
m_children.begin(); it != contours_m_children_end; ++it) {
452 (*it)->m_parent = &contours;
Class to define RGB colors available for display functionalities.
error that can be emitted by ViSP classes.
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.
unsigned int getWidth() const
unsigned int getSize() const
Type * bitmap
points toward the bitmap
unsigned int getHeight() const
unsigned char B
Blue component.
unsigned char R
Red component.
unsigned char G
Green component.
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)
vpContourType m_contourType
Contour type.
void setParent(vpContour *parent)
vpContour * m_parent
Parent contour.
std::vector< vpImagePoint > m_points
Vector of points belonging to the contour.
std::vector< vpContour * > m_children
Children contour.