Image Component Library (ICL)
FloodFiller.h
Go to the documentation of this file.
1 /********************************************************************
2 ** Image Component Library (ICL) **
3 ** **
4 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld **
5 ** Neuroinformatics Group **
6 ** Website: www.iclcv.org and **
7 ** http://opensource.cit-ec.de/projects/icl **
8 ** **
9 ** File : ICLCV/src/ICLCV/FloodFiller.h **
10 ** Module : ICLCV **
11 ** Authors: Christof Elbrechter **
12 ** **
13 ** **
14 ** GNU LESSER GENERAL PUBLIC LICENSE **
15 ** This file may be used under the terms of the GNU Lesser General **
16 ** Public License version 3.0 as published by the **
17 ** **
18 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
19 ** included in the packaging of this file. Please review the **
20 ** following information to ensure the license requirements will **
21 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt **
22 ** **
23 ** The development of this software was supported by the **
24 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. **
25 ** The Excellence Cluster EXC 277 is a grant of the Deutsche **
26 ** Forschungsgemeinschaft (DFG) in the context of the German **
27 ** Excellence Initiative. **
28 ** **
29 ********************************************************************/
30 
31 #pragma once
32 
33 #include <ICLUtils/CompatMacros.h>
34 #include <ICLCore/Img.h>
35 
36 namespace icl{
37  namespace cv{
38 
40 
72  std::vector<utils::Point> futurePoints;
73 
75  utils::Rect prepare(const utils::Size &imageSize, const utils::Point &seed);
76 
77  public:
78 
80  struct ICLCV_API Result{
81  std::vector<utils::Point> pixels;
83  } result;
84 
86 
88  const Result &apply(const core::ImgBase *image, const utils::Point &seed, double referenceValue, double threshold);
89 
90 
92 
94  const Result &applyColor(const core::ImgBase *image, const utils::Point &seed,
95  double refR, double refG, double refB, double threshold);
96 
98 
100  template<class T, class Criterion>
101  inline const Result &applyGeneric(const core::Img<T> &image, const utils::Point &seed, Criterion crit){
102  utils::Rect r = prepare(image.getSize(),seed);
103 
104  utils::Point *n = futurePoints.data(); // next point to seed from
105  utils::Point *e = n; // end of stored seed points
106 
107  *e++ = seed;
108 
109  const core::Channel<T> im = image[0];
110  core::Channel8u ff = result.ffLUT[0];
111 
112  ff(seed.x,seed.y) = 255;
113 
114  while(n != e){
115  const int x = n->x;
116  const int y = n->y;
117  ++n;
118  if(crit(im(x,y))){
119  result.pixels.push_back(utils::Point(x,y));
120  #define ICL_DYNAMIC_FLOOD_FILLER_P(x,y) \
121  if(r.contains(x,y) && !ff(x,y)){ \
122  *e++ = utils::Point(x,y); \
123  ff(x,y) = 255; \
124  }
125 
129 
132 
136  #undef ICL_DYNAMIC_FLOOD_FILLER_P
137  }
138  }
139  return result;
140  }
141 
143 
146  template<class T, class Criterion3Channels>
147  inline const Result &applyColorGeneric(const core::Img<T> &image, const utils::Point &seed, Criterion3Channels crit){
148  utils::Rect r = prepare(image.getSize(),seed);
149 
150  utils::Point *n = futurePoints.data(); // next point to seed from
151  utils::Point *e = n; // end of stored seed points
152 
153  *e++ = seed;
154 
155  const core::Channel<T> im0 = image[0];
156  const core::Channel<T> im1 = image[1];
157  const core::Channel<T> im2 = image[2];
158 
159  core::Channel8u ff = result.ffLUT[0];
160 
161  ff(seed.x,seed.y) = 255;
162 
163  while(n != e){
164  const int x = n->x;
165  const int y = n->y;
166  ++n;
167  if(crit(im0(x,y),im1(x,y),im2(x,y))){
168  result.pixels.push_back(utils::Point(x,y));
169  #define ICL_DYNAMIC_FLOOD_FILLER_P(x,y) \
170  if(r.contains(x,y) && !ff(x,y)){ \
171  *e++ = utils::Point(x,y); \
172  ff(x,y) = 255; \
173  }
174 
178 
181 
185  #undef ICL_DYNAMIC_FLOOD_FILLER_P
186  }
187  }
188  return result;
189  }
190 
192  template<class T>
194  T val;
196  inline DefaultCriterion(T val, T thresh):val(val),thresh(thresh){}
197  inline bool operator()(const T &pix) const{
198  return ::abs(val-pix) < thresh;
199  }
200  };
201 
203  template<class T>
205  T refcol[3];
207 
208  inline ReferenceColorCriterion(T refr, T refg, T refb, T maxEuclDist):
209  maxSquaredEuklDist(maxEuclDist*maxEuclDist){
210  refcol[0] = refr;
211  refcol[1] = refg;
212  refcol[2] = refb;
213  }
214  inline bool operator()( const T &r, const T &g, const T &b) const{
215  return utils::sqr(r-refcol[0]) + utils::sqr(g-refcol[1]) + utils::sqr(b-refcol[2]) < maxSquaredEuklDist;
216  }
217  };
218  };
219 
221  // specialized
222  template<>
223  struct FloodFiller::ReferenceColorCriterion<icl8u>{
224  int refcol[3];
225  int maxSquaredEuklDist;
226 
227  inline ReferenceColorCriterion(icl8u refr, icl8u refg, icl8u refb, int maxEuclDist):
228  maxSquaredEuklDist(maxEuclDist*maxEuclDist){
229  refcol[0] = refr;
230  refcol[1] = refg;
231  refcol[2] = refb;
232  }
233  inline bool operator()( const icl8u &r, const icl8u &g, const icl8u &b) const{
234  return utils::sqr(int(r)-refcol[0]) + utils::sqr(int(g)-refcol[1]) + utils::sqr(int(b)-refcol[2]) < maxSquaredEuklDist;
235  }
236  };
240  } // namespace cv
241 }
242 
243 
result structure, returned by the 'apply' methods
Definition: FloodFiller.h:80
undocument this line if you encounter any issues!
Definition: Any.h:37
Ipp8u icl8u
8Bit unsigned integer type for the ICL
Definition: BasicTypes.h:64
bool operator()(const T &pix) const
Definition: FloodFiller.h:197
T maxSquaredEuklDist
Definition: FloodFiller.h:206
bool operator()(const T &r, const T &g, const T &b) const
Definition: FloodFiller.h:214
core::Img8u ffLUT
list of filled pixels
Definition: FloodFiller.h:82
const Result & applyColorGeneric(const core::Img< T > &image, const utils::Point &seed, Criterion3Channels crit)
generic floodfilling algorithm for 3-channel color images
Definition: FloodFiller.h:147
T refcol[3]
Definition: FloodFiller.h:205
ReferenceColorCriterion(T refr, T refg, T refb, T maxEuclDist)
Definition: FloodFiller.h:208
#define ICLCV_API
Definition: CompatMacros.h:177
ICLQt_API ImgQ abs(const ImgQ &image)
calls abs ( each pixel)
T thresh
Definition: FloodFiller.h:195
Size class of the ICL.
Definition: Size.h:61
Utility helper class for faster and more convenient access to single channel image data.
Definition: Channel.h:145
#define ICL_DYNAMIC_FLOOD_FILLER_P(x, y)
static T sqr(const T &x)
square template (faster than pow(x,2)
Definition: Macros.h:212
ICLQt_API ImgQ thresh(const ImgQ &image, float threshold)
performs an image binarisation for each channel with given threshold
ICLQt_API void pix(ImgQ &image, int x, int y)
draw a single pixel into an image
T val
Definition: FloodFiller.h:194
std::vector< utils::Point > pixels
Definition: FloodFiller.h:81
predefined criterion for simple reference-value based filling for 3-channel images
Definition: FloodFiller.h:204
Point class of the ICL used e.g. for the Images ROI offset.
Definition: Point.h:58
predefined criterion for simple reference-value based filling for 1-channel images
Definition: FloodFiller.h:193
Utility class for image flood filling.
Definition: FloodFiller.h:70
Rectangle class of the ICL used e.g. for the Images ROI-rect.
Definition: Rect.h:95
const Result & applyGeneric(const core::Img< T > &image, const utils::Point &seed, Criterion crit)
generic grayscale image floodfilling using an arbitrary filling criterion
Definition: FloodFiller.h:101
std::vector< utils::Point > futurePoints
internal list of to-be-processed points
Definition: FloodFiller.h:72
DefaultCriterion(T val, T thresh)
Definition: FloodFiller.h:196
ImgBase is the Image-Interface class that provides save access to underlying Img-template .
Definition: ImgBase.h:131
const utils::Size & getSize() const
returns the size of the images
Definition: ImgBase.h:477