ReadFramework
GraphCut.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 "PixelSet.h"
37 #include "Image.h"
38 
39 #pragma warning(push, 0) // no warnings from includes
40 // Qt Includes
41 #pragma warning(pop)
42 
43 #ifndef DllCoreExport
44 #ifdef DLL_CORE_EXPORT
45 #define DllCoreExport Q_DECL_EXPORT
46 #else
47 #define DllCoreExport Q_DECL_IMPORT
48 #endif
49 #endif
50 
51 // Qt defines
53 
54 namespace rdf {
55 
56 // read defines
57 class PixelGraph;
58 
60 
61 public:
62  GraphCutConfig(const QString& name = "Graph Cut");
63 
64  double scaleFactor() const;
65  int numIter() const;
66 
67 protected:
68  void load(const QSettings& settings) override;
69  void save(QSettings& settings) const override;
70 
71  double mScaleFactor = 1000.0; // scale factor to use (faster) int instead of double
72  int mGcIter = 2; // # iterations of graph-cut (expansion)
73 };
74 
80 
81 public:
82  GraphCutPixel(const PixelSet& set);
83 
84  virtual bool isEmpty() const override;
85 
86  QSharedPointer<GraphCutConfig> config() const;
87 
88  // results - available after compute() is called
89  PixelSet set() const;
90 
91 protected:
92 
93  // input/output
96  QSharedPointer<PixelConnector> mConnector;
97 
105  QSharedPointer<GCoptimizationGeneralGraph> graphCut(const PixelGraph& graph) const;
106 
116  virtual cv::Mat costs(int numLabels) const = 0;
117 
125  virtual cv::Mat labelDistMatrix(int numLabels) const = 0;
126 
127  virtual int numLabels() const = 0;
128 };
129 
135 
136 public:
137  GraphCutOrientation(const PixelSet& set);
138 
139  virtual bool compute() override;
140 
141  cv::Mat draw(const cv::Mat& img, const QColor& col = QColor()) const;
142 
143 private:
144 
145  bool checkInput() const override;
146 
147  cv::Mat costs(int numLabels) const override;
148  cv::Mat labelDistMatrix(int numLabels) const override;
149  int numLabels() const override;
150 };
151 
157 
158 public:
159  GraphCutPixelLabel(const PixelSet& set);
160 
161  virtual bool compute() override;
162 
163  cv::Mat draw(const cv::Mat& img, const QColor& col = QColor()) const;
164 
165  void setLabelManager(const LabelManager& m);
166 
167 private:
168 
169  bool checkInput() const override;
170 
171  cv::Mat costs(int numLabels) const override;
172  cv::Mat labelDistMatrix(int numLabels) const override;
173  int numLabels() const override;
174 
175  LabelManager mManager;
176 };
177 
179 
180 public:
182 
183  int numLabels() const;
184 
185 protected:
186  void load(const QSettings& settings) override;
187  void save(QSettings& settings) const override;
188 
189  int mNumLabels = 15; // number of scale bins (empirically set to 15)
190 };
191 
192 
198 
199 public:
200  GraphCutLineSpacing(const PixelSet& set);
201 
202  virtual bool compute() override;
203 
204  QSharedPointer<GraphCutLineSpacingConfig> config() const;
205 
206  cv::Mat draw(const cv::Mat& img, const QColor& col = QColor()) const;
207 
208 private:
209 
210  bool checkInput() const override;
211 
212  cv::Mat costs(int numLabels) const override;
213  cv::Mat labelDistMatrix(int numLabels) const override;
214  int numLabels() const override;
215 
216  Histogram spacingHist() const;
217  //cv::Mat spacingHist() const;
218 
219  Histogram mSpaceHist;
220 };
221 
227 
228 public:
229  GraphCutTextLine(const QVector<PixelSet>& sets);
230 
231  virtual bool compute() override;
232 
233  cv::Mat draw(const cv::Mat& img, const QColor& col = QColor()) const;
234 
235  QVector<PixelSet> textLines();
236 
237 private:
238 
239  QVector<PixelSet> mTextLines;
240 
241  bool checkInput() const override;
242 
243  cv::Mat costs(int numLabels) const override;
244  cv::Mat labelDistMatrix(int numLabels) const override;
245  int numLabels() const override;
246 
247  cv::Mat mahalanobisDists(const PixelSet& tl, const cv::Mat& centers) const;
248  cv::Mat euclideanDists(const PixelSet& tl) const;
249  cv::Mat pixelSetCentersToMat(const PixelSet& set) const;
250 
251  void saveDistsDebug(const QString& filePath, const cv::Mat& img) const;
252 
253  template <typename num>
254  cv::Mat makeSymmetric(const cv::Mat& m) const {
255 
256  assert(m.cols == m.rows);
257  cv::Mat s = m.clone();
258 
259  // make it a metric (symmetric)
260  num* lp = s.ptr<num>();
261 
262  for (int rIdx = 0; rIdx < s.rows; rIdx++) {
263 
264  for (int cIdx = rIdx+1; cIdx < s.cols; cIdx++) {
265 
266  num* rl = lp + (rIdx * s.rows + cIdx); // row label
267  num* cl = lp + (rIdx + cIdx * s.cols); // col label
268 
269  num val = qMin(*rl, *cl);
270  *rl = val;
271  *cl = val;
272  }
273  }
274 
275  return s;
276  }
277 };
278 
279 }
PixelDistance::EdgeWeightFunction mWeightFnc
Definition: GraphCut.h:95
PixelSet mSet
Definition: GraphCut.h:94
Definition: BaseModule.h:63
Graph cut for local orientation estimation.
Definition: GraphCut.h:134
#define DllCoreExport
Definition: BaseImageElement.h:43
PixelSet stores and manipulates pixel collections.
Definition: PixelSet.h:172
QSharedPointer< PixelConnector > mConnector
Definition: GraphCut.h:96
This class manages all labels loaded. It can be used to compare LabelInfo objects, and load them.
Definition: PixelLabel.h:125
Definition: GraphCut.h:178
The base class for all graphcuts operating on pixels.
Definition: GraphCut.h:79
Textline clustering using graph-cut.
Definition: GraphCut.h:226
Graph cut for line spacing.
Definition: GraphCut.h:197
DllCoreExport typedef double(* EdgeWeightFunction)(const PixelEdge *edge)
Definition: Algorithms.h:168
Represents a pixel graph. This class comes in handy if you want to map pixel edges with pixels...
Definition: PixelSet.h:377
Definition: GraphCut.h:59
config
Definition: DependencyCollector.py:271
Definition: GCoptimization.h:569
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
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
Graph cut for pixel labeling.
Definition: GraphCut.h:156
Definition: Image.h:62