Read@CVL
LineTrace.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 #include "Shapes.h"
37 
38 #pragma warning(push, 0) // no warnings from includes
39 // Qt Includes
40 #include <QVector>
41 #pragma warning(pop)
42 
43 #pragma warning (disable: 4251) // inlined Qt functions in dll interface
44 
45 #ifndef DllCoreExport
46 #ifdef DLL_CORE_EXPORT
47 #define DllCoreExport Q_DECL_EXPORT
48 #else
49 #define DllCoreExport Q_DECL_IMPORT
50 #endif
51 #endif
52 
53 // Qt defines
54 
55 namespace rdf {
56 
58 
59 public:
61 
62  double maxSlopeRotat() const;
63  void setMaxSlopeRotat(double s);
64 
65  int minLength() const;
66  void setMinLength(int l);
67 
68  double maxGap() const;
69  void setMaxGap(double g);
70 
71  double maxAngleDiff() const;
72  void setMaxAngleDiff(double a);
73 
74  QString toString() const override;
75 
76 private:
77  void load(const QSettings& settings) override;
78  void save(QSettings& settings) const override;
79 
80  double mMaxSlopeRotat = 10.0; //filter parameter: maximal difference of line orientation compared to the result of the Rotation module (default: 10°)
81  int mMinLength = 100; //filter parameter: remove lines which are smaller (default: 100)
82  double mMaxGap = 100; //filter parameter: maximal gap between two lines in pixel (default: 100)
83  double mMaxAngleDiff = 2.0; //filter parameter: maximal angle difference between two compared and the inserted line (default: 2.0)
84 };
85 
87 
88 public:
89  LineFilter();
90 
91  QVector<rdf::Line> filterLineAngle(const QVector<rdf::Line>& lines, double angle, double angleDiff = DBL_MAX) const;
92  QVector<rdf::Line> mergeLines(const QVector<rdf::Line>& lines, QVector<rdf::Line>* gaps = 0, double maxGap = DBL_MAX, double maxAngleDiff = DBL_MAX) const;
93  QVector<rdf::Line> removeSmall(const QVector<rdf::Line>& lines, int minLineLength = 0) const;
94 
95  QSharedPointer<LineFilterConfig> config() const;
96 
97 protected:
98 
99  QSharedPointer<LineFilterConfig> mConfig;
100 };
101 
102 
104 
105 public:
106  LineTraceConfig();
107 
108  int minWidth() const;
109  void setMinWidth(int w);
110 
111  double maxLenDiff() const;
112  void setMaxLenDiff(double l);
113 
114  int maxLen() const;
115  void setMaxLen(int l);
116 
117  int minLenSecondRun() const;
118  void setMinLenSecondRun(int r);
119 
120  float maxDistExtern() const;
121  void setMaxDistExtern(float d);
122 
123  float maxAngleDiffExtern() const;
124  void setMaxAngleDiffExtern(float a);
125 
126  double maxAspectRatio() const;
127  void setMaxAspectRatio(double a);
128 
129  QString toString() const override;
130 
131 private:
132  void load(const QSettings& settings) override;
133  void save(QSettings& settings) const override;
134 
135  int mMinLenSecondRun = 60; //min len to filter after merge lines; was 60
136  double mMaxLenDiff = 1.5; //filter parameter: maximal difference in length between two successive runlengths (default: 1.5)
137  double mMaxAspectRatio = 0.3; //filter parameter: maximal aspect ratio of a line (default: 0.3f)
138  int mMinWidth = 20; //filter parameter: minimal width a line in pixel (default: 30)
139 
140  //filter Lines parameter (compared to given line vector, see std::vector<DkLineExt> filterLines(std::vector<DkLineExt> &externLines))
141  int mMaxLen = 20; //filter parameter: maximal length of a line in pixel (default: 20)
142  float mMaxDistExtern = 10.0f; //maximal Distance of the external line end points compared to a given line (default: 5 pixel)
143  float mMaxAngleDiffExtern = 20.0f / 180.0f * (float)CV_PI; //maximal Angle Difference of the external line compared to a given line (default: 20 deg)
144 };
145 
152 
153 public:
154  LineTrace(const cv::Mat& img, const cv::Mat& mask = cv::Mat());
155 
156  bool isEmpty() const override;
157  virtual bool compute() override;
158  QVector<rdf::Line> getHLines() const;
159  QVector<rdf::Line> getVLines() const;
160  QVector<rdf::Line> getLines() const;
161  void setAngle(double angle = std::numeric_limits<double>::infinity());
162  void resetAngle();
163 
164  QSharedPointer<LineTraceConfig> config() const;
165 
166  cv::Mat lineImage() const;
167  cv::Mat generatedLineImage() const;
168  static void generateLineImage(const QVector<rdf::Line>& hline, const QVector<rdf::Line>& vline, cv::Mat& img, cv::Scalar hCol = cv::Scalar(255), cv::Scalar vCol = cv::Scalar(255), cv::Point2d offset = cv::Point(0,0));
169  virtual QString toString() const override;
170 
171 protected:
172  bool checkInput() const override;
173 
174  cv::Mat mSrcImg; //the input image either 3 channel or 1 channel [0 255]
175  cv::Mat mLineImg; //the line image [0 255]
176  cv::Mat mMask; //the mask image [0 255]
177 
178  QVector<rdf::Line> hLines;
179  QVector<rdf::Line> vLines;
180 
182 
183 private:
184 
185  double mAngle = std::numeric_limits<double>::infinity(); //filter parameter: angle of the snippet determined by the skew estimation (default: 0.0f)
186 
187  float mLineProb;
188  float mLineDistProb;
189 
190  cv::Mat hDSCC(const cv::Mat& bwImg) const;
191  void filter(cv::Mat& hDSCCImg, cv::Mat& vDSCCImg);
192  void filterLines();
193  void drawGapLines(cv::Mat& img, QVector<rdf::Line> lines);
194 };
195 
197 
198 public:
200 
201  void setScale(double scale);
202  double scale() const;
203 
204  void setMergeLines(bool merge);
205  bool mergeLines() const;
206 
207  QString toString() const override;
208 
209 private:
210  void load(const QSettings& settings) override;
211  void save(QSettings& settings) const override;
212 
213  double mScale = 0.5; // initial downscaling of the image
214  bool mMergeLines = true; // if false, lines are not merged after processing (usefull if you need an exact localization)
215 };
216 
224 
225 public:
226  LineTraceLSD(const cv::Mat& img);
227 
228  bool isEmpty() const override;
229  virtual bool compute() override;
230 
231  QVector<Line> lines() const;
232  QVector<Line> separatorLines() const;
233 
234  QSharedPointer<LineTraceLSDConfig> config() const;
235  LineFilter lineFilter() const;
236 
237  virtual QString toString() const override;
238 
239  cv::Mat draw(const cv::Mat& img) const;
240 
241 protected:
242  cv::Mat mImg;
243  QVector<Line> mLines;
245 
246  bool checkInput() const override;
247 };
248 
249 }
Definition: LineTrace.h:196
QVector< rdf::Line > hLines
Definition: LineTrace.h:178
QVector< rdf::Line > vLines
Definition: LineTrace.h:179
cv::Mat mSrcImg
Definition: LineTrace.h:174
Definition: BaseModule.h:63
Definition: LineTrace.h:86
#define DllCoreExport
Definition: BaseImageElement.h:43
cv::Mat mLineImg
Definition: LineTrace.h:175
QSharedPointer< LineFilterConfig > mConfig
Definition: LineTrace.h:99
Definition: LineTrace.h:103
cv::Mat mImg
Definition: LineTrace.h:242
LineFilter mLineFilter
Definition: LineTrace.h:244
Detects Lines in a binary image. The result is a binary image containing all line elements (pixel acc...
Definition: LineTrace.h:151
bool merge(const QSharedPointer< TextLineSet > &tl1, const QSharedPointer< TextLineSet > &tl2)
Definition: PixelSet.cpp:1728
config
Definition: DependencyCollector.py:271
DllCoreExport bool save(const QImage &img, const QString &savePath, int compression=-1)
Saves the specified QImage img.
Definition: Image.cpp:180
Definition: LineTrace.h:57
Detect lines using the LSD algorithm. This line finder implements the LSD method from OpenCV contrib...
Definition: LineTrace.h:223
DllCoreExport QImage load(const QString &path, bool *ok=0)
Definition: Image.cpp:152
LineFilter mLineFilter
Definition: LineTrace.h:181
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
cv::Mat mMask
Definition: LineTrace.h:176
QVector< Line > mLines
Definition: LineTrace.h:243