CVB++ 15.0
sample_image_list.hpp
1#pragma once
2
3#include "../_cexports/c_sample_database.h"
4
5#include "../global.hpp"
6#include "../image.hpp"
7#include "../size_2d.hpp"
8#include "../point_2d.hpp"
9#include "../data_type.hpp"
10#include "sample_list.hpp"
11#include "fringes.hpp"
12
13#include <vector>
14
15namespace Cvb
16{
17 CVB_BEGIN_INLINE_NS
18
20 namespace SampleDatabase
21 {
22
31
32 namespace Private
33 {
34 // Set of helpers to add sample to a class
35 inline ReleaseObjectGuard CreateNativeImageData(CExports::TSIL hSil, const Image &srcImage,
36 Point2D<int> extractionLocation)
37 {
38 // check compatibility
39 auto datatype = CVB_CALL_CAPI(SilGetDataType(hSil));
40 CExports::cvbdim_t width = 0, height = 0, dimension = 0;
41 CExports::cvbdatatype_t type = 0;
42 CVB_CALL_CAPI_CHECKED(SilGetImageDataGeometry(datatype, width, height, dimension, type));
43 if (srcImage.PlanesCount() != static_cast<int>(dimension) || !srcImage.PlaneDataTypesIdentical()
44 || srcImage.Planes()[0].DataType() != DataType::FromNativeDescriptor(type)
45 || srcImage.Width() < static_cast<int>(width) || srcImage.Height() < static_cast<int>(height))
46 {
47 throw std::invalid_argument("the input image is not compatible with this sample list");
48 }
49
50 ReleaseObjectGuard data(CVB_CALL_CAPI(
51 SilCreateImageData(srcImage.Handle(), extractionLocation.X(), extractionLocation.Y(), width, height)));
52 return data;
53 }
54
55 inline ReleaseObjectGuard CreateNativeLabel(const String &label)
56 {
57 ReleaseObjectGuard lbl(CVB_CALL_CAPI(SilCreateStringLabelTyped(label.c_str())));
58 return lbl;
59 }
60
61 inline ReleaseObjectGuard CreateNativeLabel(const std::vector<float> &label)
62 {
63 ReleaseObjectGuard lbl(CVB_CALL_CAPI(SilCreateVectorLabel(label.data(), label.size())));
64 return lbl;
65 }
66
67 template <typename TLabel>
68 void AddSample(CExports::TSIL hSil, const TLabel &label, const Image &srcImage, Point2D<int> extractionLocation,
70 {
71 ValidateLabel(hSil, label);
72
74 {
75 auto datatype = CVB_CALL_CAPI(SilGetDataType(hSil));
76 CExports::cvbdim_t left = 0, top = 0, width = 0, height = 0, originX = 0, originY = 0;
77 CVB_CALL_CAPI_CHECKED(SilGetFeatureWindow(datatype, left, top, width, height, originX, originY));
78 Point2D<int> ftrwinOrigin(static_cast<int>(originX), static_cast<int>(originY));
79
80 extractionLocation -= ftrwinOrigin;
81 }
82
83 ReleaseObjectGuard data(CreateNativeImageData(hSil, srcImage, extractionLocation));
84 ReleaseObjectGuard lbl(CreateNativeLabel(label));
85 CVB_CALL_CAPI_CHECKED(SilAddItem(hSil, data.Handle(), lbl.Handle()));
86 }
87 } /* namespace Private */
88
90
92 class ImageCollection : public SampleCollection<std::shared_ptr<Image>>
93 {
94 public:
95 explicit ImageCollection(const SharedReleaseObjectGuard &sguardSil)
96 : SampleCollection<std::shared_ptr<Image>>(sguardSil)
97 {
98 }
99
100 protected:
101 std::shared_ptr<Image> GetData(CExports::TSILDATA dataHandle) const override
102 {
103 return GetImage(dataHandle);
104 }
105 }; /* class ImageCollection */
106
109
110 namespace Private
111 {
112
113 template <class TLabel>
114 class ImageLabelInfo
115 {
116 public:
117 ImageLabelInfo(const SharedReleaseObjectGuard &sguardSil, const TLabel &label)
118 : label_(label)
119 , samples_(std::make_shared<ImageCollection>(sguardSil))
120 {
121 }
122
123 ImageLabelInfo(const ImageLabelInfo &other) = delete;
124 ImageLabelInfo &operator=(const ImageLabelInfo &other) = delete;
125 ImageLabelInfo(ImageLabelInfo &&other) = delete;
126 ImageLabelInfo &operator=(ImageLabelInfo &&other) = delete;
127 virtual ~ImageLabelInfo() = default;
128
129 public:
130 using LabelType = TLabel;
131
132 ImageCollectionPtr Samples()
133 {
134 return samples_;
135 }
136
137 TLabel Label() const
138 {
139 return label_;
140 }
141
142 private:
143 TLabel label_;
144 ImageCollectionPtr samples_;
145 }; /* class ImageLabelInfo */
146
147 } /* namespace Private */
148
150
152 class SampleImageList : public SampleList
153 {
154 protected:
155 explicit SampleImageList(ReleaseObjectGuard &&guardSil, const String &fileName = String())
156 : SampleList(std::move(guardSil), fileName)
157 {
158 }
159
160 public:
162
166 class Fringes Fringes() const
167 {
168 auto datatype = CVB_CALL_CAPI(SilGetDataType(Handle()));
169
170 CExports::cvbdim_t left = 0, top = 0, right = 0, bottom = 0;
171 CVB_CALL_CAPI_CHECKED(SilGetFringes(datatype, left, top, right, bottom));
172
173 return SampleDatabase::Fringes(static_cast<int>(left), static_cast<int>(top), static_cast<int>(right),
174 static_cast<int>(bottom));
175 }
176
178
183 {
184 auto datatype = CVB_CALL_CAPI(SilGetDataType(Handle()));
185
186 CExports::cvbdim_t left = 0, top = 0, width = 0, height = 0, originX = 0, originY = 0;
187 CVB_CALL_CAPI_CHECKED(SilGetFeatureWindow(datatype, left, top, width, height, originX, originY));
188
189 return Size2D<int>(static_cast<int>(width), static_cast<int>(height));
190 }
191
193
198 {
199 auto datatype = CVB_CALL_CAPI(SilGetDataType(Handle()));
200
201 CExports::cvbdim_t left = 0, top = 0, width = 0, height = 0, originX = 0, originY = 0;
202 CVB_CALL_CAPI_CHECKED(SilGetFeatureWindow(datatype, left, top, width, height, originX, originY));
203
204 return Point2D<int>(static_cast<int>(left), static_cast<int>(top));
205 }
206
209
214 {
215 auto datatype = CVB_CALL_CAPI(SilGetDataType(Handle()));
216
217 CExports::cvbdim_t left = 0, top = 0, width = 0, height = 0, originX = 0, originY = 0;
218 CVB_CALL_CAPI_CHECKED(SilGetFeatureWindow(datatype, left, top, width, height, originX, originY));
219
220 return Point2D<int>(static_cast<int>(originX), static_cast<int>(originY));
221 }
222
224
229 {
230 auto datatype = CVB_CALL_CAPI(SilGetDataType(Handle()));
231
232 CExports::cvbdim_t width = 0, height = 0, dimension = 0;
233 CExports::cvbdatatype_t type = 0;
234 CVB_CALL_CAPI_CHECKED(SilGetImageDataGeometry(datatype, width, height, dimension, type));
235
236 return Size2D<int>(static_cast<int>(width), static_cast<int>(height));
237 }
238
240
244 int ImageDimension() const
245 {
246 auto datatype = CVB_CALL_CAPI(SilGetDataType(Handle()));
247
248 CExports::cvbdim_t width = 0, height = 0, dimension = 0;
249 CExports::cvbdatatype_t type = 0;
250 CVB_CALL_CAPI_CHECKED(SilGetImageDataGeometry(datatype, width, height, dimension, type));
251
252 return static_cast<int>(dimension);
253 }
254
256
261 {
262 auto datatype = CVB_CALL_CAPI(SilGetDataType(Handle()));
263
264 CExports::cvbdim_t width = 0, height = 0, dimension = 0;
265 CExports::cvbdatatype_t type = 0;
266 CVB_CALL_CAPI_CHECKED(SilGetImageDataGeometry(datatype, width, height, dimension, type));
267
269 }
270
272
284 {
285 auto datatype = CVB_CALL_CAPI(SilGetDataType(Handle()));
286
287 CVB_CALL_CAPI_CHECKED(SilSetFeatureWindow(datatype, location.X(), location.Y(), size.Width(), size.Height(),
288 origin.X(), origin.Y()));
289 }
290
293
298 bool IsCompatible(const Image &img) const
299 {
300 if (img.PlanesCount() != ImageDimension())
301 {
302 return false;
303 }
304 if (!img.PlaneDataTypesIdentical())
305 {
306 return false;
307 }
308 if (img.Planes()[0].DataType() != ImageDataType())
309 {
310 return false;
311 }
312 auto imageSize = ImageSize();
313 if (img.Width() < imageSize.Width())
314 {
315 return false;
316 }
317 if (img.Height() < imageSize.Height())
318 {
319 return false;
320 }
321 return true;
322 }
323
325
332 {
333 return Internal::DoBoolCallObjectOut<Image>([&](void *&resimg) {
334 auto dataHandle = CVB_CALL_CAPI(SilGetData(Handle(), sampleIndex));
335 CVB_CALL_CAPI_CHECKED(SilGetImageData(dataHandle, resimg));
336 return CVB_CALL_CAPI(ShareObject(resimg));
337 });
338 }
339 }; /* class SampleImageList */
340
343
344 } /* namespace SampleDatabase */
345 CVB_END_INLINE_NS
346} /* namespace Cvb */
Data type description for an image plane.
Definition data_type.hpp:23
static DataType FromNativeDescriptor(int dataTypeDescriptor) noexcept
Construct a data type descriptor from one of the native library's descriptor values.
Definition data_type.hpp:31
The Common Vision Blox image.
Definition decl_image.hpp:45
int Width() const noexcept
Width of the image in pixels.
Definition decl_image.hpp:304
int Height() const noexcept
Height of the image in pixels.
Definition decl_image.hpp:294
int PlanesCount() const noexcept
Get the number of planes for this image.
Definition decl_image.hpp:242
std::vector< ImagePlane > Planes() const noexcept
Access all planes for this image.
Definition detail_image.hpp:65
bool PlaneDataTypesIdentical() const noexcept
Check if all planes have the same data type.
Definition decl_image.hpp:252
void * Handle() const noexcept
Classic API image handle.
Definition decl_image.hpp:232
Multi-purpose 2D vector class.
Definition point_2d.hpp:20
T X() const noexcept
Gets the x-component of the point.
Definition point_2d.hpp:84
T Y() const noexcept
Gets the y-component of the point.
Definition point_2d.hpp:104
Object describing the fringes that are available around images in a sample image list.
Definition fringes.hpp:23
int ImageDimension() const
Dimension (# of planes) of the images that can be added to the image list.
Definition sample_image_list.hpp:244
void SetFeatureWindow(Point2D< int > location, Size2D< int > size, Point2D< int > origin)
Redefine the feature window of the sample image list.
Definition sample_image_list.hpp:283
Size2D< int > FeatureWindowSize() const
Size of the feature window.
Definition sample_image_list.hpp:182
bool IsCompatible(const Image &img) const
Check whether a CVB image is compatible with this image list, i.e. if it is generally possible to add...
Definition sample_image_list.hpp:298
Point2D< int > FeatureWindowOrigin() const
Location of the feature window origin in pixel coordinates measured from the left top corner of the f...
Definition sample_image_list.hpp:213
class Fringes Fringes() const
Get fringes (= size difference between the images and the feature window) of the sample images.
Definition sample_image_list.hpp:166
DataType ImageDataType() const
Data type of the images that can be added to the image list (all planes must have same data type).
Definition sample_image_list.hpp:260
std::shared_ptr< Image > GetSampleImage(int sampleIndex) const
Retrieve an image by its overall sample index (ranging from 0 to NumSamples()-1).
Definition sample_image_list.hpp:331
Size2D< int > ImageSize() const
Size of the images (in pixels) that can be added to the image list.
Definition sample_image_list.hpp:228
Point2D< int > FeatureWindowLocation() const
Location of the feature window (position of left top corner inside the image).
Definition sample_image_list.hpp:197
void * Handle() const noexcept
Classic API SIL handle.
Definition sample_list.hpp:411
Stores a pair of numbers that represents the width and the height of a subject, typically a rectangle...
Definition size_2d.hpp:20
T Height() const noexcept
Gets the vertical component of the size.
Definition size_2d.hpp:77
T Width() const noexcept
Gets the horizontal component of the size.
Definition size_2d.hpp:57
cvbbool_t ShareObject(OBJ Object)
T make_shared(T... args)
T move(T... args)
Namespace for the SampleDatabase package.
Definition decl_classification_sil.hpp:20
std::shared_ptr< SampleImageList > SampleImageListPtr
Convenience shared pointer for SampleImageList.
Definition sample_image_list.hpp:342
std::shared_ptr< ImageCollection > ImageCollectionPtr
Convenience shared pointer for ImageCollection.
Definition sample_image_list.hpp:108
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
@ OriginPosition
Position parameter is assumed to refer to the origin of the feature window.
Definition sample_image_list.hpp:29
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