3#include "../_cexports/c_minos.h"
5#include "../global.hpp"
7#include "../string.hpp"
8#include "../point_2d.hpp"
9#include "../matrix_2d.hpp"
10#include "../image.hpp"
12#include "learn_parameters.hpp"
13#include "search_result.hpp"
34 inline HandleGuard<Minos::Classifier>::HandleGuard(
void *handle) noexcept
35 : HandleGuard<Minos::Classifier>(handle, [](
void *h) { CVB_CALL_CAPI(
ReleaseObject(h)); })
93 , advanceVector_(advanceVector)
124 return advanceVector_;
134 advanceVector_ = advanceVector;
152 return (lhs.Name() != rhs.Name()) || (lhs.AdvanceVector() != rhs.AdvanceVector());
165 return (!(lhs != rhs));
171 class ClassifierModelInfoCollection
174 explicit ClassifierModelInfoCollection(
const SharedHandleGuard<Classifier> &sguard)
179 ClassifierModelInfoCollection(
const ClassifierModelInfoCollection &other)
noexcept =
default;
180 ClassifierModelInfoCollection &operator=(
const ClassifierModelInfoCollection &other)
noexcept =
default;
181 ClassifierModelInfoCollection(ClassifierModelInfoCollection &&other)
noexcept =
default;
182 ClassifierModelInfoCollection &operator=(ClassifierModelInfoCollection &&other)
noexcept =
default;
183 ~ClassifierModelInfoCollection() =
default;
193 return static_cast<int>(CVB_CALL_CAPI(NumCLFModels(shandle_.Handle())));
205 Char *pstr =
nullptr;
206 CExports::cvbdim_t advX = 0, advY = 0;
207 CVB_CALL_CAPI_CHECKED(GetCLFModelDataTyped(shandle_.Handle(), index, pstr, advX, advY));
210 Point2D<int>(
static_cast<int>(advX),
static_cast<int>(advY)));
226 CVB_CALL_CAPI_CHECKED(SetCLFModelDataTyped(shandle_.Handle(), index, model.
Name().c_str(),
238 auto count =
Count();
240 for (
decltype(count) i = 0; i < count; ++i)
249 SharedHandleGuard<Classifier> shandle_;
259 explicit Classifier(HandleGuard<Classifier> &&guard,
const String &srcFileName =
String())
261 , fileName_(srcFileName)
265 auto strDate = CVB_CALL_CAPI(CLFCreationDate(shandle_.Handle()));
288 CExports::cvbdim_t left = 0, top = 0, right = 0, bottom = 0;
289 CVB_CALL_CAPI_CHECKED(GetCLFExtent(shandle_.Handle(), left, top, right, bottom));
291 Rect<int>(
static_cast<int>(left),
static_cast<int>(top),
static_cast<int>(right),
static_cast<int>(bottom));
295 static CExports::cvbval_t MaxSearch()
noexcept
307 : Classifier(std::move(*
Load(fileName)))
326 CExports::CLF clf =
nullptr;
327 CVB_CALL_CAPI_CHECKED(LoadCLFFileTyped(fileName.c_str(), clf));
340 return Internal::DoBoolCallObjectOut<Classifier>([&](
void *&resclas) {
342 return CVB_CALL_CAPI(MemoryToCLF(
const_cast<void *
>(buffer),
static_cast<long>(size),
353 template <
class RANGE>
356 auto bufferRange = MakeRangeAdapter<std::uint8_t>(buffer);
357 return FromBuffer(bufferRange.Data(), bufferRange.Size());
369 return shandle_.Handle();
397 CVB_CALL_CAPI_CHECKED(WriteCLFFileTyped(shandle_.Handle(), fileName.c_str()));
398 fileName_ = fileName;
409 auto sizeNeeded = CVB_CALL_CAPI(GetCLFSize(
Handle()));
411 CVB_CALL_CAPI_CHECKED(CLFToMemory(shandle_.Handle(), buffer.data(), sizeNeeded));
456 return creationDate_;
466 return CVB_CALL_CAPI(IsOldCLF(shandle_.Handle()) ?
true :
false);
476 CExports::cvbval_t retval = 0;
477 CVB_CALL_CAPI_CHECKED(GetCLFTrigger(shandle_.Handle(), retval));
478 return static_cast<int>(retval);
488 CVB_CALL_CAPI_CHECKED(SetCLFTrigger(shandle_.Handle(),
static_cast<CExports::cvbval_t
>(trigger)));
499 CVB_CALL_CAPI_CHECKED(GetCLFThreshold(shandle_.Handle(), retval));
510 CVB_CALL_CAPI_CHECKED(SetCLFThreshold(shandle_.Handle(), threshold));
520 CExports::QualityMeasureMethod retval = CExports::Quality_Unnormalized;
521 CVB_CALL_CAPI_CHECKED(GetCLFQualityType(shandle_.Handle(), retval));
532 CVB_CALL_CAPI_CHECKED(
533 SetCLFQualityType(shandle_.Handle(),
static_cast<CExports::QualityMeasureMethod
>(quality)));
543 const Char *name =
nullptr;
544 CVB_CALL_CAPI(CLFMTSNameTyped(shandle_.Handle(), name));
555 CExports::TLearnControlStructure params{0};
556 CVB_CALL_CAPI_CHECKED(GetCLFLCS(shandle_.Handle(), params));
558 params.Param2 / 1000.0);
568 const Char *comment =
nullptr;
569 CVB_CALL_CAPI(CLFCommentTyped(shandle_.Handle(), comment));
570 return (comment !=
nullptr) ?
String(comment) :
String();
580 CVB_CALL_CAPI_CHECKED(SetCLFCommentTyped(shandle_.Handle(), comment.c_str()));
592 return Internal::DoBoolCallObjectOut<Classifier>([&](
void *&resclas) {
595 return CVB_CALL_CAPI(
596 CLFTransform(
Handle(),
reinterpret_cast<const CExports::TMatrix &
>(transformation), resclas));
610 return Internal::DoBoolCallObjectOut<Classifier>([&](
void *&resclas) {
611 return CVB_CALL_CAPI(CLFSetGlobalAdvance(
Handle(),
static_cast<CExports::cvbdim_t
>(advance.X()),
612 static_cast<CExports::cvbdim_t
>(advance.Y()), resclas));
638 double density = 1.0)
640 double quality = 0.0, xPos = 0.0, yPos = 0.0, dX = 0.0, dY = 0.0;
641 Char *pstr =
nullptr;
642 CExports::cvbval_t charCount = 0;
650 CVB_CALL_CAPI_CHECKED(ReadTokenFirstTyped(
652 reinterpret_cast<const CExports::TArea &
>(startAOI),
reinterpret_cast<const CExports::TArea &
>(ocrAOI),
653 MaxSearch(), quality, xPos, yPos, dX, dY, pstr, charCount, token.data()));
656 CVB_CALL_CAPI_CHECKED(ReadTokenTyped(
658 reinterpret_cast<const CExports::TArea &
>(startAOI),
reinterpret_cast<const CExports::TArea &
>(ocrAOI),
659 MaxSearch(), quality, xPos, yPos, dX, dY, pstr, charCount, token.data()));
664 return String(token.data());
687 CExports::RESULTS hSearchResults =
nullptr;
688 CVB_CALL_CAPI_CHECKED(
690 reinterpret_cast<const CExports::TArea &
>(startAOI),
691 reinterpret_cast<const CExports::TArea &
>(ocrAOI), MaxSearch(), hSearchResults));
692 ReleaseObjectGuard hSearchResHolder(hSearchResults);
693 return Private::SearchResultsToArray(hSearchResults);
708 double quality = 0.0, xPos = 0.0, yPos = 0.0, dX = 0.0, dY = 0.0;
709 Char *pstr =
nullptr;
716 CVB_CALL_CAPI_CHECKED(
718 reinterpret_cast<const CExports::TArea &
>(aoi), quality, xPos, yPos, dX, dY, pstr));
721 CVB_CALL_CAPI_CHECKED(
723 reinterpret_cast<const CExports::TArea &
>(aoi), quality, xPos, yPos, dX, dY, pstr));
726 CVB_CALL_CAPI_CHECKED(SubpixelOptimumTyped(
728 reinterpret_cast<const CExports::TArea &
>(aoi), quality, xPos, yPos, dX, dY, pstr));
763 CExports::RESULTS hSearchResults =
nullptr;
764 CVB_CALL_CAPI_CHECKED(
766 reinterpret_cast<const CExports::TArea &
>(aoi), locality, MaxSearch(), hSearchResults));
767 ReleaseObjectGuard hSearchResHolder(hSearchResults);
768 return Private::SearchResultsToArray(hSearchResults);
786 SharedHandleGuard<Classifier> shandle_;
789 std::chrono::system_clock::time_point creationDate_;
Structure that represents an area of interest in the image.
Definition area_2d.hpp:21
Rect< int > Bounds() const noexcept
Bounding rectangle of the image in pixels.
Definition decl_image.hpp:433
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
Double precision 2x2 matrix class.
Definition matrix_2d.hpp:16
Minos classifier object.
Definition classifier.hpp:256
std::vector< SearchResult > Read(const ImagePlane &plane, Area2D startAOI, Area2D ocrAOI, double density=1.0)
Reads a list of characters.
Definition classifier.hpp:685
int ContrastTrigger() const
Get the trigger value for the contrast of features to be taken into account.
Definition classifier.hpp:474
static TypedRange< std::unique_ptr< Classifier >, std::uint8_t, RANGE >::type FromBuffer(const RANGE &buffer)
Recreate a serialized Minos classifier from a byte array.
Definition classifier.hpp:354
class LearnParameters LearnParameters() const
Get the set of parameters that has been used during classifier generation.
Definition classifier.hpp:553
std::unique_ptr< Classifier > Transform(Matrix2D transformation)
Generate a new classifier by transforming this classifier with a 2x2 transformation matrix.
Definition classifier.hpp:590
std::chrono::system_clock::time_point CreationDate() const noexcept
Creation date of the classifier.
Definition classifier.hpp:454
bool IsMinos16BitClassifier() const
Returns true if the classifier was generated by Minos16Bit.
Definition classifier.hpp:464
std::vector< SearchResult > SearchAll(const ImagePlane &plane, int locality, double density=1.0)
Search all objects using this Minos classifier.
Definition classifier.hpp:780
SearchResult Search(const ImagePlane &plane, SearchMode mode, double density=1.0)
Search one object using this Minos classifier.
Definition classifier.hpp:745
static std::unique_ptr< Classifier > FromHandle(HandleGuard< Classifier > &&guard)
Creates classifier from a classic API handle.
Definition classifier.hpp:380
void SetQualityMeasure(QualityFeedback quality)
Set the type of quality feedback from the classifier.
Definition classifier.hpp:530
SearchResult Search(const ImagePlane &plane, SearchMode mode, Area2D aoi, double density=1.0)
Search one object using this Minos classifier.
Definition classifier.hpp:706
String Comment() const
Get the comment assigned to the classifier at generation time.
Definition classifier.hpp:566
std::vector< std::uint8_t > ToBuffer() const
Builds a byte buffer with the classifier.
Definition classifier.hpp:407
void Save(const String &fileName) const
Write the classifier to a file.
Definition classifier.hpp:395
Classifier(const String &fileName)
Loads a classifier with the given file name.
Definition classifier.hpp:306
void SetComment(String comment)
Set the comment assigned to the classifier at generation time.
Definition classifier.hpp:578
double Threshold() const
Get the threshold for search operations with normalized quality feedback.
Definition classifier.hpp:496
static std::unique_ptr< Classifier > Load(const String &fileName)
Load a saved classifier from a file.
Definition classifier.hpp:324
String Read(const ImagePlane &plane, ReadMode mode, Area2D startAOI, Area2D ocrAOI, SearchResult &startOrStop, double density=1.0)
Reads a string of characters.
Definition classifier.hpp:637
ClassifierModelInfoCollection Models() const
Collection of the models contained in this classifier.
Definition classifier.hpp:434
void SetThreshold(double threshold)
Set the threshold for search operations with normalized quality feedback.
Definition classifier.hpp:508
std::unique_ptr< Classifier > SetGlobalAdvanceVector(Point2D< int > advance)
Create a new classifier, that is a copy of this classifier, but with a global advance vector applied ...
Definition classifier.hpp:608
std::vector< SearchResult > SearchAll(const ImagePlane &plane, int locality, Area2D aoi, double density=1.0)
Search all objects using this Minos classifier.
Definition classifier.hpp:760
Rect< int > Extent() const noexcept
Extent of the classes in the classifier relative to the anchor point.
Definition classifier.hpp:444
String FileName() const
Name of the file from which this classifier was loaded (empty string, if this image list was neither ...
Definition classifier.hpp:421
void * Handle() const noexcept
Classic API CLF handle.
Definition classifier.hpp:367
String TrainingSetName() const
Get name of the training set from which this classifier was generated.
Definition classifier.hpp:541
static std::unique_ptr< Classifier > FromBuffer(const void *buffer, size_t size)
Recreate a serialized Minos classifier from a byte array.
Definition classifier.hpp:338
QualityFeedback QualityMeasure() const
Get the type of quality feedback from the classifier.
Definition classifier.hpp:518
void SetContrastTrigger(int trigger)
Set the trigger value for the contrast of features to be taken into account.
Definition classifier.hpp:486
Collection of model information.
Definition classifier.hpp:172
ClassifierModelInfo ReadInfo(int index) const
Retrieves the indexed model information block.
Definition classifier.hpp:203
void WriteInfo(int index, ClassifierModelInfo model)
Writes the indexed model information block.
Definition classifier.hpp:220
int Count() const
Retrieves the number of elements in the collection.
Definition classifier.hpp:191
std::vector< ClassifierModelInfo > ReadInfo() const
Retrieves all the items stored in the collection.
Definition classifier.hpp:236
Information about a Minos classifier model.
Definition classifier.hpp:83
String Name() const
Get the name of the model.
Definition classifier.hpp:102
Point2D< int > AdvanceVector() const
Get the advance vector of the model.
Definition classifier.hpp:122
void SetAdvanceVector(Point2D< int > advanceVector)
Set the advance vector of the model.
Definition classifier.hpp:132
ClassifierModelInfo(const String &name, Point2D< int > advanceVector)
Create a new model information object.
Definition classifier.hpp:91
void SetName(const String &name)
Set the name of the model.
Definition classifier.hpp:112
The set of parameters, which controls, how a classifier is being learned from a training set.
Definition learn_parameters.hpp:19
Search result returned by Minos.
Definition search_result.hpp:25
Multi-purpose 2D vector class.
Definition point_2d.hpp:20
T X() const noexcept
Gets the x-component of the point.
Definition point_2d.hpp:84
T Y() const noexcept
Gets the y-component of the point.
Definition point_2d.hpp:104
Rectangle object.
Definition rect.hpp:24
cvbbool_t ReleaseObject(OBJ &Object)
Namespace for the Minos package.
Definition classifier.hpp:29
bool operator==(const ClassifierModelInfo &lhs, const ClassifierModelInfo &rhs) noexcept
Comparison operator for ClassifierModelInfo objects.
Definition classifier.hpp:163
SearchMode
Different modes for the search calls that return a single result.
Definition classifier.hpp:61
@ FindBest
Search the whole region of interest and return the best result.
Definition classifier.hpp:65
@ FindFirst
Stop after the first result has been found.
Definition classifier.hpp:63
@ FindBestSubPixel
Search the whole region of interest and return the best result with sub pixel accuracy.
Definition classifier.hpp:67
std::shared_ptr< Classifier > ClassifierPtr
Convenience shared pointer for Classifier.
Definition classifier.hpp:794
QualityFeedback
Feedback type for search functions.
Definition classifier.hpp:52
@ Normalized
Normalized quality feedback from correlation over a classifier's features in the range [0....
Definition classifier.hpp:56
@ Unnormalized
Unnormalized quality feedback in the range [0...255].
Definition classifier.hpp:54
ReadMode
Options for the read functions.
Definition classifier.hpp:72
@ ReturnFirstPosition
Return the position of the first character.
Definition classifier.hpp:74
@ ReturnLastPosition
Return the position of the last character.
Definition classifier.hpp:76
bool operator!=(const ClassifierModelInfo &lhs, const ClassifierModelInfo &rhs) noexcept
Comparison operator for ClassifierModelInfo objects.
Definition classifier.hpp:150
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