CVB++ 14.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{
15CVB_BEGIN_INLINE_NS
16
18namespace Polimago
19{
20
23{
25 None = 0,
27 Translation = CExports::TI_Translations,
29 RotationScaleTranslation = CExports::TI_RotScaleTrans,
31 AffineGroup = CExports::TI_AffineGroup
32};
33
34// forward declaration
35namespace Training { class SearchPredictorFactory; }
36
38
41{
42private:
43 // Internal helper constructor version
44 SearchPredictor (ReleaseObjectGuard&& guard)
45 : PredictorBase (std::move (guard))
46 {
47 CVB_CALL_CAPI (PMGetSearchClfTrainParams (Handle (), trainingParameters_, searchTrainingParameters_));
48 maxNumResults_ = maxNumResultsDefault_;
49 InitResultBuffer ();
50 }
51
52 void InitResultBuffer()
53 {
54 pbuffer_.assign (MaxNumResults (), CExports::TSearchResult ());
55 }
56
57 // Helper to load a saved predictor from file
58 static CExports::TSCLF LoadInternal (const String & fileName)
59 {
60 CExports::TSCLF predictor = nullptr;
61
62 CVB_CALL_CAPI_CHECKED (PMOpenSearchClfTyped(fileName.c_str(), predictor));
63 return predictor;
64 }
65
66 void SaveFunction (const String &fileName) const override
67 {
68 CVB_CALL_CAPI_CHECKED (PMSaveSearchClfTyped(fileName.c_str(), Handle()));
69 }
70
71 std::string ObjectName () const override
72 {
73 return thisObjectName_;
74 }
75
76public:
78
83 SearchPredictor (const String & fileName)
84 : SearchPredictor (ReleaseObjectGuard(LoadInternal (fileName)))
85 {
86 fileName_ = fileName;
87 }
88
90 SearchPredictor (SearchPredictor&&) noexcept = default;
91
93 SearchPredictor& operator=(SearchPredictor&&) noexcept = default;
94
95 virtual ~SearchPredictor ()
96 {}
97
99
106 static std::unique_ptr<SearchPredictor> FromHandle (ReleaseObjectGuard&& guard)
107 {
108 if (!guard.Handle ())
109 {
110 throw std::invalid_argument ("invalid search predictor native handle");
111 }
112 return std::unique_ptr<SearchPredictor>(new SearchPredictor(std::move(guard)));
113 }
114
116
122 {
124 }
125
127
131 int MaxNumResults() const noexcept
132 {
133 return maxNumResults_;
134 }
135
137
141 void SetMaxNumResults(int maxNumResults)
142 {
143 if (maxNumResults == maxNumResults_)
144 {
145 return;
146 }
147 if (maxNumResults < 1)
148 {
149 throw std::invalid_argument ("max num results value must be >0");
150 }
151 maxNumResults_ = maxNumResults;
152 InitResultBuffer();
153 }
154
156
160 int SampleSize() const noexcept
161 {
162 return searchTrainingParameters_.SampleSize;
163 }
164
166
177 double ExtractionRadius() const noexcept
178 {
179 return searchTrainingParameters_.InvarianceParams.XYRadius;
180 }
181
183
188 {
189 return ValueRange<Angle>(Angle::FromRadians(searchTrainingParameters_.InvarianceParams.MinAngle),
190 Angle::FromRadians(searchTrainingParameters_.InvarianceParams.MaxAngle));
191 }
192
194
199 {
200 return ValueRange<double>(searchTrainingParameters_.InvarianceParams.MinScaleMinL, searchTrainingParameters_.InvarianceParams.MaxScaleMaxL);
201 }
202
204
209 {
210 return static_cast<InvarianceType>(searchTrainingParameters_.InvarianceParams.InvarianceType);
211 }
212
214
218 int NumClassificationSteps() const noexcept
219 {
220 return searchTrainingParameters_.NumClfs;
221 }
222
224
228 int FeatureResolutionStep1And2() const noexcept
229 {
230 return searchTrainingParameters_.Resolution12;
231 }
232
234
238 int FeatureResolutionRest() const noexcept
239 {
240 return searchTrainingParameters_.ResolutionRest;
241 }
242
244
259 std::vector<SearchResult> GridSearch (const Image & img, Rect<int> aoi, double gridStep, double threshold, double locality, int &numCalls)
260 {
261 VerifyCompatibility(img);
262
263 CExports::cvbval_t numCallsOut = 0;
264 auto resultCount = CVB_CALL_CAPI (PMGridSearch(Handle(), img.Handle(), aoi.Left(), aoi.Top(), aoi.Right(), aoi.Bottom(), gridStep, threshold, locality, maxNumResults_, &pbuffer_[0], numCallsOut));
265 numCalls = static_cast<int>(numCallsOut);
266
268 for (decltype(resultCount) i = 0; i < resultCount; ++i)
269 {
270 retval.push_back (SearchResult (pbuffer_[i], Handle ()));
271 }
272 return retval;
273 }
274
276
290 std::vector<SearchResult> GridSearch (const Image & img, Rect<int> aoi, double gridStep, double threshold, double locality)
291 {
292 int numCalls;
293 return GridSearch (img, aoi, gridStep, threshold, locality, numCalls);
294 }
295
297
309 bool Inspect (const Image & img, SearchResult &res, int &searchDepth)
310 {
311 VerifyCompatibility(img);
312 CExports::TSearchResult tmpRes = res.resInternal_;
313 CExports::cvbval_t tmpDepth = searchDepth;
314
315 auto retval = CVB_CALL_CAPI(PMInspect(Handle(), img.Handle(), tmpRes, tmpDepth));
316 if (retval)
317 {
318 res = SearchResult (tmpRes, Handle());
319 searchDepth = static_cast<int>(tmpDepth);
320 }
321 return retval != 0;
322 }
323
325
338 bool Inspect (const Image & img, SearchResult &res, int &searchDepth, std::vector<SearchResult> &trace)
339 {
340 VerifyCompatibility(img);
341 CExports::TSearchResult tmpRes = res.resInternal_;
342 CExports::cvbval_t tmpDepth = searchDepth;
343 CExports::TDiagnostics diag = {};
344
345 auto retval = CVB_CALL_CAPI(PMInspectD(Handle(), img.Handle(), tmpRes, tmpDepth, diag));
346 if (retval)
347 {
348 res = SearchResult (tmpRes, Handle());
349 searchDepth = static_cast<int>(tmpDepth);
350 }
351
352 trace.clear ();
353 for (int i = 0; i < searchDepth; ++i)
354 {
355 trace.push_back (SearchResult (diag[i], Handle ()));
356 }
357
358 return retval != 0;
359 }
360
362
370 {
371 return Internal::DoBoolCallObjectOut<Image>([&](void* & resimg)
372 {
373 return CVB_CALL_CAPI(PMSearchResultToImage(Handle(), sourceImage.Handle(), res.resInternal_.G, resimg));
374 });
375 }
376
380
381private:
382 static const int maxNumResultsDefault_ = 4096;
383 const std::string thisObjectName_ = "Polimago Search Predictor";
384 CExports::TTrainSearchParams searchTrainingParameters_;
385 CExports::cvbval_t maxNumResults_;
387};
388
391
392
393
394
395} /* namespace Polimago */
396CVB_END_INLINE_NS
397} /* namespace Cvb */
static Angle FromRadians(double rad, bool trim=false) noexcept
Create an angle in radians.
Definition: angle.hpp:44
The Common Vision Blox image.
Definition: decl_image.hpp:45
void * Handle() const noexcept
Classic API image handle.
Definition: decl_image.hpp:223
void * Handle() const noexcept
Classic API Polimago handle.
Definition: predictor_base.hpp:66
Base class for Polimago predictors.
Definition: predictor_base.hpp:198
Predictor that may be used for searching objects.
Definition: search_predictor.hpp:41
int NumClassificationSteps() const noexcept
Get the number of classification steps defined during training.
Definition: search_predictor.hpp:218
SearchPredictor(const String &fileName)
Load a saved Polimago search predictor from a file.
Definition: search_predictor.hpp:83
std::unique_ptr< Image > SearchResultToImage(const Image &sourceImage, SearchResult res)
Create a visual representation of a search result.
Definition: search_predictor.hpp:369
ValueRange< double > ScaleRange() const noexcept
Get the range of scale factors that was covered during classifier training.
Definition: search_predictor.hpp:198
int MaxNumResults() const noexcept
Get the maximum number of results that can be extracted in a GridSearch operation.
Definition: search_predictor.hpp:131
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:309
double ExtractionRadius() const noexcept
Get the radius for extracting positive search instances.
Definition: search_predictor.hpp:177
std::vector< SearchResult > GridSearch(const Image &img, Rect< int > aoi, double gridStep, double threshold, double locality)
Perform a grid search.
Definition: search_predictor.hpp:290
int FeatureResolutionStep1And2() const noexcept
Get the feature resolution to be used for the first two classification steps.
Definition: search_predictor.hpp:228
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:259
static std::unique_ptr< SearchPredictor > Load(const String &fileName)
Load a saved predictor from a file.
Definition: search_predictor.hpp:121
static std::unique_ptr< SearchPredictor > FromHandle(ReleaseObjectGuard &&guard)
Creates predictor from a classic API handle.
Definition: search_predictor.hpp:106
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:338
ValueRange< Angle > RotationRange() const noexcept
Get the range of angles that was covered during classifier training.
Definition: search_predictor.hpp:187
void SetMaxNumResults(int maxNumResults)
Set the maximum number of results that can be extracted in a GridSearch operation....
Definition: search_predictor.hpp:141
SearchPredictor(SearchPredictor &&) noexcept=default
Move constructor.
int FeatureResolutionRest() const noexcept
Get the feature resolution to be used for the third and later classification steps.
Definition: search_predictor.hpp:238
int SampleSize() const noexcept
Get the sample size that has been used in each training set.
Definition: search_predictor.hpp:160
InvarianceType Invariances() const noexcept
Get the invariances that have been trained on this classifier.
Definition: search_predictor.hpp:208
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
Container for range definitions.
Definition: value_range.hpp:17
InvarianceType
Invariance types that can be defined for training.
Definition: search_predictor.hpp:23
@ AffineGroup
Affine group (i.e. 2x2 matrix plus translation).
@ RotationScaleTranslation
Rotation + Scale + Translation.
Root namespace for the Image Manager interface.
Definition: c_barcode.h:24