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 
13 namespace 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 { class SearchPredictorFactory; }
36 
38 
41 {
42 private:
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 
76 public:
78 
82  SearchPredictor (const String & fileName)
83  : SearchPredictor (ReleaseObjectGuard(LoadInternal (fileName)))
84  {
85  fileName_ = fileName;
86  }
87 
89  SearchPredictor (SearchPredictor&&) noexcept = default;
90 
92  SearchPredictor& operator=(SearchPredictor&&) noexcept = default;
93 
94  virtual ~SearchPredictor ()
95  {}
96 
98 
105  static std::unique_ptr<SearchPredictor> FromHandle (ReleaseObjectGuard&& guard)
106  {
107  if (!guard.Handle ())
108  {
109  throw std::invalid_argument ("invalid search predictor native handle");
110  }
111  return std::unique_ptr<SearchPredictor>(new SearchPredictor(std::move(guard)));
112  }
113 
115 
120  static std::unique_ptr<SearchPredictor> Load (const String & fileName)
121  {
123  }
124 
126 
130  int MaxNumResults() const noexcept
131  {
132  return maxNumResults_;
133  }
134 
136 
140  void SetMaxNumResults(int maxNumResults)
141  {
142  if (maxNumResults == maxNumResults_)
143  {
144  return;
145  }
146  if (maxNumResults < 1)
147  {
148  throw std::invalid_argument ("max num results value must be >0");
149  }
150  maxNumResults_ = maxNumResults;
151  InitResultBuffer();
152  }
153 
155 
159  int SampleSize() const noexcept
160  {
161  return searchTrainingParameters_.SampleSize;
162  }
163 
165 
176  double ExtractionRadius() const noexcept
177  {
178  return searchTrainingParameters_.InvarianceParams.XYRadius;
179  }
180 
182 
187  {
188  return ValueRange<Angle>(Angle::FromRadians(searchTrainingParameters_.InvarianceParams.MinAngle),
189  Angle::FromRadians(searchTrainingParameters_.InvarianceParams.MaxAngle));
190  }
191 
193 
198  {
199  return ValueRange<double>(searchTrainingParameters_.InvarianceParams.MinScaleMinL, searchTrainingParameters_.InvarianceParams.MaxScaleMaxL);
200  }
201 
203 
207  InvarianceType Invariances() const noexcept
208  {
209  return static_cast<InvarianceType>(searchTrainingParameters_.InvarianceParams.InvarianceType);
210  }
211 
213 
217  int NumClassificationSteps() const noexcept
218  {
219  return searchTrainingParameters_.NumClfs;
220  }
221 
223 
227  int FeatureResolutionStep1And2() const noexcept
228  {
229  return searchTrainingParameters_.Resolution12;
230  }
231 
233 
237  int FeatureResolutionRest() const noexcept
238  {
239  return searchTrainingParameters_.ResolutionRest;
240  }
241 
243 
258  std::vector<SearchResult> GridSearch (const Image & img, Rect<int> aoi, double gridStep, double threshold, double locality, int &numCalls)
259  {
260  VerifyCompatibility(img);
261 
262  CExports::cvbval_t numCallsOut = 0;
263  auto resultCount = CVB_CALL_CAPI (PMGridSearch(Handle(), img.Handle(), aoi.Left(), aoi.Top(), aoi.Right(), aoi.Bottom(), 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, double locality)
290  {
291  int numCalls;
292  return GridSearch (img, aoi, gridStep, threshold, locality, numCalls);
293  }
294 
296 
308  bool Inspect (const Image & img, SearchResult &res, int &searchDepth)
309  {
310  VerifyCompatibility(img);
311  CExports::TSearchResult tmpRes = res.resInternal_;
312  CExports::cvbval_t tmpDepth = searchDepth;
313 
314  auto retval = CVB_CALL_CAPI(PMInspect(Handle(), img.Handle(), tmpRes, tmpDepth));
315  if (retval)
316  {
317  res = SearchResult (tmpRes, Handle());
318  searchDepth = static_cast<int>(tmpDepth);
319  }
320  return retval != 0;
321  }
322 
324 
337  bool Inspect (const Image & img, SearchResult &res, int &searchDepth, std::vector<SearchResult> &trace)
338  {
339  VerifyCompatibility(img);
340  CExports::TSearchResult tmpRes = res.resInternal_;
341  CExports::cvbval_t tmpDepth = searchDepth;
342  CExports::TDiagnostics diag = {};
343 
344  auto retval = CVB_CALL_CAPI(PMInspectD(Handle(), img.Handle(), tmpRes, tmpDepth, diag));
345  if (retval)
346  {
347  res = SearchResult (tmpRes, Handle());
348  searchDepth = static_cast<int>(tmpDepth);
349  }
350 
351  trace.clear ();
352  for (int i = 0; i < searchDepth; ++i)
353  {
354  trace.push_back (SearchResult (diag[i], Handle ()));
355  }
356 
357  return retval != 0;
358  }
359 
361 
369  {
370  return Internal::DoBoolCallObjectOut<Image>([&](void* & resimg)
371  {
372  return CVB_CALL_CAPI(PMSearchResultToImage(Handle(), sourceImage.Handle(), res.resInternal_.G, resimg));
373  });
374  }
375 
379 
380 private:
381  static const int maxNumResultsDefault_ = 4096;
382  const std::string thisObjectName_ = "Polimago Search Predictor";
383  CExports::TTrainSearchParams searchTrainingParameters_;
384  CExports::cvbval_t maxNumResults_;
386 };
387 
390 
391 
392 
393 
394 } /* namespace Polimago */
395 CVB_END_INLINE_NS
396 } /* namespace Cvb */
STL class.
T Top() const noexcept
Gets first row of the rectangle.
Definition: rect.hpp:111
std::unique_ptr< Image > SearchResultToImage(const Image &sourceImage, SearchResult res)
Create a visual representation of a search result.
Definition: search_predictor.hpp:368
InvarianceType
Invariance types that can be defined for training.
Definition: search_predictor.hpp:22
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:337
Base class for Polimago predictors.
Definition: predictor_base.hpp:197
Affine group (i.e. 2x2 matrix plus translation).
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:258
SearchPredictor(const String &fileName)
Load a saved Polimago search predictor from a file.
Definition: search_predictor.hpp:82
T Bottom() const noexcept
Gets bottom row of the rectangle (still inside the rectangle).
Definition: rect.hpp:151
static Angle FromRadians(double rad, bool trim=false) noexcept
Create an angle in radians.
Definition: angle.hpp:44
int SampleSize() const noexcept
Get the sample size that has been used in each training set.
Definition: search_predictor.hpp:159
ValueRange< Angle > RotationRange() const noexcept
Get the range of angles that was covered during classifier training.
Definition: search_predictor.hpp:186
T Left() const noexcept
Gets first column of the rectangle.
Definition: rect.hpp:91
void * Handle() const noexcept
Classic API Polimago handle.
Definition: predictor_base.hpp:66
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:308
int FeatureResolutionRest() const noexcept
Get the feature resolution to be used for the third and later classification steps.
Definition: search_predictor.hpp:237
STL class.
Root namespace for the Image Manager interface.
Definition: version.hpp:11
Search results as provided by a Search Classifier.
Definition: search_result.hpp:20
The Common Vision Blox image.
Definition: decl_image.hpp:44
InvarianceType Invariances() const noexcept
Get the invariances that have been trained on this classifier.
Definition: search_predictor.hpp:207
T Right() const noexcept
Gets rightmost column of the rectangle (still inside the rectangle).
Definition: rect.hpp:131
static std::unique_ptr< SearchPredictor > Load(const String &fileName)
Load a saved predictor from a file.
Definition: search_predictor.hpp:120
void * Handle() const noexcept
Classic API image handle.
Definition: decl_image.hpp:223
double ExtractionRadius() const noexcept
Get the radius for extracting positive search instances.
Definition: search_predictor.hpp:176
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
STL class.
STL class.
The enum element indicating undefined state.
Predictor that may be used for searching objects.
Definition: search_predictor.hpp:40
Factory class for the generation of search predictors.
Definition: search_predictor_factory.hpp:29
int NumClassificationSteps() const noexcept
Get the number of classification steps defined during training.
Definition: search_predictor.hpp:217
Container for range definitions.
Definition: value_range.hpp:16
int MaxNumResults() const noexcept
Get the maximum number of results that can be extracted in a GridSearch operation.
Definition: search_predictor.hpp:130
ValueRange< double > ScaleRange() const noexcept
Get the range of scale factors that was covered during classifier training.
Definition: search_predictor.hpp:197
int FeatureResolutionStep1And2() const noexcept
Get the feature resolution to be used for the first two classification steps.
Definition: search_predictor.hpp:227
void SetMaxNumResults(int maxNumResults)
Set the maximum number of results that can be extracted in a GridSearch operation....
Definition: search_predictor.hpp:140
static std::unique_ptr< SearchPredictor > FromHandle(ReleaseObjectGuard &&guard)
Creates predictor from a classic API handle.
Definition: search_predictor.hpp:105