Visual Servoing Platform  version 3.6.1 under development (2024-07-27)
vpFloodFill.cpp
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  * Flood fill algorithm.
32  */
33 /*
34  * Copyright (c) 2004-2007, Lode Vandevenne
35  *
36  * All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions are met:
40  *
41  * * Redistributions of source code must retain the above copyright notice,
42  * this list of conditions and the following disclaimer.
43  * * Redistributions in binary form must reproduce the above copyright
44  * notice, this list of conditions and the following disclaimer in the
45  * documentation and/or other materials provided with the distribution.
46  *
47  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
48  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
49  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
50  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
51  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
52  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
53  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
54  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
55  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
56  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
57  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58  *
59 */
60 
66 #include <queue>
67 #include <visp3/imgproc/vpImgproc.h>
68 
69 namespace VISP_NAMESPACE_NAME
70 {
71 
72 void floodFill(vpImage<unsigned char> &I, const vpImagePoint &seedPoint, const unsigned char oldValue,
73  const unsigned char newValue, const vpImageMorphology::vpConnexityType &connexity)
74 {
75  // Code from Lode Vandevenne tutorial.
76  // Naive modification for 8-connexity implementation
77  if ((oldValue == newValue) || (I.getSize() == 0)) {
78  return;
79  }
80 
81  std::queue<vpImagePoint> seed_queue;
82 
83  // Add initial seed point
84  seed_queue.push(seedPoint);
85 
86  while (!seed_queue.empty()) {
87  vpImagePoint current_seed = seed_queue.front();
88  seed_queue.pop();
89 
90  unsigned int x = static_cast<unsigned int>(current_seed.get_j());
91  unsigned int y = static_cast<unsigned int>(current_seed.get_i());
92  int x1 = static_cast<int>(x);
93 
94  // Find most left pixel
95  while ((x1 >= 0) && (I[y][x1] == oldValue)) {
96  --x1;
97  }
98  ++x1;
99 
100  bool spanAbove = false, spanBelow = false;
101 
102  while ((x1 < static_cast<int>(I.getWidth())) && (I[y][x1] == oldValue)) {
103  I[y][x1] = newValue;
104 
105  if ((!spanAbove) && (y > 0)) {
106  if (I[y - 1][x1] == oldValue) {
107  // North
108  spanAbove = true;
109  seed_queue.push(vpImagePoint(y - 1, x1));
110  }
111 
112  if (connexity != vpImageMorphology::CONNEXITY_4) {
113  if ((x1 > 0) && (I[y - 1][x1 - 1] == oldValue)) {
114  // North west
115  spanAbove = true;
116  seed_queue.push(vpImagePoint(y - 1, x1 - 1));
117  }
118  if ((x1 < (static_cast<int>(I.getWidth()) - 1)) && (I[y - 1][x1 + 1] == oldValue)) {
119  // North east
120  spanAbove = true;
121  seed_queue.push(vpImagePoint(y - 1, x1 + 1));
122  }
123  }
124  }
125  else if (spanAbove && (y > 0) && (I[y - 1][x1] != oldValue)) {
126  spanAbove = false;
127  }
128 
129  if ((!spanBelow) && (y < (I.getHeight() - 1))) {
130  if (I[y + 1][x1] == oldValue) {
131  // South
132  seed_queue.push(vpImagePoint(y + 1, x1));
133  spanBelow = true;
134  }
135 
136  if (connexity != vpImageMorphology::CONNEXITY_4) {
137  if ((x1 > 0) && (I[y + 1][x1 - 1] == oldValue)) {
138  // South west
139  seed_queue.push(vpImagePoint(y + 1, x1 - 1));
140  spanBelow = true;
141  }
142  if ((x1 < (static_cast<int>(I.getWidth()) - 1)) && (I[y + 1][x1 + 1] == oldValue)) {
143  // South east
144  seed_queue.push(vpImagePoint(y + 1, x1 + 1));
145  spanBelow = true;
146  }
147  }
148  }
149  else if (spanBelow && (y < (I.getHeight() - 1)) && (I[y + 1][x1] != oldValue)) {
150  spanBelow = false;
151  }
152 
153  // TODO: improve 8-connexity
154  if (connexity != vpImageMorphology::CONNEXITY_4) {
155  spanBelow = false;
156  spanAbove = false;
157  }
158 
159  ++x1;
160  }
161  }
162 }
163 
164 } // namespace
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
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getSize() const
Definition: vpImage.h:221
unsigned int getHeight() const
Definition: vpImage.h:181
VISP_EXPORT void floodFill(VISP_NAMESPACE_ADDRESSING vpImage< unsigned char > &I, const VISP_NAMESPACE_ADDRESSING vpImagePoint &seedPoint, const unsigned char oldValue, const unsigned char newValue, const VISP_NAMESPACE_ADDRESSING vpImageMorphology::vpConnexityType &connexity=VISP_NAMESPACE_ADDRESSING vpImageMorphology::CONNEXITY_4)