CVB++ 14.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{
44public:
45 InstanceInfo (const InstanceInfo&) = default;
46 InstanceInfo& operator= (const InstanceInfo&) = default;
47
48private:
49 InstanceInfo (const SharedHandleGuard<TrainingSet> &sguardTrSet, CExports::MTSINSTANCE nativeHandle)
50 : sguardTrSet_(sguardTrSet), nativeHandle_ (nativeHandle)
51 { }
53 friend class ImageInstanceInfoCollection;
54
55public:
57
62 int Index () const
63 {
64 return static_cast<int> (CVB_CALL_CAPI (InstanceIndex(Handle())));
65 }
66
68
74 {
75 return Internal::DoHandleCallObjectOut<class Image>(CVB_CALL_CAPI(CreateImageFromInstance(Handle())));
76 }
77
79
84#if 0
85 double GetCorrelation () const
86 {
87 }
88#endif
89
91
96 ImageInfo SourceImage () const;
97
99
105 {
106 double dX = CVB_CALL_CAPI (InstanceX(Handle()));
107 double dY = CVB_CALL_CAPI (InstanceY(Handle()));
108 return Point2D<double> (dX, dY);
109 }
110
112
117 ModelInfo Model () const;
118
120
126 void * Handle() const noexcept
127 {
128 return nativeHandle_;
129 }
130
131private:
132 SharedHandleGuard<TrainingSet> sguardTrSet_;
133 CExports::MTSINSTANCE nativeHandle_;
134}; /* class InstanceInfo */
135
137
144inline bool operator== (const InstanceInfo &lhs, const InstanceInfo &rhs) noexcept
145{
146 return (lhs.Handle() == rhs.Handle());
147}
148
150
157inline bool operator!= (const InstanceInfo &lhs, const InstanceInfo &rhs) noexcept
158{
159 return (!(lhs == rhs));
160}
161
162namespace Private
163{
164 template<class T>
165 auto ReadInfos (const T &collection) -> std::vector<decltype(collection.ReadInfo(0))>
166 {
167 auto count = collection.Count ();
168 std::vector<decltype(collection.ReadInfo (0))> info;
169 for (decltype(count) i = 0; i < count; ++i)
170 {
171 info.push_back (collection.ReadInfo (i));
172 }
173
174 return info;
175 }
176
177 template<class Tcol, class Tobj> int IndexOf (const Tcol &collection, const Tobj &obj)
178 {
179 auto count = collection.Count ();
180 for (decltype(count) i = 0; i < count; ++i)
181 {
182 if (collection.ReadInfo (i) == obj)
183 {
184 return i;
185 }
186 }
187 return -1;
188 }
189} /* namespace Private */
190
192
195{
196public:
197 InstanceInfoCollectionGeneric (const SharedHandleGuard<TrainingSet> &sguard)
198 : shandle_(sguard)
199 {}
200
203
204 virtual ~InstanceInfoCollectionGeneric() = default;
205
207
212 int Count () const
213 {
214 return GetCount ();
215 }
216
218
224 InstanceInfo ReadInfo (int index) const
225 {
226 auto instHandle = GetInstanceNativeHandle (index);
227 if (instHandle == nullptr)
228 {
229 Utilities::SystemInfo::ThrowLastError ();
230 }
231 return InstanceInfo (shandle_, instHandle);
232 }
233
235
241 {
242 return Private::ReadInfos (*this);
243 }
244
246
253 int IndexOf (const InstanceInfo &obj) const
254 {
255 return Private::IndexOf (*this, obj);
256 }
257
259
269 bool Remove (const InstanceInfo &instance)
270 {
271 auto count = Count ();
272 for (decltype(count) i = 0; i < count; ++i)
273 {
274 if (ReadInfo(i) == instance)
275 {
276 CVB_CALL_CAPI_CHECKED (RemoveMTSInstance(instance.Handle()));
277 return true;
278 }
279 }
280 return false;
281 }
282
284
294 bool RemoveAt (int index)
295 {
296 return Remove (ReadInfo (index));
297 }
298
299protected:
300 virtual int GetCount () const = 0;
301 virtual CExports::MTSINSTANCE GetInstanceNativeHandle (int index) const = 0;
302
303protected:
304 SharedHandleGuard<TrainingSet> shandle_;
305}; /* class InstanceInfoCollectionGeneric */
306
308
311{
312public:
313 InstanceInfoCollection (const SharedHandleGuard<TrainingSet> &sguard)
315 collectionParent_(sguard.Handle())
316 {}
317
319 InstanceInfoCollection& operator= (const InstanceInfoCollection&) = default;
320
321 virtual ~InstanceInfoCollection() = default;
322
323protected:
324 int GetCount () const override
325 {
326 return static_cast<int> (CVB_CALL_CAPI (NumMTSInstances(collectionParent_)));
327 }
328
329 CExports::MTSINSTANCE GetInstanceNativeHandle (int index) const override
330 {
331 return CVB_CALL_CAPI (MTSInstance (collectionParent_, index));
332 }
333
334private:
335 CExports::MTSINSTANCE collectionParent_;
336}; /* class InstanceInfoCollection */
337
339
342{
343public:
344 ImageInstanceInfoCollection (const SharedHandleGuard<TrainingSet> &sguard, CExports::MTSIMAGE collectionParent)
346 collectionParent_(collectionParent)
347 {}
348
350 ImageInstanceInfoCollection& operator= (const ImageInstanceInfoCollection&) = default;
351
352 virtual ~ImageInstanceInfoCollection() = default;
353
355
365 InstanceInfo Add (const String &name, bool askForce, Point2D<double> location)
366 {
367 double dX = location.X();
368 double dY = location.Y();
369 CExports::MTSINSTANCE instance;
370 CVB_CALL_CAPI_CHECKED (NewMTSInstanceTyped(collectionParent_, name.c_str(), askForce, dX, dY, instance));
371 if (instance == nullptr)
372 {
373 throw std::runtime_error("failed to add sample to the class");
374 }
375 return InstanceInfo (shandle_, instance);
376 }
377
378protected:
379 int GetCount () const override
380 {
381 return static_cast<int> (CVB_CALL_CAPI (NumMTSImageInstances(collectionParent_)));
382 }
383
384 CExports::MTSINSTANCE GetInstanceNativeHandle (int index) const override
385 {
386 return CVB_CALL_CAPI (MTSImageInstance (collectionParent_, index));
387 }
388
389private:
390 CExports::MTSIMAGE collectionParent_;
391}; /* class ImageInstanceInfoCollection */
392
394
397{
398public:
399 ModelInstanceInfoCollection (const SharedHandleGuard<TrainingSet> &sguard, CExports::MTSMODEL collectionParent)
401 collectionParent_(collectionParent)
402 {}
403
405 ModelInstanceInfoCollection& operator= (const ModelInstanceInfoCollection&) = default;
406
407 virtual ~ModelInstanceInfoCollection() = default;
408
409protected:
410 int GetCount () const override
411 {
412 return static_cast<int> (CVB_CALL_CAPI (NumMTSModelInstances(collectionParent_)));
413 }
414
415 CExports::MTSINSTANCE GetInstanceNativeHandle (int index) const override
416 {
417 return CVB_CALL_CAPI (MTSModelInstance (collectionParent_, index));
418 }
419
420private:
421 CExports::MTSMODEL collectionParent_;
422}; /* class ModelInstanceInfoCollection */
423
425
428{
429public:
430 ImageInfo (const ImageInfo&) = default;
431 ImageInfo& operator= (const ImageInfo&) = default;
432
433private:
434 ImageInfo (const SharedHandleGuard<TrainingSet> &sguardTrSet, int index)
435 : sguardTrSet_(sguardTrSet),
436 nativeHandle_ (CVB_CALL_CAPI (MTSImage(sguardTrSet.Handle(), index))),
437 instances_ (sguardTrSet_, nativeHandle_)
438 {
439 if (nativeHandle_ == nullptr)
440 {
441 Utilities::SystemInfo::ThrowLastError();
442 }
443 }
444 friend class InstanceInfo;
445 friend class ImageInfoCollection;
446
447public:
449
454 int Index () const
455 {
456 return static_cast<int> (CVB_CALL_CAPI (MTSImageIndex(Handle())));
457 }
458
460
466 {
467 return Internal::DoBoolCallObjectOut<class Image>([&](void* & resimg)
468 {
469 resimg = CVB_CALL_CAPI(GetImageFromImage(Handle()));
470 return CVB_CALL_CAPI(ShareObject (resimg));
471 });
472 }
473
474
476
482 {
483 /* Note: the binary compatibility between Matrix and TMatrix is guaranteed, therefore the cast below is legal. */
484 CVB_CALL_CAPI_CHECKED(TransformMTSImage(Handle(), reinterpret_cast<const CExports::TMatrix&>(matrix)));
485 }
486
488
495 std::vector<SearchResult> CheckConsistency (double threshold, double density = 1.0)
496 {
497 CExports::RESULTS hSearchResults;
498 CVB_CALL_CAPI_CHECKED (MTSImageCheck (Handle(), threshold, static_cast<int>(density * 1000.0), nullptr, nullptr, hSearchResults));
499 ReleaseObjectGuard hSearchResHolder (hSearchResults);
500 return Private::SearchResultsToArray(hSearchResults);
501 }
502
504
512 {
513 return instances_;
514 }
515
517
523 void * Handle() const noexcept
524 {
525 return nativeHandle_;
526 }
527
528private:
529 SharedHandleGuard<TrainingSet> sguardTrSet_;
530 CExports::MTSIMAGE nativeHandle_;
532}; /* class ImageInfo*/
533
535
542inline bool operator== (const ImageInfo &lhs, const ImageInfo &rhs) noexcept
543{
544 return (lhs.Handle() == rhs.Handle());
545}
546
548
555inline bool operator!= (const ImageInfo &lhs, const ImageInfo &rhs) noexcept
556{
557 return (!(lhs == rhs));
558}
559
561{
562 auto imageIndex = CVB_CALL_CAPI(GetImageFromInstance(Handle()));
563 return ImageInfo(sguardTrSet_, CVB_CALL_CAPI(MTSImageIndex(imageIndex)));
564}
565
567
570{
571public:
572 ImageInfoCollection (const SharedHandleGuard<TrainingSet> &sguard)
573 : shandle_(sguard)
574 {}
575
576 ImageInfoCollection (const ImageInfoCollection&) = default;
577 ImageInfoCollection& operator= (const ImageInfoCollection&) = default;
578
580
585 int Count () const
586 {
587 return static_cast<int> (CVB_CALL_CAPI (NumMTSImages(shandle_.Handle())));
588 }
589
591
597 ImageInfo ReadInfo (int index) const
598 {
599 return ImageInfo (shandle_, index);
600 }
601
603
609 {
610 return Private::ReadInfos (*this);
611 }
612
614
621 int IndexOf (const ImageInfo &obj) const
622 {
623 return Private::IndexOf (*this, obj);
624 }
625
627
635 void Add (const ImagePlane &plane)
636 {
637 auto h = CVB_CALL_CAPI (NewMTSImageIndex(shandle_.Handle(), plane.Parent().Handle(), plane.Plane()));
638 if (h == nullptr)
639 {
640 Utilities::SystemInfo::ThrowLastError ();
641 }
642 }
643
645
655 bool Remove (const ImageInfo &image)
656 {
657 auto count = Count ();
658 for (decltype(count) i = 0; i < count; ++i)
659 {
660 if (ReadInfo(i) == image)
661 {
662 CVB_CALL_CAPI_CHECKED (RemoveMTSImage(image.Handle()));
663 return true;
664 }
665 }
666 return false;
667 }
668
670
680 bool RemoveAt (int index)
681 {
682 return Remove (ReadInfo (index));
683 }
684
685private:
686 SharedHandleGuard<TrainingSet> shandle_;
687}; /* class ImageInfoCollection */
688
690
693{
694public:
695 ModelInfo (const ModelInfo&) = default;
696 ModelInfo& operator= (const ModelInfo&) = default;
697
698private:
699 ModelInfo (const SharedHandleGuard<TrainingSet> &sguardTrSet, int index)
700 : sguardTrSet_(sguardTrSet),
701 nativeHandle_ (CVB_CALL_CAPI (MTSModel(sguardTrSet.Handle(), index))),
702 instances_ (sguardTrSet_, nativeHandle_)
703 {
704 if (nativeHandle_ == nullptr)
705 {
706 Utilities::SystemInfo::ThrowLastError ();
707 }
708 }
709 friend class InstanceInfo;
710 friend class ModelInfoCollection;
711
712public:
714
719 int Index () const
720 {
721 return static_cast<int> (CVB_CALL_CAPI (MTSModelIndex(Handle())));
722 }
723
725
731 {
732 return Internal::DoBoolCallObjectOut<class Image>([&](void* & resimg)
733 {
734 resimg = CVB_CALL_CAPI(GetImageFromModel(Handle()));
735 return CVB_CALL_CAPI(ShareObject (resimg));
736 });
737 }
738
740
744 String Name() const
745 {
746 const Char *name = nullptr;
747 CVB_CALL_CAPI (GetModelNameTyped(Handle(), name));
748 return (name != nullptr) ? String (name) : String ();
749 }
750
752
761 void SetName(const String &name)
762 {
763 CVB_CALL_CAPI_CHECKED (SetModelNameTyped(Handle(), name.c_str()));
764 }
765
767
772 {
773 double advX, advY;
774 CVB_CALL_CAPI_CHECKED (GetModelAdvance (Handle(), advX, advY));
775 return Point2D<double> (advX, advY);
776 }
777
779
784 {
785 CVB_CALL_CAPI_CHECKED (SetModelAdvance (Handle(), advanceVector.X(), advanceVector.Y()));
786 }
787
789
794 {
795 /* Note: the binary compatibility between Area2D and TArea is guaranteed, therefore the cast below is legal. */
796 Area2D featureWindowNative;
797 CVB_CALL_CAPI_CHECKED (GetFeatureWindow (Handle(), reinterpret_cast<CExports::TArea&>(featureWindowNative)));
798 return Round (Rect<double> (featureWindowNative.P0().X(), featureWindowNative.P0().Y(), featureWindowNative.P1().X(), featureWindowNative.P2().Y()));
799 }
800
802
806 void SetFeatureWindow(Rect<int> featureWindow)
807 {
808 /* Note: the binary compatibility between Area2D and TArea is guaranteed, therefore the cast below is legal. */
809 auto featureWindowNative = Area2D(static_cast<Rect<double>>(featureWindow));
810 CVB_CALL_CAPI_CHECKED (SetFeatureWindow (Handle(), reinterpret_cast<CExports::TArea&>(featureWindowNative)));
811 }
812
814
819 {
820 return Image()->CoordinateSystem().Translation();
821 }
822
824
829 {
830 auto cs = Image()->CoordinateSystem();
831 cs.Translation() = origin;
832 Image()->CoordinateSystem() = cs;
833 CVB_CALL_CAPI_CHECKED (TranslateModelOrigin(Handle()));
834 }
835
837
845 {
846 return instances_;
847 }
848
850
858 std::vector<SearchResult> CheckConsistency (const ImageInfo &trainingSetImage, double threshold, double density = 1.0)
859 {
860 CExports::RESULTS hSearchResults;
861 CVB_CALL_CAPI_CHECKED (MTSModelImageCheck (Handle(), trainingSetImage.Handle(), threshold, static_cast<int>(density * 1000.0), nullptr, nullptr, hSearchResults));
862 ReleaseObjectGuard hSearchResHolder (hSearchResults);
863 return Private::SearchResultsToArray(hSearchResults);
864 }
865
867
874 std::vector<SearchResult> CheckConsistency (double threshold, double density = 1.0)
875 {
876 CExports::RESULTS hSearchResults;
877 CVB_CALL_CAPI_CHECKED (MTSModelCheck (Handle(), threshold, static_cast<int>(density * 1000.0), nullptr, nullptr, hSearchResults));
878 ReleaseObjectGuard hSearchResHolder (hSearchResults);
879 return Private::SearchResultsToArray(hSearchResults);
880 }
881
883
893 {
894 int left = INT_MAX, top = INT_MAX, right = INT_MAX, bottom = INT_MAX;
895
896 for (auto&& i : instances_.ReadInfos())
897 {
898 auto loc_x = std::lround(i.Location ().X ());
899 if (loc_x < left)
900 {
901 left = loc_x;
902 }
903 auto loc_y = std::lround(i.Location ().Y ());
904 if (loc_y < top)
905 {
906 top = loc_y;
907 }
908 auto srcimg_w = i.SourceImage ().Image ()->Width ();
909 if (srcimg_w - loc_x < right)
910 {
911 right = srcimg_w - loc_x - 1;
912 }
913 auto srcimg_h = i.SourceImage ().Image ()->Height ();
914 if (srcimg_h - loc_y < bottom)
915 {
916 bottom = srcimg_h - loc_y - 1;
917 }
918 }
919 left = -left;
920 top = -top;
921
922 return Rect<int>(left, top, right, bottom);
923 }
924
926
932 {
933 int left = -FeatureWindow().Left();
934 int top = -FeatureWindow().Top();
935 int right = image.Image()->Width() - FeatureWindow().Right() - 1;
936 int bottom = image.Image()->Height() - FeatureWindow().Bottom() - 1;
937 return Rect<int>(left, top, right, bottom);
938 }
939
941
947 void * Handle() const noexcept
948 {
949 return nativeHandle_;
950 }
951
952private:
953 SharedHandleGuard<TrainingSet> sguardTrSet_;
954 CExports::MTSMODEL nativeHandle_;
956}; /* class ModelInfo */
957
959
966inline bool operator== (const ModelInfo &lhs, const ModelInfo &rhs) noexcept
967{
968 return (lhs.Handle() == rhs.Handle());
969}
970
972
979inline bool operator!= (const ModelInfo &lhs, const ModelInfo &rhs) noexcept
980{
981 return (!(lhs == rhs));
982}
983
985{
986 auto modelIndex = CVB_CALL_CAPI(GetModelFromInstance(nativeHandle_));
987 return ModelInfo(sguardTrSet_, CVB_CALL_CAPI(MTSModelIndex(modelIndex)));
988}
989
991
994{
995public:
996 ModelInfoCollection (const SharedHandleGuard<TrainingSet> &sguard)
997 : shandle_(sguard)
998 {}
999
1000 ModelInfoCollection (const ModelInfoCollection&) = default;
1001 ModelInfoCollection& operator= (const ModelInfoCollection&) = default;
1002
1004
1009 int Count () const
1010 {
1011 return static_cast<int> (CVB_CALL_CAPI (NumMTSModels(shandle_.Handle())));
1012 }
1013
1015
1021 ModelInfo ReadInfo (int index) const
1022 {
1023 return ModelInfo (shandle_, index);
1024 }
1025
1027
1033 {
1034 return Private::ReadInfos (*this);
1035 }
1036
1038
1045 int IndexOf (const ModelInfo &obj) const
1046 {
1047 return Private::IndexOf (*this, obj);
1048 }
1049
1051
1057 bool Contains (const String &name)
1058 {
1059 auto count = Count ();
1060 for (decltype(count) i = 0; i < count; ++i)
1061 {
1062 if (ReadInfo(i).Name () == name)
1063 {
1064 return true;
1065 }
1066 }
1067 return false;
1068 }
1069
1071
1079 void Add (const ImageInfo &image, const String &name, Point2D<double> location, Rect<int> featureWindow)
1080 {
1081 /* Note: the binary compatibility between Area2D and TArea is guaranteed, therefore the cast below is legal. */
1082 auto featureWindowNative = Area2D(static_cast<Rect<double>>(featureWindow));
1083 auto instance = CVB_CALL_CAPI (NewMTSModelTyped(image.Handle(), name.c_str(), location.X(), location.Y(), reinterpret_cast<CExports::TArea&>(featureWindowNative)));
1084 if (instance == nullptr)
1085 {
1086 Utilities::SystemInfo::ThrowLastError ();
1087 }
1088 }
1089
1091
1101 bool Remove (const ModelInfo &model)
1102 {
1103 auto count = Count ();
1104 for (decltype(count) i = 0; i < count; ++i)
1105 {
1106 if (ReadInfo(i) == model)
1107 {
1108 CVB_CALL_CAPI_CHECKED (RemoveMTSModel(model.Handle()));
1109 return true;
1110 }
1111 }
1112 return false;
1113 }
1114
1116
1126 bool RemoveAt (int index)
1127 {
1128 return Remove (ReadInfo (index));
1129 }
1130
1132
1138 {
1139 CVB_CALL_CAPI_CHECKED(SetMTSGlobalAdvance(shandle_.Handle(), true, vec.X(), vec.Y()));
1140 }
1141
1142private:
1143 SharedHandleGuard<TrainingSet> shandle_;
1144}; /* class ModelInfoCollection */
1145
1146
1148
1151{
1152private:
1153 // Internal helper constructor version
1154 TrainingSet (HandleGuard<TrainingSet>&& guard, const String &srcFileName = String())
1155 : shandle_(std::move(guard)),
1156 fileName_ (srcFileName),
1157 instances_ (shandle_), images_ (shandle_), models_ (shandle_)
1158 {
1159 }
1160
1161 // Helper to create an empty training set
1162 static CExports::MTS CreateInternal ()
1163 {
1164 CExports::MTS mts = CVB_CALL_CAPI (CreateMTS(3, 0.6));
1165 if (mts == nullptr)
1166 {
1167 Utilities::SystemInfo::ThrowLastError();
1168 }
1169 return mts;
1170 }
1171
1172 // Helper to load a training set from file
1173 static CExports::MTS LoadInternal (const String & fileName)
1174 {
1175 CExports::MTS mts = nullptr;
1176
1177 CVB_CALL_CAPI_CHECKED (LoadMTSFileTyped (fileName.c_str(), mts));
1178 return mts;
1179 }
1180
1181public:
1183
1187 : TrainingSet(HandleGuard<TrainingSet>(CreateInternal ()))
1188 {
1189 }
1190
1192
1196 TrainingSet(const String & fileName)
1197 : TrainingSet(HandleGuard<TrainingSet>(LoadInternal (fileName)), fileName)
1198 {
1199 }
1200
1202 TrainingSet (TrainingSet&&) noexcept = default;
1203
1205 TrainingSet& operator=(TrainingSet&&) noexcept = default;
1206
1207public:
1209
1214 static std::unique_ptr<TrainingSet> Load (const String & fileName)
1215 {
1216 return std::unique_ptr<TrainingSet>(new TrainingSet(fileName));
1217 }
1218
1220
1226 static std::unique_ptr<TrainingSet> FromBuffer (const void *buffer, size_t size)
1227 {
1228 return Internal::DoBoolCallObjectOut<TrainingSet>([&](void* & restset)
1229 {
1230 /* Note: need to cast-away const of "buffer" parameter to match the C-API, the function does not modify it. */
1231 return CVB_CALL_CAPI(MemoryToMTS (const_cast<void*>(buffer), static_cast<long>(size), restset));
1232 });
1233 }
1234
1236
1241 template <class RANGE>
1242 static typename TypedRange<std::unique_ptr<TrainingSet>, std::uint8_t, RANGE>::type FromBuffer (const RANGE & buffer)
1243 {
1244 auto bufferRange = MakeRangeAdapter<std::uint8_t> (buffer);
1245 return FromBuffer (bufferRange.Data (), bufferRange.Size ());
1246 }
1247
1249
1255 void * Handle() const noexcept
1256 {
1257 return shandle_.Handle();
1258 }
1259
1261
1268 static std::unique_ptr<TrainingSet> FromHandle (HandleGuard<TrainingSet>&& guard)
1269 {
1270 if (!guard.Handle ())
1271 {
1272 throw std::invalid_argument ("invalid training set native handle");
1273 }
1274 return std::unique_ptr<TrainingSet>(new TrainingSet(std::move(guard)));
1275 }
1276
1278
1284 void Save (const String & fileName) const
1285 {
1286 CVB_CALL_CAPI_CHECKED (WriteMTSFileTyped (Handle(), fileName.c_str()));
1287 fileName_ = fileName;
1288 }
1289
1291
1297 {
1298 auto sizeNeeded = CVB_CALL_CAPI (GetMTSSize(Handle()));
1299 std::vector<std::uint8_t> buffer (sizeNeeded);
1300 CVB_CALL_CAPI_CHECKED (MTSToMemory (Handle(), buffer.data(), sizeNeeded));
1301 return buffer;
1302 }
1303
1305
1312 {
1313 return Internal::DoBoolCallObjectOut<TrainingSet>([&](void* & resclas)
1314 {
1315 /* Note: the binary compatibility between Matrix and TMatrix is guaranteed, therefore the cast below is legal. */
1316 return CVB_CALL_CAPI(MTSTransform (Handle(), reinterpret_cast<const CExports::TMatrix&>(transformation), resclas));
1317 });
1318 }
1319
1321
1328 std::vector<SearchResult> CheckConsistency (double threshold, double density = 1.0)
1329 {
1330 CExports::RESULTS hSearchResults;
1331 CVB_CALL_CAPI_CHECKED (MTSConsistencyCheck (Handle (), threshold, static_cast<int>(density * 1000.0), nullptr, nullptr, hSearchResults));
1332 ReleaseObjectGuard hSearchResHolder (hSearchResults);
1333 return Private::SearchResultsToArray(hSearchResults);
1334 }
1335
1337
1344 int ClassCount() const
1345 {
1346 return static_cast<int> (CVB_CALL_CAPI (NumMTSClasses(Handle())));
1347 }
1348
1350
1358 {
1359 return instances_;
1360 }
1361
1363
1367 int InstancesTotal() const
1368 {
1369 return instances_.Count();
1370 }
1371
1373
1381 {
1382 return images_;
1383 }
1384
1386
1394 {
1395 return models_;
1396 }
1397
1399
1404 {
1405 return fileName_;
1406 }
1407
1409
1415 bool IsModified() const
1416 {
1417 CExports::cvbbool_t retval;
1418 CVB_CALL_CAPI_CHECKED (GetMTSModified(Handle(), retval));
1419 return retval != 0;
1420 }
1421
1423
1429 void SetIsModified(bool isModified)
1430 {
1431 CVB_CALL_CAPI_CHECKED (SetMTSModified(Handle(), isModified));
1432 }
1433
1435
1440 {
1441 CExports::cvbdim_t left, top, right, bottom;
1442 CVB_CALL_CAPI_CHECKED (GetMTSExtent (Handle (), left, top, right, bottom));
1443 return Rect<int> (static_cast<int>(left), static_cast<int>(top), static_cast<int>(right), static_cast<int>(bottom));
1444 }
1445
1447
1452 {
1453 Area2D area;
1454 /* Note: the binary compatibility between Area2D and TArea is guaranteed, therefore the cast below is legal. */
1455 CVB_CALL_CAPI_CHECKED (GetMTSLastFeatureWnd(Handle(), reinterpret_cast<CExports::TArea&>(area)));
1456 return Round (Rect<double>(area.P0().X(), area.P0().Y(), area.P1().X(), area.P2().Y()));
1457 }
1458
1460
1465 {
1466 const Char *comment = nullptr;
1467 CVB_CALL_CAPI (MTSCommentTyped(Handle(), comment));
1468 return (comment != nullptr) ? String (comment) : String ();
1469 }
1470
1472
1476 void SetComment(String comment)
1477 {
1478 CVB_CALL_CAPI_CHECKED (SetMTSCommentTyped (Handle(), comment.c_str()));
1479 }
1480
1482
1486 double ExpectationRadius() const
1487 {
1488 double expectationRadius, correlationThreshold;
1489 CVB_CALL_CAPI_CHECKED (GetMTSCorelParams(Handle(), expectationRadius, correlationThreshold));
1490 return expectationRadius;
1491 }
1492
1494
1498 void SetExpectationRadius(double expectationRadius)
1499 {
1500 CVB_CALL_CAPI_CHECKED (SetMTSCorelParams (Handle(), expectationRadius, CorrelationThreshold()));
1501 }
1502
1504
1509 {
1510 double expectationRadius, correlationThreshold;
1511 CVB_CALL_CAPI_CHECKED (GetMTSCorelParams(Handle(), expectationRadius, correlationThreshold));
1512 return correlationThreshold;
1513 }
1514
1516
1520 void SetCorrelationThreshold(double correlationThreshold)
1521 {
1522 CVB_CALL_CAPI_CHECKED (SetMTSCorelParams (Handle(), ExpectationRadius(), correlationThreshold));
1523 }
1524
1525private:
1526 SharedHandleGuard<TrainingSet> shandle_;
1527 mutable String fileName_;
1528 InstanceInfoCollection instances_;
1529 ImageInfoCollection images_;
1530 ModelInfoCollection models_;
1531}; /* class TrainingSet */
1532
1535
1536
1537} /* namespace Minos */
1538CVB_END_INLINE_NS
1539} /* 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:570
void Add(const ImagePlane &plane)
Add a new training set image to the parent training set.
Definition: training_set.hpp:635
ImageInfo ReadInfo(int index) const
Retrieves the indexed image information block.
Definition: training_set.hpp:597
std::vector< ImageInfo > ReadInfos() const
Retrieves all the items stored in the collection.
Definition: training_set.hpp:608
int Count() const
Retrieves the number of elements in the collection.
Definition: training_set.hpp:585
int IndexOf(const ImageInfo &obj) const
Determine the index of an image information object inside this collection.
Definition: training_set.hpp:621
bool Remove(const ImageInfo &image)
Remove a training set image along with its instances from the training set.
Definition: training_set.hpp:655
bool RemoveAt(int index)
Remove a training set image along with its instances from the training set.
Definition: training_set.hpp:680
Image that has been added to a training set.
Definition: training_set.hpp:428
std::vector< SearchResult > CheckConsistency(double threshold, double density=1.0)
Test this image for potentially forgotten instances to be trained.
Definition: training_set.hpp:495
ImageInstanceInfoCollection Instances() const
The instances belonging to this model.
Definition: training_set.hpp:511
void TransformImageAndInstances(Matrix2D matrix)
Transform this image and all instances trained from this image using a 2x2 matrix.
Definition: training_set.hpp:481
std::unique_ptr< class Image > Image() const
Image representation of this object.
Definition: training_set.hpp:465
int Index() const
Index of this object in the parent's collection.
Definition: training_set.hpp:454
void * Handle() const noexcept
Classic API MTSIMAGE handle.
Definition: training_set.hpp:523
Class that maintains the collection of Training Images inside a Minos Training Set.
Definition: training_set.hpp:342
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:365
Class that maintains the collection of Training Images inside a Minos object.
Definition: training_set.hpp:195
int Count() const
Retrieves the number of elements in the collection.
Definition: training_set.hpp:212
std::vector< InstanceInfo > ReadInfos() const
Retrieves all the items stored in the collection.
Definition: training_set.hpp:240
InstanceInfo ReadInfo(int index) const
Retrieves the indexed instance information block.
Definition: training_set.hpp:224
bool Remove(const InstanceInfo &instance)
Remove an instance from the training set.
Definition: training_set.hpp:269
int IndexOf(const InstanceInfo &obj) const
Determine the index of an image information object inside this collection.
Definition: training_set.hpp:253
bool RemoveAt(int index)
Remove an instance from the training set.
Definition: training_set.hpp:294
Class that maintains the collection of Training Images inside a Minos Training Set.
Definition: training_set.hpp:311
Instance information that has been added to a training set.
Definition: training_set.hpp:43
ImageInfo SourceImage() const
Retrieve the correlation between this instance image and the model image.
Definition: training_set.hpp:560
std::unique_ptr< class Image > Image() const
Image representation of this object.
Definition: training_set.hpp:73
int Index() const
Index of this object in the parent's collection.
Definition: training_set.hpp:62
Point2D< double > Location() const
Position in the SourceImage from which this instance has been extracted.
Definition: training_set.hpp:104
ModelInfo Model() const
The Training Set Model into which this instance has been trained.
Definition: training_set.hpp:984
void * Handle() const noexcept
Classic API MTSINSTANCE handle.
Definition: training_set.hpp:126
Class that maintains the collection of Training Images inside a Minos Training Set.
Definition: training_set.hpp:994
std::vector< ModelInfo > ReadInfos() const
Retrieves all the items stored in the collection.
Definition: training_set.hpp:1032
bool Remove(const ModelInfo &model)
Remove a model (and all the instances extracted for that model!) from the training set.
Definition: training_set.hpp:1101
int Count() const
Retrieves the number of elements in the collection.
Definition: training_set.hpp:1009
bool Contains(const String &name)
Check if the collection contains a model with the specified name.
Definition: training_set.hpp:1057
int IndexOf(const ModelInfo &obj) const
Determine the index of an image information object inside this collection.
Definition: training_set.hpp:1045
void SetGlobalAdvanceVector(Point2D< double > vec)
Set an advance vector for all available models.
Definition: training_set.hpp:1137
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:1079
ModelInfo ReadInfo(int index) const
Retrieves the indexed model information block.
Definition: training_set.hpp:1021
bool RemoveAt(int index)
Remove a model (and all the instances extracted for that model!) from the training set.
Definition: training_set.hpp:1126
Model that has been added to a training set.
Definition: training_set.hpp:693
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:874
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:931
Point2D< double > AdvanceVector() const
Get the advance vector associated with this model.
Definition: training_set.hpp:771
Point2D< double > Origin() const
Get the origin of the model in terms of coordinates inside the feature window.
Definition: training_set.hpp:818
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:858
void SetAdvanceVector(Point2D< double > advanceVector)
Set the advance vector associated with this model.
Definition: training_set.hpp:783
std::unique_ptr< class Image > Image() const
Image representation of this object.
Definition: training_set.hpp:730
String Name() const
Get the name of the model.
Definition: training_set.hpp:744
void SetFeatureWindow(Rect< int > featureWindow)
Set the feature window for this model.
Definition: training_set.hpp:806
int Index() const
Index of this object in the parent's collection.
Definition: training_set.hpp:719
void SetOrigin(Point2D< double > origin)
Set the origin of the model in terms of coordinates inside the feature window.
Definition: training_set.hpp:828
Rect< int > GetMaxFeatureWindow() const
Retrieve the maximum values that may be set as feature window for this model.
Definition: training_set.hpp:892
ModelInstanceInfoCollection Instances() const
The instances belonging to this model.
Definition: training_set.hpp:844
void SetName(const String &name)
Set the name of the model.
Definition: training_set.hpp:761
Rect< int > FeatureWindow() const
Get the feature window for this model.
Definition: training_set.hpp:793
void * Handle() const noexcept
Classic API MTSMODEL handle.
Definition: training_set.hpp:947
Class that maintains the collection of Training Images inside a Minos Training Set.
Definition: training_set.hpp:397
A Minos Training Set from which a classifier can be generated.
Definition: training_set.hpp:1151
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:1242
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:1311
ImageInfoCollection Images() const
The images contained in this training set.
Definition: training_set.hpp:1380
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:1328
double CorrelationThreshold() const
Gets the correlation threshold. The correlation value under which Minos proposes generating a new mod...
Definition: training_set.hpp:1508
int ClassCount() const
The number of identifiable classes inside this training set.
Definition: training_set.hpp:1344
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:1226
void SetIsModified(bool isModified)
Set a flag that informs about unsaved modifications to the training set.
Definition: training_set.hpp:1429
TrainingSet(const String &fileName)
Load a saved training set from a file.
Definition: training_set.hpp:1196
static std::unique_ptr< TrainingSet > FromHandle(HandleGuard< TrainingSet > &&guard)
Creates training set from a classic API handle.
Definition: training_set.hpp:1268
TrainingSet()
Create an empty training set.
Definition: training_set.hpp:1186
String Comment() const
Get the comment assigned to the training set at generation time.
Definition: training_set.hpp:1464
ModelInfoCollection Models() const
The models contained in this training set.
Definition: training_set.hpp:1393
std::vector< std::uint8_t > ToBuffer() const
Serializes the training set into a buffer.
Definition: training_set.hpp:1296
InstanceInfoCollection Instances() const
The instances contained in this training set.
Definition: training_set.hpp:1357
void SetExpectationRadius(double expectationRadius)
Set the expectation radius - the radius that is searched for the best occurrence of a sample,...
Definition: training_set.hpp:1498
void Save(const String &fileName) const
Write the training set to a file.
Definition: training_set.hpp:1284
Rect< int > Extent() const
Extent of the classes in the training set relative to the anchor point.
Definition: training_set.hpp:1439
bool IsModified() const
Get a flag that informs about unsaved modifications to the training set.
Definition: training_set.hpp:1415
void SetComment(String comment)
Set the comment assigned to the training set at generation time.
Definition: training_set.hpp:1476
int InstancesTotal() const
Total number of instances currently trained in this training set.
Definition: training_set.hpp:1367
static std::unique_ptr< TrainingSet > Load(const String &fileName)
Load a saved training set from a file.
Definition: training_set.hpp:1214
Rect< int > LastFeatureWindow() const
Feature window of the last model that has been created for the training set.
Definition: training_set.hpp:1451
void SetCorrelationThreshold(double correlationThreshold)
Set the correlation threshold - the correlation value below which Minos will suggest the generation o...
Definition: training_set.hpp:1520
TrainingSet(TrainingSet &&) noexcept=default
Move constructor.
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:1403
void * Handle() const noexcept
Classic API CLF handle.
Definition: training_set.hpp:1255
double ExpectationRadius() const
Get the expectation radius - the radius that is searched for the best occurrence of a sample,...
Definition: training_set.hpp:1486
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:59
Point2D< int > Round(const Point2D< T > &rhs) noexcept
Round to an integer point.
Definition: point_2d.hpp:380