5#include "../global.hpp"
6#include "../string.hpp"
9#include "../value_range.hpp"
10#include "../_detail/detail_pixel_list.hpp"
11#include "../image.hpp"
12#include "../exception.hpp"
14#include "search_result.hpp"
15#include "shapefinder2.hpp"
17#include "../_cexports/c_sf_private.h"
24 inline HandleGuard<ShapeFinder2::Classifier>::HandleGuard(
void *handle) noexcept
25 : HandleGuard<ShapeFinder2::Classifier>(handle, [](
void *handle) { CVB_CALL_CAPI(
ReleaseObject(handle)); })
63 return std::make_unique<Classifier>(fileName);
93 return handle_.Handle();
103 if (!CVB_CALL_CAPI(StoreSFTyped(
Handle(), fileName.c_str())))
104 Utilities::SystemInfo::ThrowLastError();
118 HandleGuard<Image> guard(CExports::GetSFImage(
static_cast<CExports::cvbdim_t
>(layer),
Handle()));
129 return CExports::GetSFClassNumber(
Handle());
149 CVB_CALL_CAPI(SetSFCommentTyped(
Handle(), comment.c_str()));
159 Char *strComment =
nullptr;
160 CVB_CALL_CAPI(GetSFCommentTyped(
Handle(), strComment));
161 return ((strComment !=
nullptr) ?
String(strComment) :
String());
171 int val = CExports::GetSFGradienttype(
Handle()) & 0xFF;
182 return featureWindow_;
192 return CExports::GetSFThreshold(
Handle());
202 if (!CExports::SetSFThreshold(
Handle(), threshold))
203 Utilities::SystemInfo::ThrowLastError();
213 auto byteValue = CExports::GetSFAngularTolerance(
Handle());
214 auto rad = byteValue < 128 ? static_cast<double>(byteValue) * CVB_M_PI / 127.0
215 : (
static_cast<double>(byteValue) - 255.0) * CVB_M_PI / 127.0;
227 auto rad = angularTolerance.
Rad();
229 static_cast<std::uint8_t>(
std::round(rad > 0 ? rad * 127.0 / CVB_M_PI : 255.0 - (rad * 127.0 / CVB_M_PI)));
231 if (!CExports::SetSFAngularTolerance(
Handle(), byteValue))
232 Utilities::SystemInfo::ThrowLastError();
242 return CExports::GetSFFeatureNumber(
Handle());
254 return CExports::SF2UseCuda(
Handle(),
static_cast<CExports::SF2CudaStatus
>(status));
274 double relativeThreshold,
int minimumThreshold,
int coarseLocality,
277 CExports::TSearchAllParams searchParams =
278 GetSearchAllParameters(precision, relativeThreshold, minimumThreshold, coarseLocality);
279 if (!CExports::SetSF2SearchAllPars(
Handle(), searchParams))
280 Utilities::SystemInfo::ThrowLastError();
281 CExports::TSymmetryParams symmetryLimits = GetSymmetryParameters(rotationRange, scaleRange);
283 CExports::PIXELLIST lst =
nullptr;
284 if (!CExports::SF2SearchEx(
Handle(), plane.
Parent().
Handle(), plane.
Plane(), aoi.Left(), aoi.Top(), aoi.Right(),
285 aoi.Bottom(), symmetryLimits, lst))
286 Utilities::SystemInfo::ThrowLastError();
288 return ResultListFromPixelList(precision, lst);
306 double relativeThreshold,
int minimumThreshold,
int coarseLocality)
308 CExports::TSearchAllParams searchParams =
309 GetSearchAllParameters(precision, relativeThreshold, minimumThreshold, coarseLocality);
310 if (!CExports::SetSF2SearchAllPars(
Handle(), searchParams))
311 Utilities::SystemInfo::ThrowLastError();
313 CExports::PIXELLIST lst =
nullptr;
316 Utilities::SystemInfo::ThrowLastError();
318 return ResultListFromPixelList(precision, lst);
328 return trainingWindow_;
338 return fineFeatures_->ToPoints();
348 return coarseFeatures_->ToPoints();
388 return contrastMode_;
398 return CExports::GetSF2ASteps(
Handle());
408 return CExports::GetSF2RSteps(
Handle());
412 explicit Classifier(HandleGuard<Classifier> &&guard) noexcept
422 static ValueRange<double> ScaleRangeDefault() noexcept
424 return ValueRange<double>(1.0, 1.0);
430 CExports::cvbdim_t left = 0;
431 CExports::cvbdim_t top = 0;
432 CExports::cvbdim_t right = 0;
433 CExports::cvbdim_t bottom = 0;
434 CVB_CALL_CAPI_CHECKED(GetSFFeatureWindow(
Handle(), left, top, right, bottom));
435 featureWindow_ = Cvb::Rect<int>(left, top, right, bottom);
442 CVB_CALL_CAPI_CHECKED(GetSF2TrainingWindow(
Handle(), left, top, right, bottom));
443 trainingWindow_ = Cvb::Rect<int>(left, top, right, bottom);
446 CExports::PIXELLIST fineFeatures =
nullptr;
447 CExports::PIXELLIST coarseFeatures =
nullptr;
448 CExports::cvbval_t coarseScale = coarseScale_;
449 CVB_CALL_CAPI_CHECKED(GetSF2Features(
Handle(), fineFeatures, coarseFeatures, coarseScale));
450 fineFeatures_ = Internal::PixelList::FromHandle(HandleGuard<Internal::PixelList>(fineFeatures));
451 coarseFeatures_ = Internal::PixelList::FromHandle(HandleGuard<Internal::PixelList>(coarseFeatures));
454 CExports::TSymmetryParams tmp = {0};
455 CVB_CALL_CAPI_CHECKED(GetSF2Symmetries(
Handle(), tmp));
457 scale_ = Cvb::ValueRange<double>(tmp.R0, tmp.R1);
461 static std::vector<Cvb::ShapeFinder2::SearchResult> ResultListFromPixelList(
PrecisionMode precision,
462 CExports::PIXELLIST pixelList)
464 auto pl = Internal::PixelList::FromHandle(HandleGuard<Internal::PixelList>(pixelList));
465 int plCount = pl->Count();
466 std::vector<Cvb::ShapeFinder2::SearchResult> res(
static_cast<std::size_t
>(plCount));
467 for (
int i = 0; i < plCount; i++)
469 res[i] = Cvb::ShapeFinder2::SearchResult(precision, pl->ToValues(i));
474 static CExports::TSearchAllParams GetSearchAllParameters(
PrecisionMode precision,
double relativeThreshold,
475 int minimumThreshold,
int coarseLocality)
477 CExports::TSearchAllParams p = {0};
478 p.Precision = (int)precision;
479 p.LocXY = coarseLocality;
482 p.RelativeThreshold =
static_cast<int>(relativeThreshold * 100 + 0.5);
483 p.MinimalThreshold = minimumThreshold;
484 p.MaxNumSolutions = SearchAllResultCountMax;
488 static CExports::TSymmetryParams GetSymmetryParameters(ValueRange<Angle> rotationRange,
489 ValueRange<double> scaleRange)
491 CExports::TSymmetryParams p = {0};
493 p.A0 = rotationRange.Min().Deg();
494 p.A1 = rotationRange.Max().Deg();
495 p.R0 = scaleRange.Min();
496 p.R1 = scaleRange.Max();
500 static inline std::unique_ptr<Classifier> Load(
const String &fileName)
502 return Internal::DoBoolCallObjectOut<Classifier>(
503 [&](
void *&handle) {
return CVB_CALL_CAPI(LoadSFTyped(handle, fileName.c_str())); });
507 static const int SearchAllResultCountMax = 10000;
509 HandleGuard<Classifier> handle_;
513 int coarseScale_ = 0;
514 Cvb::Rect<int> featureWindow_;
515 Cvb::Rect<int> trainingWindow_;
516 std::unique_ptr<Internal::PixelList> fineFeatures_;
517 std::unique_ptr<Internal::PixelList> coarseFeatures_;
518 Cvb::ValueRange<Cvb::Angle> rotation_;
519 Cvb::ValueRange<double> scale_;
Object for convenient and type - safe handling of angles.
Definition angle.hpp:16
static Angle FromRadians(double rad, bool trim=false) noexcept
Create an angle in radians.
Definition angle.hpp:39
double Rad() const noexcept
Get the value in radians.
Definition angle.hpp:68
void SetIsTrimmed(bool trim) noexcept
Set trimming of the value of the angle to the range -PI...PI.
Definition angle.hpp:120
static Angle FromDegrees(double deg, bool trim=false) noexcept
Create an angle in degrees.
Definition angle.hpp:25
Classifier(const String &fileName)
Loads a classifier with the given file name.
Definition classifier.hpp:71
static std::unique_ptr< Image > FromHandle(HandleGuard< Image > &&guard)
Creates an image from a classic API handle.
Definition decl_image.hpp:155
void * Handle() const noexcept
Classic API image handle.
Definition decl_image.hpp:232
Image plane information container.
Definition decl_image_plane.hpp:29
int Plane() const noexcept
Plane index in the image, to which this plane refers to.
Definition decl_image_plane.hpp:147
const Image & Parent() const noexcept
Image to which this plane descriptor refers to.
Definition detail_image_plane.hpp:87
Rectangle object.
Definition rect.hpp:24
ShapeFinder2 classifier object.
Definition classifier.hpp:36
static std::unique_ptr< Classifier > Create(const String &fileName)
Creates a classifier object loading a classifier file.
Definition classifier.hpp:61
double RotationStep()
Step size at which the rotations in the classifier have been generated.
Definition classifier.hpp:396
String Comment() const noexcept
Get the comment assigned to the classifier.
Definition classifier.hpp:157
ValueRange< Angle > Rotation()
Range of rotations for which this classifier has been generated.
Definition classifier.hpp:366
int ContrastThreshold() const noexcept
Get the threshold for the gradient slope.
Definition classifier.hpp:190
static std::unique_ptr< Classifier > FromHandle(HandleGuard< Classifier > &&guard)
Creates a classifier from a classic API handle.
Definition classifier.hpp:46
void SetContrastThreshold(int threshold)
Set the threshold for the gradient slope.
Definition classifier.hpp:200
ValueRange< double > Scale()
Range of scales for which this classifier has been generated.
Definition classifier.hpp:376
Cvb::ShapeFinder2::ContrastMode ContrastMode()
Contrast mode for which this classifier has been created.
Definition classifier.hpp:386
Cvb::ShapeFinder2::GradientType GradientType() const noexcept
Get the gradient type this classifier uses for feature extraction.
Definition classifier.hpp:169
double ScaleStep()
Step size at which the scales in the classifier have been generated.
Definition classifier.hpp:406
int NumLayers() const noexcept
Number of layers in the classifier.
Definition classifier.hpp:127
std::vector< SearchResult > SearchAll(const ImagePlane &plane, Rect< int > aoi, PrecisionMode precision, double relativeThreshold, int minimumThreshold, int coarseLocality, ValueRange< Angle > rotationRange, ValueRange< double > scaleRange)
Use this classifier to perform a ShapeFinder search on an image plane.
Definition classifier.hpp:273
Rect< int > FeatureWindow() const noexcept
Get the feature window of this classifier.
Definition classifier.hpp:180
void SetAngularTolerance(Angle angularTolerance)
Set the acceptance parameter for feature gradient angles.
Definition classifier.hpp:224
void Save(const String &fileName) const
Writes the classifier to a file.
Definition classifier.hpp:101
int FeatureCount() const noexcept
Get the number of features this classifier contains.
Definition classifier.hpp:240
Classifier(const String &fileName)
Loads a classifier with the given file name.
Definition classifier.hpp:71
std::unique_ptr< Image > Visualization(int layer=-1) const
Creates and returns a color coded image representation of this ShapeFinder classifier.
Definition classifier.hpp:113
std::vector< Point2D< double > > CoarseFeatures() const noexcept
Features the classifier uses on the coarsely granular level.
Definition classifier.hpp:346
Angle AngularTolerance() const noexcept
Get the acceptance parameter for feature gradient angles.
Definition classifier.hpp:211
Rect< int > TrainingWindow() const noexcept
Training window of the classes in the classifier relative to the anchor point.
Definition classifier.hpp:326
bool UseCuda(CudaStatus status) const noexcept
Set the flag to use CUDA if possible or to force not using CUDA.
Definition classifier.hpp:252
std::vector< Point2D< double > > FineFeatures() const noexcept
The features the classifier uses on the finely granular level.
Definition classifier.hpp:336
String FileName() const
Gets the name of the file, from which this classifier was loaded.
Definition classifier.hpp:137
std::vector< SearchResult > SearchAll(const ImagePlane &plane, Rect< int > aoi, PrecisionMode precision, double relativeThreshold, int minimumThreshold, int coarseLocality)
Use this classifier to perform a ShapeFinder search on an image plane.
Definition classifier.hpp:305
void * Handle() const noexcept
Classic API classifier handle.
Definition classifier.hpp:91
void SetComment(const String &comment) noexcept
Set the comment assigned to the classifier.
Definition classifier.hpp:147
int CoarseScale() const noexcept
Scale factor between the fine and the coarse feature level.
Definition classifier.hpp:356
Container for range definitions.
Definition value_range.hpp:17
cvbbool_t ReleaseObject(OBJ &Object)
Namespace for the ShapeFinder2 package.
Definition classifier.hpp:30
GradientType
Type of Gradient used for feature extraction.
Definition shapefinder2.hpp:56
CudaStatus
ShapeFinder2 CUDA status enum.
Definition shapefinder2.hpp:65
PrecisionMode
Controls precision over accuracy for ShapeFinder 1 type searches.
Definition shapefinder2.hpp:42
ContrastMode
Normal contrast features.
Definition shapefinder2.hpp:32
@ Normal
Normal contrast features.
Definition shapefinder2.hpp:34
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
char Char
Character type for wide characters or unicode characters.
Definition string.hpp:63
std::string String
String for wide characters or unicode characters.
Definition string.hpp:49