CVB++ 14.0
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
12namespace Cvb
13{
14CVB_BEGIN_INLINE_NS
15
17namespace Polimago
18{
19
21
24{
25private:
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
55public:
57
62 RegressionPredictor (const String & fileName)
63 : RegressionPredictor (ReleaseObjectGuard(LoadInternal (fileName)))
64 {
65 fileName_ = fileName;
66 }
67
70
72 RegressionPredictor& operator=(RegressionPredictor&&) noexcept = default;
73
74 virtual ~RegressionPredictor ()
75 {}
76
78
85 static std::unique_ptr<RegressionPredictor> FromHandle (ReleaseObjectGuard&& guard)
86 {
87 if (!guard.Handle ())
88 {
89 throw std::invalid_argument ("invalid regression predictor native handle");
90 }
92 }
93
95
101 {
103 }
104
106
111 {
112 return static_cast<int>(CVB_CALL_CAPI(PMGetOutputDimension(Handle())));
113 }
114
116
123 {
124 VerifyCompatibility(img, pos);
125
127 CVB_CALL_CAPI_CHECKED (PMPredictVector(Handle(), img.Handle(), pos.X(), pos.Y(), &retval[0]));
128 return retval;
129 }
130
131private:
132 const std::string thisObjectName_ = "Polimago Regression Predictor";
133};
134
137
138
139
140} /* namespace Polimago */
141CVB_END_INLINE_NS
142} /* namespace Cvb */
The Common Vision Blox image.
Definition: decl_image.hpp:45
T X() const noexcept
Gets the x-component of the point.
Definition: point_2d.hpp:86
T Y() const noexcept
Gets the y-component of the point.
Definition: point_2d.hpp:106
void * Handle() const noexcept
Classic API Polimago handle.
Definition: predictor_base.hpp:66
Base class for Polimago predictors.
Definition: predictor_base.hpp:284
Polimago Regression predictor.
Definition: regression_predictor.hpp:24
std::vector< double > PredictVector(const Image &img, Point2D< int > pos) const
Calculate a regression result on a given location.
Definition: regression_predictor.hpp:122
RegressionPredictor(RegressionPredictor &&) noexcept=default
Move constructor.
int RegressionDimension() const
Regression result dimension.
Definition: regression_predictor.hpp:110
static std::unique_ptr< RegressionPredictor > Load(const String &fileName)
Load a saved predictor from a file.
Definition: regression_predictor.hpp:100
static std::unique_ptr< RegressionPredictor > FromHandle(ReleaseObjectGuard &&guard)
Creates predictor from a classic API handle.
Definition: regression_predictor.hpp:85
RegressionPredictor(const String &fileName)
Load a saved Polimago regression predictor from a file.
Definition: regression_predictor.hpp:62
Root namespace for the Image Manager interface.
Definition: c_barcode.h:24