libcrn  3.9.5
A document image processing library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNBlock.h
Go to the documentation of this file.
1 /* Copyright 2006-2016 INSA Lyon, CoReNum, ENS-Lyon
2  *
3  * This file is part of libcrn.
4  *
5  * libcrn is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser 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  * libcrn 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 Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with libcrn. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * file: CRNBlock.h
19  * \author Yann LEYDIER, Asma OUJI
20  */
21 
22 #ifndef CRNBLOCK_HEADER
23 #define CRNBLOCK_HEADER
24 
25 #include <CRNSavable.h>
26 #include <CRNGeometry/CRNRect.h>
27 #include <CRNData/CRNMap.h>
28 #include <CRNData/CRNVector.h>
29 #include <CRNImage/CRNImage.h>
31 #include <CRNBlockPtr.h>
32 
33 namespace crn
34 {
35  namespace xml
36  {
37  class Document;
38  }
39  /****************************************************************************/
52  class Block: public Savable
53  {
54  public:
56  static SBlock New(const SImage &src, const String &nam = U"");
58  static SBlock New(const Path &ifname, const Path &xfname, const String &nam = U"");
60  virtual ~Block() override;
61 
62  Block(const Block&) = delete;
63  Block& operator=(const Block&) = delete;
64  Block(Block&&) = delete;
65  Block& operator=(Block&&) = delete;
66 
68  void SetFilename(const Path &nam) { setFilename(nam); }
69 
71  const Rect& GetAbsoluteBBox() const noexcept { return bbox; }
73  Rect GetRelativeBBox() const;
75  void SetAbsoluteBBox(const Rect &newbox);
77  void SetRelativeBBox(Rect newbox);
78 
80  WBlock GetParent() { return parent; }
82  WCBlock GetParent() const { return parent; }
84  WBlock GetTop();
86  const String& GetParentTree() const noexcept { return parenttree; }
88  bool IsParent(const Block &b) const;
89 
91  SBlock AddChildAbsolute(const String &tree, Rect clip);
93  SBlock AddChildAbsolute(const String &tree, Rect clip, const String &name);
95  SBlock AddChildAbsoluteAt(const String &tree, Rect clip, size_t pos);
97  SBlock AddChildAbsoluteAt(const String &tree, Rect clip, const String &name, size_t pos);
99  SBlock AddChildRelative(const String &tree, Rect clip);
101  SBlock AddChildRelative(const String &tree, Rect clip, const String &name);
103  SBlock AddChildRelativeAt(const String &tree, Rect clip, size_t pos);
105  SBlock AddChildRelativeAt(const String &tree, Rect clip, const String &name, size_t pos);
107  size_t GetNbChildren(const String &tree) const;
109  SBlock GetChild(const String &tree, size_t num);
111  SCBlock GetChild(const String &tree, size_t num) const;
113  SBlock GetChild(const String &tree, const String &name);
115  SCBlock GetChild(const String &tree, const String &name) const;
117  void RemoveChild(const String &tree, size_t num);
119  void RemoveChild(const String &tree, const String &name);
121  void RemoveChild(const String &tree, SBlock b);
123  void RemoveChildren(const String &tree, const std::set<SBlock> &toremove);
124 
126  std::vector<String> GetTreeNames() const;
128  bool HasTree(const String &tname) const;
130  void RemoveTree(const String &tname);
131 
133  bool Append(const Path &fname);
135  void Save(const Path &fname);
143  void Save() { Save(GetFilename()); }
144 
146  SImageRGB GetRGB();
148  SImageGray GetGray(bool create = true);
150  SImageBW GetBW(bool create = true);
152  SImageGradient GetGradient(bool create = true, double sigma = -1, size_t diffusemaxiter = 0, double diffusemaxdiv = std::numeric_limits<double>::max());
154  void ReloadImage();
156  void FlushAll(bool recursive = false);
158  void FlushRGB(bool recursive = false);
160  void FlushGray(bool recursive = false);
162  void FlushBW(bool recursive = false);
164  void FlushGradient(bool recursive = false);
166  void SubstituteRGB(const SImageRGB &img);
168  void SubstituteGray(const SImageGray &img);
170  void SubstituteBW(const SImageBW &img);
172  void SubstituteGradient(const SImageGradient &img);
173 
175  UImageIntGray ExtractCC(const String &tree);
176 
178  void FilterMinAnd(const String &tree, size_t minw, size_t minh);
180  void FilterMinOr(const String &tree, size_t minw, size_t minh);
182  void FilterMaxAnd(const String &tree, size_t maxw, size_t maxh);
184  void FilterMaxOr(const String &tree, size_t maxw, size_t maxh);
186  void FilterBorders(const String &tree, size_t margin);
188  void FilterWidthRatio(const String &tree, double ratio);
190  void FilterHeightRatio(const String &tree, double ratio);
192  bool MergeChildren(const String &tree, double overlap, ImageIntGray *imap = nullptr);
194  void MergeSiblings(const String &tree, size_t index1, size_t index2, ImageIntGray *imap = nullptr);
196  void MergeSiblings(const String &tree, Block &sb1, Block &sb2, ImageIntGray *imap = nullptr);
198  void GetTreeMeans(const String &tree, double *mwidth, double *mheight, double *marea) const;
199 
200 #include <CRNBlockIterator.h>
202  block_iterator BlockBegin(const String &tree);
204  block_iterator BlockEnd(const String &tree);
205 
207  const_block_iterator BlockBegin(const String &tree) const;
209  const_block_iterator BlockEnd(const String &tree) const;
210 
212  SVector GetTree(const String &name)
213  {
214  if (child->Find(name) == child->end()) return nullptr;
215  else return std::static_pointer_cast<Vector>(child->Get(name));
216  }
218  SCVector GetTree(const String &name) const
219  {
220  if (child->Find(name) == child->end()) return nullptr;
221  else return std::static_pointer_cast<const Vector>(child->Get(name));
222  }
223 
225  void SortTree(const String &name, Direction direction);
226 
230  pixel_iterator PixelBegin(const String &tree, size_t num) const;
232  pixel_iterator PixelBegin(const SBlock &b) const;
234  pixel_iterator PixelEnd(const String &tree, size_t num) const;
236  pixel_iterator PixelEnd(const SBlock &b) const;
237 
240  {
241  public:
243  masked_pixel_iterator() noexcept:pixel_iterator(),offsetx(0),offsety(0),value(0) {}
246  pixel_iterator(it),mask(it.mask),offsetx(it.offsetx),offsety(it.offsety),value(it.value)
247  {
248  if (mask)
249  if (mask->At(pos.X - offsetx, pos.Y - offsety) != value)
250  operator++();
251  }
253  masked_pixel_iterator(const Rect &r, const SImageBW &ibw, int ox, int oy, pixel::BW val = pixel::BWBlack) noexcept:
254  pixel_iterator(r),mask(ibw),offsetx(ox),offsety(oy),value(val)
255  {
256  if (mask->At(pos.X - offsetx, pos.Y - offsety) != value)
257  operator++();
258  }
260  virtual ~masked_pixel_iterator() override {}
263  {
265  mask = it.mask;
266  value = it.value;
267  offsetx = it.offsetx;
268  offsety = it.offsety;
269  return *this;
270  }
272  virtual const masked_pixel_iterator& operator++() noexcept override
273  {
275  while (valid)
276  {
277  if (mask->At(pos.X - offsetx, pos.Y - offsety) == value)
278  break;
280  }
281  return *this;
282  }
283  private:
284  SCImageBW mask;
285  int offsetx, offsety;
286  pixel::BW value;
287  };
289  masked_pixel_iterator MaskedPixelBegin(const String &tree, size_t num, pixel::BW mask_value = pixel::BWBlack);
291  masked_pixel_iterator MaskedPixelEnd(const String &tree, size_t num, pixel::BW mask_value = pixel::BWBlack);
293  masked_pixel_iterator MaskedPixelBegin(const SBlock &b, pixel::BW mask_value = pixel::BWBlack);
295  masked_pixel_iterator MaskedPixelEnd(const SBlock &b, pixel::BW mask_value = pixel::BWBlack);
296 
297  private:
299  Block(const SImage &src, const String &nam = U"");
301  Block(const Path &ifname, const Path &xfname, const String &nam = U"");
303  static SBlock create(const WBlock &par, const String &tree, const Rect &clip, const String &nam = U"");
305  Block(const WBlock &par, const String &tree, const Rect &clip, const String &nam = U"");
306 
308  void addToXml(xml::Document &parent);
310  void addToXml(xml::Element &parent);
312  void addTreeFromXml(xml::Element &bnode);
314  void openImage(void);
316  void refreshSources();
317 
318  SMap child;
320  SVector getChildList(const String &name);
321  WBlock self;
322  WBlock parent;
323  String parenttree;
325  Path imagefilename;
326  bool image_is_open;
327  SImageRGB srcRGB;
329  SImageRGB get_srcRGB(void);
330  SImageGray srcGray;
332  SImageGray get_srcGray(void);
333  SImageBW srcBW;
335  SImageBW get_srcBW(void);
336  SImageGradient srcGradient;
338  SImageGradient get_srcGradient(void);
339  Rect bbox;
340  SImageRGB buffRGB;
341  SImageGray buffGray;
342  SImageBW buffBW;
343  SImageGradient buffGradient;
344  double grad_sigma;
345  size_t grad_diffusemaxiter;
346  double grad_diffusemaxdiv;
347  };
348 }
349 #endif
block_iterator BlockBegin(const String &tree)
Returns an iterator on the first block of a tree.
Definition: CRNBlock.cpp:334
bool Append(const Path &fname)
Appends child trees from a file.
Definition: CRNBlock.cpp:1079
virtual const iterator & operator++() noexcept
Definition: CRNRect.cpp:483
void Save()
Saves the child trees into the default file.
Definition: CRNBlock.h:143
Abstract class for images.
Definition: CRNImage.h:141
SBlock AddChildRelativeAt(const String &tree, Rect clip, size_t pos)
Adds a child to current block.
Definition: CRNBlock.cpp:726
virtual ~masked_pixel_iterator() override
Destructor.
Definition: CRNBlock.h:260
SVector GetTree(const String &name)
Returns a list of children. Can be used with CRN_FOREACH.
Definition: CRNBlock.h:212
void FilterMaxOr(const String &tree, size_t maxw, size_t maxh)
Filters a child tree.
Definition: CRNBlock.cpp:2163
void FilterHeightRatio(const String &tree, double ratio)
Filters a child tree.
Definition: CRNBlock.cpp:2244
void FlushGray(bool recursive=false)
Frees the local gray buffer.
Definition: CRNBlock.cpp:1363
SBlock AddChildRelative(const String &tree, Rect clip)
Adds a child to current block.
Definition: CRNBlock.cpp:670
Iterator on the blocks of a child tree.
XML element.
Definition: CRNXml.h:135
void RemoveChildren(const String &tree, const std::set< SBlock > &toremove)
Removes a set of blocks from a child tree.
Definition: CRNBlock.cpp:2058
void FilterBorders(const String &tree, size_t margin)
Filters a child tree.
Definition: CRNBlock.cpp:2187
iterator & operator=(const iterator &)=default
complex base abstract class
Definition: CRNSavable.h:58
block_iterator BlockEnd(const String &tree)
Returns an iterator after the last block of a tree.
Definition: CRNBlock.cpp:354
void FilterMinOr(const String &tree, size_t minw, size_t minh)
Filters a child tree.
Definition: CRNBlock.cpp:2113
void SetAbsoluteBBox(const Rect &newbox)
Sets the absolute bounding box of the block.
Definition: CRNBlock.cpp:254
std::vector< String > GetTreeNames() const
Gets the list of the tree names.
Definition: CRNBlock.cpp:986
WBlock GetTop()
Gets a reference to the topmost parent of the block.
Definition: CRNBlock.cpp:2480
void SetFilename(const Path &nam)
Sets the default filename of the block.
Definition: CRNBlock.h:68
XML document.
Definition: CRNXml.h:429
WBlock GetParent()
Gets a reference to the parent of the block.
Definition: CRNBlock.h:80
void MergeSiblings(const String &tree, size_t index1, size_t index2, ImageIntGray *imap=nullptr)
Merges two subblocks.
Definition: CRNBlock.cpp:2385
Direction
An enumeration of directions.
Definition: CRNMath.h:122
virtual ~Block() override
Destructor.
Definition: CRNBlock.cpp:516
void FilterMinAnd(const String &tree, size_t minw, size_t minh)
Filters a child tree.
Definition: CRNBlock.cpp:2080
bool IsParent(const Block &b) const
Checks if a block is a parent of current.
Definition: CRNBlock.cpp:2495
A UTF32 character string class.
Definition: CRNString.h:61
SImageBW GetBW(bool create=true)
Returns a pointer to the local b&w buffer.
Definition: CRNBlock.cpp:932
pixel_iterator PixelEnd(const String &tree, size_t num) const
Returns an iterator after the last pixel of the block.
Definition: CRNBlock.cpp:2558
bool MergeChildren(const String &tree, double overlap, ImageIntGray *imap=nullptr)
Merges overlapping children in a tree.
Definition: CRNBlock.cpp:2279
void SubstituteGradient(const SImageGradient &img)
Substitutes the Gradient buffer with a new one.
Definition: CRNBlock.cpp:1525
SImageGray GetGray(bool create=true)
Returns a pointer to the local gray buffer.
Definition: CRNBlock.cpp:862
static SBlock New(const SImage &src, const String &nam=U"")
Top block creator.
Definition: CRNBlock.cpp:46
A block.
Definition: CRNBlock.h:52
crn::Point2DInt pos
Definition: CRNRect.h:741
virtual const masked_pixel_iterator & operator++() noexceptoverride
Go to next pixel in mask.
Definition: CRNBlock.h:272
const Rect & GetAbsoluteBBox() const noexcept
Gets the absolute bounding box of the block.
Definition: CRNBlock.h:71
A convenience class for file paths.
Definition: CRNPath.h:39
masked_pixel_iterator MaskedPixelBegin(const String &tree, size_t num, pixel::BW mask_value=pixel::BWBlack)
Returns a masked iterator on the first pixel of the block.
Definition: CRNBlock.cpp:2600
value_type X
Definition: CRNPoint2D.h:63
masked_pixel_iterator(const Rect &r, const SImageBW &ibw, int ox, int oy, pixel::BW val=pixel::BWBlack) noexcept
Constructor.
Definition: CRNBlock.h:253
SBlock AddChildAbsoluteAt(const String &tree, Rect clip, size_t pos)
Adds a child to current block.
Definition: CRNBlock.cpp:602
void SortTree(const String &name, Direction direction)
Sorts a child tree.
Definition: CRNBlock.cpp:2691
void FlushBW(bool recursive=false)
Frees the local b&w buffer.
Definition: CRNBlock.cpp:1385
Data vector class.
Definition: CRNVector.h:42
Rect GetRelativeBBox() const
Gets the relative bounding box of the block.
Definition: CRNBlock.cpp:316
SImageGradient GetGradient(bool create=true, double sigma=-1, size_t diffusemaxiter=0, double diffusemaxdiv=std::numeric_limits< double >::max())
Returns a pointer to the local gradient buffer.
Definition: CRNBlock.cpp:1205
bool BW
Definition: CRNPixel.h:40
const Path & GetFilename() const noexcept
Returns the file name of the object.
Definition: CRNSavable.h:116
SBlock GetChild(const String &tree, size_t num)
Gets a block of a child tree.
Definition: CRNBlock.cpp:1897
void SubstituteBW(const SImageBW &img)
Substitutes the BW buffer with a new one.
Definition: CRNBlock.cpp:1495
void RemoveChild(const String &tree, size_t num)
Removes a block of a child tree.
Definition: CRNBlock.cpp:1995
void setFilename(const Path &fname)
Overwrites the filename.
Definition: CRNSavable.h:119
void FlushGradient(bool recursive=false)
Frees the local gradient buffer.
Definition: CRNBlock.cpp:1407
void FilterMaxAnd(const String &tree, size_t maxw, size_t maxh)
Filters a child tree.
Definition: CRNBlock.cpp:2138
value_type Y
Definition: CRNPoint2D.h:63
void GetTreeMeans(const String &tree, double *mwidth, double *mheight, double *marea) const
Returns the mean width, height and area of the subblocks.
Definition: CRNBlock.cpp:1845
bool HasTree(const String &tname) const
Checks if a child tree exists.
Definition: CRNBlock.cpp:1172
UImageIntGray ExtractCC(const String &tree)
Creates a child tree with connected components.
Definition: CRNBlock.cpp:1640
SCVector GetTree(const String &name) const
Returns a list of children. Can be used with CRN_FOREACH.
Definition: CRNBlock.h:218
Block & operator=(const Block &)=delete
masked_pixel_iterator() noexcept
Invalid iterator constructor.
Definition: CRNBlock.h:243
void FlushRGB(bool recursive=false)
Frees the local RGB buffer.
Definition: CRNBlock.cpp:1341
SImageRGB GetRGB()
Returns a pointer to the local RGB buffer.
Definition: CRNBlock.cpp:795
void RemoveTree(const String &tname)
Deletes a child tree.
Definition: CRNBlock.cpp:1187
iterator for a Rect
Definition: CRNRect.h:723
std::shared_ptr< ImageBase > SImage
Definition: CRNImage.h:101
SBlock AddChildAbsolute(const String &tree, Rect clip)
Adds a child to current block.
Definition: CRNBlock.cpp:548
WCBlock GetParent() const
Gets a reference to the parent of the block.
Definition: CRNBlock.h:82
void SubstituteRGB(const SImageRGB &img)
Substitutes the RGB buffer with a new one.
Definition: CRNBlock.cpp:1435
size_t GetNbChildren(const String &tree) const
Gets the number of blocks in a child tree.
Definition: CRNBlock.cpp:1877
void ReloadImage()
Reloads the image.
Definition: CRNBlock.cpp:1304
Iterator on the pixels the block with BW buffer as mask.
Definition: CRNBlock.h:239
masked_pixel_iterator MaskedPixelEnd(const String &tree, size_t num, pixel::BW mask_value=pixel::BWBlack)
Returns a masked iterator on the first pixel of the block.
Definition: CRNBlock.cpp:2649
const Iterator on the blocks of a child tree
void FilterWidthRatio(const String &tree, double ratio)
Filters a child tree.
Definition: CRNBlock.cpp:2215
pixel_iterator PixelBegin(const String &tree, size_t num) const
Returns an iterator on the first pixel of the block.
Definition: CRNBlock.cpp:2515
Block(const Block &)=delete
void SubstituteGray(const SImageGray &img)
Substitutes the Gray buffer with a new one.
Definition: CRNBlock.cpp:1465
const String & GetParentTree() const noexcept
Gets the name of the parent tree of the block.
Definition: CRNBlock.h:86
masked_pixel_iterator & operator=(const masked_pixel_iterator &it) noexcept
Copy operator.
Definition: CRNBlock.h:262
masked_pixel_iterator(const masked_pixel_iterator &it)
Copy constructor.
Definition: CRNBlock.h:245
void SetRelativeBBox(Rect newbox)
Sets the absolute bounding box of the block.
Definition: CRNBlock.cpp:300
A rectangle class.
Definition: CRNRect.h:46
void FlushAll(bool recursive=false)
Frees the local buffers.
Definition: CRNBlock.cpp:1327