Beatmup
flood_fill.h
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2019, lnstadrum
4 
5  This program 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 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 #include "../geometry.h"
21 #include "../parallelism.h"
22 #include "../bitmap/abstract_bitmap.h"
23 #include "../contours/contours.h"
24 #include <vector>
25 
26 namespace Beatmup {
27 
28  /**
29  Flood fill algorithm implementation.
30  Discovers areas of similar colors up to a tolerance threshold around given positions (seeds) in the input image.
31  These areas are filled with white color in another image (output). If the output bitmap is a binary mask,
32  corresponding pixels are set to `1`. The rest of the output image remains unchanged.
33  Optionally, computes contours around the discovered areas and stores the contour positions.
34  Also optionally, applies post-processing by dilating or eroding the discovered regions in the output image.
35  */
36  class FloodFill : public AbstractTask, private BitmapContentLock {
37  public:
38  /**
39  Morphological postprocessing operation applied to discovered connected components
40  */
42  NONE = 0, //!< no postprocessing
43  DILATE, //!< apply a dilatation
44  ERODE //!< apply an erosion
45  };
46 
47  protected:
49  *input, //!< input bitmap
50  *output, //!< resulting mask
51  *ignoredSeeds; //!< 1-bit bitmap storing flags marking used pixels
52 
53  IntPoint maskPos; //!< left-top corner of the mask to compute over the input bitmap
54  IntRectangle bounds; //!< mask bounds
55  std::mutex access; //!< access control assuring internal thread safety
56  std::vector<IntPoint> seeds; //!< set of starting points (seed points)
57  std::vector<IntegerContour2D*> contours; //!< contours to store borders for each seed
58  BorderMorphology borderMorphology; //!< border morphological postprocessing
59 
60  float tolerance; //!< intensity tolerance value
61  float
64  bool computeContours; //!< if `true`, border contours will be computed per each seed
65 
66  public:
67  FloodFill();
68  ~FloodFill();
69 
70  /**
71  Returns input bitmap (null if not set yet)
72  */
73  const AbstractBitmap* getInput() const { return input; }
74 
75  /**
76  Returns output bitmap (null if not set yet)
77  */
78  const AbstractBitmap* getOutput() const { return output; }
79 
80  /**
81  Returns bounding box of the computed mask
82  */
83  IntRectangle getBounds() const { return bounds; }
84 
85  /**
86  Returns number of detected contours
87  */
88  int getContourCount() const { return contours.size(); }
89 
90  /**
91  Returns a contour by index if computeContours was `true`, throws an exception otherwise
92  */
93  const IntegerContour2D& getContour(int contourIndex) const;
94 
95  /**
96  Sets the input bitmap
97  */
98  void setInput(AbstractBitmap*);
99 
100  /**
101  Specifies the bitmap to put the resulting mask to
102  */
103  void setOutput(AbstractBitmap*);
104 
105  /**
106  Specifies left-top corner position of the mask to compute inside the input bitmap
107  */
108  void setMaskPos(const IntPoint&);
109 
110  /**
111  Specifies a set of seeds (starting points)
112  */
113  void setSeeds(const IntPoint seeds[], int seedCount);
114  void setSeeds(const int seedsXY[], int seedCount);
115 
116  /**
117  Sets the intensity tolerance threshold used to decide on similarity of neighboring pixels.
118  */
119  void setTolerance(float);
120 
121  /**
122  Returns yjr intensity tolerance threshold.
123  */
124  inline float getTolerance() const { return tolerance; };
125 
126  /**
127  Specifies a morphological operation to apply to the mask border.
128  \param operation A postprocessing operation
129  \param holdRadius Erosion/dilation hold radius (output values set to 1)
130  \param releaseRadius Erosion/dilation radius of transition from 1 to 0
131  */
132  void setBorderPostprocessing(BorderMorphology operation, float holdRadius, float releaseRadius);
133 
134  /**
135  Enables or disables contours computation.
136  */
137  void setComputeContours(bool);
138 
139  ThreadIndex getMaxThreads() const;
140  virtual bool process(TaskThread& thread) final;
141  virtual void beforeProcessing(ThreadIndex threadCount, ProcessingTarget target, GraphicPipeline* gpu) final;
142  virtual void afterProcessing(ThreadIndex threadCount, GraphicPipeline* gpu, bool aborted) final;
143  };
144 
145 }
A very basic class for any image.
Task: an operation that can be executed by multiple threads in parallel.
Definition: parallelism.h:90
Makes sure the bitmap content is accessible within an image processing task.
Definition: content_lock.h:34
Flood fill algorithm implementation.
Definition: flood_fill.h:36
std::mutex access
access control assuring internal thread safety
Definition: flood_fill.h:55
void setSeeds(const IntPoint seeds[], int seedCount)
Specifies a set of seeds (starting points)
Definition: flood_fill.cpp:56
IntRectangle getBounds() const
Returns bounding box of the computed mask.
Definition: flood_fill.h:83
BorderMorphology
Morphological postprocessing operation applied to discovered connected components.
Definition: flood_fill.h:41
@ DILATE
apply a dilatation
Definition: flood_fill.h:43
@ NONE
no postprocessing
Definition: flood_fill.h:42
@ ERODE
apply an erosion
Definition: flood_fill.h:44
const AbstractBitmap * getInput() const
Returns input bitmap (null if not set yet)
Definition: flood_fill.h:73
AbstractBitmap * ignoredSeeds
1-bit bitmap storing flags marking used pixels
Definition: flood_fill.h:51
BorderMorphology borderMorphology
border morphological postprocessing
Definition: flood_fill.h:58
void setComputeContours(bool)
Enables or disables contours computation.
Definition: flood_fill.cpp:70
std::vector< IntegerContour2D * > contours
contours to store borders for each seed
Definition: flood_fill.h:57
void setInput(AbstractBitmap *)
Sets the input bitmap.
Definition: flood_fill.cpp:41
AbstractBitmap * output
resulting mask
Definition: flood_fill.h:50
void setBorderPostprocessing(BorderMorphology operation, float holdRadius, float releaseRadius)
Specifies a morphological operation to apply to the mask border.
Definition: flood_fill.cpp:166
void setMaskPos(const IntPoint &)
Specifies left-top corner position of the mask to compute inside the input bitmap.
Definition: flood_fill.cpp:51
const IntegerContour2D & getContour(int contourIndex) const
Returns a contour by index if computeContours was true, throws an exception otherwise.
Definition: flood_fill.cpp:173
virtual void afterProcessing(ThreadIndex threadCount, GraphicPipeline *gpu, bool aborted) final
Instruction called after the task is executed.
Definition: flood_fill.cpp:152
float getTolerance() const
Returns yjr intensity tolerance threshold.
Definition: flood_fill.h:124
IntPoint maskPos
left-top corner of the mask to compute over the input bitmap
Definition: flood_fill.h:53
virtual void beforeProcessing(ThreadIndex threadCount, ProcessingTarget target, GraphicPipeline *gpu) final
Instruction called before the task is executed.
Definition: flood_fill.cpp:136
std::vector< IntPoint > seeds
set of starting points (seed points)
Definition: flood_fill.h:56
void setTolerance(float)
Sets the intensity tolerance threshold used to decide on similarity of neighboring pixels.
Definition: flood_fill.cpp:161
float tolerance
intensity tolerance value
Definition: flood_fill.h:60
AbstractBitmap * input
input bitmap
Definition: flood_fill.h:49
const AbstractBitmap * getOutput() const
Returns output bitmap (null if not set yet)
Definition: flood_fill.h:78
IntRectangle bounds
mask bounds
Definition: flood_fill.h:54
virtual bool process(TaskThread &thread) final
Executes the task on CPU within a given thread.
Definition: flood_fill.cpp:80
int getContourCount() const
Returns number of detected contours.
Definition: flood_fill.h:88
bool computeContours
if true, border contours will be computed per each seed
Definition: flood_fill.h:64
void setOutput(AbstractBitmap *)
Specifies the bitmap to put the resulting mask to.
Definition: flood_fill.cpp:46
ThreadIndex getMaxThreads() const
Gives the upper limint on the number of threads the task may be performed by.
Definition: flood_fill.cpp:75
Internal low-level GPU control API.
Definition: pipeline.h:33
A sequence of integer-valued 2D points.
Definition: contours.h:33
Thread executing tasks.
Definition: parallelism.h:154
unsigned char ThreadIndex
number of threads / thread index
Definition: parallelism.h:68
ProcessingTarget
Definition: basic_types.h:55