CVB++ 14.1
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{
16CVB_BEGIN_INLINE_NS
17
19namespace Polimago
20{
21
22namespace Private
23{
24inline 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{
35 None,
37 Linear
38};
39
41
44{
45public:
47 PolimagoFactoryCreatedObject& operator=(const PolimagoFactoryCreatedObject& other) = delete;
50 virtual ~PolimagoFactoryCreatedObject() = default;
51
52protected:
53 explicit PolimagoFactoryCreatedObject (ReleaseObjectGuard&& guard)
54 : handle_(std::move(guard)),
55 trainingParameters_(),
56 fileName_()
57 {}
58
59
60
61public:
63
69 void * Handle() const noexcept
70 {
71 return handle_.Handle();
72 }
73
75
80 {
81 return fileName_;
82 }
83
85
90 void Save (const String & fileName) const
91 {
92 SaveFunction (fileName);
93 fileName_ = fileName;
94 }
95
97
101 double Lambda() const noexcept
102 {
103 return trainingParameters_.Lambda;
104 }
105
107
111 double Offset() const noexcept
112 {
113 return trainingParameters_.Offset;
114 }
115
117
122 {
123 if (trainingParameters_.FeatureMap.Interpolate == 0)
124 {
126 }
127 else
128 {
130 }
131 }
132
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, trainingParameters_.FeatureMap.FWR, trainingParameters_.FeatureMap.FWB);
151 }
152
154
158 Size2D<int> RetinaSize() const noexcept
159 {
160 return Size2D<int> (trainingParameters_.FeatureMap.RetinaW, trainingParameters_.FeatureMap.RetinaH);
161 }
162
164
169 {
170 return Point2D<double> (trainingParameters_.FeatureMap.CorrectX, trainingParameters_.FeatureMap.CorrectY);
171 }
172
174
179 {
180 return Private::GetPreprocessing (trainingParameters_.FeatureMap);
181 }
182
183protected:
184 virtual void SaveFunction (const String &fileName) const = 0;
185 virtual std::string ObjectName () const = 0;
186
187 CExports::TTrainParams& TrainingParameters() noexcept
188 {
189 return trainingParameters_;
190 }
191
192 const CExports::TTrainParams& TrainingParameters() const noexcept
193 {
194 return trainingParameters_;
195 }
196
197 mutable String fileName_; // NOLINT(cppcoreguidelines-non-private-member-variables-in-classes)
198
199private:
200 ReleaseObjectGuard handle_;
201 CExports::TTrainParams trainingParameters_;
202
203};
204
207
209
212{
213protected:
214 explicit PredictorBase (ReleaseObjectGuard&& guard)
215 : PolimagoFactoryCreatedObject (std::move (guard))
216 {}
217
218
219public:
221
227 bool IsCompatible (const Image &img, Point2D<int> pos) const
228 {
229 try
230 {
231 VerifyCompatibility (img, pos);
232 return true;
233 }
234 catch (std::exception &)
235 {
236 return false;
237 }
238 }
239
241
246 bool IsCompatible (const Image &img) const
247 {
248 try
249 {
250 VerifyCompatibility (img);
251 return true;
252 }
253 catch (std::exception &)
254 {
255 return false;
256 }
257 }
258
259protected:
260 void VerifyCompatibility (const Image &img) const
261 {
262 if (img.PlanesCount () != ImagePlanes ())
263 {
264 throw std::invalid_argument ("The supplied image is not compatible with this classifier.");
265 }
266 if ((img.Width () < FeatureWindowExtent ().Width ()) || (img.Height () < FeatureWindowExtent ().Height ()))
267 {
268 throw std::invalid_argument ("The supplied image is not compatible with this classifier.");
269 }
270 }
271
272 void VerifyCompatibility (const Image &img, Point2D<int> pos) const
273 {
274 VerifyCompatibility (img);
275 if ((pos.X () < abs (FeatureWindowExtent ().Left ())) || (pos.Y () < abs (FeatureWindowExtent ().Top ())))
276 {
277 throw std::invalid_argument ("The selected position cannot be evaluated because there is not enough image data available around it.");
278 }
279 if (((pos.X () + FeatureWindowExtent ().Right () >= img.Width ())) || ((pos.Y () + FeatureWindowExtent ().Bottom () >= img.Height ())))
280 {
281 throw std::invalid_argument ("The selected position cannot be evaluated because there is not enough image data available around it.");
282 }
283 }
284};
285
288
290
293{
294protected:
295 explicit PredictorBaseEx (ReleaseObjectGuard&& guard)
296 : PredictorBase (std::move (guard))
297 {
298 CVB_CALL_CAPI(PMGetClfTrainParams(Handle(), TrainingParameters()));
299 }
300
301
302public:
304
308 int FeatureResolution() const noexcept
309 {
310 return TrainingParameters().FeatureMap.FeatureResolution;
311 }
312};
313
316
317
319
325inline int GetGranularity (const String &preproCode)
326{
327 std::string asciiCode (Internal::CastToAscii(preproCode));
328 return CVB_CALL_CAPI(PMGetGranularity(asciiCode.c_str()));
329}
330
331
332} /* namespace Polimago */
333CVB_END_INLINE_NS
334} /* namespace Cvb */
The Common Vision Blox image.
Definition: decl_image.hpp:45
Base class for Polimago objects created by one of the factory classes.
Definition: predictor_base.hpp:44
String Preprocessing() const
Preprocessing code with which this object was generated.
Definition: predictor_base.hpp:178
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:158
double Offset() const noexcept
Intercept weight that has been used for generating this object.
Definition: predictor_base.hpp:111
double Lambda() const noexcept
Regularization value that has been used for generating this object.
Definition: predictor_base.hpp:101
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:90
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:168
InterpolationType Interpolation() const noexcept
Interpolation setting used for generating this object.
Definition: predictor_base.hpp:121
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:79
void * Handle() const noexcept
Classic API Polimago handle.
Definition: predictor_base.hpp:69
Base class for Polimago predictors.
Definition: predictor_base.hpp:293
int FeatureResolution() const noexcept
Feature resolution value with which the classifier was trained.
Definition: predictor_base.hpp:308
Base class for Polimago predictors.
Definition: predictor_base.hpp:212
bool IsCompatible(const Image &img) const
Verify the compatibility of a CVB image with this classifier.
Definition: predictor_base.hpp:246
bool IsCompatible(const Image &img, Point2D< int > pos) const
Verify the compatibility of a CVB image with this classifier.
Definition: predictor_base.hpp:227
@ Private
Private name space.
int GetGranularity(const String &preproCode)
The function returns the granularity associated with a preprocessing code.
Definition: predictor_base.hpp:325
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.
@ None
Image data is (if necessary) extracted without interpolation.
Root namespace for the Image Manager interface.
Definition: c_barcode.h:24
std::string String
String for wide characters or unicode characters.
Definition: string.hpp:56