CVB++ 15.0
decl_regression_sil.hpp
1#pragma once
2
3#include "../../_cexports/c_sample_database.h"
4
5#include "../../global.hpp"
6#include "../../image.hpp"
7#include "../sample_image_list.hpp"
8#include "../sample_list_image_data_type.hpp"
9
10#include <memory>
11#include <utility>
12#include <vector>
13
14namespace Cvb
15{
16 CVB_BEGIN_INLINE_NS
17
19 namespace SampleDatabase
20 {
21
23
26 class ImageRegressionLabelInfo
27 {
28 public:
29 ImageRegressionLabelInfo(const SharedReleaseObjectGuard &sguardSil, const std::vector<float> &label)
30 : shandleSil_(sguardSil)
31 , labelInfo_(sguardSil, label)
32 {
33 }
34
35 ImageRegressionLabelInfo(const ImageRegressionLabelInfo &other) = delete;
36 ImageRegressionLabelInfo &operator=(const ImageRegressionLabelInfo &other) = delete;
37 ImageRegressionLabelInfo(ImageRegressionLabelInfo &&other) = delete;
38 ImageRegressionLabelInfo &operator=(ImageRegressionLabelInfo &&other) = delete;
39 virtual ~ImageRegressionLabelInfo() = 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
77 {
78 return labelInfo_.Label();
79 }
80
82
89 void Add(const Image &srcImage, Point2D<int> extractionLocation,
91 {
92 Private::AddSample(shandleSil_.Handle(), Label(), srcImage, extractionLocation, mode);
93 }
94
96
102 void Relabel(const std::vector<float> &label)
103 {
104 Private::Relabel(shandleSil_.Handle(), Index(), label);
105 }
106
107 private:
108 SharedReleaseObjectGuard shandleSil_;
109 Private::ImageLabelInfo<std::vector<float>> labelInfo_;
110 }; /* class ImageRegressionLabelInfo */
111
114
116
119 class ImageRegressionInfoCollection : public LabelInfoCollection<ImageRegressionLabelInfo>
120 {
121 public:
122 explicit ImageRegressionInfoCollection(const SharedReleaseObjectGuard &sguardSil)
123 : LabelInfoCollection<ImageRegressionLabelInfo>(sguardSil)
124 {
125 }
126
127 public:
130
139 void Add(const std::vector<float> &label, const Image &srcImage, Point2D<int> extractionLocation,
141 {
142 Private::AddSample(shandleSil_.Handle(), label, srcImage, extractionLocation, mode);
143 }
144
146
152 bool HasValue(const std::vector<float> &label)
153 {
154 auto count = Count();
155 for (int i = 0; i < count; ++i)
156 {
157 if (ReadInfo(i)->Label() == label)
158 {
159 return true;
160 }
161 }
162 return false;
163 }
164
165 protected:
166 std::unique_ptr<ImageRegressionLabelInfo> GetInfo(int index) const override
167 {
168 auto labelType = CVB_CALL_CAPI(SilGetLabelType(shandleSil_.Handle()));
169 size_t dim = 0;
170 CVB_CALL_CAPI_CHECKED(SilGetFloatVectorLabelDim(labelType, dim));
171 auto label = CVB_CALL_CAPI(SilGetLabel(shandleSil_.Handle(), index));
172 std::vector<float> floatLabel(dim);
173 CVB_CALL_CAPI_CHECKED(SilGetFloatVectorLabel(label, floatLabel.data(), dim));
174 return std::make_unique<ImageRegressionLabelInfo>(shandleSil_, floatLabel);
175 }
176
177 }; /* class ImageRegressionInfoCollection */
178
181
182 // forward declaration
184
186
189 class SampleRegressionImageList : public SampleImageList
190 {
191 private:
192 explicit SampleRegressionImageList(ReleaseObjectGuard &&guardSil, const String &fileName = String())
193 : SampleImageList(std::move(guardSil), fileName)
194 , regressionDimension_(static_cast<int>(Private::GetFloatRegressionDimension(Handle())))
196 {
197 }
198
199 public:
201
207 SampleRegressionImageList(const SampleListImageDataType &imageDataType, int regressionDimension)
208 : SampleRegressionImageList(CreateInternal(imageDataType, regressionDimension))
209 {
210 }
211
213
217 explicit SampleRegressionImageList(const String &fileName)
218 : SampleRegressionImageList(ReleaseObjectGuard(LoadInternal(fileName, CExports::SLT_Float)), fileName)
219 {
220 }
221
222 private:
223 // Helper to create the instance (from data type)
224 static ReleaseObjectGuard CreateInternal(const SampleListImageDataType &imageDataType, int regressionDimension)
225 {
226 if ((regressionDimension <= 0) || (regressionDimension > RegressionDimensionMax))
227 {
228 throw std::out_of_range("regression dimension value must be within <1, 16>");
229 }
230 ReleaseObjectGuard lblType(CVB_CALL_CAPI(SilCreateLabelTypeFloatVector(regressionDimension)));
231 ReleaseObjectGuard sil(CVB_CALL_CAPI(SilCreate(lblType.Handle(), imageDataType.Handle())));
232 return sil;
233 }
234
235 public:
237 static constexpr int RegressionDimensionMax = 16;
238
240
249 const String &fileName = String())
250 {
251 if (!guard.Handle())
252 {
253 throw std::invalid_argument("invalid regression image list native handle");
254 }
255 return std::unique_ptr<SampleRegressionImageList>(new SampleRegressionImageList(std::move(guard), fileName));
256 }
257
259
263 int RegressionDimension() const noexcept
264 {
265 return regressionDimension_;
266 }
267
269
274 {
275 return values_;
276 }
277
279
285 {
286 return TransformData<SampleRegressionImageList>(transformCallback);
287 }
288
290
296 {
297 return TransformLabels<SampleRegressionImageList>(transformCallback);
298 }
299
301
307
309
315 {
316 auto dataHandle = CVB_CALL_CAPI(SilGetData(Handle(), sampleIndex));
317 auto labelRef = CVB_CALL_CAPI(SilGetLabelReference(dataHandle, 0));
318 auto labelIndex = CVB_CALL_CAPI(SilGetLabelIndex(Handle(), labelRef));
319 return Values()->ReadInfo(labelIndex);
320 }
321
322 private:
323 int regressionDimension_;
325 }; /* class SampleRegressionImageList */
326
329
330 } /* namespace SampleDatabase */
331 CVB_END_INLINE_NS
332} /* namespace Cvb */
The Common Vision Blox image.
Definition decl_image.hpp:50
Multi-purpose 2D vector class.
Definition point_2d.hpp:20
bool HasValue(const std::vector< float > &label)
Check if a given value is already present in the values collection.
Definition decl_regression_sil.hpp:152
void Add(const std::vector< float > &label, const Image &srcImage, Point2D< int > extractionLocation, SampleExtractionMode mode=SampleExtractionMode::TopLeftCorner)
Add a sample to a new value. If the class indicated by the label already exists the sample will be ad...
Definition decl_regression_sil.hpp:139
void Relabel(const std::vector< float > &label)
Modify the label of a sample set, but not to one that already exists in the sample list.
Definition decl_regression_sil.hpp:102
ImageCollectionPtr Samples()
Get samples under this label.
Definition decl_regression_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_regression_sil.hpp:89
std::vector< float > Label() const
Get label under which this class is stored.
Definition decl_regression_sil.hpp:76
std::vector< float > LabelType
Type of the labels.
Definition decl_regression_sil.hpp:43
int Index() const
Get the index of this label.
Definition decl_regression_sil.hpp:63
std::unique_ptr< ImageRegressionLabelInfo > ReadInfo(int index) const
Definition sample_list.hpp:115
Classifier type that operates on images.
Definition decl_classification_sil.hpp:213
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
static std::unique_ptr< SampleRegressionImageList > FromHandle(ReleaseObjectGuard &&guard, const String &fileName=String())
Creates regression image list from a classic API handle.
Definition decl_regression_sil.hpp:248
SampleRegressionImageList(const String &fileName)
Load a sample regression image from a file.
Definition decl_regression_sil.hpp:217
static constexpr int RegressionDimensionMax
Maximum regression dimension to be used.
Definition decl_regression_sil.hpp:237
std::unique_ptr< SampleRegressionImageList > Transform(LabelTransformVectorToVector transformCallback) const
Transform this sample image list into a new sample image list by transforming each label inside it.
Definition decl_regression_sil.hpp:295
ImageRegressionInfoCollectionPtr Values()
Get the class collection.
Definition decl_regression_sil.hpp:273
int RegressionDimension() const noexcept
Get the dimension of the regression labels used in this sample image list.
Definition decl_regression_sil.hpp:263
std::unique_ptr< SampleRegressionImageList > Transform(DataTransformImageToImage transformCallback) const
Transform this sample image list into a new sample image list by transforming each sample inside it.
Definition decl_regression_sil.hpp:284
std::unique_ptr< ImageRegressionLabelInfo > GetValueForSample(int sampleIndex)
Retrieve the label information that goes with a given sample.
Definition decl_regression_sil.hpp:314
SampleRegressionImageList(const SampleListImageDataType &imageDataType, int regressionDimension)
Create a sample image list suitable for regression tasks.
Definition decl_regression_sil.hpp:207
T make_shared(T... args)
T move(T... args)
Namespace for the SampleDatabase package.
Definition decl_classification_sil.hpp:20
std::function< std::vector< float >(const std::vector< float > &labelIn)> LabelTransformVectorToVector
Callback for label transformation.
Definition sample_list.hpp:339
std::shared_ptr< ImageRegressionInfoCollection > ImageRegressionInfoCollectionPtr
Convenience shared pointer for ImageRegressionInfoCollection.
Definition decl_regression_sil.hpp:180
std::shared_ptr< ImageCollection > ImageCollectionPtr
Convenience shared pointer for ImageCollection.
Definition sample_image_list.hpp:108
std::shared_ptr< SampleRegressionImageList > SampleRegressionImageListPtr
Convenience shared pointer for SampleRegressionImageList.
Definition decl_regression_sil.hpp:328
std::function< String(const std::vector< float > &labelIn)> LabelTransformVectorToString
Callback for label transformation.
Definition sample_list.hpp:347
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< ImageRegressionLabelInfo > ImageRegressionLabelInfoPtr
Convenience shared pointer for ImageRegressionLabelInfo.
Definition decl_regression_sil.hpp:113
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
std::string String
String for wide characters or unicode characters.
Definition string.hpp:49