CVB++ 15.0
decl_classification_sil.hpp
1#pragma once
2
3#include "../../_cexports/c_sample_database.h"
4
5#include "../../global.hpp"
6#include "../../image.hpp"
7#include "../../string.hpp"
8#include "../sample_image_list.hpp"
9#include "../sample_list_image_data_type.hpp"
10
11#include <memory>
12#include <utility>
13
14namespace Cvb
15{
16 CVB_BEGIN_INLINE_NS
17
20 {
21
23
26 class ImageClassificationLabelInfo
27 {
28 public:
29 ImageClassificationLabelInfo(const SharedReleaseObjectGuard &sguardSil, const String &label)
30 : shandleSil_(sguardSil)
31 , labelInfo_(sguardSil, label)
32 {
33 }
34
35 ImageClassificationLabelInfo(const ImageClassificationLabelInfo &other) = delete;
36 ImageClassificationLabelInfo &operator=(const ImageClassificationLabelInfo &other) = delete;
37 ImageClassificationLabelInfo(ImageClassificationLabelInfo &&other) = delete;
38 ImageClassificationLabelInfo &operator=(ImageClassificationLabelInfo &&other) = delete;
39 virtual ~ImageClassificationLabelInfo() = default;
40
41 public:
44
46
51 {
52 return labelInfo_.Samples();
53 }
54
56
63 int Index() const
64 {
65 return 0;
66 }
67
69
76 String Label() const
77 {
78 return labelInfo_.Label();
79 }
80
82
86 bool IsExcluded() const
87 {
88 CExports::cvbbool_t retval = false;
89 auto label = CVB_CALL_CAPI(SilGetLabel(shandleSil_.Handle(), Index()));
90 CVB_CALL_CAPI_CHECKED(SilGetExcludedFlag(label, retval));
91 return retval != 0;
92 }
93
95
99 void SetIsExcluded(bool isExcluded)
100 {
101 auto label = CVB_CALL_CAPI(SilGetLabel(shandleSil_.Handle(), Index()));
102 CVB_CALL_CAPI_CHECKED(SilSetExcludedFlag(label, isExcluded));
103 }
104
106
113 void Add(const Image &srcImage, Point2D<int> extractionLocation,
115 {
116 Private::AddSample(shandleSil_.Handle(), Label(), srcImage, extractionLocation, mode);
117 }
118
120
126 void Relabel(const String &label)
127 {
128 Private::Relabel(shandleSil_.Handle(), Index(), label);
129 }
130
131 private:
132 SharedReleaseObjectGuard shandleSil_;
133 Private::ImageLabelInfo<String> labelInfo_;
134 }; /* class ImageClassificationLabelInfo */
135
138
140
143 class ImageClassificationInfoCollection : public LabelInfoCollection<ImageClassificationLabelInfo>
144 {
145 public:
146 explicit ImageClassificationInfoCollection(const SharedReleaseObjectGuard &sguardSil)
147 : LabelInfoCollection<ImageClassificationLabelInfo>(sguardSil)
148 {
149 }
150
151 public:
154
163 void Add(const String &label, const Image &srcImage, Point2D<int> extractionLocation,
165 {
166 Private::AddSample(shandleSil_.Handle(), label, srcImage, extractionLocation, mode);
167 }
168
170
176 bool HasLabel(const String &label)
177 {
178 auto count = Count();
179 for (int i = 0; i < count; ++i)
180 {
181 if (ReadInfo(i)->Label() == label)
182 {
183 return true;
184 }
185 }
186 return false;
187 }
188
189 protected:
190 std::unique_ptr<ImageClassificationLabelInfo> GetInfo(int index) const override
191 {
192 auto label = CVB_CALL_CAPI(SilGetLabel(shandleSil_.Handle(), index));
193 size_t bufferSize = 0;
194 CVB_CALL_CAPI_CHECKED(SilGetStringLabelTyped(label, static_cast<Char *>(nullptr), bufferSize));
195 std::vector<Char> stringLabel(bufferSize);
196 CVB_CALL_CAPI_CHECKED(SilGetStringLabelTyped(label, stringLabel.data(), bufferSize));
197 return std::make_unique<ImageClassificationLabelInfo>(shandleSil_,
198 String(stringLabel.begin(), stringLabel.end()));
199 }
200 }; /* class ImageClassificationInfoCollection */
201
204
205 // forward declaration
207
209
212 class SampleClassificationImageList : public SampleImageList
213 {
214 private:
215 explicit SampleClassificationImageList(ReleaseObjectGuard &&guardSil, const String &fileName = String())
216 : SampleImageList(std::move(guardSil), fileName)
218 {
219 }
220
221 public:
223
228 : SampleClassificationImageList(CreateInternal(imageDataType))
229 {
230 }
231
233
237 explicit SampleClassificationImageList(const String &fileName)
238 : SampleClassificationImageList(ReleaseObjectGuard(LoadInternal(fileName, CExports::SLT_String)), fileName)
239 {
240 }
241
242 private:
243 // Helper to create the instance (from datatype)
244 static ReleaseObjectGuard CreateInternal(const SampleListImageDataType &imageDataType)
245 {
246 ReleaseObjectGuard lblType(CVB_CALL_CAPI(SilCreateLabelTypeString()));
247 ReleaseObjectGuard sil(CVB_CALL_CAPI(SilCreate(lblType.Handle(), imageDataType.Handle())));
248 return sil;
249 }
250
251 public:
253
262 const String &fileName = String())
263 {
264 if (!guard.Handle())
265 {
266 throw std::invalid_argument("invalid classification image list native handle");
267 }
269 new SampleClassificationImageList(std::move(guard), fileName));
270 }
271
273
279 {
280 CExports::TSIL sil = nullptr;
281
282 CVB_CALL_CAPI_CHECKED(SilImportFromMantoTyped(fileName.c_str(), sil));
283 ReleaseObjectGuard silGuard(sil);
284
285 return SampleClassificationImageList::FromHandle(std::move(silGuard), fileName);
286 }
287
289
294 {
295 return classes_;
296 }
297
299
305 {
306 return TransformData<SampleClassificationImageList>(transformCallback);
307 }
308
310
316 {
317 return TransformLabels<SampleClassificationImageList>(transformCallback);
318 }
319
321
327
329
335 {
336 auto dataHandle = CVB_CALL_CAPI(SilGetData(Handle(), sampleIndex));
337 auto labelRef = CVB_CALL_CAPI(SilGetLabelReference(dataHandle, 0));
338 auto labelIndex = CVB_CALL_CAPI(SilGetLabelIndex(Handle(), labelRef));
339 return Classes()->ReadInfo(labelIndex);
340 }
341
344
350 void GetIndicesForSampleIndex(int sampleIndex, int &clsIdx, int &splIdx) const
351 {
352 clsIdx = -1;
353 splIdx = -1;
354 auto dataHandle = CVB_CALL_CAPI(SilGetData(Handle(), sampleIndex));
355 auto labelRef = CVB_CALL_CAPI(SilGetLabelReference(dataHandle, 0));
356 auto count = CVB_CALL_CAPI(SilGetNumDataReferences(labelRef));
357 clsIdx = static_cast<int>(CVB_CALL_CAPI(SilGetLabelIndex(Handle(), labelRef)));
358 for (decltype(count) i = 0; i < count; ++i)
359 {
360 auto testDataHandle = CVB_CALL_CAPI(SilGetDataByLabel(Handle(), clsIdx, i));
361 if (testDataHandle == dataHandle)
362 {
363 splIdx = static_cast<int>(i);
364 break;
365 }
366 }
367 }
368
369 private:
371 }; /* class SampleClassificationImageList */
372
375
376 } /* namespace SampleDatabase */
377 CVB_END_INLINE_NS
378} /* namespace Cvb */
The Common Vision Blox image.
Definition decl_image.hpp:50
Multi-purpose 2D vector class.
Definition point_2d.hpp:20
bool HasLabel(const String &label)
Check if a given label is already present in the classes collection.
Definition decl_classification_sil.hpp:176
void Add(const String &label, const Image &srcImage, Point2D< int > extractionLocation, SampleExtractionMode mode=SampleExtractionMode::TopLeftCorner)
Add a sample to a new class. If the class indicated by the label already exists the sample will be ad...
Definition decl_classification_sil.hpp:163
void SetIsExcluded(bool isExcluded)
Set the exclusion state of this class.
Definition decl_classification_sil.hpp:99
ImageCollectionPtr Samples()
Get samples under this label.
Definition decl_classification_sil.hpp:50
void Add(const Image &srcImage, Point2D< int > extractionLocation, SampleExtractionMode mode=SampleExtractionMode::TopLeftCorner)
Add a new sample to an existing class.
Definition decl_classification_sil.hpp:113
String Label() const
Get label under which this class is stored.
Definition decl_classification_sil.hpp:76
void Relabel(const String &label)
Modify the label of a sample set, but not to one that already exists in the sample list.
Definition decl_classification_sil.hpp:126
String LabelType
Type of the labels.
Definition decl_classification_sil.hpp:43
int Index() const
Get the index of this label.
Definition decl_classification_sil.hpp:63
bool IsExcluded() const
Get the exclusion state of this class.
Definition decl_classification_sil.hpp:86
std::unique_ptr< ImageClassificationLabelInfo > ReadInfo(int index) const
Definition sample_list.hpp:115
SampleClassificationImageList(const SampleListImageDataType &imageDataType)
Create a sample image list suitable for classification tasks.
Definition decl_classification_sil.hpp:227
SampleClassificationImageList(const String &fileName)
Load a sample classification image from a file.
Definition decl_classification_sil.hpp:237
std::unique_ptr< ImageClassificationLabelInfo > GetClassFromSample(int sampleIndex)
Retrieve the label information that goes with a given sample.
Definition decl_classification_sil.hpp:334
static std::unique_ptr< SampleClassificationImageList > ImportMantoSil(const String &fileName)
Load a Manto2 sample list from disc.
Definition decl_classification_sil.hpp:278
static std::unique_ptr< SampleClassificationImageList > FromHandle(ReleaseObjectGuard &&guard, const String &fileName=String())
Creates classification image list from a classic API handle.
Definition decl_classification_sil.hpp:261
ImageClassificationInfoCollectionPtr Classes()
Get the class collection.
Definition decl_classification_sil.hpp:293
std::unique_ptr< SampleClassificationImageList > Transform(LabelTransformStringToString transformCallback) const
Transform this sample image list into a new sample image list by transforming each label inside it.
Definition decl_classification_sil.hpp:315
void GetIndicesForSampleIndex(int sampleIndex, int &clsIdx, int &splIdx) const
For a given sample index (running from 0 to NumSamples()-1) this method determines the indices of the...
Definition decl_classification_sil.hpp:350
std::unique_ptr< SampleClassificationImageList > Transform(DataTransformImageToImage transformCallback) const
Transform this sample image list into a new sample image list by transforming each sample inside it.
Definition decl_classification_sil.hpp:304
void * Handle() const noexcept
Classic API SIL handle.
Definition sample_list.hpp:411
Descriptor for image data to be digested by an image list.
Definition sample_list_image_data_type.hpp:25
void * Handle() const noexcept
Classic API SIL handle.
Definition sample_list_image_data_type.hpp:105
Classifier type that operates on images.
Definition decl_regression_sil.hpp:190
T make_shared(T... args)
T move(T... args)
Namespace for the SampleDatabase package.
Definition decl_classification_sil.hpp:20
std::shared_ptr< ImageClassificationInfoCollection > ImageClassificationInfoCollectionPtr
Convenience shared pointer for ImageClassificationInfoCollection.
Definition decl_classification_sil.hpp:203
std::function< std::vector< float >(const String &labelIn)> LabelTransformStringToVector
Callback for label transformation.
Definition sample_list.hpp:331
std::shared_ptr< ImageClassificationLabelInfo > ImageClassificationLabelInfoPtr
Convenience shared pointer for ImageClassificationLabelInfo.
Definition decl_classification_sil.hpp:137
std::shared_ptr< ImageCollection > ImageCollectionPtr
Convenience shared pointer for ImageCollection.
Definition sample_image_list.hpp:108
std::function< std::unique_ptr< Image >(const Image &imgIn)> DataTransformImageToImage
Image data transformation callback.
Definition sample_list.hpp:355
SampleExtractionMode
Possible approaches to the sample extraction in the "Add" methods of image list classes.
Definition sample_image_list.hpp:25
@ TopLeftCorner
Position parameter is assumed to refer to the top left corner of the feature window.
Definition sample_image_list.hpp:27
std::shared_ptr< SampleClassificationImageList > SampleClassificationImageListPtr
Convenience shared pointer for SampleClassificationImageList.
Definition decl_classification_sil.hpp:374
std::function< String(const String &labelIn)> LabelTransformStringToString
Callback for label transformation.
Definition sample_list.hpp:323
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