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 
14 namespace 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 (fm.Code);
27  return String (s.begin (), s.end ());
28 }
29 } /* namespace Private */
30 
33 {
35  None,
37  Linear
38 };
39 
41 
44 {
45 protected:
46  PolimagoFactoryCreatedObject (ReleaseObjectGuard&& guard)
47  : handle_(std::move(guard)),
48  trainingParameters_(),
49  fileName_()
50  {}
51 
53 
54  PolimagoFactoryCreatedObject& operator=(PolimagoFactoryCreatedObject&&) noexcept = default;
55 
56  virtual ~PolimagoFactoryCreatedObject () = default;
57 
58 public:
60 
66  void * Handle() const noexcept
67  {
68  return handle_.Handle();
69  }
70 
72 
76  String FileName() const
77  {
78  return fileName_;
79  }
80 
82 
87  void Save (const String & fileName) const
88  {
89  SaveFunction (fileName);
90  fileName_ = fileName;
91  }
92 
94 
98  double Lambda() const noexcept
99  {
100  return trainingParameters_.Lambda;
101  }
102 
104 
108  double Offset() const noexcept
109  {
110  return trainingParameters_.Offset;
111  }
112 
114 
119  {
120  if (trainingParameters_.FeatureMap.Interpolate == 0)
121  {
123  }
124  else
125  {
127  }
128  }
129 
131 
135  int ImagePlanes() const noexcept
136  {
137  return trainingParameters_.FeatureMap.NumImgPlanes;
138  }
139 
141 
146  {
147  return Rect<int> (trainingParameters_.FeatureMap.FWL, trainingParameters_.FeatureMap.FWT, trainingParameters_.FeatureMap.FWR, trainingParameters_.FeatureMap.FWB);
148  }
149 
151 
155  Size2D<int> RetinaSize() const noexcept
156  {
157  return Size2D<int> (trainingParameters_.FeatureMap.RetinaW, trainingParameters_.FeatureMap.RetinaH);
158  }
159 
161 
165  Point2D<double> Correction() const noexcept
166  {
167  return Point2D<double> (trainingParameters_.FeatureMap.CorrectX, trainingParameters_.FeatureMap.CorrectY);
168  }
169 
171 
176  {
177  return Private::GetPreprocessing (trainingParameters_.FeatureMap);
178  }
179 
180 protected:
181  virtual void SaveFunction (const String &fileName) const = 0;
182  virtual std::string ObjectName () const = 0;
183 
184 private:
185  ReleaseObjectGuard handle_;
186 protected:
187  CExports::TTrainParams trainingParameters_;
188  mutable String fileName_;
189 };
190 
193 
195 
198 {
199 protected:
200  PredictorBase (ReleaseObjectGuard&& guard)
201  : PolimagoFactoryCreatedObject (std::move (guard))
202  {}
203 
204  PredictorBase (PredictorBase&&) noexcept = default;
205 
206  PredictorBase& operator=(PredictorBase&&) noexcept = default;
207 
208  virtual ~PredictorBase () = default;
209 
210 public:
212 
218  bool IsCompatible (const Image &img, Point2D<int> pos) const
219  {
220  try
221  {
222  VerifyCompatibility (img, pos);
223  return true;
224  }
225  catch (std::exception &)
226  {
227  return false;
228  }
229  }
230 
232 
237  bool IsCompatible (const Image &img) const
238  {
239  try
240  {
241  VerifyCompatibility (img);
242  return true;
243  }
244  catch (std::exception &)
245  {
246  return false;
247  }
248  }
249 
250 protected:
251  void VerifyCompatibility (const Image &img) const
252  {
253  if (img.PlanesCount () != ImagePlanes ())
254  {
255  throw std::invalid_argument ("The supplied image is not compatible with this classifier.");
256  }
257  if ((img.Width () < FeatureWindowExtent ().Width ()) || (img.Height () < FeatureWindowExtent ().Height ()))
258  {
259  throw std::invalid_argument ("The supplied image is not compatible with this classifier.");
260  }
261  }
262 
263  void VerifyCompatibility (const Image &img, Point2D<int> pos) const
264  {
265  VerifyCompatibility (img);
266  if ((pos.X () < abs (FeatureWindowExtent ().Left ())) || (pos.Y () < abs (FeatureWindowExtent ().Top ())))
267  {
268  throw std::invalid_argument ("The selected position cannot be evaluated because there is not enough image data available around it.");
269  }
270  if (((pos.X () + FeatureWindowExtent ().Right () >= img.Width ())) || ((pos.Y () + FeatureWindowExtent ().Bottom () >= img.Height ())))
271  {
272  throw std::invalid_argument ("The selected position cannot be evaluated because there is not enough image data available around it.");
273  }
274  }
275 };
276 
279 
281 
284 {
285 protected:
286  PredictorBaseEx (ReleaseObjectGuard&& guard)
287  : PredictorBase (std::move (guard))
288  {
289  CVB_CALL_CAPI(PMGetClfTrainParams(Handle(), trainingParameters_));
290  }
291 
292  PredictorBaseEx (PredictorBaseEx&&) noexcept = default;
293 
294  PredictorBaseEx& operator=(PredictorBaseEx&&) noexcept = default;
295 
296  virtual ~PredictorBaseEx () = default;
297 
298 public:
300 
304  int FeatureResolution() const noexcept
305  {
306  return trainingParameters_.FeatureMap.FeatureResolution;
307  }
308 };
309 
312 
313 
315 
321 inline int GetGranularity (const String &preproCode)
322 {
323  std::string asciiCode (Internal::CastToAscii(preproCode));
324  return CVB_CALL_CAPI(PMGetGranularity(asciiCode.c_str()));
325 }
326 
327 
328 } /* namespace Polimago */
329 CVB_END_INLINE_NS
330 } /* namespace Cvb */
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:135
T Width() const noexcept
Gets the width of the rectangle.
Definition: rect.hpp:171
double Offset() const noexcept
Intercept weight that has been used for generating this object.
Definition: predictor_base.hpp:108
STL class.
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:76
InterpolationType Interpolation() const noexcept
Interpolation setting used for generating this object.
Definition: predictor_base.hpp:118
double Lambda() const noexcept
Regularization value that has been used for generating this object.
Definition: predictor_base.hpp:98
Rect< int > FeatureWindowExtent() const noexcept
The feature window extent that has been used during classifier training.
Definition: predictor_base.hpp:145
Base class for Polimago objects created by one of the factory classes.
Definition: predictor_base.hpp:43
std::string String
String for wide characters or unicode characters.
Definition: string.hpp:45
bool IsCompatible(const Image &img) const
Verify the compatibility of a CVB image with this classifier.
Definition: predictor_base.hpp:237
Base class for Polimago predictors.
Definition: predictor_base.hpp:197
Image data is (if necessary) extracted with linear interpolation.
T Bottom() const noexcept
Gets bottom row of the rectangle (still inside the rectangle).
Definition: rect.hpp:151
void * Handle() const noexcept
Classic API Polimago handle.
Definition: predictor_base.hpp:66
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:155
STL class.
Root namespace for the Image Manager interface.
Definition: version.hpp:11
The Common Vision Blox image.
Definition: decl_image.hpp:44
String Preprocessing() const
Preprocessing code with which this object was generated.
Definition: predictor_base.hpp:175
int GetGranularity(const String &preproCode)
The function returns the granularity associated with a preprocessing code.
Definition: predictor_base.hpp:321
Base class for Polimago predictors.
Definition: predictor_base.hpp:283
T Right() const noexcept
Gets rightmost column of the rectangle (still inside the rectangle).
Definition: rect.hpp:131
int PlanesCount() const noexcept
Get the number of planes for this image.
Definition: decl_image.hpp:230
bool IsCompatible(const Image &img, Point2D< int > pos) const
Verify the compatibility of a CVB image with this classifier.
Definition: predictor_base.hpp:218
STL class.
T Height() const noexcept
Gets the height of the rectangle.
Definition: rect.hpp:191
The enum element indicating undefined state.
void Save(const String &fileName) const
Save this object into a file.
Definition: predictor_base.hpp:87
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:165
int Width() const noexcept
Width of the image in pixels.
Definition: decl_image.hpp:285
int FeatureResolution() const noexcept
Feature resolution value with which the classifier was trained.
Definition: predictor_base.hpp:304
InterpolationType
Interpolation to be used when extracting image data for classifier generation.
Definition: predictor_base.hpp:32
int Height() const noexcept
Height of the image in pixels.
Definition: decl_image.hpp:278