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)); })
92 return handle_.Handle();
102 if (!CVB_CALL_CAPI(StoreSFTyped(
Handle(), fileName.c_str())))
103 Utilities::SystemInfo::ThrowLastError();
117 HandleGuard<Image> guard(CExports::GetSFImage(
static_cast<CExports::cvbdim_t
>(layer),
Handle()));
118 return Image::FromHandle<Image>(std::move(guard));
128 return CExports::GetSFClassNumber(
Handle());
148 CVB_CALL_CAPI(SetSFCommentTyped(
Handle(), comment.c_str()));
158 Char *strComment =
nullptr;
159 CVB_CALL_CAPI(GetSFCommentTyped(
Handle(), strComment));
160 return ((strComment !=
nullptr) ?
String(strComment) :
String());
170 int val = CExports::GetSFGradienttype(
Handle()) & 0xFF;
181 return featureWindow_;
191 return CExports::GetSFThreshold(
Handle());
201 if (!CExports::SetSFThreshold(
Handle(), threshold))
202 Utilities::SystemInfo::ThrowLastError();
212 auto byteValue = CExports::GetSFAngularTolerance(
Handle());
213 auto rad = byteValue < 128 ? static_cast<double>(byteValue) * CVB_M_PI / 127.0 : (
static_cast<double>(byteValue) - 255.0) * CVB_M_PI / 127.0;
225 auto rad = angularTolerance.
Rad();
226 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)));
228 if (!CExports::SetSFAngularTolerance(
Handle(), byteValue))
229 Utilities::SystemInfo::ThrowLastError();
239 return CExports::GetSFFeatureNumber(
Handle());
251 return CExports::SF2UseCuda(
Handle(),
static_cast<CExports::SF2CudaStatus
>(status));
269 CExports::TSearchAllParams searchParams = GetSearchAllParameters(precision, relativeThreshold, minimumThreshold, coarseLocality);
270 if (!CExports::SetSF2SearchAllPars(
Handle(), searchParams))
271 Utilities::SystemInfo::ThrowLastError();
272 CExports::TSymmetryParams symmetryLimits = GetSymmetryParameters(rotationRange, scaleRange);
274 CExports::PIXELLIST lst =
nullptr;
275 if (!CExports::SF2SearchEx(
Handle(), plane.Parent().Handle(), plane.Plane(), aoi.Left(), aoi.Top(), aoi.Right(), aoi.Bottom(), symmetryLimits, lst))
276 Utilities::SystemInfo::ThrowLastError();
278 return ResultListFromPixelList(precision, lst);
294 CExports::TSearchAllParams searchParams = GetSearchAllParameters(precision, relativeThreshold, minimumThreshold, coarseLocality);
295 if (!CExports::SetSF2SearchAllPars(
Handle(), searchParams))
296 Utilities::SystemInfo::ThrowLastError();
298 CExports::PIXELLIST lst =
nullptr;
299 if (!CExports::SF2Search(
Handle(), plane.Parent().Handle(), plane.Plane(), aoi.Left(), aoi.Top(), aoi.Right(), aoi.Bottom(), lst))
300 Utilities::SystemInfo::ThrowLastError();
302 return ResultListFromPixelList(precision, lst);
312 return trainingWindow_;
322 return fineFeatures_->ToPoints();
332 return coarseFeatures_->ToPoints();
372 return contrastMode_;
382 return CExports::GetSF2ASteps(
Handle());
392 return CExports::GetSF2RSteps(
Handle());
398 Classifier(HandleGuard<Classifier>&& guard) noexcept
399 : handle_(std::move(guard))
404 : handle_(std::move(other.handle_))
410 static ValueRange<Angle> RotationRangeDefault() noexcept
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;
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;
469 p.Precision = (int)precision;
470 p.LocXY = coarseLocality;
473 p.RelativeThreshold = (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;
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_;
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
Image plane information container.
Definition: decl_image_plane.hpp:33
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:380
String Comment() const noexcept
Get the comment assigned to the classifier.
Definition: classifier.hpp:156
ValueRange< Angle > Rotation()
Range of rotations for which this classifier has been generated.
Definition: classifier.hpp:350
int ContrastThreshold() const noexcept
Get the threshold for the gradient slope.
Definition: classifier.hpp:189
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:199
ValueRange< double > Scale()
Range of scales for which this classifier has been generated.
Definition: classifier.hpp:360
Cvb::ShapeFinder2::ContrastMode ContrastMode()
Contrast mode for which this classifier has been created.
Definition: classifier.hpp:370
Cvb::ShapeFinder2::GradientType GradientType() const noexcept
Get the gradient type this classifier uses for feature extraction.
Definition: classifier.hpp:168
double ScaleStep()
Step size at which the scales in the classifier have been generated.
Definition: classifier.hpp:390
int NumLayers() const noexcept
Number of layers in the classifier.
Definition: classifier.hpp:126
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:267
Rect< int > FeatureWindow() const noexcept
Get the feature window of this classifier.
Definition: classifier.hpp:179
void SetAngularTolerance(Angle angularTolerance)
Set the acceptance parameter for feature gradient angles.
Definition: classifier.hpp:222
void Save(const String &fileName) const
Writes the classifier to a file.
Definition: classifier.hpp:100
int FeatureCount() const noexcept
Get the number of features this classifier contains.
Definition: classifier.hpp:237
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:112
std::vector< Point2D< double > > CoarseFeatures() const noexcept
Features the classifier uses on the coarsely granular level.
Definition: classifier.hpp:330
Angle AngularTolerance() const noexcept
Get the acceptance parameter for feature gradient angles.
Definition: classifier.hpp:210
Rect< int > TrainingWindow() const noexcept
Training window of the classes in the classifier relative to the anchor point.
Definition: classifier.hpp:310
bool UseCuda(CudaStatus status) const noexcept
Set the flag to use CUDA if possible or to force not using CUDA.
Definition: classifier.hpp:249
std::vector< Point2D< double > > FineFeatures() const noexcept
The features the classifier uses on the finely granular level.
Definition: classifier.hpp:320
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:292
void * Handle() const noexcept
Classic API classifier handle.
Definition: classifier.hpp:90
void SetComment(const String &comment) noexcept
Set the comment assigned to the classifier.
Definition: classifier.hpp:146
int CoarseScale() const noexcept
Scale factor between the fine and the coarse feature level.
Definition: classifier.hpp:340
String FileName() const noexcept
Gets the name of the file, from which this classifier was loaded.
Definition: classifier.hpp:136
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:24
char Char
Character type for wide characters or unicode characters.
Definition: string.hpp:59
std::string String
String for wide characters or unicode characters.
Definition: string.hpp:45