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"
25inline HandleGuard<ShapeFinder2::Classifier>::HandleGuard(
void * handle) noexcept
26 : HandleGuard<ShapeFinder2::Classifier>(handle, [](
void* handle) { CVB_CALL_CAPI(ReleaseObject(handle)); })
66 return std::make_unique<Classifier>(fileName);
96 return handle_.Handle();
106 if (!CVB_CALL_CAPI(StoreSFTyped(
Handle(), fileName.c_str())))
107 Utilities::SystemInfo::ThrowLastError();
121 HandleGuard<Image> guard(CExports::GetSFImage(
static_cast<CExports::cvbdim_t
>(layer),
Handle()));
122 return Image::FromHandle<Image>(std::move(guard));
132 return CExports::GetSFClassNumber(
Handle());
152 CVB_CALL_CAPI(SetSFCommentTyped(
Handle(), comment.c_str()));
162 Char *strComment =
nullptr;
163 CVB_CALL_CAPI(GetSFCommentTyped(
Handle(), strComment));
164 return ((strComment !=
nullptr) ?
String(strComment) :
String());
174 int val = CExports::GetSFGradienttype(
Handle()) & 0xFF;
185 return featureWindow_;
195 return CExports::GetSFThreshold(
Handle());
205 if (!CExports::SetSFThreshold(
Handle(), threshold))
206 Utilities::SystemInfo::ThrowLastError();
216 auto byteValue = CExports::GetSFAngularTolerance(
Handle());
217 auto rad = byteValue < 128 ? static_cast<double>(byteValue) * CVB_M_PI / 127.0 : (
static_cast<double>(byteValue) - 255.0) * CVB_M_PI / 127.0;
229 auto rad = angularTolerance.
Rad();
230 auto byteValue =
static_cast<std::uint8_t>(std::round(rad > 0 ? rad * 127.0 / CVB_M_PI : 255.0 - (rad * 127.0 / CVB_M_PI)));
232 if (!CExports::SetSFAngularTolerance(
Handle(), byteValue))
233 Utilities::SystemInfo::ThrowLastError();
243 return CExports::GetSFFeatureNumber(
Handle());
255 return CExports::SF2UseCuda(
Handle(),
static_cast<CExports::SF2CudaStatus
>(status));
273 CExports::TSearchAllParams searchParams = GetSearchAllParameters(precision, relativeThreshold, minimumThreshold, coarseLocality);
274 if (!CExports::SetSF2SearchAllPars(
Handle(), searchParams))
275 Utilities::SystemInfo::ThrowLastError();
276 CExports::TSymmetryParams symmetryLimits = GetSymmetryParameters(rotationRange, scaleRange);
278 CExports::PIXELLIST lst =
nullptr;
279 if (!CExports::SF2SearchEx(
Handle(), plane.
Parent().
Handle(), plane.
Plane(), aoi.Left(), aoi.Top(), aoi.Right(), aoi.Bottom(), symmetryLimits, lst))
280 Utilities::SystemInfo::ThrowLastError();
282 return ResultListFromPixelList(precision, lst);
298 CExports::TSearchAllParams searchParams = GetSearchAllParameters(precision, relativeThreshold, minimumThreshold, coarseLocality);
299 if (!CExports::SetSF2SearchAllPars(
Handle(), searchParams))
300 Utilities::SystemInfo::ThrowLastError();
302 CExports::PIXELLIST lst =
nullptr;
303 if (!CExports::SF2Search(
Handle(), plane.
Parent().
Handle(), plane.
Plane(), aoi.Left(), aoi.Top(), aoi.Right(), aoi.Bottom(), lst))
304 Utilities::SystemInfo::ThrowLastError();
306 return ResultListFromPixelList(precision, lst);
316 return trainingWindow_;
326 return fineFeatures_->ToPoints();
336 return coarseFeatures_->ToPoints();
376 return contrastMode_;
386 return CExports::GetSF2ASteps(
Handle());
396 return CExports::GetSF2RSteps(
Handle());
402 explicit Classifier(HandleGuard<Classifier>&& guard) noexcept
403 : handle_(std::move(guard))
415 static ValueRange<double> ScaleRangeDefault() noexcept
417 return ValueRange<double>(1.0, 1.0);
423 CExports::cvbdim_t left = 0;
424 CExports::cvbdim_t top = 0;
425 CExports::cvbdim_t right = 0;
426 CExports::cvbdim_t bottom = 0;
427 CVB_CALL_CAPI_CHECKED(GetSFFeatureWindow(
Handle(), left, top, right, bottom));
435 CVB_CALL_CAPI_CHECKED(GetSF2TrainingWindow(
Handle(), left, top, right, bottom));
439 CExports::PIXELLIST fineFeatures =
nullptr;
440 CExports::PIXELLIST coarseFeatures =
nullptr;
441 CExports::cvbval_t coarseScale = coarseScale_;
442 CVB_CALL_CAPI_CHECKED(GetSF2Features(
Handle(), fineFeatures, coarseFeatures, coarseScale));
443 fineFeatures_ = Internal::PixelList::FromHandle(HandleGuard<Internal::PixelList>(fineFeatures));
444 coarseFeatures_ = Internal::PixelList::FromHandle(HandleGuard<Internal::PixelList>(coarseFeatures));
447 CExports::TSymmetryParams tmp = { 0 };
448 CVB_CALL_CAPI_CHECKED(GetSF2Symmetries(
Handle(), tmp));
456 auto pl = Internal::PixelList::FromHandle(HandleGuard<Internal::PixelList>(pixelList));
457 int plCount = pl->Count();
459 for (
int i = 0; i < plCount; i++)
466 static CExports::TSearchAllParams GetSearchAllParameters(
PrecisionMode precision,
double relativeThreshold,
int minimumThreshold,
int coarseLocality)
468 CExports::TSearchAllParams p = { 0 };
469 p.Precision = (int)precision;
470 p.LocXY = coarseLocality;
473 p.RelativeThreshold =
static_cast<int>(relativeThreshold * 100 + 0.5);
474 p.MinimalThreshold = minimumThreshold;
475 p.MaxNumSolutions = SearchAllResultCountMax;
479 static CExports::TSymmetryParams GetSymmetryParameters(ValueRange<Angle> rotationRange, ValueRange<double> scaleRange)
481 CExports::TSymmetryParams p = { 0 };
483 p.A0 = rotationRange.Min().Deg();
484 p.A1 = rotationRange.Max().Deg();
485 p.R0 = scaleRange.Min();
486 p.R1 = scaleRange.Max();
492 return Internal::DoBoolCallObjectOut<Classifier>([&](
void* & handle)
494 return CVB_CALL_CAPI(LoadSFTyped(handle, fileName.c_str()));
501 static const int SearchAllResultCountMax = 10000;
503 HandleGuard<Classifier> handle_;
507 int coarseScale_ = 0;
520using ShapeFinder2::Classifier;
Object for convenient and type - safe handling of angles.
Definition: angle.hpp:19
static Angle FromRadians(double rad, bool trim=false) noexcept
Create an angle in radians.
Definition: angle.hpp:44
double Rad() const noexcept
Get the value in radians.
Definition: angle.hpp:66
void SetIsTrimmed(bool trim) noexcept
Set trimming of the value of the angle to the range -PI...PI.
Definition: angle.hpp:119
static Angle FromDegrees(double deg, bool trim=false) noexcept
Create an angle in degrees.
Definition: angle.hpp:30
void * Handle() const noexcept
Classic API image handle.
Definition: decl_image.hpp:226
Image plane information container.
Definition: decl_image_plane.hpp:33
int Plane() const noexcept
Plane index in the image, to which this plane refers to.
Definition: decl_image_plane.hpp:167
const Image & Parent() const noexcept
Image to which this plane descriptor refers to.
Definition: detail_image_plane.hpp:87
ShapeFinder2 classifier object.
Definition: classifier.hpp:38
static std::unique_ptr< Classifier > Create(const String &fileName)
Creates a classifier object loading a classifier file.
Definition: classifier.hpp:64
double RotationStep()
Step size at which the rotations in the classifier have been generated.
Definition: classifier.hpp:384
String Comment() const noexcept
Get the comment assigned to the classifier.
Definition: classifier.hpp:160
ValueRange< Angle > Rotation()
Range of rotations for which this classifier has been generated.
Definition: classifier.hpp:354
int ContrastThreshold() const noexcept
Get the threshold for the gradient slope.
Definition: classifier.hpp:193
static std::unique_ptr< Classifier > FromHandle(HandleGuard< Classifier > &&guard)
Creates a classifier from a classic API handle.
Definition: classifier.hpp:49
void SetContrastThreshold(int threshold)
Set the threshold for the gradient slope.
Definition: classifier.hpp:203
ValueRange< double > Scale()
Range of scales for which this classifier has been generated.
Definition: classifier.hpp:364
Cvb::ShapeFinder2::ContrastMode ContrastMode()
Contrast mode for which this classifier has been created.
Definition: classifier.hpp:374
Cvb::ShapeFinder2::GradientType GradientType() const noexcept
Get the gradient type this classifier uses for feature extraction.
Definition: classifier.hpp:172
double ScaleStep()
Step size at which the scales in the classifier have been generated.
Definition: classifier.hpp:394
int NumLayers() const noexcept
Number of layers in the classifier.
Definition: classifier.hpp:130
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:271
Rect< int > FeatureWindow() const noexcept
Get the feature window of this classifier.
Definition: classifier.hpp:183
void SetAngularTolerance(Angle angularTolerance)
Set the acceptance parameter for feature gradient angles.
Definition: classifier.hpp:226
void Save(const String &fileName) const
Writes the classifier to a file.
Definition: classifier.hpp:104
int FeatureCount() const noexcept
Get the number of features this classifier contains.
Definition: classifier.hpp:241
Classifier(const String &fileName)
Loads a classifier with the given file name.
Definition: classifier.hpp:74
std::unique_ptr< Image > Visualization(int layer=-1) const
Creates and returns a color coded image representation of this ShapeFinder classifier.
Definition: classifier.hpp:116
std::vector< Point2D< double > > CoarseFeatures() const noexcept
Features the classifier uses on the coarsely granular level.
Definition: classifier.hpp:334
Angle AngularTolerance() const noexcept
Get the acceptance parameter for feature gradient angles.
Definition: classifier.hpp:214
Rect< int > TrainingWindow() const noexcept
Training window of the classes in the classifier relative to the anchor point.
Definition: classifier.hpp:314
bool UseCuda(CudaStatus status) const noexcept
Set the flag to use CUDA if possible or to force not using CUDA.
Definition: classifier.hpp:253
std::vector< Point2D< double > > FineFeatures() const noexcept
The features the classifier uses on the finely granular level.
Definition: classifier.hpp:324
String FileName() const
Gets the name of the file, from which this classifier was loaded.
Definition: classifier.hpp:140
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:296
void * Handle() const noexcept
Classic API classifier handle.
Definition: classifier.hpp:94
void SetComment(const String &comment) noexcept
Set the comment assigned to the classifier.
Definition: classifier.hpp:150
int CoarseScale() const noexcept
Scale factor between the fine and the coarse feature level.
Definition: classifier.hpp:344
Search result as returned by the classifier.
Definition: search_result.hpp:24
Container for range definitions.
Definition: value_range.hpp:17
GradientType
Type of Gradient used for feature extraction.
Definition: shapefinder2.hpp:59
CudaStatus
ShapeFinder2 CUDA status enum.
Definition: shapefinder2.hpp:68
PrecisionMode
Controls precision over accuracy for ShapeFinder 1 type searches.
Definition: shapefinder2.hpp:47
ContrastMode
Normal contrast features.
Definition: shapefinder2.hpp:37
@ Normal
Normal contrast features.
Root namespace for the Image Manager interface.
Definition: c_barcode.h:15
char Char
Character type for wide characters or unicode characters.
Definition: string.hpp:70
std::string String
String for wide characters or unicode characters.
Definition: string.hpp:56