Read@CVL
Pixel.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 "Utils.h"
38 #include "PixelLabel.h"
39 #include "Algorithms.h"
40 #include "ScaleFactory.h"
41 
42 #pragma warning(push, 0) // no warnings from includes
43 #include <QObject>
44 #include <QSharedPointer>
45 #include <QVector>
46 #include <QMap>
47 #pragma warning(pop)
48 
49 #ifndef DllCoreExport
50 #ifdef DLL_CORE_EXPORT
51 #define DllCoreExport Q_DECL_EXPORT
52 #else
53 #define DllCoreExport Q_DECL_IMPORT
54 #endif
55 #endif
56 
57 // Qt defines
58 
59 namespace rdf {
60 
61 class Pixel;
62 class PixelEdge;
63 class PixelLabel;
64 class TextLine;
65 
67 
68 public:
69  MserBlob(const std::vector<cv::Point>& pts = std::vector<cv::Point>(),
70  const Rect& bbox = Rect(),
71  const QString& id = QString());
72 
73  bool isNull() const;
74 
75  double area() const;
76  Vector2D center() const;
77  Rect bbox() const;
78 
79  std::vector<cv::Point> pts() const;
80  std::vector<cv::Point> relativePts(const Vector2D& origin) const;
81 
82  //double uniqueArea(const QVector<MserBlob>& blobs) const;
83  void draw(QPainter& p);
84 
85  // conversions
86  QSharedPointer<Pixel> toPixel() const;
87  cv::Mat toBinaryMask() const;
88 
89  double overlapArea(const Rect& r) const;
90 
91 protected:
94  std::vector<cv::Point> mPts;
95 };
96 
103 
104 public:
105  PixelStats(const cv::Mat& orHist = cv::Mat(),
106  const cv::Mat& sparsity = cv::Mat(),
107  double scale = 0.0,
108  QSharedPointer<ScaleFactory> scaleFactory = QSharedPointer<ScaleFactory>(new ScaleFactory()),
109  const QString& id = QString());
110 
111  /* row index of data */
112  enum DataIndex {
113  all_data = -1,
118 
120  };
121 
122  bool isEmpty() const;
123 
124  void setScaleFactory(const QSharedPointer<ScaleFactory>& scaleFactory);
125 
126  void setOrientationIndex(int orIdx);
127  void setLineSpacing(int ls);
128  int orientationIndex() const;
129  int numOrientations() const;
130  double orientation() const;
131  Vector2D orVec() const;
132 
133  void scale(double factor) override;
134  double scaleFactor() const;
135  int lineSpacingIndex() const;
136  double lineSpacing() const;
137 
138  double minVal() const;
139  cv::Mat data(const DataIndex& dIdx = all_data);
140 
141  QString toString() const;
142 
143 protected:
144  cv::Mat mData; // MxN orientation histograms M ... idx_end and N ... number of orientations
145  double mScale = 0.0;
146  double mMinVal = 0.0;
147  QSharedPointer<ScaleFactory> mScaleFactory;
148 
149  int mHistSize = 0;
150  int mOrIdx = -1;
151  int mLineSpacing = -1;
152 
153  void convertData(const cv::Mat& orHist, const cv::Mat& sparsity);
154 };
155 
157 
158 public:
159 
160  enum Type {
165 
166  type_end
167 
168  };
169 
170  PixelTabStop(const Type& type = type_none);
171 
172 
173  static PixelTabStop create(const QSharedPointer<Pixel>& pixel, const QVector<QSharedPointer<PixelEdge> >& edges);
174 
175  double orientation() const;
176  Type type() const;
177 
178 protected:
180 };
181 
189 
190 public:
191  Pixel();
192  Pixel(const Ellipse& ellipse, const Rect& bbox = Rect(), const QString& id = QString());
193 
194  bool isNull() const;
195 
196  inline Vector2D center() const {
197  return mEllipse.center();
198  };
199 
200  Rect bbox() const {
201  return mBBox;
202  };
203 
204  Vector2D size() const;
205  double angle() const;
206  Ellipse ellipse() const;
207 
208  void scale(double factor) override;
209  void move(const Vector2D& vec);
210 
211  void addStats(const QSharedPointer<PixelStats>& stats);
212  QSharedPointer<PixelStats> stats(int idx = -1) const;
213 
214  void setTabStop(const PixelTabStop& tabStop);
215  PixelTabStop tabStop() const;
216 
217  void setLabel(const QSharedPointer<PixelLabel>& label);
218  QSharedPointer<PixelLabel> label() const;
219 
220  void setPyramidLevel(int level);
221  int pyramidLevel() const;
222 
223  cv::KeyPoint toKeyPoint() const;
224 
225  void setValue(double value);
226  double value() const;
227 
228  cv::Mat toBinaryMask(const Rect& r) const;
229 
230  enum mDrawFlags {
231  draw_none = 0x00,
232  draw_ellipse = 0x01,
233  draw_stats = 0x02,
234  draw_center = 0x04,
235  draw_id = 0x08,
236  draw_label_colors = 0x10,
237  draw_tab_stops = 0x20,
238 
239  draw_all = 0x3f,
240  };
242 
243  void draw(QPainter& p, double alpha = 0.3, const DrawFlags& df = DrawFlags() | /*draw_stats |*/ draw_ellipse | draw_label_colors) const;
244 
245 protected:
246  bool mIsNull = true;
247 
250  QVector<QSharedPointer<PixelStats> > mStats;
252  QSharedPointer<PixelLabel> mLabel = QSharedPointer<PixelLabel>::create();
253  double mValue = 0.0; // e.g. gradient magnitude for GridPixels
254 
255  int mPyramidLevel = 0; // 1.0/std::pow(2, mPyramidLevel) scales back to the pyramid
256 };
257 
259 
260 public:
261  PixelEdge();
262 
263  PixelEdge(const QSharedPointer<Pixel> first,
264  const QSharedPointer<Pixel> second,
265  const QString& id = QString());
266 
267  friend DllCoreExport bool operator<(const PixelEdge& pe1, const PixelEdge& pe2);
268  friend DllCoreExport bool operator<(const QSharedPointer<PixelEdge>& pe1, const QSharedPointer<PixelEdge>& pe2);
269 
270  virtual bool lessThan(const PixelEdge& e) const;
271 
272  bool isNull() const;
273 
274  void setEdgeWeightFunction(PixelDistance::EdgeWeightFunction& fnc);
275  virtual double edgeWeightConst() const;
276  virtual double edgeWeight();
277  Line edge() const;
278 
279  QSharedPointer<Pixel> first() const;
280  QSharedPointer<Pixel> second() const;
281 
282  void scale(double s) override;
283  void draw(QPainter& p) const;
284 
285 protected:
286  bool mIsNull = true;
287  double mEdgeWeight = DBL_MAX;
288 
290  QSharedPointer<Pixel> mFirst;
291  QSharedPointer<Pixel> mSecond;
293 };
294 
296 
297 public:
298  LineEdge();
299 
300  LineEdge(const PixelEdge& pe);
301 
302  LineEdge(const QSharedPointer<Pixel> first,
303  const QSharedPointer<Pixel> second,
304  const QString& id = QString());
305  friend DllCoreExport bool operator<(const QSharedPointer<LineEdge>& le1, const QSharedPointer<LineEdge>& le2);
306 
307  virtual double edgeWeightConst() const override;
308 
309 protected:
310  double mEdgeWeight = 0.0;
311 
312  double statsWeight(const QSharedPointer<Pixel>& pixel) const;
313  double calcWeight() const;
314 };
315 
316 }
Vector2D mCenter
Definition: Pixel.h:92
Definition: Pixel.h:117
std::vector< cv::Point > mPts
Definition: Pixel.h:94
Line mEdge
Definition: Pixel.h:292
Definition: Pixel.h:114
PixelTabStop mTabStop
Definition: Pixel.h:251
This class holds Pixel statistics which are found when computing the local orientation.
Definition: Pixel.h:102
Definition: Pixel.h:164
Definition: Shapes.h:412
Rect bbox() const
Definition: Pixel.h:200
QVector< QSharedPointer< PixelStats > > mStats
Definition: Pixel.h:250
Definition: Pixel.h:66
Type
Definition: Pixel.h:160
#define DllCoreExport
Definition: BaseImageElement.h:43
Definition: Pixel.h:156
Ellipse mEllipse
Definition: Pixel.h:248
Flags turns enums into typesave flags It is strongly related (copied) from Useage: enum mDrawFlags { ...
Definition: Utils.h:166
Rect mBBox
Definition: Pixel.h:249
Definition: Pixel.h:258
QSharedPointer< Pixel > mFirst
Definition: Pixel.h:290
Definition: Pixel.h:119
Definition: Pixel.h:115
DllCoreExport typedef double(* EdgeWeightFunction)(const PixelEdge *edge)
Definition: Algorithms.h:168
DataIndex
Definition: Pixel.h:112
Definition: Shapes.h:135
QSharedPointer< Pixel > mSecond
Definition: Pixel.h:291
bool operator<(const PixelEdge &pe1, const PixelEdge &pe2)
Definition: Pixel.cpp:537
Definition: Pixel.h:161
Definition: Pixel.h:163
Definition: BaseImageElement.h:53
Flags< mDrawFlags > DrawFlags
Definition: Pixel.h:241
level
Definition: DependencyCollector.py:25
Definition: ScaleFactory.h:95
A basic line class including stroke width (thickness).
Definition: Shapes.h:68
cv::Mat mData
Definition: Pixel.h:144
Definition: Pixel.h:162
Definition: Pixel.h:116
This class represents a single instance of super pixels which are needed for the layout analysis...
Definition: Pixel.h:188
mDrawFlags
Definition: Pixel.h:230
Definition: Algorithms.cpp:45
Type mType
Definition: Pixel.h:179
Vector2D center() const
Definition: Pixel.h:196
QSharedPointer< ScaleFactory > mScaleFactory
Definition: Pixel.h:147
Definition: Pixel.h:295
Definition: Shapes.h:344
DllCoreExport double spacingWeighted(const PixelEdge *edge)
Returns the edge weight normalized by the line spacing. This function returns an edge weight that is ...
Definition: Algorithms.cpp:391
Rect mBBox
Definition: Pixel.h:93