Read@CVL
PixelLabel.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 "Drawer.h"
36 #include "BaseImageElement.h"
37 
38 #pragma warning (disable: 4251) // inlined Qt functions in dll interface
39 
40 #pragma warning(push, 0) // no warnings from includes
41 #include <QColor>
42 #include <QStringList>
43 #include <QVector>
44 #pragma warning(pop)
45 
46 #ifndef DllCoreExport
47 #ifdef DLL_CORE_EXPORT
48 #define DllCoreExport Q_DECL_EXPORT
49 #else
50 #define DllCoreExport Q_DECL_IMPORT
51 #endif
52 #endif
53 
54 namespace cv {
55  namespace ml {
56  class StatModel;
57  class RTrees;
58  }
59 }
60 
61 // Qt defines
62 class QJsonObject;
63 class QPainter;
64 
65 namespace rdf {
66 
67 // read defines
68 class Pixel;
69 class Region;
70 
75 
76 public:
77 
79  label_unknown = 0, // unknown is not -1 because we only have unsigned bytes for labels
80 
81  label_end
82  };
83 
84  LabelInfo(int id = label_unknown, const QString& mName = QString());
85  bool operator== (const LabelInfo& l1) const;
86  bool operator!= (const LabelInfo& l1) const;
87  DllCoreExport friend QDataStream& operator<< (QDataStream& s, const LabelInfo& v);
88  DllCoreExport friend QDebug operator<< (QDebug d, const LabelInfo& v);
89 
90  bool isNull() const;
91  bool contains(const QString& key) const;
92 
93  int id() const;
94  QString name() const;
95  QColor color() const;
96  QColor visColor() const;
97  bool isBackground() const;
98 
99  QString toString() const;
100 
101  static LabelInfo fromString(const QString& str);
102 
103  void toJson(QJsonObject& jo) const;
104  static LabelInfo fromJson(const QJsonObject& jo);
105  static QString jsonKey();
106  static int color2Id(const QColor& col);
107 
108  // create default labels
109  static LabelInfo unknownLabel();
110 
111 protected:
112 
113  int mId = label_unknown;
114  bool mIsBackground = false;
115  QString mName = "unknown";
116  QStringList mAlias;
117  QColor mVisColor = ColorManager::darkGray();
118 };
119 
126 
127 public:
128  LabelManager();
129 
130  bool isEmpty() const;
131  int size() const;
132  static LabelManager read(const QString& filePath);
133  static LabelManager fromJson(const QJsonObject& jo);
134  void toJson(QJsonObject& jo) const;
135 
136  void add(const LabelInfo& label);
137  bool contains(const LabelInfo& label) const;
138  bool containsId(const LabelInfo& label) const;
139 
140  QString toString() const;
141 
142  LabelInfo find(const QString& str) const;
143  LabelInfo find(const Region& r) const;
144  LabelInfo find(int id) const;
145  int indexOf(int id) const;
146  LabelInfo backgroundLabel() const;
147 
148  QVector<LabelInfo> labelInfos() const;
149 
150  static QString jsonKey();
151 
152  void draw(QPainter& p) const;
153 
154 protected:
155  QVector<LabelInfo> mLookups;
156 };
157 
159 
160 public:
161  PixelVotes(const LabelManager& lm = LabelManager(), const QString& id = QString());
162 
163  bool isEmpty() const;
164 
165  void setRawVotes(const cv::Mat& rawVotes);
166  cv::Mat data() const;
167 
168  int labelIndex() const;
169 
170 protected:
172  cv::Mat mVotes;
173  double mNumTrees = 0;
174 
175  int maxVote() const;
176 };
177 
179 
180 public:
181  PixelLabel(const QString& id = QString());
182 
183  bool isEvaluated() const;
184 
185  void setLabel(const LabelInfo& label);
186  LabelInfo predicted() const;
187 
188  void setTrueLabel(const LabelInfo& label);
189  LabelInfo trueLabel() const;
190 
191  void setVotes(const PixelVotes& votes);
192  PixelVotes votes() const;
193 
194 protected:
195  LabelInfo mTrueLabel = LabelInfo::label_unknown;
196  LabelInfo mLabel = LabelInfo::label_unknown;
198 };
199 
201 
202 public:
203  SuperPixelModel(const LabelManager& labelManager = LabelManager(), const cv::Ptr<cv::ml::StatModel>& model = cv::Ptr<cv::ml::StatModel>());
204 
205  bool isEmpty() const;
206 
207  cv::Ptr<cv::ml::StatModel> model() const;
208  cv::Ptr<cv::ml::RTrees> randomTrees() const;
209  LabelManager manager() const;
210 
211  QVector<PixelLabel> classify(const cv::Mat& features) const;
212 
213  bool write(const QString& filePath) const;
214  static QSharedPointer<SuperPixelModel> read(const QString& filePath);
215 
216 protected:
217  cv::Ptr<cv::ml::StatModel> mModel;
219 
220  static cv::Ptr<cv::ml::RTrees> readRTreesModel(QJsonObject& jo);
221  void toJson(QJsonObject& jo) const;
222 
223 };
224 
225 }
QVector< LabelInfo > mLookups
Definition: PixelLabel.h:155
PixelVotes mVotes
Definition: PixelLabel.h:197
#define DllCoreExport
Definition: BaseImageElement.h:43
Definition: PixelLabel.h:158
QDataStream & operator<<(QDataStream &s, const BaseElement &e)
Definition: BaseImageElement.cpp:81
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
LabelManager mManager
Definition: PixelLabel.h:218
bool operator==(const cv::KeyPoint &kpl, const cv::KeyPoint &kpr)
Definition: ImageProcessor.cpp:45
Definition: PixelLabel.h:200
Definition: BaseImageElement.h:53
LabelManager mManager
Definition: PixelLabel.h:171
DllCoreExport QColor darkGray(double alpha=1.0)
Returns a dark gray.
Definition: Drawer.cpp:146
cv::Mat mVotes
Definition: PixelLabel.h:172
Definition: Elements.h:65
DefaultLabels
Definition: PixelLabel.h:78
Definition: Algorithms.cpp:45
Definition: PixelLabel.h:178
This class is used for mapping classes (e.g. handwriting, decoration)
Definition: PixelLabel.h:74
bool operator!=(const BaseElement &l, const BaseElement &r)
Returns true if l and r do not have the same id.
Definition: BaseImageElement.cpp:77
QSharedPointer< TextLineSet > find(const QString &id, const QVector< QSharedPointer< TextLineSet > > &tl)
cv::Ptr< cv::ml::StatModel > mModel
Definition: PixelLabel.h:217
QStringList mAlias
Definition: PixelLabel.h:116