regression_predictor.hpp
1 #pragma once
2 
3 #include "../_cexports/c_polimago.h"
4 
5 #include "../global.hpp"
6 #include "../string.hpp"
7 #include "predictor_base.hpp"
8 
9 #include <utility>
10 #include <vector>
11 
12 namespace Cvb
13 {
14 CVB_BEGIN_INLINE_NS
15 
17 namespace Polimago
18 {
19 
21 
24 {
25 private:
26  // Internal helper constructor version
27  RegressionPredictor (ReleaseObjectGuard&& guard)
28  : PredictorBaseEx (std::move (guard))
29  {
30  if (trainingParameters_.Usage != CExports::TClassifierUsage::CU_Regression)
31  {
32  throw std::runtime_error (std::string("The object is not a ") + thisObjectName_);
33  }
34  }
35 
36  // Helper to load a saved predictor from file
37  static CExports::TCLF LoadInternal (const String & fileName)
38  {
39  CExports::TCLF predictor = nullptr;
40 
41  CVB_CALL_CAPI_CHECKED (PMOpenClfTyped(fileName.c_str(), predictor));
42  return predictor;
43  }
44 
45  void SaveFunction (const String &fileName) const override
46  {
47  CVB_CALL_CAPI_CHECKED (PMSaveClfTyped(fileName.c_str(), Handle()));
48  }
49 
50  std::string ObjectName () const override
51  {
52  return thisObjectName_;
53  }
54 
55 public:
57 
61  RegressionPredictor (const String & fileName)
62  : RegressionPredictor (ReleaseObjectGuard(LoadInternal (fileName)))
63  {
64  fileName_ = fileName;
65  }
66 
68  RegressionPredictor (RegressionPredictor&&) noexcept = default;
69 
71  RegressionPredictor& operator=(RegressionPredictor&&) noexcept = default;
72 
73  virtual ~RegressionPredictor ()
74  {}
75 
77 
84  static std::unique_ptr<RegressionPredictor> FromHandle (ReleaseObjectGuard&& guard)
85  {
86  if (!guard.Handle ())
87  {
88  throw std::invalid_argument ("invalid regression predictor native handle");
89  }
90  return std::unique_ptr<RegressionPredictor>(new RegressionPredictor(std::move(guard)));
91  }
92 
94 
100  {
102  }
103 
105 
110  {
111  return static_cast<int>(CVB_CALL_CAPI(PMGetOutputDimension(Handle())));
112  }
113 
115 
122  {
123  VerifyCompatibility(img, pos);
124 
126  CVB_CALL_CAPI_CHECKED (PMPredictVector(Handle(), img.Handle(), pos.X(), pos.Y(), &retval[0]));
127  return retval;
128  }
129 
130 private:
131  const std::string thisObjectName_ = "Polimago Regression Predictor";
132 };
133 
136 
137 
138 
139 } /* namespace Polimago */
140 CVB_END_INLINE_NS
141 } /* namespace Cvb */
RegressionPredictor(const String &fileName)
Load a saved Polimago regression predictor from a file.
Definition: regression_predictor.hpp:61
STL class.
std::vector< double > PredictVector(const Image &img, Point2D< int > pos) const
Calculate a regression result on a given location.
Definition: regression_predictor.hpp:121
void * Handle() const noexcept
Classic API Polimago handle.
Definition: predictor_base.hpp:66
STL class.
Root namespace for the Image Manager interface.
Definition: version.hpp:11
The Common Vision Blox image.
Definition: decl_image.hpp:44
T Y() const noexcept
Gets the y-component of the point.
Definition: point_2d.hpp:106
Base class for Polimago predictors.
Definition: predictor_base.hpp:283
Polimago Regression predictor.
Definition: regression_predictor.hpp:23
void * Handle() const noexcept
Classic API image handle.
Definition: decl_image.hpp:223
T X() const noexcept
Gets the x-component of the point.
Definition: point_2d.hpp:86
STL class.
static std::unique_ptr< RegressionPredictor > Load(const String &fileName)
Load a saved predictor from a file.
Definition: regression_predictor.hpp:99
int RegressionDimension() const
Regression result dimension.
Definition: regression_predictor.hpp:109
static std::unique_ptr< RegressionPredictor > FromHandle(ReleaseObjectGuard &&guard)
Creates predictor from a classic API handle.
Definition: regression_predictor.hpp:84