ReadFramework
Algorithms.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 
37 #pragma warning(push, 0) // no warnings from includes
38 #include <QSharedPointer>
39 #include <opencv2/core.hpp>
40 #pragma warning(pop)
41 
42 #pragma warning (disable: 4251) // inlined Qt functions in dll interface
43 
44 #ifndef DllCoreExport
45 #ifdef DLL_CORE_EXPORT
46 #define DllCoreExport Q_DECL_EXPORT
47 #else
48 #define DllCoreExport Q_DECL_IMPORT
49 #endif
50 #endif
51 
52 // Qt defines
53 class QSettings;
54 
55 namespace rdf {
56 
57 // read defines
58 class Pixel;
59 class PixelEdge;
60 
65 
66 public:
67 
68  static int doubleEqual(double a, double b);
69  static double absAngleDiff(double a, double b);
70  static double signedAngleDiff(double a, double b);
71 
72  // convenience functions
73  static double min(const QVector<double>& vec);
74  static double max(const QVector<double>& vec);
75 
76  static double normAngleRad(double angle, double startIvl = 0.0, double endIvl = 2.0*CV_PI);
77  static double angleDist(double angle1, double angle2, double maxAngle = 2.0*CV_PI);
78 
79  // template functions --------------------------------------------------------------------
80 
88  template <typename numFmt>
89  static double statMoment(const QList<numFmt>& valuesIn, double momentValue, bool interpolated = true) {
90 
91  // no stat moment if we have 1 value
92  if (valuesIn.size() == 1)
93  return valuesIn[0];
94 
95  // return mean if we have two & interpolation is turned on
96  if (valuesIn.size() == 2 && interpolated) {
97  return (valuesIn[0] + valuesIn[1]) / 2.0;
98  }
99  else if (valuesIn.size() == 2)
100  return valuesIn[0];
101 
102  QList<numFmt> values = valuesIn;
103  qSort(values);
104 
105  size_t lSize = values.size();
106  double moment = -1;
107  unsigned int momIdx = cvCeil(lSize*momentValue);
108  unsigned int idx = 1;
109 
110  // find the statistical moment
111  for (auto val : values) {
112 
113  // skip
114  if (idx < momIdx) {
115  idx++;
116  continue;
117  }
118 
119  // compute mean between this and the next element
120  if (lSize % 2 == 0 && momIdx < lSize && interpolated)
121  moment = ((double)val + values[idx+1])*0.5;
122  else
123  moment = (double)val;
124  break;
125  }
126 
127  return moment;
128  }
129 
130 };
131 
136 
137 public:
138  LineFitting(const QVector<Vector2D>& pts);
139 
140  Line fitLineLMS() const;
141  Line fitLine() const;
142 
143 protected:
144  // parameters:
145  int mNumSets = 1000; // # random sets generated
146  int mSetSize = 2; // if 2, lines are directly returned
147  double mEps = 0.1; // if LMS is smaller than that, we break
148  double mMinLength = 2; // minimum line length
149 
150  QVector<Vector2D> mPts;
151 
152  void sample(const QVector<Vector2D>& pts, QVector<Vector2D>& set, int setSize = 2) const;
153  double medianResiduals(const QVector<Vector2D>& pts, const Line& line) const;
154 };
155 
156 // pixel distance functions
157 namespace PixelDistance {
158  DllCoreExport double euclidean(const Pixel* px1, const Pixel* px2);
159  DllCoreExport double mahalanobis(const Pixel* px1, const Pixel* px2);
160  DllCoreExport double bhattacharyya(const Pixel* px1, const Pixel* px2);
161  DllCoreExport double angleWeighted(const Pixel* px1, const Pixel* px2);
162 
163  DllCoreExport typedef double (*PixelDistanceFunction)(const Pixel* px1, const Pixel* px2);
164 
165  DllCoreExport double spacingWeighted(const PixelEdge* edge);
166  DllCoreExport double orientationWeighted(const PixelEdge* edge);
167  DllCoreExport double euclidean(const PixelEdge* edge);
168  DllCoreExport typedef double (*EdgeWeightFunction)(const PixelEdge* edge);
169 }
170 
171 }
DllCoreExport double euclidean(const PixelEdge *edge)
Definition: Algorithms.cpp:454
#define DllCoreExport
Definition: BaseImageElement.h:43
DllCoreExport typedef double(* PixelDistanceFunction)(const Pixel *px1, const Pixel *px2)
Definition: Algorithms.h:163
Definition: Pixel.h:258
DllCoreExport double angleWeighted(const Pixel *px1, const Pixel *px2)
Angle weighted pixel distance. If the pixel&#39;s local orientations are not computed, this method defaults to the euclidean distance.
Definition: Algorithms.cpp:365
DllCoreExport typedef double(* EdgeWeightFunction)(const PixelEdge *edge)
Definition: Algorithms.h:168
Contains basic algorithms to manipulate images.
Definition: Algorithms.h:64
DllCoreExport double bhattacharyya(const Pixel *px1, const Pixel *px2)
Definition: Algorithms.cpp:331
DllCoreExport double mahalanobis(const Pixel *px1, const Pixel *px2)
Definition: Algorithms.cpp:315
A basic line class including stroke width (thickness).
Definition: Shapes.h:68
This class represents a single instance of super pixels which are needed for the layout analysis...
Definition: Pixel.h:188
static double statMoment(const QList< numFmt > &valuesIn, double momentValue, bool interpolated=true)
Computes robust statistical moments (quantiles).
Definition: Algorithms.h:89
Definition: Algorithms.cpp:45
QVector< Vector2D > mPts
Definition: Algorithms.h:150
Implements robust line fitting algorithms.
Definition: Algorithms.h:135
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
DllCoreExport double orientationWeighted(const PixelEdge *edge)
Definition: Algorithms.cpp:422