Read@CVL
WriterDatabase.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 #pragma warning(push, 0) // no warnings from includes
38 // Qt Includes
39 #include <QString>
40 #include <QVector>
41 #include <opencv2/imgproc.hpp>
42 #include <opencv2/ml.hpp>
43 #pragma warning(pop)
44 
45 #ifndef DllCoreExport
46 #ifdef DLL_CORE_EXPORT
47 #define DllCoreExport Q_DECL_EXPORT
48 #else
49 #define DllCoreExport Q_DECL_IMPORT
50 #endif
51 #endif
52 
53 
54 #pragma warning(disable: 4251) // dll interface
55 
56 
57 // Qt defines
58 
59 namespace rdf {
60  class WriterImage;
61  class WriterVocabulary;
62 
64  public:
66 
67  int type() const;
68  void setType(int type);
69 
70  int numberOfClusters() const;
71  void setNumberOfCluster(int num);
72 
73  int numberOfPCA() const;
74  void setNumberOfPCA(int num);
75 
76  int numberOfPCAWhitening() const;
77  void setNumberOfPCAWhitening(int num);
78 
79  int maxSIFTSize() const;
80  void setMaxSIFTSize(int maxSize);
81 
82  int minSIFTSize() const;
83  void setMINSIFTSize(int minSize);
84 
85  double powerNormalization() const;
86  void setPowerNormalization(float power);
87 
88  bool l2before() const;
89  void setL2Before(bool performL2);
90 
91  //QString toString() const override;
92 
93  protected:
94  void load(const QSettings& settings) override;
95  void save(QSettings& settings) const override;
96 
97  private:
98  int mType = -1;
99  int mNumberOfClusters = 50;
100  int mNumberOfPCA = 96;
101  int mNumverOfPCAWhitening = 0;
102  int mMaxSIFTSize = 70;
103  int mMinSIFTSize = 20;
104  double mPowerNormalization = 0.5;
105  bool mL2NormBefore = false;
106  };
107 
109 
110  public:
112 
113  void loadVocabulary(const QString filePath);
114  void saveVocabulary(const QString filePath);
115 
116  cv::Mat calcualteDistanceMatrix(cv::Mat hists) const;
117 
118  bool isEmpty() const;
119 
120  enum type {
123 
124  WI_UNDEFINED
125  };
126 
127  void setVocabulary(cv::Mat voc);
128  cv::Mat vocabulary() const;
129  void setEM(cv::Ptr<cv::ml::EM> em);
130  cv::Ptr<cv::ml::EM> em() const;
131  void setPcaMean(cv::Mat mean);
132  cv::Mat pcaMean() const;
133  void setPcaEigenvectors(cv::Mat ev);
134  cv::Mat pcaEigenvectors() const;
135  void setPcaEigenvalues(cv::Mat ev);
136  cv::Mat pcaEigenvalues() const;
137  void setPcaWhiteMean(cv::Mat mean);
138  cv::Mat pcaWhiteMean() const;
139  void setPcaWhiteEigenvectors(cv::Mat ev);
140  cv::Mat pcaWhiteEigenvectors() const;
141  void setPcaWhiteEigenvalues(cv::Mat ev);
142  cv::Mat pcaWhiteEigenvalues() const;
143  void setL2Mean(const cv::Mat l2mean);
144  cv::Mat l2Mean() const;
145  void setL2Sigma(const cv::Mat l2sigma);
146  cv::Mat l2Sigma() const;
147  void setHistL2Mean(const cv::Mat mean);
148  cv::Mat histL2Mean() const;
149  void setHistL2Sigma(const cv::Mat sigma);
150  cv::Mat histL2Sigma() const;
151  void setNumberOfCluster(const int number);
152  int numberOfCluster() const;
153  void setNumberOfPCA(const int number);
154  int numberOfPCA() const;
155  void setType(const int type);
156  int type() const;
157  void setNote(QString note);
158  void setMinimumSIFTSize(const int size);
159  int minimumSIFTSize() const;
160  void setMaximumSIFTSize(const int size);
161  int maximumSIFTSize() const;
162  void setPowerNormalization(const double power);
163  double powerNormalization() const;
164  void setNumOfPCAWhiteComp(const int numOfComp);
165  int numberOfPCAWhiteningComponents() const;
166 
167  void setL2Before(const bool l2before);
168  bool l2before() const;
169 
170  QString note() const;
171  QString toString() const;
172 
173  QString vocabularyPath() const;
174 
175  cv::Mat generateHist(cv::Mat desc) const;
176 
177  cv::Mat applyPCA(cv::Mat desc) const;
178 
179  private:
184  QString debugName();
185  cv::Mat generateHistBOW(cv::Mat desc) const;
186  cv::Mat generateHistGMM(cv::Mat desc) const;
187 
188  cv::Mat l2Norm(cv::Mat desc, cv::Mat mean, cv::Mat sigma) const;
189 
190 
191  cv::Mat mVocabulary = cv::Mat();
192  cv::Ptr<cv::ml::EM> mEM;
193  cv::Mat mPcaMean = cv::Mat();
194  cv::Mat mPcaEigenvectors = cv::Mat();
195  cv::Mat mPcaEigenvalues = cv::Mat();
196  cv::Mat mPcaWhiteMean = cv::Mat();
197  cv::Mat mPcaWhiteEigenvectors = cv::Mat();
198  cv::Mat mPcaWhiteEigenvalues = cv::Mat();
199  cv::Mat mL2Mean = cv::Mat();
200  cv::Mat mL2Sigma = cv::Mat();
201  cv::Mat mHistL2Mean = cv::Mat();
202  cv::Mat mHistL2Sigma = cv::Mat();
203 
204  int mNumberOfClusters = -1;
205  int mNumberPCA = -1;
206  int mType = WI_UNDEFINED;
207  int mMinimumSIFTSize = -1;
208  int mMaximumSIFTSize = -1;
209  double mPowerNormalization = 1;
210  QString mNote = QString();
211 
212  QString mVocabularyPath = QString();
213  bool mL2Before = false;
214  int mNumPCAWhiteComponents = 0;
215  };
216 
217 // read defines
219 
220  public:
221  WriterDatabase();
222 
223  void addFile(const QString filePath);
224  void addFile(WriterImage wi);
225  void generateVocabulary();
226 
227  void setVocabulary(const WriterVocabulary voc);
228  WriterVocabulary vocabulary() const;
229  void saveVocabulary(QString filePath);
230 
231  void evaluateDatabase(QStringList classLabels, QStringList filePaths, QString filePath = QString());
232  void evaluateDatabase(cv::Mat hists, QStringList classLabels, QStringList filePaths, QString filePath = QString()) const;
233 
234  void writeCompetitionEvaluationFile(QStringList imageNames, QString outputPath) const;
235  void writeCompetitionEvaluationFile(cv::Mat hists, QStringList imageNames, QString outputPath) const;
236 
237  private:
238  QString debugName() const;
239  cv::Mat calculatePCA(const cv::Mat desc, bool normalizeBefore = false);
240  void generateBOW(cv::Mat desc);
241  void generateGMM(cv::Mat desc);
242  void writeMatToFile(const cv::Mat, const QString filePath) const;
243  void loadFeatures(const QString filePath, cv::Mat& descriptors, QVector<cv::KeyPoint>& keypoints);
244  QVector<QVector<cv::KeyPoint> > mKeyPoints;
245  QVector<cv::Mat> mDescriptors;
246  WriterVocabulary mVocabulary = WriterVocabulary();
247  };
248 }
Definition: WriterDatabase.h:218
Definition: BaseModule.h:63
#define DllCoreExport
Definition: BaseImageElement.h:43
type
Returns the vocabulary type.
Definition: WriterDatabase.h:120
Definition: WriterDatabase.h:121
Definition: WriterDatabase.h:122
Definition: WriterRetrieval.h:114
Definition: WriterDatabase.h:108
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
Definition: Algorithms.cpp:45
Definition: WriterDatabase.h:63