Read@CVL
PixelSet.h
Go to the documentation of this file.
1 /*******************************************************************************************************
2  ReadFramework is the basis for modules developed at CVL/TU Wien for the EU project READ.
3 
4  Copyright (C) 2016 Markus Diem <diem@caa.tuwien.ac.at>
5  Copyright (C) 2016 Stefan Fiel <fiel@caa.tuwien.ac.at>
6  Copyright (C) 2016 Florian Kleber <kleber@caa.tuwien.ac.at>
7 
8  This file is part of ReadFramework.
9 
10  ReadFramework is free software: you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  ReadFramework is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with this program. If not, see <http://www.gnu.org/licenses/>.
22 
23  The READ project has received funding from the European Union’s Horizon 2020
24  research and innovation programme under grant agreement No 674943
25 
26  related links:
27  [1] http://www.caa.tuwien.ac.at/cvl/
28  [2] https://transkribus.eu/Transkribus/
29  [3] https://github.com/TUWien/
30  [4] http://nomacs.org
31  *******************************************************************************************************/
32 
33 #pragma once
34 
35 #include "Shapes.h"
36 #include "BaseImageElement.h"
37 #include "Pixel.h"
38 #include "Algorithms.h"
39 
40 #pragma warning(push, 0) // no warnings from includes
41 #include <QObject>
42 #include <QSharedPointer>
43 #include <QVector>
44 #include <QMap>
45 #pragma warning(pop)
46 
47 #ifndef DllCoreExport
48 #ifdef DLL_CORE_EXPORT
49 #define DllCoreExport Q_DECL_EXPORT
50 #else
51 #define DllCoreExport Q_DECL_IMPORT
52 #endif
53 #endif
54 
55 // Qt defines
56 class QPainter;
57 
58 namespace rdf {
59 
60 // elements
61 class TextLine;
62 class PixelSet;
63 
72 
73 public:
75 
76  virtual QVector<QSharedPointer<PixelEdge> > connect(const QVector<QSharedPointer<Pixel> >& pixels) const = 0;
77  void setDistanceFunction(const PixelDistance::PixelDistanceFunction& distFnc);
78  void setStopLines(const QVector<Line>& stopLines);
79 
80 protected:
82  QVector<Line> mStopLines;
83 
84  QVector<QSharedPointer<PixelEdge> > filter(QVector<QSharedPointer<PixelEdge> >& edges) const;
85 };
86 
92 
93 public:
95  virtual QVector<QSharedPointer<PixelEdge> > connect(const QVector<QSharedPointer<Pixel> >& pixels) const override;
96 };
97 
107 
108 public:
110  virtual QVector<QSharedPointer<PixelEdge> > connect(const QVector<QSharedPointer<Pixel> >& pixels) const override;
111 };
112 
113 
120 
121 public:
122  RegionPixelConnector(double multiplier = 2.0);
123 
124  virtual QVector<QSharedPointer<PixelEdge> > connect(const QVector<QSharedPointer<Pixel> >& pixels) const override;
125 
126  void setRadius(double radius);
127  void setLineSpacingMultiplier(double multiplier);
128 
129 protected:
130  double mRadius = 0.0;
131  double mMultiplier = 2.0;
132 };
133 
139 
140 public:
142 
143  virtual QVector<QSharedPointer<PixelEdge> > connect(const QVector<QSharedPointer<Pixel> >& pixels) const override;
144 
145  void setLineSpacingMultiplier(double multiplier);
146 
147 protected:
148  double mMultiplier = 1.0;
149 
150 };
151 
157 
158 public:
160 
161  virtual QVector<QSharedPointer<PixelEdge> > connect(const QVector<QSharedPointer<Pixel> >& pixels) const override;
162 
163 protected:
164  double mEpsMultiplier = 1.2;
165 
166 };
167 
173 
174 public:
175  PixelSet();
176  PixelSet(const QVector<QSharedPointer<Pixel> >& set);
177 
181 
182  connect_end
183  };
184 
185  enum mDrawFlags {
186  draw_nothing = 0x0,
187  draw_pixels = 0x1,
188  draw_poly = 0x2,
189  draw_rect = 0x4,
190 
191  draw_end
192  };
194 
195  void operator+=(const PixelSet& set);
196 
197  QSharedPointer<Pixel> operator[](int idx) const;
198  void operator<<(const QSharedPointer<Pixel>& pixel);
199  void operator<<(const PixelSet& set);
200 
201  bool isEmpty() const;
202  bool contains(const QSharedPointer<Pixel>& pixel) const;
203 
204  // functions that change the set
205  virtual void add(const QSharedPointer<Pixel>& pixel);
206  virtual void remove(const QSharedPointer<Pixel>& pixel);
207  virtual void append(const QVector<QSharedPointer<Pixel> >& set);
208  virtual void scale(double factor);
209  virtual void filterDuplicates(int eps = 5);
210 
211  QVector<QSharedPointer<Pixel> > pixels() const {
212  return mSet;
213  };
214 
215  int size() const;
216  QVector<Vector2D> pointSet(double offsetAngle = 0.0) const;
217  QVector<Vector2D> centers() const;
218  Polygon convexHull() const;
219  Rect boundingBox() const;
220  Line fitLine(double offsetAngle = 0.0) const;
221  Ellipse fitEllipse() const;
222 
223  Vector2D center() const;
224  Vector2D meanCenter() const;
225  double orientation(double statMoment = 0.5) const;
226  double lineSpacing(double statMoment = 0.5) const;
227  double area() const;
228 
229  QSharedPointer<Pixel> find(const QString& id) const;
230 
231  QSharedPointer<TextLine> toTextLine() const;
232 
233  virtual void draw(
234  QPainter& p,
235  const DrawFlags& options = DrawFlags() | draw_pixels | draw_poly,
237 
238  static QVector<QSharedPointer<PixelEdge> > connect(const QVector<QSharedPointer<Pixel> >& superPixels, const ConnectionMode& mode = connect_Delaunay);
239  static QVector<PixelSet> fromEdges(const QVector<QSharedPointer<PixelEdge> >& edges);
240  static PixelSet merge(const QVector<PixelSet>& sets);
241  QVector<PixelSet> splitScales() const;
242 
243  virtual QString toString() const override;
244 
245 protected:
246  QVector<QSharedPointer<Pixel> > mSet;
247 
248  Polygon polygon(const QVector<Vector2D>& pts) const;
249 };
250 
252 
253 public:
254  TextLineSet();
255  TextLineSet(const QVector<QSharedPointer<Pixel> >& set);
256 
257  // functions that change the set
258  void add(const QSharedPointer<Pixel>& pixel) override;
259  void remove(const QSharedPointer<Pixel>& pixel) override;
260  void append(const QVector<QSharedPointer<Pixel> >& set) override;
261  void scale(double factor) override;
262  void update();
263 
264  void draw(QPainter& p, const DrawFlags& options = PixelSet::draw_poly,
265  const Pixel::DrawFlags& pixelOptions = Pixel::draw_ellipse) const override;
266 
267  Line line() const;
268  double error() const;
269  double computeError(const QVector<Vector2D>& pts) const;
270  double density() const;
271 
272 protected:
274  double mLineErr = DBL_MAX;
275 
276  void updateLine();
277 };
278 
279 namespace TextLineHelper {
280 
281  QVector<QSharedPointer<TextLineSet> > filterLowDensity(const QVector<QSharedPointer<TextLineSet> >& textLines);
282  QVector<QSharedPointer<TextLineSet> > filterHeight(const QVector<QSharedPointer<TextLineSet> >& textLines, double minHeight = 5, double maxHeight = 5000);
283  QVector<QSharedPointer<TextLineSet> > filterAngle(const QVector<QSharedPointer<TextLineSet> >& textLines, double maxAngle = 4 * DK_DEG2RAD);
284 
285  void mergeStableTextLines(QVector<QSharedPointer<TextLineSet> >& textLines);
286 
287  QSharedPointer<TextLineSet> find(const QString& id, const QVector<QSharedPointer<TextLineSet> >& tl);
288  bool merge(const QSharedPointer<TextLineSet> & tl1, const QSharedPointer<TextLineSet> & tl2);
289 
290 }
291 
301 
302 public:
303  TextBlock(const Polygon& poly = Polygon(), const QString& id = "");
304 
305  enum mDrawFlags {
306  draw_nothing = 0x0,
307  draw_poly = 0x1,
308  draw_text_lines = 0x2,
309  draw_pixels = 0x4,
310 
311  draw_end
312  };
313 
315 
316  void addPixels(const PixelSet& ps);
317  PixelSet pixelSet() const;
318 
319  void scale(double factor) override;
320 
321  Polygon poly() const;
322 
323  void setTextLines(const QVector<QSharedPointer<TextLineSet> >& textLines);
324  QVector<QSharedPointer<TextLineSet> > textLines() const;
325  bool remove(const QSharedPointer<TextLineSet>& tl);
326  void cleanTextLines();
327 
328  QSharedPointer<Region> toTextRegion() const;
329 
330  void draw(QPainter& p, const DrawFlags& df = DrawFlags() | draw_poly | draw_text_lines);
331  virtual QString toString() const override;
332 
333 private:
334  Polygon mPoly;
335  PixelSet mSet;
336  QVector<QSharedPointer<TextLineSet> > mTextLines;
337 };
338 
347 
348 public:
349  TextBlockSet(const QVector<Polygon>& regions = QVector<Polygon>());
350  TextBlockSet(const QVector<QSharedPointer<Region>>& regions);
351 
352  void operator<<(const TextBlock& block);
353 
354  bool isEmpty() const;
355 
356  void scale(double factor) override;
357 
358  void setPixels(const PixelSet& ps);
359  QVector<QSharedPointer<TextBlock> > textBlocks() const;
360 
361  QSharedPointer<Region> toTextRegion() const;
362 
363  void removeWeakTextLines() const;
364 
365 private:
366 
367  QVector<QSharedPointer<TextBlock> > mTextBlocks;
368 };
369 
370 
378 
379 public:
380  PixelGraph();
381  PixelGraph(const PixelSet& set);
382 
383  enum SortMode {
387  sort_distance, // euclidean
388 
389  sort_end
390  };
391 
392  bool isEmpty() const;
393 
394  void draw(QPainter& p, const PixelDistance::EdgeWeightFunction* weightFnc = 0, double dynamicRange = 1.0) const;
395  void connect(const PixelConnector& connector = DelaunayPixelConnector(), const SortMode& sort = sort_none);
396 
397  PixelSet set() const;
398  QVector<QSharedPointer<PixelEdge> > edges(const QString& pixelID) const;
399  QVector<QSharedPointer<PixelEdge> > edges(const QVector<int>& edgeIDs) const;
400  QVector<QSharedPointer<PixelEdge> > edges() const;
401 
402  int pixelIndex(const QString & pixelID) const;
403  QVector<int> edgeIndexes(const QString & pixelID) const;
404 
405 protected:
407  QVector<QSharedPointer<PixelEdge> > mEdges;
408 
409  QMap<QString, int> mPixelLookup; // maps pixel IDs to their current vector index
410  QMap<QString, QVector<int> > mPixelEdges; // maps pixel IDs to their corresponding edge index
411 
412 };
413 
418 
419 public:
420  DBScanPixel(const PixelSet& pixels);
421 
422  void compute();
423 
424  void setMaxDistance(double dist);
425  void setEpsilonMultiplier(double eps);
426  void setDistanceFunction(const PixelDistance::PixelDistanceFunction& distFnc);
427  void setFast(bool f);
428 
429  QVector<PixelSet> sets() const;
430  QVector<QSharedPointer<PixelEdge> > edges() const;
431 
432 protected:
433  PixelSet mPixels; // input
434 
435 
436  enum Label {
437  not_visited = 0,
440 
441  cluster0
442  };
443 
444  // cache
445  cv::Mat mDists;
446  cv::Mat mLabels;
447  unsigned int* mLabelPtr;
448 
449  unsigned int mCLabel = cluster0;
450 
451  // parameters
453  double mMaxDistance = 0;
454  double mEpsMultiplier = 2.0;
455  int mMinPts = 3;
456  bool mFast = false;
457 
458  void expandCluster(int pixelIndex, unsigned int clusterIndex, const QVector<int>& neighbors, double eps, int minPts) const;
459  QVector<int> regionQuery(int pixelIdx, double eps) const;
460 
461  cv::Mat calcDists(const PixelSet& pixels) const;
462 };
463 
464 }
cv::Mat mDists
Definition: PixelSet.h:445
Label
Definition: PixelSet.h:436
Abstract class PixelConnector. This is the base class for all pixel connecting classes which implemen...
Definition: PixelSet.h:71
Connects Pixels using the DBScan.
Definition: PixelSet.h:156
PixelSet mPixels
Definition: PixelSet.h:433
Definition: Pixel.h:236
Definition: PixelSet.h:387
Definition: Shapes.h:412
QVector< QSharedPointer< TextLineSet > > filterHeight(const QVector< QSharedPointer< TextLineSet > > &textLines, double minHeight=5, double maxHeight=5000)
Represents a text block. A single text block has a boundary region and (possibly) a set of super pixe...
Definition: PixelSet.h:300
ConnectionMode
Definition: PixelSet.h:178
Definition: PixelSet.h:384
#define DllCoreExport
Definition: BaseImageElement.h:43
Fully connected graph. Super pixels are connected with all other super pixels within a region...
Definition: PixelSet.h:119
DllCoreExport typedef double(* PixelDistanceFunction)(const Pixel *px1, const Pixel *px2)
Definition: Algorithms.h:163
PixelSet stores and manipulates pixel collections.
Definition: PixelSet.h:172
Flags turns enums into typesave flags It is strongly related (copied) from Useage: enum mDrawFlags { ...
Definition: Utils.h:166
cv::Mat mLabels
Definition: PixelSet.h:446
QDataStream & operator<<(QDataStream &s, const BaseElement &e)
Definition: BaseImageElement.cpp:81
DllCoreExport double euclidean(const Pixel *px1, const Pixel *px2)
Euclidean distance between the pixel&#39;s centers.
Definition: Algorithms.cpp:309
void mergeStableTextLines(QVector< QSharedPointer< TextLineSet > > &textLines)
QVector< QSharedPointer< Pixel > > pixels() const
Definition: PixelSet.h:211
DBScan clustering for pixels.
Definition: PixelSet.h:417
QVector< Line > mStopLines
Definition: PixelSet.h:82
unsigned int * mLabelPtr
Definition: PixelSet.h:447
Definition: PixelSet.h:188
DllCoreExport typedef double(* EdgeWeightFunction)(const PixelEdge *edge)
Definition: Algorithms.h:168
QVector< QSharedPointer< Pixel > > mSet
Definition: PixelSet.h:246
mDrawFlags
Definition: PixelSet.h:185
Definition: PixelSet.h:385
QMap< QString, QVector< int > > mPixelEdges
Definition: PixelSet.h:410
Represents a pixel graph. This class comes in handy if you want to map pixel edges with pixels...
Definition: PixelSet.h:377
Definition: PixelSet.h:251
bool merge(const QSharedPointer< TextLineSet > &tl1, const QSharedPointer< TextLineSet > &tl2)
Definition: PixelSet.cpp:1728
QVector< QSharedPointer< PixelEdge > > mEdges
Definition: PixelSet.h:407
Definition: Shapes.h:135
QMap< QString, int > mPixelLookup
Definition: PixelSet.h:409
Definition: PixelSet.h:180
Definition: BaseImageElement.h:53
PixelDistance::PixelDistanceFunction mDistanceFnc
Definition: PixelSet.h:81
SortMode
Definition: PixelSet.h:383
A basic line class including stroke width (thickness).
Definition: Shapes.h:68
Definition: PixelSet.h:439
Definition: Shapes.h:493
Flags< mDrawFlags > DrawFlags
Definition: PixelSet.h:314
Connects pixels using the Delaunay triangulation.
Definition: PixelSet.h:91
Stores all text blocks. This class is used to group super pixels with respect to layout constrains (e...
Definition: PixelSet.h:346
QVector< QSharedPointer< TextLineSet > > filterAngle(const QVector< QSharedPointer< TextLineSet > > &textLines, double maxAngle=4 *DK_DEG2RAD)
QVector< QSharedPointer< TextLineSet > > filterLowDensity(const QVector< QSharedPointer< TextLineSet > > &textLines)
Definition: Algorithms.cpp:45
Definition: Pixel.h:232
Definition: PixelSet.h:438
#define DK_DEG2RAD
Definition: Utils.h:55
PixelSet mSet
Definition: PixelSet.h:406
Flags< mDrawFlags > DrawFlags
Definition: PixelSet.h:193
Connects tab stops.
Definition: PixelSet.h:138
Definition: Shapes.h:344
Definition: PixelSet.h:179
Connects pixels using the Voronoi diagram. NOTE: this is highly experimental for it creates new pixel...
Definition: PixelSet.h:106
Definition: PixelSet.h:386
mDrawFlags
Definition: PixelSet.h:305
QSharedPointer< TextLineSet > find(const QString &id, const QVector< QSharedPointer< TextLineSet > > &tl)
Line mLine
Definition: PixelSet.h:273