Read@CVL
SuperPixelTrainer.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 
38 #pragma warning(push, 0) // no warnings from includes
39 
40 #pragma warning(pop)
41 
42 #ifndef DllCoreExport
43 #ifdef DLL_CORE_EXPORT
44 #define DllCoreExport Q_DECL_EXPORT
45 #else
46 #define DllCoreExport Q_DECL_IMPORT
47 #endif
48 #endif
49 
50 namespace cv {
51  namespace ml {
52  class TrainData;
53  class RTrees;
54  }
55 }
56 
57 // Qt defines
58 
59 namespace rdf {
60 
61 // read defines
62 class Region;
63 class RootRegion;
64 class PageElement;
65 
71 
72 public:
73  FeatureCollection(const cv::Mat& descriptors = cv::Mat(), const LabelInfo& label = LabelInfo());
74  friend DllCoreExport bool operator==(const FeatureCollection& fcl, const FeatureCollection& fcr);
75 
76  QJsonObject toJson(const QString& filePath = "") const;
77  static FeatureCollection read(QJsonObject& jo, const QString& filePath = "");
78 
79  void append(const cv::Mat& descriptor);
80  LabelInfo label() const;
81  void setDescriptors(const cv::Mat& desc);
82  cv::Mat descriptors() const;
83  int numDescriptors() const;
84 
85  static QVector<FeatureCollection> split(const cv::Mat& descriptors, const PixelSet& set);
86  static QString jsonKey();
87 
88 protected:
89  cv::Mat mDesc;
91 };
92 
100 
101 public:
102  FeatureCollectionManager(const cv::Mat& descriptors = cv::Mat(), const PixelSet& set = PixelSet());
103 
104  bool isEmpty() const;
105 
106  void write(const QString& filePath) const;
107  static FeatureCollectionManager read(const QString& filePath);
108 
109  void add(const FeatureCollection& collection);
110  void merge(const FeatureCollectionManager& other);
111  void normalize(int minFeaturesPerClass, int maxFeaturesPerClass);
112 
113  QVector<FeatureCollection> collection() const;
114 
115  int numFeatures() const;
116 
117  QString toString() const;
118  cv::Ptr<cv::ml::TrainData> toCvTrainData(int maxSamples = -1) const;
119  LabelManager toLabelManager() const;
120 
121 protected:
122  QVector<FeatureCollection> mCollection;
123 
124  cv::Mat allFeatures() const;
125  cv::Mat allLabels() const;
126 };
127 
136 
137 public:
139 
140  QString featureFilePath() const;
141  QString labelConfigFilePath() const;
142  int maxNumFeaturesPerImage() const;
143  int minNumFeaturesPerClass() const;
144  int maxNumFeaturesPerClass() const;
145  QString backgroundLabelName() const;
146 
147  virtual QString toString() const override;
148 
149 protected:
150 
151  void load(const QSettings& settings) override;
152  void save(QSettings& settings) const override;
153 
156  QString mBackgroundLabelName = ""; // class name of background pixels (if empty, they are treated as unknown i.e. not trained)
157  int mMaxNumFeaturesPerImage = 1000000; // 1e6
158  int mMinNumFeaturesPerClass = 10000; // 1e4
159  int mMaxNumFeaturesPerClass = 10000; // 1e4;
160 
161 };
162 
170 
171 public:
172  SuperPixelLabeler(const QVector<QSharedPointer<MserBlob> >& blobs, const Rect& imgRect);
173  SuperPixelLabeler(const PixelSet& set, const Rect& imgRect);
174 
175  bool isEmpty() const override;
176  bool compute() override;
177  QSharedPointer<SuperPixelLabelerConfig> config() const;
178 
179  cv::Mat draw(const cv::Mat& img, bool drawPixels = true) const;
180  QString toString() const override;
181 
182  void setFilePath(const QString& filePath);
183  void setRootRegion(const QSharedPointer<RootRegion>& region);
184  void setLabelManager(const LabelManager& manager);
185  void setPage(const QSharedPointer<PageElement>& page);
186  QImage createLabelImage(const Rect& imgRect, bool visualize = false) const;
187 
188  PixelSet set() const;
189 
190 private:
191  QVector<QSharedPointer<MserBlob> > mBlobs;
192 
193  QSharedPointer<RootRegion> mGtRegion;
194  QSharedPointer<PageElement> mPage;
195  Rect mImgRect;
196  LabelManager mManager;
197  QString mGlobalName;
198 
199  // results
200  PixelSet mSet;
201 
202  bool checkInput() const override;
203  PixelSet labelBlobs(const cv::Mat& labelImg, const QVector<QSharedPointer<MserBlob> >& blobs) const;
204  PixelSet labelPixels(const cv::Mat& labelImg, const PixelSet& set) const;
205  QString parseLabel(const QString& filePath) const;
206 
207  void setBackgroundLabelName(const QString& name);
208 };
209 
211 
212 public:
214 
215  QStringList featureCachePaths() const;
216  QString modelPath() const;
217 
218  virtual QString toString() const override;
219 
220  void setNumTrees(int numTrees);
221  int numTrees() const;
222 
223 protected:
224 
225  QStringList mFeatureCachePaths;
226  QString mModelPath;
227  int mNumTrees = 150;
228 
229  void load(const QSettings& settings) override;
230  void save(QSettings& settings) const override;
231 };
232 
240 
241 public:
243 
244  bool isEmpty() const override;
245  bool compute() override;
246  QSharedPointer<SuperPixelTrainerConfig> config() const;
247 
248  cv::Mat draw(const cv::Mat& img) const;
249  QString toString() const override;
250 
251  // no read function -> see SuperPixelClassifier
252  bool write(const QString& filePath) const;
253  QSharedPointer<SuperPixelModel> model() const;
254 
255 private:
256 
257  FeatureCollectionManager mFeatureManager;
258 
259  // results
260  cv::Ptr<cv::ml::RTrees> mModel;
261 
262  bool checkInput() const override;
263 };
264 
265 }
QVector< FeatureCollection > mCollection
Definition: SuperPixelTrainer.h:122
QStringList mFeatureCachePaths
Definition: SuperPixelTrainer.h:225
QString mModelPath
Definition: SuperPixelTrainer.h:226
Definition: BaseModule.h:63
Manages FeatureCollections. Hence, each label (e.g. printed text) is stored here along with it&#39;s feat...
Definition: SuperPixelTrainer.h:99
#define DllCoreExport
Definition: BaseImageElement.h:43
PixelSet stores and manipulates pixel collections.
Definition: PixelSet.h:172
This class manages all labels loaded. It can be used to compare LabelInfo objects, and load them.
Definition: PixelLabel.h:125
Definition: Drawer.h:53
FeatureCollection maps one LabelInfo to its features. In addition it handles the I/O using Json...
Definition: SuperPixelTrainer.h:70
LabelInfo mLabel
Definition: SuperPixelTrainer.h:90
QString mFeatureFilePath
Definition: SuperPixelTrainer.h:154
bool operator==(const cv::KeyPoint &kpl, const cv::KeyPoint &kpr)
Definition: ImageProcessor.cpp:45
bool merge(const QSharedPointer< TextLineSet > &tl1, const QSharedPointer< TextLineSet > &tl2)
Definition: PixelSet.cpp:1728
config
Definition: DependencyCollector.py:271
Converts GT information to label images. This class is used to assign a GT label to each SuperPixel c...
Definition: SuperPixelTrainer.h:239
cv::Mat mDesc
Definition: SuperPixelTrainer.h:89
Converts GT information to label images. This class is used to assign a GT label to each SuperPixel c...
Definition: SuperPixelTrainer.h:169
DllCoreExport bool save(const QImage &img, const QString &savePath, int compression=-1)
Saves the specified QImage img.
Definition: Image.cpp:180
Definition: SuperPixelTrainer.h:210
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
This class is used for mapping classes (e.g. handwriting, decoration)
Definition: PixelLabel.h:74
QString mLabelConfigFilePath
Definition: SuperPixelTrainer.h:155
Definition: Shapes.h:344
This class configures the feature collection process. It controls I/O paths for feature labels and fe...
Definition: SuperPixelTrainer.h:135