CVB++ 15.0
search_predictor.hpp
1#pragma once
2
3#include "../_cexports/c_polimago.h"
4
5#include "../global.hpp"
6#include "../value_range.hpp"
7#include "../angle.hpp"
8#include "predictor_base.hpp"
9#include "search_result.hpp"
10
11#include <vector>
12
13namespace Cvb
14{
15 CVB_BEGIN_INLINE_NS
16
18 namespace Polimago
19 {
20
22 enum class InvarianceType
23 {
25 None = 0,
27 Translation = CExports::TI_Translations,
29 RotationScaleTranslation = CExports::TI_RotScaleTrans,
31 AffineGroup = CExports::TI_AffineGroup
32 };
33
34 // forward declaration
35 namespace Training
36 {
37 class SearchPredictorFactory;
38 }
39
41
43 class SearchPredictor : public PredictorBase
44 {
45 private:
46 // Internal helper constructor version
47 explicit SearchPredictor(ReleaseObjectGuard &&guard) // NOLINT(cppcoreguidelines-pro-type-member-init)
48 : PredictorBase(std::move(guard))
49 , maxNumResults_(maxNumResultsDefault_)
50 {
51 CVB_CALL_CAPI(PMGetSearchClfTrainParams(Handle(), TrainingParameters(), searchTrainingParameters_));
52 InitResultBuffer();
53 }
54
55 void InitResultBuffer()
56 {
57 pbuffer_.assign(MaxNumResults(), CExports::TSearchResult());
58 }
59
60 // Helper to load a saved predictor from file
61 static CExports::TSCLF LoadInternal(const String &fileName)
62 {
63 CExports::TSCLF predictor = nullptr;
64
65 CVB_CALL_CAPI_CHECKED(PMOpenSearchClfTyped(fileName.c_str(), predictor));
66 return predictor;
67 }
68
69 void SaveFunction(const String &fileName) const override
70 {
71 CVB_CALL_CAPI_CHECKED(PMSaveSearchClfTyped(fileName.c_str(), Handle()));
72 }
73
74 std::string ObjectName() const override
75 {
76 return thisObjectName_;
77 }
78
79 public:
81
85 explicit SearchPredictor(const String &fileName)
86 : SearchPredictor(ReleaseObjectGuard(LoadInternal(fileName)))
87 {
88 fileName_ = fileName;
89 }
90
92
99 static std::unique_ptr<SearchPredictor> FromHandle(ReleaseObjectGuard &&guard)
100 {
101 if (!guard.Handle())
102 {
103 throw std::invalid_argument("invalid search predictor native handle");
104 }
105 return std::unique_ptr<SearchPredictor>(new SearchPredictor(std::move(guard)));
106 }
107
109
115 {
116 return std::make_unique<SearchPredictor>(fileName);
117 }
118
120
124 int MaxNumResults() const noexcept
125 {
126 return maxNumResults_;
127 }
128
131
135 void SetMaxNumResults(int maxNumResults)
136 {
137 if (maxNumResults == maxNumResults_)
138 {
139 return;
140 }
141 if (maxNumResults < 1)
142 {
143 throw std::invalid_argument("max num results value must be >0");
144 }
145 maxNumResults_ = maxNumResults;
146 InitResultBuffer();
147 }
148
150
154 int SampleSize() const noexcept
155 {
156 return searchTrainingParameters_.SampleSize;
157 }
158
160
171 double ExtractionRadius() const noexcept
172 {
173 return searchTrainingParameters_.InvarianceParams.XYRadius;
174 }
175
177
182 {
183 return ValueRange<Angle>(Angle::FromRadians(searchTrainingParameters_.InvarianceParams.MinAngle),
184 Angle::FromRadians(searchTrainingParameters_.InvarianceParams.MaxAngle));
185 }
186
188
193 {
194 return ValueRange<double>(searchTrainingParameters_.InvarianceParams.MinScaleMinL,
195 searchTrainingParameters_.InvarianceParams.MaxScaleMaxL);
196 }
197
199
204 {
205 return static_cast<InvarianceType>(searchTrainingParameters_.InvarianceParams.InvarianceType);
206 }
207
209
213 int NumClassificationSteps() const noexcept
214 {
215 return searchTrainingParameters_.NumClfs;
216 }
217
219
223 int FeatureResolutionStep1And2() const noexcept
224 {
225 return searchTrainingParameters_.Resolution12;
226 }
227
229
233 int FeatureResolutionRest() const noexcept
234 {
235 return searchTrainingParameters_.ResolutionRest;
236 }
237
239
255 std::vector<SearchResult> GridSearch(const Image &img, Rect<int> aoi, double gridStep, double threshold,
256 double locality, int &numCalls)
257 {
258 VerifyCompatibility(img);
259
260 CExports::cvbval_t numCallsOut = 0;
261 auto resultCount =
262 CVB_CALL_CAPI(PMGridSearch(Handle(), img.Handle(), aoi.Left(), aoi.Top(), aoi.Right(), aoi.Bottom(),
263 gridStep, threshold, locality, maxNumResults_, &pbuffer_[0], numCallsOut));
264 numCalls = static_cast<int>(numCallsOut);
265
267 for (decltype(resultCount) i = 0; i < resultCount; ++i)
268 {
269 retval.push_back(SearchResult(pbuffer_[i], Handle()));
270 }
271 return retval;
272 }
273
275
289 std::vector<SearchResult> GridSearch(const Image &img, Rect<int> aoi, double gridStep, double threshold,
290 double locality)
291 {
292 int numCalls = 0;
293 return GridSearch(img, aoi, gridStep, threshold, locality, numCalls);
294 }
295
298
312 bool Inspect(const Image &img, SearchResult &res, int &searchDepth)
313 {
314 VerifyCompatibility(img);
315 CExports::TSearchResult tmpRes = res.resInternal_;
316 CExports::cvbval_t tmpDepth = searchDepth;
317
318 auto retval = CVB_CALL_CAPI(PMInspect(Handle(), img.Handle(), tmpRes, tmpDepth));
319 if (retval)
320 {
321 res = SearchResult(tmpRes, Handle());
322 searchDepth = static_cast<int>(tmpDepth);
323 }
324 return retval != 0;
325 }
326
329
344 bool Inspect(const Image &img, SearchResult &res, int &searchDepth, std::vector<SearchResult> &trace)
345 {
346 VerifyCompatibility(img);
347 CExports::TSearchResult tmpRes = res.resInternal_;
348 CExports::cvbval_t tmpDepth = searchDepth;
349 CExports::TDiagnostics diag = {};
350
351 auto retval = CVB_CALL_CAPI(PMInspectD(Handle(), img.Handle(), tmpRes, tmpDepth, diag));
352 if (retval)
353 {
354 res = SearchResult(tmpRes, Handle());
355 searchDepth = static_cast<int>(tmpDepth);
356 }
357
358 trace.clear();
359 for (int i = 0; i < searchDepth; ++i)
360 {
361 trace.push_back(SearchResult(diag[i], Handle()));
362 }
363
364 return retval != 0;
365 }
366
368
377 {
378 return Internal::DoBoolCallObjectOut<Image>([&](void *&resimg) {
379 return CVB_CALL_CAPI(PMSearchResultToImage(Handle(), sourceImage.Handle(), res.resInternal_.G, resimg));
380 });
381 }
382
386
387 private:
388 static const int maxNumResultsDefault_ = 4096;
389 const std::string thisObjectName_ = "Polimago Search Predictor";
390 CExports::TTrainSearchParams searchTrainingParameters_;
391 CExports::cvbval_t maxNumResults_;
393 };
394
397
398 } /* namespace Polimago */
399 CVB_END_INLINE_NS
400} /* namespace Cvb */
static Angle FromRadians(double rad, bool trim=false) noexcept
Create an angle in radians.
Definition angle.hpp:39
The Common Vision Blox image.
Definition decl_image.hpp:50
void * Handle() const noexcept
Classic API image handle.
Definition decl_image.hpp:237
void * Handle() const noexcept
Classic API Polimago handle.
Definition predictor_base.hpp:68
int NumClassificationSteps() const noexcept
Get the number of classification steps defined during training.
Definition search_predictor.hpp:213
SearchPredictor(const String &fileName)
Load a saved Polimago search predictor from a file.
Definition search_predictor.hpp:85
std::unique_ptr< Image > SearchResultToImage(const Image &sourceImage, SearchResult res)
Create a visual representation of a search result.
Definition search_predictor.hpp:376
ValueRange< double > ScaleRange() const noexcept
Get the range of scale factors that was covered during classifier training.
Definition search_predictor.hpp:192
int MaxNumResults() const noexcept
Get the maximum number of results that can be extracted in a GridSearch operation.
Definition search_predictor.hpp:124
bool Inspect(const Image &img, SearchResult &res, int &searchDepth)
Carries out the operation that GridSearch executes for a grid point, starting at the perspective and ...
Definition search_predictor.hpp:312
double ExtractionRadius() const noexcept
Get the radius for extracting positive search instances.
Definition search_predictor.hpp:171
std::vector< SearchResult > GridSearch(const Image &img, Rect< int > aoi, double gridStep, double threshold, double locality)
Perform a grid search.
Definition search_predictor.hpp:289
int FeatureResolutionStep1And2() const noexcept
Get the feature resolution to be used for the first two classification steps.
Definition search_predictor.hpp:223
std::vector< SearchResult > GridSearch(const Image &img, Rect< int > aoi, double gridStep, double threshold, double locality, int &numCalls)
Perform a grid search.
Definition search_predictor.hpp:255
static std::unique_ptr< SearchPredictor > Load(const String &fileName)
Load a saved predictor from a file.
Definition search_predictor.hpp:114
static std::unique_ptr< SearchPredictor > FromHandle(ReleaseObjectGuard &&guard)
Creates predictor from a classic API handle.
Definition search_predictor.hpp:99
bool Inspect(const Image &img, SearchResult &res, int &searchDepth, std::vector< SearchResult > &trace)
Carries out the operation that GridSearch executes for a grid point, starting at the perspective and ...
Definition search_predictor.hpp:344
ValueRange< Angle > RotationRange() const noexcept
Get the range of angles that was covered during classifier training.
Definition search_predictor.hpp:181
void SetMaxNumResults(int maxNumResults)
Set the maximum number of results that can be extracted in a GridSearch operation....
Definition search_predictor.hpp:135
int FeatureResolutionRest() const noexcept
Get the feature resolution to be used for the third and later classification steps.
Definition search_predictor.hpp:233
int SampleSize() const noexcept
Get the sample size that has been used in each training set.
Definition search_predictor.hpp:154
InvarianceType Invariances() const noexcept
Get the invariances that have been trained on this classifier.
Definition search_predictor.hpp:203
Search results as provided by a Search Classifier.
Definition search_result.hpp:21
Factory class for the generation of search predictors.
Definition search_predictor_factory.hpp:30
Rectangle object.
Definition rect.hpp:24
Container for range definitions.
Definition value_range.hpp:17
T move(T... args)
Namespace for the Polimago package.
Definition classification_predictor.hpp:38
std::shared_ptr< SearchPredictor > SearchPredictorPtr
Convenience shared pointer for SearchPredictor.
Definition search_predictor.hpp:396
@ None
The enum element indicating undefined state.
Definition classification_predictor.hpp:44
InvarianceType
Invariance types that can be defined for training.
Definition search_predictor.hpp:23
@ AffineGroup
Affine group (i.e. 2x2 matrix plus translation).
Definition search_predictor.hpp:31
@ Translation
Translation.
Definition search_predictor.hpp:27
@ RotationScaleTranslation
Rotation + Scale + Translation.
Definition search_predictor.hpp:29
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