Read@CVL
SkewEstimation.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 "PixelSet.h"
38 
39 #pragma warning(push, 0) // no warnings from includes
40 // Qt Includes
41 #include <QObject>
42 #include <QVector>
43 #include <QVector4D>
44 
45 #include <opencv2/core.hpp>
46 #pragma warning(pop)
47 
48 #pragma warning (disable: 4251) // inlined Qt functions in dll interface
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 
64 
65  public:
67 
68  int width() const;
69  void setWidth(int w);
70 
71  int height() const;
72  void setHeight(int h);
73 
74  int delta() const;
75  void setDelta(int d);
76 
77  int epsilon() const;
78  void setEpsilon(int e);
79 
80  int minLineLength() const;
81  void setMinLineLength(int l);
82 
83  //int minLineProjLength() const;
84  //void setMinLineProjLength(int l);
85 
86  double sigma() const;
87  void setSigma(double s);
88 
89  double thr() const;
90  void setThr(double t);
91 
92  int kMax() const;
93  void setKMax(int k);
94 
95  int nIter() const;
96  void setNIter(int n);
97 
98  QString toString() const override;
99 
100  private:
101  void load(const QSettings& settings) override;
102  void save(QSettings& settings) const override;
103 
104  int mW = 168; //49 according to the paper, best for document: 60
105  int mH = 56; //12 according to the paper, best for document: 28
106  int mDelta = 20; //according to the paper
107  int mEpsilon = 2; //according to the paper
108 
109  int mMinLineLength = 50;
110  //int mMinLineProjLength = 50 / 4;
111  double mSigma = 0.3; //according to the paper, best for document: 0.5
112  //double mThr = 0.1; //according to the paper
113  double mThr = 4.0; //according to the paper
114 
115  int mKMax = 7; //according to the paper
116  int mNIter = 200; //according to the paper
117  };
118 
119 
126 
127  public:
128  enum EdgeDirection { HORIZONTAL = 0, VERTICAL };
129 
130  BaseSkewEstimation(const cv::Mat& img = cv::Mat(), const cv::Mat& mask = cv::Mat());
131  void setImages(const cv::Mat& img, const cv::Mat& mask = cv::Mat());
132 
133  bool compute();
134  double getAngle();
135  QVector<QVector4D> getSelectedLines() const;
136 
137  bool isEmpty() const override;
138  virtual QString toString() const override;
139 
140  void setFixedThr(bool f);
141 
142  QSharedPointer<BaseSkewEstimationConfig> config() const;
143 
144  protected:
145  cv::Mat mSrcImg; //the input image either 3 channel or 1 channel [0 255]
146  cv::Mat mMask; //the mask image [0 255]
147 
148  cv::Mat separability(const cv::Mat& srcImg, int w, int h, const cv::Mat& mask = cv::Mat());
149  cv::Mat edgeMap(const cv::Mat& separability, double thr, EdgeDirection direction = HORIZONTAL, const cv::Mat& mask = cv::Mat()) const;
150  QVector<QVector3D> computeWeights(cv::Mat edgeMap, int delta, int epsilon, EdgeDirection direction = HORIZONTAL);
151  //according to paper eta should be 0.5
152  double skewEst(const QVector<QVector3D>& weights, double imgDiagonal, bool& ok, double eta=0.35);
153  bool checkInput() const override;
154 
155  private:
156 
157  double mSkewAngle = 0.0;
158  //double mWeightEps = 0.5;
159  int mRotationFactor = 1; //needed if we want to transpose the image in the beginning...
160  bool mFixedThr = true;
161 
162  cv::Mat mIntegralImg;
163  cv::Mat mIntegralSqdImg;
164 
165  QVector<QVector4D> mSelectedLines;
166  QVector<int> mSelectedLineTypes;
167 
168  };
169 
171 
172  public:
174 
175  virtual QString toString() const override;
176 
177  double minAngle() const;
178  double maxAngle() const;
179 
180  protected:
181 
182  void load(const QSettings& settings) override;
183  void save(QSettings& settings) const override;
184 
185  double mMinAngle = -CV_PI; // minimum angle expected in radians
186  double mMaxAngle = CV_PI; // maximum angle expected in radians
187  };
188 
189 
191 
192  public:
193  TextLineSkew(const cv::Mat& img = cv::Mat());
194 
195  bool compute();
196 
197  double getAngle();
198 
199  bool isEmpty() const override;
200  virtual QString toString() const override;
201 
202  QSharedPointer<TextLineSkewConfig> config() const;
203 
204  cv::Mat draw(const cv::Mat& img) const;
205  cv::Mat rotated(const cv::Mat& img) const;
206 
207  protected:
208  cv::Mat mImg; //the input image either 3 channel or 1 channel [0 255]
209 
211  QSharedPointer<ScaleFactory> mScaleFactory;
212 
213  double mAngle = 0.0;
214 
215  bool checkInput() const override;
216  };
217 
218 
219 
220 
221 }
Definition: BaseModule.h:63
#define DllCoreExport
Definition: BaseImageElement.h:43
PixelSet stores and manipulates pixel collections.
Definition: PixelSet.h:172
Definition: SkewEstimation.h:63
The class estimates the skew of a document page. The methodology is based on "Skew estimation of natu...
Definition: SkewEstimation.h:125
cv::Mat mImg
Definition: SkewEstimation.h:208
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
DllCoreExport QImage load(const QString &path, bool *ok=0)
Definition: Image.cpp:152
EdgeDirection
Definition: SkewEstimation.h:128
cv::Mat mSrcImg
Definition: SkewEstimation.h:145
QSharedPointer< ScaleFactory > mScaleFactory
Definition: SkewEstimation.h:211
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
Definition: SkewEstimation.h:170
Definition: SkewEstimation.h:190
cv::Mat mMask
Definition: SkewEstimation.h:146
PixelSet mSet
Definition: SkewEstimation.h:210