CVB++ 15.0
training_set.hpp
1#pragma once
2
3#include "../_cexports/c_minos.h"
4
5#include "../global.hpp"
6#include "../image.hpp"
7#include "../matrix_2d.hpp"
8#include "../point_2d.hpp"
9#include "../rect.hpp"
10#include "../area_2d.hpp"
11
12#include "search_result.hpp"
13
14#include <vector>
15#include <climits>
16#include <utility>
17#include <memory>
18
19namespace Cvb
20{
21CVB_BEGIN_INLINE_NS
22
23namespace Minos { class TrainingSet; }
24
25template <>
26inline HandleGuard<Minos::TrainingSet>::HandleGuard(void * handle) noexcept
27 : HandleGuard<Minos::TrainingSet>(handle, [](void* h) { CVB_CALL_CAPI(ReleaseObject(h)); })
28{
29}
30
32namespace Minos
33{
34
35// forward declarations
36class ImageInfo;
37class ModelInfo;
38
40
43{
45 friend class ImageInstanceInfoCollection;
46 struct PrivateTag {};
47public:
48 InstanceInfo (const InstanceInfo& other) = delete;
49 InstanceInfo& operator= (const InstanceInfo& other) = delete;
50 InstanceInfo(InstanceInfo&& other) = delete;
51 InstanceInfo& operator= (InstanceInfo&& other) = delete;
52 virtual ~InstanceInfo() = default;
53
54 InstanceInfo (const SharedHandleGuard<TrainingSet> &sguardTrSet, CExports::MTSINSTANCE nativeHandle, PrivateTag)
55 : sguardTrSet_(sguardTrSet), nativeHandle_ (nativeHandle)
56 { }
57
58
60
65 int Index () const
66 {
67 return static_cast<int> (CVB_CALL_CAPI (InstanceIndex(Handle())));
68 }
69
71
77 {
78 return Internal::DoHandleCallObjectOut<class Image>(CVB_CALL_CAPI(CreateImageFromInstance(Handle())));
79 }
80
82
87#if 0
88 double GetCorrelation () const
89 {
90 }
91#endif
92
94
100
102
108 {
109 double dX = CVB_CALL_CAPI (InstanceX(Handle()));
110 double dY = CVB_CALL_CAPI (InstanceY(Handle()));
111 return Point2D<double> (dX, dY);
112 }
113
115
121
123
129 void * Handle() const noexcept
130 {
131 return nativeHandle_;
132 }
133
134private:
135 SharedHandleGuard<TrainingSet> sguardTrSet_;
136 CExports::MTSINSTANCE nativeHandle_;
137}; /* class InstanceInfo */
138
140
147inline bool operator== (const InstanceInfo &lhs, const InstanceInfo &rhs) noexcept
148{
149 return (lhs.Handle() == rhs.Handle());
150}
151
153
160inline bool operator!= (const InstanceInfo &lhs, const InstanceInfo &rhs) noexcept
161{
162 return (!(lhs == rhs));
163}
164
165namespace Private
166{
167 template<class T>
168 auto ReadInfos (const T &collection) -> std::vector<decltype(collection.ReadInfo(0))>
169 {
170 auto count = collection.Count ();
171 std::vector<decltype(collection.ReadInfo (0))> info;
172 for (decltype(count) i = 0; i < count; ++i)
173 {
174 info.push_back (collection.ReadInfo (i));
175 }
176
177 return info;
178 }
179
180 template<class Tcol, class Tobj> int IndexOf (const Tcol &collection, const Tobj &obj)
181 {
182 auto count = collection.Count ();
183 for (decltype(count) i = 0; i < count; ++i)
184 {
185 auto readInfo = collection.ReadInfo(i);
186 if (*readInfo == obj)
187 {
188 return i;
189 }
190 }
191 return -1;
192 }
193} /* namespace Private */
194
196
199{
200public:
201 explicit InstanceInfoCollectionGeneric (const SharedHandleGuard<TrainingSet> &sguard)
202 : shandle_(sguard)
203 {}
204
206 InstanceInfoCollectionGeneric& operator= (const InstanceInfoCollectionGeneric& other) = delete;
209 virtual ~InstanceInfoCollectionGeneric() = default;
210
212
217 int Count () const
218 {
219 return GetCount ();
220 }
221
223
230 {
231 auto instHandle = GetInstanceNativeHandle (index);
232 if (instHandle == nullptr)
233 {
234 Utilities::SystemInfo::ThrowLastError ();
235 }
236 return std::make_unique<InstanceInfo>(shandle_, instHandle, InstanceInfo::PrivateTag{});
237 }
238
240
246 {
247 return Private::ReadInfos (*this);
248 }
249
251
258 int IndexOf (const InstanceInfo &obj) const
259 {
260 return Private::IndexOf (*this, obj);
261 }
262
264
274 bool Remove (const InstanceInfo &instance)
275 {
276 auto count = Count ();
277 for (decltype(count) i = 0; i < count; ++i)
278 {
279 if (*ReadInfo(i) == instance)
280 {
281 CVB_CALL_CAPI_CHECKED (RemoveMTSInstance(instance.Handle()));
282 return true;
283 }
284 }
285 return false;
286 }
287
289
299 bool RemoveAt (int index)
300 {
301 return Remove (*ReadInfo (index));
302 }
303
304protected:
305 virtual int GetCount () const = 0;
306 virtual CExports::MTSINSTANCE GetInstanceNativeHandle (int index) const = 0;
307
308protected:
309 SharedHandleGuard<TrainingSet> shandle_; // NOLINT(cppcoreguidelines-non-private-member-variables-in-classes)
310}; /* class InstanceInfoCollectionGeneric */
311
313
316{
317public:
318 explicit InstanceInfoCollection (const SharedHandleGuard<TrainingSet> &sguard)
320 collectionParent_(sguard.Handle())
321 {}
322
323
324protected:
325 int GetCount () const override
326 {
327 return static_cast<int> (CVB_CALL_CAPI (NumMTSInstances(collectionParent_)));
328 }
329
330 CExports::MTSINSTANCE GetInstanceNativeHandle (int index) const override
331 {
332 return CVB_CALL_CAPI (MTSInstance (collectionParent_, index));
333 }
334
335private:
336 CExports::MTSINSTANCE collectionParent_;
337}; /* class InstanceInfoCollection */
338
340
343{
344public:
345 ImageInstanceInfoCollection (const SharedHandleGuard<TrainingSet> &sguard, CExports::MTSIMAGE collectionParent)
347 collectionParent_(collectionParent)
348 {}
349
350
352
362 std::unique_ptr<InstanceInfo> Add (const String &name, bool askForce, Point2D<double> location)
363 {
364 double dX = location.X();
365 double dY = location.Y();
366 CExports::MTSINSTANCE instance = nullptr;
367 CVB_CALL_CAPI_CHECKED (NewMTSInstanceTyped(collectionParent_, name.c_str(), askForce, dX, dY, instance));
368 if (instance == nullptr)
369 {
370 throw std::runtime_error("failed to add sample to the class");
371 }
372 return std::make_unique<InstanceInfo>(shandle_, instance, InstanceInfo::PrivateTag{});
373 }
374
375protected:
376 int GetCount () const override
377 {
378 return static_cast<int> (CVB_CALL_CAPI (NumMTSImageInstances(collectionParent_)));
379 }
380
381 CExports::MTSINSTANCE GetInstanceNativeHandle (int index) const override
382 {
383 return CVB_CALL_CAPI (MTSImageInstance (collectionParent_, index));
384 }
385
386private:
387 CExports::MTSIMAGE collectionParent_;
388}; /* class ImageInstanceInfoCollection */
389
391
394{
395public:
396 ModelInstanceInfoCollection (const SharedHandleGuard<TrainingSet> &sguard, CExports::MTSMODEL collectionParent)
398 collectionParent_(collectionParent)
399 {}
400
401protected:
402 int GetCount () const override
403 {
404 return static_cast<int> (CVB_CALL_CAPI (NumMTSModelInstances(collectionParent_)));
405 }
406
407 CExports::MTSINSTANCE GetInstanceNativeHandle (int index) const override
408 {
409 return CVB_CALL_CAPI (MTSModelInstance (collectionParent_, index));
410 }
411
412private:
413 CExports::MTSMODEL collectionParent_;
414}; /* class ModelInstanceInfoCollection */
415
417
420{
421 friend class InstanceInfo;
422 friend class ImageInfoCollection;
423
424 struct PrivateTag {};
425
426public:
427 ImageInfo (const ImageInfo& other) = delete;
428 ImageInfo& operator= (const ImageInfo& other) = delete;
429 ImageInfo(ImageInfo&& other) = delete;
430 ImageInfo& operator= (ImageInfo&& other) = delete;
431 virtual ~ImageInfo() = default;
432
433 ImageInfo (const SharedHandleGuard<TrainingSet> &sguardTrSet, int index, PrivateTag)
434 : sguardTrSet_(sguardTrSet),
435 nativeHandle_ (CVB_CALL_CAPI (MTSImage(sguardTrSet.Handle(), index))),
436 instances_ (sguardTrSet_, nativeHandle_)
437 {
438 if (nativeHandle_ == nullptr)
439 {
440 Utilities::SystemInfo::ThrowLastError();
441 }
442 }
443
444
445public:
447
452 int Index () const
453 {
454 return static_cast<int> (CVB_CALL_CAPI (MTSImageIndex(Handle())));
455 }
456
458
464 {
465 return Internal::DoBoolCallObjectOut<class Image>([&](void* & resimg)
466 {
467 resimg = CVB_CALL_CAPI(GetImageFromImage(Handle()));
468 return CVB_CALL_CAPI(ShareObject (resimg));
469 });
470 }
471
472
474
480 {
481 /* Note: the binary compatibility between Matrix and TMatrix is guaranteed, therefore the cast below is legal. */
482 CVB_CALL_CAPI_CHECKED(TransformMTSImage(Handle(), reinterpret_cast<const CExports::TMatrix&>(matrix)));
483 }
484
486
493 std::vector<SearchResult> CheckConsistency (double threshold, double density = 1.0)
494 {
495 CExports::RESULTS hSearchResults = nullptr;
496 CVB_CALL_CAPI_CHECKED (MTSImageCheck (Handle(), threshold, static_cast<int>(density * 1000.0), nullptr, nullptr, hSearchResults));
497 ReleaseObjectGuard hSearchResHolder (hSearchResults);
498 return Private::SearchResultsToArray(hSearchResults);
499 }
500
502
510 {
511 return instances_;
512 }
513
515
521 void * Handle() const noexcept
522 {
523 return nativeHandle_;
524 }
525
526private:
527 SharedHandleGuard<TrainingSet> sguardTrSet_;
528 CExports::MTSIMAGE nativeHandle_;
530}; /* class ImageInfo*/
531
533
540inline bool operator== (const ImageInfo &lhs, const ImageInfo &rhs) noexcept
541{
542 return (lhs.Handle() == rhs.Handle());
543}
544
546
553inline bool operator!= (const ImageInfo &lhs, const ImageInfo &rhs) noexcept
554{
555 return (!(lhs == rhs));
556}
557
559{
560 auto imageIndex = CVB_CALL_CAPI(GetImageFromInstance(Handle()));
561 return std::make_unique<ImageInfo>(sguardTrSet_, CVB_CALL_CAPI(MTSImageIndex(imageIndex)), ImageInfo::PrivateTag{});
562}
563
565
568{
569public:
570 explicit ImageInfoCollection (const SharedHandleGuard<TrainingSet> &sguard)
571 : shandle_(sguard)
572 {}
573
574 ImageInfoCollection (const ImageInfoCollection& other) = delete;
575 ImageInfoCollection& operator= (const ImageInfoCollection& other) = delete;
577 ImageInfoCollection& operator= (ImageInfoCollection&& other) = delete;
578 virtual ~ImageInfoCollection() = default;
579
581
586 int Count () const
587 {
588 return static_cast<int> (CVB_CALL_CAPI (NumMTSImages(shandle_.Handle())));
589 }
590
592
599 {
600 return std::make_unique<ImageInfo>(shandle_, index, ImageInfo::PrivateTag{});
601 }
602
604
610 {
611 return Private::ReadInfos (*this);
612 }
613
615
622 int IndexOf (const ImageInfo &obj) const
623 {
624 return Private::IndexOf (*this, obj);
625 }
626
628
636 void Add (const ImagePlane &plane)
637 {
638 auto h = CVB_CALL_CAPI (NewMTSImageIndex(shandle_.Handle(), plane.Parent().Handle(), plane.Plane()));
639 if (h == nullptr)
640 {
641 Utilities::SystemInfo::ThrowLastError ();
642 }
643 }
644
646
656 bool Remove (const ImageInfo &image)
657 {
658 auto count = Count ();
659 for (decltype(count) i = 0; i < count; ++i)
660 {
661 if (*ReadInfo(i) == image)
662 {
663 CVB_CALL_CAPI_CHECKED (RemoveMTSImage(image.Handle()));
664 return true;
665 }
666 }
667 return false;
668 }
669
671
681 bool RemoveAt (int index)
682 {
683 return Remove (*ReadInfo (index));
684 }
685
686private:
687 SharedHandleGuard<TrainingSet> shandle_;
688}; /* class ImageInfoCollection */
689
691
694{
695 friend class InstanceInfo;
696 friend class ModelInfoCollection;
697 struct PrivateTag {};
698
699public:
700 ModelInfo (const ModelInfo& other) = delete;
701 ModelInfo& operator= (const ModelInfo& other) = delete;
702 ModelInfo(ModelInfo&& other) = delete;
703 ModelInfo& operator= (ModelInfo&& other) = delete;
704 virtual ~ModelInfo() = default;
705
706 ModelInfo (const SharedHandleGuard<TrainingSet> &sguardTrSet, int index, PrivateTag)
707 : sguardTrSet_(sguardTrSet),
708 nativeHandle_ (CVB_CALL_CAPI (MTSModel(sguardTrSet.Handle(), index))),
709 instances_ (sguardTrSet_, nativeHandle_)
710 {
711 if (nativeHandle_ == nullptr)
712 {
713 Utilities::SystemInfo::ThrowLastError ();
714 }
715 }
716
717
718public:
720
725 int Index () const
726 {
727 return static_cast<int> (CVB_CALL_CAPI (MTSModelIndex(Handle())));
728 }
729
731
737 {
738 return Internal::DoBoolCallObjectOut<class Image>([&](void* & resimg)
739 {
740 resimg = CVB_CALL_CAPI(GetImageFromModel(Handle()));
741 return CVB_CALL_CAPI(ShareObject (resimg));
742 });
743 }
744
746
750 String Name() const
751 {
752 const Char *name = nullptr;
753 CVB_CALL_CAPI (GetModelNameTyped(Handle(), name));
754 return (name != nullptr) ? String (name) : String ();
755 }
756
758
767 void SetName(const String &name)
768 {
769 CVB_CALL_CAPI_CHECKED (SetModelNameTyped(Handle(), name.c_str()));
770 }
771
773
778 {
780 CVB_CALL_CAPI_CHECKED (GetModelAdvance (Handle(), advX, advY));
781 return Point2D<double> (advX, advY);
782 }
783
785
790 {
791 CVB_CALL_CAPI_CHECKED (SetModelAdvance (Handle(), advanceVector.X(), advanceVector.Y()));
792 }
793
795
800 {
801 /* Note: the binary compatibility between Area2D and TArea is guaranteed, therefore the cast below is legal. */
802 Area2D featureWindowNative;
803 CVB_CALL_CAPI_CHECKED (GetFeatureWindow (Handle(), reinterpret_cast<CExports::TArea&>(featureWindowNative)));
804 return Round (Rect<double> (featureWindowNative.P0().X(), featureWindowNative.P0().Y(), featureWindowNative.P1().X(), featureWindowNative.P2().Y()));
805 }
806
808
812 void SetFeatureWindow(Rect<int> featureWindow)
813 {
814 /* Note: the binary compatibility between Area2D and TArea is guaranteed, therefore the cast below is legal. */
815 auto featureWindowNative = Area2D(static_cast<Rect<double>>(featureWindow));
816 CVB_CALL_CAPI_CHECKED (SetFeatureWindow (Handle(), reinterpret_cast<CExports::TArea&>(featureWindowNative)));
817 }
818
820
825 {
826 return Image()->CoordinateSystem().Translation();
827 }
828
830
835 {
836 auto cs = Image()->CoordinateSystem();
837 cs.Translation() = origin;
838 Image()->CoordinateSystem() = cs;
839 CVB_CALL_CAPI_CHECKED (TranslateModelOrigin(Handle()));
840 }
841
843
851 {
852 return instances_;
853 }
854
856
864 std::vector<SearchResult> CheckConsistency (const ImageInfo &trainingSetImage, double threshold, double density = 1.0)
865 {
866 CExports::RESULTS hSearchResults = nullptr;
867 CVB_CALL_CAPI_CHECKED (MTSModelImageCheck (Handle(), trainingSetImage.Handle(), threshold, static_cast<int>(density * 1000.0), nullptr, nullptr, hSearchResults));
868 ReleaseObjectGuard hSearchResHolder (hSearchResults);
869 return Private::SearchResultsToArray(hSearchResults);
870 }
871
873
880 std::vector<SearchResult> CheckConsistency (double threshold, double density = 1.0)
881 {
882 CExports::RESULTS hSearchResults = nullptr;
883 CVB_CALL_CAPI_CHECKED (MTSModelCheck (Handle(), threshold, static_cast<int>(density * 1000.0), nullptr, nullptr, hSearchResults));
884 ReleaseObjectGuard hSearchResHolder (hSearchResults);
885 return Private::SearchResultsToArray(hSearchResults);
886 }
887
889
899 {
900 int left = INT_MAX, top = INT_MAX, right = INT_MAX, bottom = INT_MAX;
901
902 for (auto&& i : instances_.ReadInfos())
903 {
904 auto loc_x = static_cast<int>(std::lround(i->Location ().X ()));
905 if (loc_x < left)
906 {
907 left = loc_x;
908 }
909 auto loc_y = static_cast<int>(std::lround(i->Location ().Y ()));
910 if (loc_y < top)
911 {
912 top = loc_y;
913 }
914 auto srcimg_w = i->SourceImage ()->Image ()->Width ();
915 if (srcimg_w - loc_x < right)
916 {
917 right = srcimg_w - loc_x - 1;
918 }
919 auto srcimg_h = i->SourceImage ()->Image ()->Height ();
920 if (srcimg_h - loc_y < bottom)
921 {
922 bottom = srcimg_h - loc_y - 1;
923 }
924 }
925 left = -left;
926 top = -top;
927
928 return Rect<int>(left, top, right, bottom);
929 }
930
932
938 {
939 int left = -FeatureWindow().Left();
940 int top = -FeatureWindow().Top();
941 int right = image.Image()->Width() - FeatureWindow().Right() - 1;
942 int bottom = image.Image()->Height() - FeatureWindow().Bottom() - 1;
943 return Rect<int>(left, top, right, bottom);
944 }
945
947
953 void * Handle() const noexcept
954 {
955 return nativeHandle_;
956 }
957
958private:
959 SharedHandleGuard<TrainingSet> sguardTrSet_;
960 CExports::MTSMODEL nativeHandle_;
962}; /* class ModelInfo */
963
965
972inline bool operator== (const ModelInfo &lhs, const ModelInfo &rhs) noexcept
973{
974 return (lhs.Handle() == rhs.Handle());
975}
976
978
985inline bool operator!= (const ModelInfo &lhs, const ModelInfo &rhs) noexcept
986{
987 return (!(lhs == rhs));
988}
989
991{
992 auto modelIndex = CVB_CALL_CAPI(GetModelFromInstance(nativeHandle_));
993 return std::make_unique<ModelInfo>(sguardTrSet_, CVB_CALL_CAPI(MTSModelIndex(modelIndex)), ModelInfo::PrivateTag{});
994}
995
997
1000{
1001public:
1002 explicit ModelInfoCollection (const SharedHandleGuard<TrainingSet> &sguard)
1003 : shandle_(sguard)
1004 {}
1005
1006 ModelInfoCollection (const ModelInfoCollection&) noexcept = delete;
1007 ModelInfoCollection& operator= (const ModelInfoCollection&) noexcept = delete;
1008 ModelInfoCollection(ModelInfoCollection&& other) noexcept = delete;
1009 ModelInfoCollection& operator= (ModelInfoCollection&& other) noexcept = delete;
1010 ~ModelInfoCollection() = default;
1011
1013
1018 int Count () const
1019 {
1020 return static_cast<int> (CVB_CALL_CAPI (NumMTSModels(shandle_.Handle())));
1021 }
1022
1024
1031 {
1032 return std::make_unique<ModelInfo>(shandle_, index, ModelInfo::PrivateTag{});
1033 }
1034
1036
1042 {
1043 return Private::ReadInfos (*this);
1044 }
1045
1047
1054 int IndexOf (const ModelInfo &obj) const
1055 {
1056 return Private::IndexOf (*this, obj);
1057 }
1058
1060
1066 bool Contains (const String &name)
1067 {
1068 auto count = Count ();
1069 for (decltype(count) i = 0; i < count; ++i)
1070 {
1071 if (ReadInfo(i)->Name () == name)
1072 {
1073 return true;
1074 }
1075 }
1076 return false;
1077 }
1078
1080
1088 void Add (const ImageInfo &image, const String &name, Point2D<double> location, Rect<int> featureWindow)
1089 {
1090 /* Note: the binary compatibility between Area2D and TArea is guaranteed, therefore the cast below is legal. */
1091 auto featureWindowNative = Area2D(static_cast<Rect<double>>(featureWindow));
1092 auto instance = CVB_CALL_CAPI (NewMTSModelTyped(image.Handle(), name.c_str(), location.X(), location.Y(), reinterpret_cast<CExports::TArea&>(featureWindowNative)));
1093 if (instance == nullptr)
1094 {
1095 Utilities::SystemInfo::ThrowLastError ();
1096 }
1097 }
1098
1100
1110 bool Remove (const ModelInfo &model)
1111 {
1112 auto count = Count ();
1113 for (decltype(count) i = 0; i < count; ++i)
1114 {
1115 if (*ReadInfo(i) == model)
1116 {
1117 CVB_CALL_CAPI_CHECKED (RemoveMTSModel(model.Handle()));
1118 return true;
1119 }
1120 }
1121 return false;
1122 }
1123
1125
1135 bool RemoveAt (int index)
1136 {
1137 return Remove (*ReadInfo (index));
1138 }
1139
1141
1147 {
1148 CVB_CALL_CAPI_CHECKED(SetMTSGlobalAdvance(shandle_.Handle(), true, vec.X(), vec.Y()));
1149 }
1150
1151private:
1152 SharedHandleGuard<TrainingSet> shandle_;
1153}; /* class ModelInfoCollection */
1154
1155
1157
1160{
1161private:
1162 // Internal helper constructor version
1163 explicit TrainingSet (HandleGuard<TrainingSet>&& guard, const String &srcFileName = String())
1164 : shandle_(std::move(guard)),
1165 fileName_ (srcFileName),
1166 instances_ (shandle_), images_ (shandle_), models_ (shandle_)
1167 {
1168 }
1169
1170 // Helper to create an empty training set
1171 static CExports::MTS CreateInternal ()
1172 {
1173 CExports::MTS mts = CVB_CALL_CAPI (CreateMTS(3, 0.6));
1174 if (mts == nullptr)
1175 {
1176 Utilities::SystemInfo::ThrowLastError();
1177 }
1178 return mts;
1179 }
1180
1181 // Helper to load a training set from file
1182 static CExports::MTS LoadInternal (const String & fileName)
1183 {
1184 CExports::MTS mts = nullptr;
1185
1186 CVB_CALL_CAPI_CHECKED (LoadMTSFileTyped (fileName.c_str(), mts));
1187 return mts;
1188 }
1189
1190public:
1192
1196 : TrainingSet(HandleGuard<TrainingSet>(CreateInternal ()))
1197 {
1198 }
1199
1201
1205 explicit TrainingSet(const String & fileName)
1206 : TrainingSet(HandleGuard<TrainingSet>(LoadInternal (fileName)), fileName)
1207 {
1208 }
1209
1210
1211 TrainingSet(const TrainingSet& other) = delete;
1212 TrainingSet& operator=(const TrainingSet & other) = delete;
1213 TrainingSet (TrainingSet&& other) = delete;
1214 TrainingSet& operator=(TrainingSet&& other) = delete;
1215 ~TrainingSet() = default;
1216
1217public:
1219
1224 static std::unique_ptr<TrainingSet> Load (const String & fileName)
1225 {
1226 return std::make_unique<TrainingSet>(fileName);
1227 }
1228
1230
1236 static std::unique_ptr<TrainingSet> FromBuffer (const void *buffer, size_t size)
1237 {
1238 return Internal::DoBoolCallObjectOut<TrainingSet>([&](void* & restset)
1239 {
1240 /* Note: need to cast-away const of "buffer" parameter to match the C-API, the function does not modify it. */
1241 return CVB_CALL_CAPI(MemoryToMTS (const_cast<void*>(buffer), static_cast<long>(size), restset)); // NOLINT(cppcoreguidelines-pro-type-const-cast)
1242 });
1243 }
1244
1246
1251 template <class RANGE>
1252 static typename TypedRange<std::unique_ptr<TrainingSet>, std::uint8_t, RANGE>::type FromBuffer (const RANGE & buffer)
1253 {
1254 auto bufferRange = MakeRangeAdapter<std::uint8_t> (buffer);
1255 return FromBuffer (bufferRange.Data (), bufferRange.Size ());
1256 }
1257
1259
1265 void * Handle() const noexcept
1266 {
1267 return shandle_.Handle();
1268 }
1269
1271
1278 static std::unique_ptr<TrainingSet> FromHandle (HandleGuard<TrainingSet>&& guard)
1279 {
1280 if (!guard.Handle ())
1281 {
1282 throw std::invalid_argument ("invalid training set native handle");
1283 }
1284 return std::unique_ptr<TrainingSet>(new TrainingSet(std::move(guard)));
1285 }
1286
1288
1294 void Save (const String & fileName) const
1295 {
1296 CVB_CALL_CAPI_CHECKED (WriteMTSFileTyped (Handle(), fileName.c_str()));
1297 fileName_ = fileName;
1298 }
1299
1301
1307 {
1308 auto sizeNeeded = CVB_CALL_CAPI (GetMTSSize(Handle()));
1309 std::vector<std::uint8_t> buffer (sizeNeeded);
1310 CVB_CALL_CAPI_CHECKED (MTSToMemory (Handle(), buffer.data(), sizeNeeded));
1311 return buffer;
1312 }
1313
1315
1322 {
1323 return Internal::DoBoolCallObjectOut<TrainingSet>([&](void* & resclas)
1324 {
1325 /* Note: the binary compatibility between Matrix and TMatrix is guaranteed, therefore the cast below is legal. */
1326 return CVB_CALL_CAPI(MTSTransform (Handle(), reinterpret_cast<const CExports::TMatrix&>(transformation), resclas));
1327 });
1328 }
1329
1331
1338 std::vector<SearchResult> CheckConsistency (double threshold, double density = 1.0)
1339 {
1340 CExports::RESULTS hSearchResults = nullptr;
1341 CVB_CALL_CAPI_CHECKED (MTSConsistencyCheck (Handle (), threshold, static_cast<int>(density * 1000.0), nullptr, nullptr, hSearchResults));
1342 ReleaseObjectGuard hSearchResHolder (hSearchResults);
1343 return Private::SearchResultsToArray(hSearchResults);
1344 }
1345
1347
1354 int ClassCount() const
1355 {
1356 return static_cast<int> (CVB_CALL_CAPI (NumMTSClasses(Handle())));
1357 }
1358
1360
1368 {
1369 return instances_;
1370 }
1371
1373
1377 int InstancesTotal() const
1378 {
1379 return instances_.Count();
1380 }
1381
1383
1391 {
1392 return images_;
1393 }
1394
1396
1404 {
1405 return models_;
1406 }
1407
1409
1414 {
1415 return fileName_;
1416 }
1417
1419
1425 bool IsModified() const
1426 {
1427 CExports::cvbbool_t retval = false;
1428 CVB_CALL_CAPI_CHECKED (GetMTSModified(Handle(), retval));
1429 return retval != 0;
1430 }
1431
1433
1439 void SetIsModified(bool isModified)
1440 {
1441 CVB_CALL_CAPI_CHECKED (SetMTSModified(Handle(), isModified));
1442 }
1443
1445
1450 {
1451 CExports::cvbdim_t left = 0, top = 0, right = 0, bottom = 0;
1452 CVB_CALL_CAPI_CHECKED (GetMTSExtent (Handle (), left, top, right, bottom));
1453 return Rect<int> (static_cast<int>(left), static_cast<int>(top), static_cast<int>(right), static_cast<int>(bottom));
1454 }
1455
1457
1462 {
1463 Area2D area;
1464 /* Note: the binary compatibility between Area2D and TArea is guaranteed, therefore the cast below is legal. */
1465 CVB_CALL_CAPI_CHECKED (GetMTSLastFeatureWnd(Handle(), reinterpret_cast<CExports::TArea&>(area)));
1466 return Round (Rect<double>(area.P0().X(), area.P0().Y(), area.P1().X(), area.P2().Y()));
1467 }
1468
1470
1475 {
1476 const Char *comment = nullptr;
1477 CVB_CALL_CAPI (MTSCommentTyped(Handle(), comment));
1478 return (comment != nullptr) ? String (comment) : String ();
1479 }
1480
1482
1486 void SetComment(String comment)
1487 {
1488 CVB_CALL_CAPI_CHECKED (SetMTSCommentTyped (Handle(), comment.c_str()));
1489 }
1490
1492
1496 double ExpectationRadius() const
1497 {
1498 double expectationRadius = std::numeric_limits<double>::quiet_NaN(), correlationThreshold = std::numeric_limits<double>::quiet_NaN();
1499 CVB_CALL_CAPI_CHECKED (GetMTSCorelParams(Handle(), expectationRadius, correlationThreshold));
1500 return expectationRadius;
1501 }
1502
1504
1508 void SetExpectationRadius(double expectationRadius)
1509 {
1510 CVB_CALL_CAPI_CHECKED (SetMTSCorelParams (Handle(), expectationRadius, CorrelationThreshold()));
1511 }
1512
1514
1519 {
1520 double expectationRadius = std::numeric_limits<double>::quiet_NaN(), correlationThreshold = std::numeric_limits<double>::quiet_NaN();
1521 CVB_CALL_CAPI_CHECKED (GetMTSCorelParams(Handle(), expectationRadius, correlationThreshold));
1522 return correlationThreshold;
1523 }
1524
1526
1530 void SetCorrelationThreshold(double correlationThreshold)
1531 {
1532 CVB_CALL_CAPI_CHECKED (SetMTSCorelParams (Handle(), ExpectationRadius(), correlationThreshold));
1533 }
1534
1535private:
1536 SharedHandleGuard<TrainingSet> shandle_;
1537 mutable String fileName_;
1538 InstanceInfoCollection instances_;
1539 ImageInfoCollection images_;
1540 ModelInfoCollection models_;
1541}; /* class TrainingSet */
1542
1545
1546
1547} /* namespace Minos */
1548CVB_END_INLINE_NS
1549} /* namespace Cvb */
Structure that represents an area of interest in the image.
Definition: area_2d.hpp:21
Point2D< double > P2() const noexcept
Gets P2 of the area (top right corner).
Definition: area_2d.hpp:117
Point2D< double > P0() const noexcept
Gets P0 of the area (top left corner).
Definition: area_2d.hpp:89
Point2D< double > P1() const noexcept
Gets P1 of the area (lower left corner).
Definition: area_2d.hpp:103
Image plane information container.
Definition: decl_image_plane.hpp:33
Double precision 2x2 matrix class.
Definition: matrix_2d.hpp:16
Class that maintains the collection of Training Images inside a Minos Training Set.
Definition: training_set.hpp:568
void Add(const ImagePlane &plane)
Add a new training set image to the parent training set.
Definition: training_set.hpp:636
std::vector< std::unique_ptr< ImageInfo > > ReadInfos() const
Retrieves all the items stored in the collection.
Definition: training_set.hpp:609
int Count() const
Retrieves the number of elements in the collection.
Definition: training_set.hpp:586
int IndexOf(const ImageInfo &obj) const
Determine the index of an image information object inside this collection.
Definition: training_set.hpp:622
bool Remove(const ImageInfo &image)
Remove a training set image along with its instances from the training set.
Definition: training_set.hpp:656
std::unique_ptr< ImageInfo > ReadInfo(int index) const
Retrieves the indexed image information block.
Definition: training_set.hpp:598
bool RemoveAt(int index)
Remove a training set image along with its instances from the training set.
Definition: training_set.hpp:681
Image that has been added to a training set.
Definition: training_set.hpp:420
std::vector< SearchResult > CheckConsistency(double threshold, double density=1.0)
Test this image for potentially forgotten instances to be trained.
Definition: training_set.hpp:493
void TransformImageAndInstances(Matrix2D matrix)
Transform this image and all instances trained from this image using a 2x2 matrix.
Definition: training_set.hpp:479
std::unique_ptr< class Image > Image() const
Image representation of this object.
Definition: training_set.hpp:463
int Index() const
Index of this object in the parent's collection.
Definition: training_set.hpp:452
ImageInstanceInfoCollection & Instances()
The instances belonging to this model.
Definition: training_set.hpp:509
void * Handle() const noexcept
Classic API MTSIMAGE handle.
Definition: training_set.hpp:521
Class that maintains the collection of Training Images inside a Minos Training Set.
Definition: training_set.hpp:343
std::unique_ptr< InstanceInfo > Add(const String &name, bool askForce, Point2D< double > location)
Extract a new instance from the parent image and added it to the model with the specified name.
Definition: training_set.hpp:362
Class that maintains the collection of Training Images inside a Minos object.
Definition: training_set.hpp:199
std::unique_ptr< InstanceInfo > ReadInfo(int index) const
Retrieves the indexed instance information block.
Definition: training_set.hpp:229
int Count() const
Retrieves the number of elements in the collection.
Definition: training_set.hpp:217
std::vector< std::unique_ptr< InstanceInfo > > ReadInfos() const
Retrieves all the items stored in the collection.
Definition: training_set.hpp:245
bool Remove(const InstanceInfo &instance)
Remove an instance from the training set.
Definition: training_set.hpp:274
int IndexOf(const InstanceInfo &obj) const
Determine the index of an image information object inside this collection.
Definition: training_set.hpp:258
bool RemoveAt(int index)
Remove an instance from the training set.
Definition: training_set.hpp:299
Class that maintains the collection of Training Images inside a Minos Training Set.
Definition: training_set.hpp:316
Instance information that has been added to a training set.
Definition: training_set.hpp:43
std::unique_ptr< ModelInfo > Model() const
The Training Set Model into which this instance has been trained.
Definition: training_set.hpp:990
std::unique_ptr< class Image > Image() const
Image representation of this object.
Definition: training_set.hpp:76
int Index() const
Index of this object in the parent's collection.
Definition: training_set.hpp:65
Point2D< double > Location() const
Position in the SourceImage from which this instance has been extracted.
Definition: training_set.hpp:107
std::unique_ptr< ImageInfo > SourceImage() const
Retrieve the correlation between this instance image and the model image.
Definition: training_set.hpp:558
void * Handle() const noexcept
Classic API MTSINSTANCE handle.
Definition: training_set.hpp:129
Class that maintains the collection of Training Images inside a Minos Training Set.
Definition: training_set.hpp:1000
bool Remove(const ModelInfo &model)
Remove a model (and all the instances extracted for that model!) from the training set.
Definition: training_set.hpp:1110
std::vector< std::unique_ptr< ModelInfo > > ReadInfos() const
Retrieves all the items stored in the collection.
Definition: training_set.hpp:1041
std::unique_ptr< ModelInfo > ReadInfo(int index) const
Retrieves the indexed model information block.
Definition: training_set.hpp:1030
int Count() const
Retrieves the number of elements in the collection.
Definition: training_set.hpp:1018
bool Contains(const String &name)
Check if the collection contains a model with the specified name.
Definition: training_set.hpp:1066
int IndexOf(const ModelInfo &obj) const
Determine the index of an image information object inside this collection.
Definition: training_set.hpp:1054
void SetGlobalAdvanceVector(Point2D< double > vec)
Set an advance vector for all available models.
Definition: training_set.hpp:1146
void Add(const ImageInfo &image, const String &name, Point2D< double > location, Rect< int > featureWindow)
Add a new model (plus the first instance of that model) to the training set.
Definition: training_set.hpp:1088
bool RemoveAt(int index)
Remove a model (and all the instances extracted for that model!) from the training set.
Definition: training_set.hpp:1135
Model that has been added to a training set.
Definition: training_set.hpp:694
const ModelInstanceInfoCollection & Instances() const
The instances belonging to this model.
Definition: training_set.hpp:850
std::vector< SearchResult > CheckConsistency(double threshold, double density=1.0)
Test all images in the parent training set for instances of this model that might have been forgotten...
Definition: training_set.hpp:880
Rect< int > GetExtractableArea(const ImageInfo &image) const
Determine the maximum area from which an instance of this model may be extracted from a given trainin...
Definition: training_set.hpp:937
Point2D< double > AdvanceVector() const
Get the advance vector associated with this model.
Definition: training_set.hpp:777
Point2D< double > Origin() const
Get the origin of the model in terms of coordinates inside the feature window.
Definition: training_set.hpp:824
std::vector< SearchResult > CheckConsistency(const ImageInfo &trainingSetImage, double threshold, double density=1.0)
Test the image referenced by trainingSetImage for instances of this model that might have been forgot...
Definition: training_set.hpp:864
void SetAdvanceVector(Point2D< double > advanceVector)
Set the advance vector associated with this model.
Definition: training_set.hpp:789
std::unique_ptr< class Image > Image() const
Image representation of this object.
Definition: training_set.hpp:736
String Name() const
Get the name of the model.
Definition: training_set.hpp:750
void SetFeatureWindow(Rect< int > featureWindow)
Set the feature window for this model.
Definition: training_set.hpp:812
int Index() const
Index of this object in the parent's collection.
Definition: training_set.hpp:725
void SetOrigin(Point2D< double > origin)
Set the origin of the model in terms of coordinates inside the feature window.
Definition: training_set.hpp:834
Rect< int > GetMaxFeatureWindow() const
Retrieve the maximum values that may be set as feature window for this model.
Definition: training_set.hpp:898
void SetName(const String &name)
Set the name of the model.
Definition: training_set.hpp:767
Rect< int > FeatureWindow() const
Get the feature window for this model.
Definition: training_set.hpp:799
void * Handle() const noexcept
Classic API MTSMODEL handle.
Definition: training_set.hpp:953
Class that maintains the collection of Training Images inside a Minos Training Set.
Definition: training_set.hpp:394
A Minos Training Set from which a classifier can be generated.
Definition: training_set.hpp:1160
static TypedRange< std::unique_ptr< TrainingSet >, std::uint8_t, RANGE >::type FromBuffer(const RANGE &buffer)
Recreate a serialized Minos training set from a byte array.
Definition: training_set.hpp:1252
std::unique_ptr< TrainingSet > Transform(Matrix2D transformation)
Generate a new training set by transforming this training set with a 2x2 transformation matrix.
Definition: training_set.hpp:1321
std::vector< SearchResult > CheckConsistency(double threshold, double density=1.0)
Test all images in the parent training set for instances of this model that might have been forgotten...
Definition: training_set.hpp:1338
double CorrelationThreshold() const
Gets the correlation threshold. The correlation value under which Minos proposes generating a new mod...
Definition: training_set.hpp:1518
int ClassCount() const
The number of identifiable classes inside this training set.
Definition: training_set.hpp:1354
static std::unique_ptr< TrainingSet > FromBuffer(const void *buffer, size_t size)
Recreate a serialized Minos training set from a byte array.
Definition: training_set.hpp:1236
void SetIsModified(bool isModified)
Set a flag that informs about unsaved modifications to the training set.
Definition: training_set.hpp:1439
ModelInfoCollection & Models()
The models contained in this training set.
Definition: training_set.hpp:1403
TrainingSet(const String &fileName)
Load a saved training set from a file.
Definition: training_set.hpp:1205
static std::unique_ptr< TrainingSet > FromHandle(HandleGuard< TrainingSet > &&guard)
Creates training set from a classic API handle.
Definition: training_set.hpp:1278
TrainingSet()
Create an empty training set.
Definition: training_set.hpp:1195
String Comment() const
Get the comment assigned to the training set at generation time.
Definition: training_set.hpp:1474
std::vector< std::uint8_t > ToBuffer() const
Serializes the training set into a buffer.
Definition: training_set.hpp:1306
void SetExpectationRadius(double expectationRadius)
Set the expectation radius - the radius that is searched for the best occurrence of a sample,...
Definition: training_set.hpp:1508
void Save(const String &fileName) const
Write the training set to a file.
Definition: training_set.hpp:1294
Rect< int > Extent() const
Extent of the classes in the training set relative to the anchor point.
Definition: training_set.hpp:1449
bool IsModified() const
Get a flag that informs about unsaved modifications to the training set.
Definition: training_set.hpp:1425
void SetComment(String comment)
Set the comment assigned to the training set at generation time.
Definition: training_set.hpp:1486
int InstancesTotal() const
Total number of instances currently trained in this training set.
Definition: training_set.hpp:1377
static std::unique_ptr< TrainingSet > Load(const String &fileName)
Load a saved training set from a file.
Definition: training_set.hpp:1224
Rect< int > LastFeatureWindow() const
Feature window of the last model that has been created for the training set.
Definition: training_set.hpp:1461
void SetCorrelationThreshold(double correlationThreshold)
Set the correlation threshold - the correlation value below which Minos will suggest the generation o...
Definition: training_set.hpp:1530
String FileName() const
Name of the file, from which this training set was loaded (empty string if this image list was neithe...
Definition: training_set.hpp:1413
void * Handle() const noexcept
Classic API CLF handle.
Definition: training_set.hpp:1265
ImageInfoCollection & Images()
The images contained in this training set.
Definition: training_set.hpp:1390
InstanceInfoCollection & Instances()
The instances contained in this training set.
Definition: training_set.hpp:1367
double ExpectationRadius() const
Get the expectation radius - the radius that is searched for the best occurrence of a sample,...
Definition: training_set.hpp:1496
T X() const noexcept
Gets the x-component of the point.
Definition: point_2d.hpp:86
T Y() const noexcept
Gets the y-component of the point.
Definition: point_2d.hpp:106
T Bottom() const noexcept
Gets bottom row of the rectangle (still inside the rectangle).
Definition: rect.hpp:151
T Top() const noexcept
Gets first row of the rectangle.
Definition: rect.hpp:111
T Right() const noexcept
Gets rightmost column of the rectangle (still inside the rectangle).
Definition: rect.hpp:131
T Left() const noexcept
Gets first column of the rectangle.
Definition: rect.hpp:91
T count(T... args)
bool operator==(const ClassifierModelInfo &lhs, const ClassifierModelInfo &rhs) noexcept
Comparison operator for ClassifierModelInfo objects.
Definition: classifier.hpp:159
bool operator!=(const ClassifierModelInfo &lhs, const ClassifierModelInfo &rhs) noexcept
Comparison operator for ClassifierModelInfo objects.
Definition: classifier.hpp:146
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:70
Point2D< int > Round(const Point2D< T > &rhs) noexcept
Round to an integer point.
Definition: point_2d.hpp:380
T quiet_NaN(T... args)