Read@CVL
SuperPixel.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 "BaseModule.h"
36 
37 #include "ScaleFactory.h"
38 #include "Shapes.h"
39 #include "Pixel.h"
40 #include "PixelSet.h"
41 #include "Image.h" // TODO: remove (with GridPixel)
42 
43 #pragma warning(push, 0) // no warnings from includes
44 #include <QRectF>
45 #include <QVector>
46 #include <QMap>
47 #include <QPainter>
48 #pragma warning(pop)
49 
50 #ifndef DllCoreExport
51 #ifdef DLL_CORE_EXPORT
52 #define DllCoreExport Q_DECL_EXPORT
53 #else
54 #define DllCoreExport Q_DECL_IMPORT
55 #endif
56 #endif
57 
58 // Qt defines
59 
60 namespace rdf {
61 
62 // read defines
63 
69 
70 public:
71  SuperPixelBase(const cv::Mat& img);
72 
73  bool isEmpty() const override;
74 
75  virtual PixelSet pixelSet() const;
76 
77  void setPyramidLevel(int level);
78  int pyramidLevel();
79 
80 protected:
81  cv::Mat mSrcImg;
83 
84  int mPyramidLevel = 0;
85 
86  bool checkInput() const override;
87 };
88 
94 
95 public:
97 
98  virtual QString toString() const override;
99 
100  int mserMinArea() const;
101  int mserMaxArea() const;
102  int erosionStep() const;
103 
104  void setNumErosionLayers(int numLayers);
105  int numErosionLayers() const;
106 
107 
108 protected:
109  int mMserMinArea = 25;
110  int mMserMaxArea = 500;
111  int mErosionStep = 4;
112  int mNumErosionLayers = 3;
113 
114  void load(const QSettings& settings) override;
115  void save(QSettings& settings) const override;
116 };
117 
124 
125 public:
127 
128  void append(const MserContainer& o);
129 
130  QVector<QSharedPointer<MserBlob> > toBlobs() const;
131  size_t size() const;
132 
133  std::vector<std::vector<cv::Point> > pixels;
134  std::vector<cv::Rect> boxes;
135 };
136 
145 
146 public:
147  SuperPixel(const cv::Mat& img);
148 
149  bool compute() override;
150 
151  QString toString() const override;
152  QSharedPointer<SuperPixelConfig> config() const;
153 
154  // results - available after compute() is called
155  QVector<QSharedPointer<MserBlob> > getMserBlobs() const;
156 
157  cv::Mat draw(const cv::Mat& img, const QColor& col = QColor()) const;
158  cv::Mat drawMserBlobs(const cv::Mat& img, const QColor& col = QColor()) const;
159 
160 private:
161  // results
162  QVector<QSharedPointer<MserBlob> > mBlobs;
163 
164  QSharedPointer<MserContainer> getBlobs(const cv::Mat& img, int kernelSize) const;
165  QSharedPointer<MserContainer> mser(const cv::Mat& img) const;
166  int filterAspectRatio(MserContainer& blobs, double aRatio = 0.1) const;
167  int filterDuplicates(MserContainer& blobs, int eps = 5, int upperBound = -1) const;
168 
169 };
170 
176 
177 public:
178  LinePixelConfig();
179 
180  virtual QString toString() const override;
181 
182  int minLineLength() const;
183 
184 protected:
185  int mMinLineLength = 5;
186 
187  void load(const QSettings& settings) override;
188  void save(QSettings& settings) const override;
189 };
190 
196 
197 public:
198  LineSuperPixel(const cv::Mat& img);
199 
200  bool compute() override;
201 
202  QString toString() const override;
203  QSharedPointer<LinePixelConfig> config() const;
204 
205  cv::Mat draw(const cv::Mat& img) const;
206 
207 protected:
208 
209  // results
210  QVector<Line> mLines; // debug only
211 
212  bool checkInput() const override;
213 };
214 
220 
221 public:
222  GridPixelConfig();
223 
224  virtual QString toString() const override;
225 
226  bool autoWindowSize() const;
227  void estimateWindowSize(int height, int numTiles = 100);
228  int winSize() const;
229  double winOverlap() const;
230  double minEnergy() const;
231  bool applyLineMask() const;
232 
233 protected:
234  bool mAutoWinSize = true; // if true, the window size is determined w.r.t the image resolution
235  int mWinSize = 5; // the window size in px per scale
236  double mWinOverlap = 0.5; // the window overlaps
237  double mMinEnergy = 0.07; // minimum energy per cell
238  bool mLineMask = true; // if true, straight lines are removed
239 
240  void load(const QSettings& settings) override;
241  void save(QSettings& settings) const override;
242 };
243 
245 
246 public:
247  GridPixel(int index = -1, int numColumns = -1);
248 
249  bool operator==(const GridPixel& gpr);
250  void compute(const cv::Mat& mag, const cv::Mat& phase, const cv::Mat& weight = cv::Mat());
251 
252  bool isDead() const;
253  void kill();
254  void move(const Vector2D& vec);
255 
256  int row() const;
257  int col() const;
258 
259  int orIdx() const;
260  double edgeStrength() const;
261 
262  Ellipse ellipse() const;
263  void draw(QPainter& p) const;
264 
265  int index(int row, int col) const;
266  QVector<int> neighbors() const;
267 
268  QSharedPointer<Pixel> toPixel() const;
269 
270 private:
271 
272  int mIndex = -1;
273  int mNumColumns = -1;
274 
275  // results
276  double mEdgeStrength = 0;
277  int mOrIdx = -1;
278 
279  //Histogram mOrHist;
280  Ellipse mEllipse;
281 };
282 
288 
289 public:
290  GridSuperPixel(const cv::Mat& img);
291 
292  bool compute() override;
293 
294  QString toString() const override;
295  QSharedPointer<GridPixelConfig> config() const;
296 
297  cv::Mat draw(const cv::Mat& img, const QColor& col = QColor()) const;
298 
299 private:
300 
301  QVector<QSharedPointer<GridPixel> > mGridPixel; // debug only
302 
303  QMap<int, QSharedPointer<GridPixel> > computeGrid(const cv::Mat& mag, const cv::Mat& phase, int winSize, double winOverlap) const;
304  QVector<QSharedPointer<GridPixel> > merge(const QMap<int, QSharedPointer<GridPixel> >& pixels, const cv::Mat& mag, const cv::Mat& phase) const;
305 
306  // filtering
307  PixelSet filter(const PixelSet& set, double clusterStrength = 4.0) const;
308  QVector<PixelSet> cluster(const PixelSet& set) const;
309 
310  void edges(const cv::Mat& src, cv::Mat& magnitude, cv::Mat& orientation) const;
311  cv::Mat lineMask(const cv::Mat& src) const;
312 };
313 
319 
320 public:
322 
323  virtual QString toString() const override;
324 
325  int maxScale() const;
326  int minScale() const;
327  Vector2D scaleIvl() const;
328  int numOrientations() const;
329  int histSize() const;
330 
331  // changable parameters
332  void setNumOrientations(int numOr);
333  void setMaxScale(int maxScale);
334  void setMinScale(int minScale);
335 
336 protected:
337  int mMaxScale = 256; // radius (in px) of the maximum scale
338  int mMinScale = 128; // radius (in px) of the minimum scale
339  int mNumOr = 32; // number of orientation histograms
340  int mHistSize = 64; // size of the orientation histogram
341 
342  void load(const QSettings& settings) override;
343  void save(QSettings& settings) const override;
344 };
345 
351 
352 public:
353  LocalOrientation(const PixelSet& set = PixelSet());
354 
355  bool isEmpty() const override;
356  bool compute() override;
357 
358  QString toString() const override;
359  QSharedPointer<LocalOrientationConfig> config() const;
360 
361  // results - available after compute() is called
362  PixelSet set() const;
363 
364  cv::Mat draw(const cv::Mat& img, const QString& id, double radius) const;
365 
366 private:
367 
368  // input/output
369  PixelSet mSet;
370 
371  bool checkInput() const override;
372 
373  void computeScales(Pixel* pixel, const QVector<Pixel*>& set) const;
374  void computeAllOrHists(Pixel* pixel, const QVector<Pixel*>& set, double radius) const;
375  void computeOrHist(const Pixel* pixel,
376  const QVector<const Pixel*>& set,
377  const Vector2D& histVec,
378  cv::Mat& orHist,
379  float& sparsity) const;
380 
381 };
382 
383 }
Definition: SuperPixel.h:244
MserContainer()
Definition: SuperPixel.h:126
Configuration class for MserSuperPixel.
Definition: SuperPixel.h:93
Definition: Shapes.h:412
Definition: BaseModule.h:63
SuperPixel generator using MSER regions. An erosion pyramid improves the MSER regions specifically if...
Definition: SuperPixel.h:144
#define DllCoreExport
Definition: BaseImageElement.h:43
PixelSet stores and manipulates pixel collections.
Definition: PixelSet.h:172
Container for MSER elements. This class maps OpenCVs MSER regions with their bounding boxes...
Definition: SuperPixel.h:123
Configuration class for LineSuperPixel.
Definition: SuperPixel.h:175
std::vector< cv::Rect > boxes
Definition: SuperPixel.h:134
Grid based SuperPixel extraction.
Definition: SuperPixel.h:287
QVector< Line > mLines
Definition: SuperPixel.h:210
PixelSet mSet
Definition: SuperPixel.h:82
bool merge(const QSharedPointer< TextLineSet > &tl1, const QSharedPointer< TextLineSet > &tl2)
Definition: PixelSet.cpp:1728
Definition: Shapes.h:135
config
Definition: DependencyCollector.py:271
Local orientation estimation (using Il Koo&#39;s method).
Definition: SuperPixel.h:350
bool operator==(const BaseElement &l, const QString &id)
Definition: BaseImageElement.cpp:57
Definition: BaseImageElement.h:53
Base class implementation for SuperPixel generating modules.
Definition: SuperPixel.h:68
Definition: BaseModule.h:107
level
Definition: DependencyCollector.py:25
std::vector< std::vector< cv::Point > > pixels
Definition: SuperPixel.h:133
Configuration file for local orientation extraction.
Definition: SuperPixel.h:318
DllCoreExport bool save(const QImage &img, const QString &savePath, int compression=-1)
Saves the specified QImage img.
Definition: Image.cpp:180
This class represents a single instance of super pixels which are needed for the layout analysis...
Definition: Pixel.h:188
DllCoreExport QImage load(const QString &path, bool *ok=0)
Definition: Image.cpp:152
SuperPixel generator based on the LSD line detector.
Definition: SuperPixel.h:195
This is the base class for all modules. It provides all functions which are implemented by the module...
Definition: BaseModule.h:126
Definition: Algorithms.cpp:45
Configuration class for GridSuperPixel.
Definition: SuperPixel.h:219
cv::Mat mSrcImg
Definition: SuperPixel.h:81