Read@CVL
SuperPixelScaleSpace.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 "SuperPixel.h"
36 
37 #include "Image.h"
38 #include "ImageProcessor.h"
39 #include "Settings.h"
40 
41 #pragma warning(push, 0) // no warnings from includes
42 #include <opencv2/core.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 // Qt defines
54 
55 namespace rdf {
56 
57 // read defines
58 
60 
61 public:
63 
64  virtual QString toString() const override;
65 
66  int numLayers() const;
67  int minLayer() const;
68 
69 protected:
70  int mNumLayers = 3;
71  int mMinLayer = 0;
72 
73  void load(const QSettings& settings) override;
74  void save(QSettings& settings) const override;
75 };
76 
83 template <class SuperPixelModule>
85  static_assert(std::is_base_of<SuperPixelBase, SuperPixelModule>::value, "T must derive from SuperPixelBase"); // requires C++11
86 
87 public:
88  ScaleSpaceSuperPixel(const cv::Mat & img) : SuperPixelBase(img) {
89  mConfig = QSharedPointer<ScaleSpaceSPConfig>::create();
90  }
91 
92  bool compute() override {
93 
94  if (!checkInput())
95  return false;
96 
97  Timer dt;
98 
99  cv::Mat img = mSrcImg.clone();
100  img = IP::grayscale(img);
101  cv::normalize(img, img, 255, 0, cv::NORM_MINMAX);
102 
103  Config::instance().global().setNumScales(config()->numLayers());
104 
105  int idCnt = 0;
106 
107  for (int idx = 0; idx < config()->numLayers(); idx++) {
108 
109  // compute super pixel
110  if (config()->minLayer() <= idx) {
111 
112  SuperPixelModule spm(img);
113  spm.setPyramidLevel(idx);
114  qDebug() << "computing new layer...";
115 
116  // get super pixels of the current scale
117  if (!spm.compute())
118  mWarning << "could not compute super pixels for layer #" << idx;
119 
120  PixelSet set = spm.pixelSet();
121 
122  // assign the pyramid level
123  for (auto p : set.pixels()) {
124  p->setPyramidLevel(idx);
125  // make ID unique for scale space
126  p->setId(QString::number(idCnt));
127  idCnt++;
128  }
129 
130  if (idx > 0) {
131 
132  // re-scale
133  double sf = std::pow(2, idx);
134  set.scale(sf);
135  }
136 
137  mSet += set;
138  }
139 
140  cv::resize(img, img, cv::Size(), 0.5, 0.5, CV_INTER_AREA);
141  }
142 
143  // filter from all scales
144  mSet.filterDuplicates();
145  mInfo << mSet.size() << "pixels extracted in" << dt;
146 
147  return true;
148  }
149 
150  cv::Mat draw(const cv::Mat & img, const QColor& col = QColor()) const {
151 
152  QImage qImg = Image::mat2QImage(img, true);
153 
154  QPainter p(&qImg);
155 
156  QVector<PixelSet> s = mSet.splitScales();
157  for (int idx = s.size() - 1; idx >= 0; idx--) {
158 
159  if (!col.isValid())
160  p.setPen(ColorManager::randColor());
161  p.setOpacity(0.5);
163  //qDebug() << "scale" << idx << ":" << *s[idx];
164  }
165 
166  return Image::qImage2Mat(qImg);
167  }
168 
169  QString toString() const override {
170  return config()->toString();
171  }
172 
173  QSharedPointer<ScaleSpaceSPConfig> config() const {
174  return qSharedPointerDynamicCast<ScaleSpaceSPConfig>(mConfig);
175  }
176 
177 };
178 
179 // NOTE: we need this instantiation for the DllExport...
180 // -> you have to add each SuperPixel module that you want to export
181 // or you get an unresolved external
182 template class ScaleSpaceSuperPixel<SuperPixel>;
184 
185 
186 }
ScaleSpaceSuperPixel(const cv::Mat &img)
Definition: SuperPixelScaleSpace.h:88
Definition: Pixel.h:233
Definition: Utils.h:128
Definition: BaseModule.h:63
static Config & instance()
Definition: Settings.cpp:101
#define DllCoreExport
Definition: BaseImageElement.h:43
bool compute() override
Runs the algorithm implemented by the module.
Definition: SuperPixelScaleSpace.h:92
DllCoreExport QColor randColor(double alpha=1.0)
Returns a pleasent color.
Definition: Drawer.cpp:79
PixelSet stores and manipulates pixel collections.
Definition: PixelSet.h:172
Flags turns enums into typesave flags It is strongly related (copied) from Useage: enum mDrawFlags { ...
Definition: Utils.h:166
DllCoreExport QImage mat2QImage(const cv::Mat &img, bool toRGB=false)
Converts a cv::Mat to QImage.
Definition: Image.cpp:106
#define mInfo
Definition: BaseModule.h:59
QString toString() const override
Converts the module&#39;s parameters and results to a string.
Definition: SuperPixelScaleSpace.h:169
Definition: SuperPixelScaleSpace.h:59
static cv::Mat grayscale(const cv::Mat &src)
Returns the Luminance channel of Luv which is better than the RGB2Gray.
Definition: ImageProcessor.cpp:69
Creates a scale space and runs the SuperPixelModule on each scale. Use this class to extract SuperPix...
Definition: SuperPixelScaleSpace.h:84
static GlobalConfig & global()
Definition: Settings.cpp:109
cv::Mat draw(const cv::Mat &img, const QColor &col=QColor()) const
Definition: SuperPixelScaleSpace.h:150
config
Definition: DependencyCollector.py:271
void setId(const QString &id)
Sets the (preferably unique) ID. If no ID is set, a unqiue ID is generated and assigned.
Definition: BaseImageElement.cpp:99
Base class implementation for SuperPixel generating modules.
Definition: SuperPixel.h:68
#define mWarning
Definition: BaseModule.h:60
DllCoreExport bool save(const QImage &img, const QString &savePath, int compression=-1)
Saves the specified QImage img.
Definition: Image.cpp:180
QSharedPointer< ScaleSpaceSPConfig > config() const
Definition: SuperPixelScaleSpace.h:173
DllCoreExport QImage load(const QString &path, bool *ok=0)
Definition: Image.cpp:152
Definition: Algorithms.cpp:45
Definition: Pixel.h:232
Definition: PixelSet.h:187
void setNumScales(int ns)
Definition: Settings.cpp:74
DllCoreExport cv::Mat qImage2Mat(const QImage &img)
Converts a QImage to a cv::Mat.
Definition: Image.cpp:60