CVB++ 15.0
predictor_base.hpp
1#pragma once
2
3#include "../_cexports/c_polimago.h"
4
5#include "../global.hpp"
6#include "../image.hpp"
7#include "../string.hpp"
8#include "../rect.hpp"
9#include "../size_2d.hpp"
10#include "../point_2d.hpp"
11
12#include <utility>
13
14namespace Cvb
15{
16 CVB_BEGIN_INLINE_NS
17
19 namespace Polimago
20 {
21
22 namespace Private
23 {
24 inline String GetPreprocessing(CExports::TFeatureMap fm)
25 {
26 std::string s(std::begin(fm.Code));
27 return String(s.begin(), s.end());
28 }
29 } /* namespace Private */
30
33 {
38 };
39
41
43 class PolimagoFactoryCreatedObject
44 {
45 public:
46 PolimagoFactoryCreatedObject(const PolimagoFactoryCreatedObject &other) = delete;
47 PolimagoFactoryCreatedObject &operator=(const PolimagoFactoryCreatedObject &other) = delete;
48 PolimagoFactoryCreatedObject(PolimagoFactoryCreatedObject &&other) = delete;
49 PolimagoFactoryCreatedObject &operator=(PolimagoFactoryCreatedObject &&other) = delete;
50 virtual ~PolimagoFactoryCreatedObject() = default;
51
52 protected:
53 explicit PolimagoFactoryCreatedObject(ReleaseObjectGuard &&guard)
54 : handle_(std::move(guard))
55 , trainingParameters_()
56 , fileName_()
57 {
58 }
59
60 public:
62
68 void *Handle() const noexcept
69 {
70 return handle_.Handle();
71 }
72
74
79 {
80 return fileName_;
81 }
82
84
89 void Save(const String &fileName) const
90 {
91 SaveFunction(fileName);
92 fileName_ = fileName;
93 }
94
96
100 double Lambda() const noexcept
101 {
102 return trainingParameters_.Lambda;
103 }
104
106
110 double Offset() const noexcept
111 {
112 return trainingParameters_.Offset;
113 }
114
116
121 {
122 if (trainingParameters_.FeatureMap.Interpolate == 0)
123 {
125 }
126 else
127 {
129 }
130 }
131
134
138 int ImagePlanes() const noexcept
139 {
140 return trainingParameters_.FeatureMap.NumImgPlanes;
141 }
142
144
149 {
150 return Rect<int>(trainingParameters_.FeatureMap.FWL, trainingParameters_.FeatureMap.FWT,
151 trainingParameters_.FeatureMap.FWR, trainingParameters_.FeatureMap.FWB);
152 }
153
156
160 Size2D<int> RetinaSize() const noexcept
161 {
162 return Size2D<int>(trainingParameters_.FeatureMap.RetinaW, trainingParameters_.FeatureMap.RetinaH);
163 }
164
167
172 {
173 return Point2D<double>(trainingParameters_.FeatureMap.CorrectX, trainingParameters_.FeatureMap.CorrectY);
174 }
175
177
182 {
183 return Private::GetPreprocessing(trainingParameters_.FeatureMap);
184 }
185
186 protected:
187 virtual void SaveFunction(const String &fileName) const = 0;
188 virtual std::string ObjectName() const = 0;
189
190 CExports::TTrainParams &TrainingParameters() noexcept
191 {
192 return trainingParameters_;
193 }
194
195 const CExports::TTrainParams &TrainingParameters() const noexcept
196 {
197 return trainingParameters_;
198 }
199
200 mutable String fileName_; // NOLINT(cppcoreguidelines-non-private-member-variables-in-classes)
201
202 private:
203 ReleaseObjectGuard handle_;
204 CExports::TTrainParams trainingParameters_;
205 };
206
209
211
213 class PredictorBase : public PolimagoFactoryCreatedObject
214 {
215 protected:
216 explicit PredictorBase(ReleaseObjectGuard &&guard)
217 : PolimagoFactoryCreatedObject(std::move(guard))
218 {
219 }
220
221 public:
223
230 bool IsCompatible(const Image &img, Point2D<int> pos) const
231 {
232 try
233 {
234 VerifyCompatibility(img, pos);
235 return true;
236 }
237 catch (std::exception &)
238 {
239 return false;
240 }
241 }
242
244
250 bool IsCompatible(const Image &img) const
251 {
252 try
253 {
254 VerifyCompatibility(img);
255 return true;
256 }
257 catch (std::exception &)
258 {
259 return false;
260 }
261 }
262
263 protected:
264 void VerifyCompatibility(const Image &img) const
265 {
266 if (img.PlanesCount() != ImagePlanes())
267 {
268 throw std::invalid_argument("The supplied image is not compatible with this classifier.");
269 }
270 if ((img.Width() < FeatureWindowExtent().Width()) || (img.Height() < FeatureWindowExtent().Height()))
271 {
272 throw std::invalid_argument("The supplied image is not compatible with this classifier.");
273 }
274 }
275
276 void VerifyCompatibility(const Image &img, Point2D<int> pos) const
277 {
278 VerifyCompatibility(img);
279 if ((pos.X() < abs(FeatureWindowExtent().Left())) || (pos.Y() < abs(FeatureWindowExtent().Top())))
280 {
281 throw std::invalid_argument(
282 "The selected position cannot be evaluated because there is not enough image data available around it.");
283 }
284 if (((pos.X() + FeatureWindowExtent().Right() >= img.Width()))
285 || ((pos.Y() + FeatureWindowExtent().Bottom() >= img.Height())))
286 {
287 throw std::invalid_argument(
288 "The selected position cannot be evaluated because there is not enough image data available around it.");
289 }
290 }
291 };
292
295
297
299 class PredictorBaseEx : public PredictorBase
300 {
301 protected:
302 explicit PredictorBaseEx(ReleaseObjectGuard &&guard)
303 : PredictorBase(std::move(guard))
304 {
305 CVB_CALL_CAPI(PMGetClfTrainParams(Handle(), TrainingParameters()));
306 }
307
308 public:
310
314 int FeatureResolution() const noexcept
315 {
316 return TrainingParameters().FeatureMap.FeatureResolution;
317 }
318 };
319
322
324
330 inline int GetGranularity(const String &preproCode)
331 {
332 std::string asciiCode(Internal::CastToAscii(preproCode));
333 return CVB_CALL_CAPI(PMGetGranularity(asciiCode.c_str()));
334 }
335
336 } /* namespace Polimago */
337 CVB_END_INLINE_NS
338} /* namespace Cvb */
The Common Vision Blox image.
Definition decl_image.hpp:50
Multi-purpose 2D vector class.
Definition point_2d.hpp:20
String Preprocessing() const
Preprocessing code with which this object was generated.
Definition predictor_base.hpp:181
Size2D< int > RetinaSize() const noexcept
Size of the 'Retina' in pixels. The retina is the set of paxels onto which the input image is project...
Definition predictor_base.hpp:160
double Offset() const noexcept
Intercept weight that has been used for generating this object.
Definition predictor_base.hpp:110
double Lambda() const noexcept
Regularization value that has been used for generating this object.
Definition predictor_base.hpp:100
Rect< int > FeatureWindowExtent() const noexcept
The feature window extent that has been used during classifier training.
Definition predictor_base.hpp:148
int ImagePlanes() const noexcept
The plane count of the images that have been used for generating this classifier. Image on which this...
Definition predictor_base.hpp:138
void Save(const String &fileName) const
Save this object into a file.
Definition predictor_base.hpp:89
Point2D< double > Correction() const noexcept
Correction factors in X and Y direction required to rescale the input images for projection onto the ...
Definition predictor_base.hpp:171
InterpolationType Interpolation() const noexcept
Interpolation setting used for generating this object.
Definition predictor_base.hpp:120
String FileName() const
Name of the file the object has been loaded from (or empty string if the object was not loaded).
Definition predictor_base.hpp:78
void * Handle() const noexcept
Classic API Polimago handle.
Definition predictor_base.hpp:68
int FeatureResolution() const noexcept
Feature resolution value with which the classifier was trained.
Definition predictor_base.hpp:314
bool IsCompatible(const Image &img) const
Verify the compatibility of a CVB image with this classifier.
Definition predictor_base.hpp:250
bool IsCompatible(const Image &img, Point2D< int > pos) const
Verify the compatibility of a CVB image with this classifier.
Definition predictor_base.hpp:230
Rectangle object.
Definition rect.hpp:24
Stores a pair of numbers that represents the width and the height of a subject, typically a rectangle...
Definition size_2d.hpp:20
T move(T... args)
Namespace for the Polimago package.
Definition classification_predictor.hpp:38
int GetGranularity(const String &preproCode)
The function returns the granularity associated with a preprocessing code.
Definition predictor_base.hpp:330
std::shared_ptr< PredictorBaseEx > PredictorBaseExPtr
Convenience shared pointer for PredictorBaseEx.
Definition predictor_base.hpp:321
std::shared_ptr< PredictorBase > PredictorBasePtr
Convenience shared pointer for PredictorBase.
Definition predictor_base.hpp:294
@ None
The enum element indicating undefined state.
Definition classification_predictor.hpp:44
InterpolationType
Interpolation to be used when extracting image data for classifier generation.
Definition predictor_base.hpp:33
@ Linear
Image data is (if necessary) extracted with linear interpolation.
Definition predictor_base.hpp:37
@ None
Image data is (if necessary) extracted without interpolation.
Definition predictor_base.hpp:35
std::shared_ptr< PolimagoFactoryCreatedObject > PolimagoFactoryCreatedObjectPtr
Convenience shared pointer for PolimagoFactoryCreatedObject.
Definition predictor_base.hpp:208
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
std::string String
String for wide characters or unicode characters.
Definition string.hpp:49