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
15
16
17namespace Cvb
18{
19CVB_BEGIN_INLINE_NS
20
22namespace SampleDatabase
23{
24
25
28{
33};
34
35namespace Private
36{
37// Set of helpers to add sample to a class
38inline ReleaseObjectGuard CreateNativeImageData(CExports::TSIL hSil, const Image &srcImage, Point2D<int> extractionLocation)
39{
40 // check compatibility
41 auto datatype = CVB_CALL_CAPI(SilGetDataType(hSil));
42 CExports::cvbdim_t width = 0, height = 0, dimension = 0;
43 CExports::cvbdatatype_t type = 0;
44 CVB_CALL_CAPI_CHECKED(SilGetImageDataGeometry(datatype, width, height, dimension, type));
45 if (srcImage.PlanesCount() != static_cast<int>(dimension)
46 || !srcImage.PlaneDataTypesIdentical()
47 || srcImage.Planes()[0].DataType() != DataType::FromNativeDescriptor (type)
48 || srcImage.Width() < static_cast<int>(width)
49 || srcImage.Height() < static_cast<int>(height))
50 {
51 throw std::invalid_argument ("the input image is not compatible with this sample list");
52 }
53
54 ReleaseObjectGuard data (CVB_CALL_CAPI(SilCreateImageData(srcImage.Handle(), extractionLocation.X(), extractionLocation.Y(), width, height)));
55 return data;
56}
57
58inline ReleaseObjectGuard CreateNativeLabel (const String &label)
59{
60 ReleaseObjectGuard lbl (CVB_CALL_CAPI(SilCreateStringLabelTyped(label.c_str())));
61 return lbl;
62}
63
64inline ReleaseObjectGuard CreateNativeLabel (const std::vector<float> &label)
65{
66 ReleaseObjectGuard lbl (CVB_CALL_CAPI(SilCreateVectorLabel(label.data(), label.size())));
67 return lbl;
68}
69
70template<typename TLabel>
71void AddSample(CExports::TSIL hSil, const TLabel &label, const Image &srcImage, Point2D<int> extractionLocation, SampleExtractionMode mode = SampleExtractionMode::TopLeftCorner)
72{
73 ValidateLabel (hSil, label);
74
76 {
77 auto datatype = CVB_CALL_CAPI(SilGetDataType(hSil));
78 CExports::cvbdim_t left = 0, top = 0, width = 0, height = 0, originX = 0, originY = 0;
79 CVB_CALL_CAPI_CHECKED(SilGetFeatureWindow(datatype, left, top, width, height, originX, originY));
80 Point2D<int> ftrwinOrigin (static_cast<int>(originX), static_cast<int>(originY));
81
82 extractionLocation -= ftrwinOrigin;
83 }
84
85 ReleaseObjectGuard data (CreateNativeImageData (hSil, srcImage, extractionLocation));
86 ReleaseObjectGuard lbl (CreateNativeLabel (label));
87 CVB_CALL_CAPI_CHECKED(SilAddItem(hSil, data.Handle(), lbl.Handle()));
88}
89} /* namespace Private */
90
91
92
94
96class ImageCollection : public SampleCollection<std::shared_ptr<Image>>
97{
98public:
99 explicit ImageCollection (const SharedReleaseObjectGuard &sguardSil)
101 {}
102
103protected:
104 std::shared_ptr<Image> GetData (CExports::TSILDATA dataHandle) const override
105 {
106 return GetImage (dataHandle);
107 }
108}; /* class ImageCollection */
109
112
113namespace Private
114{
115
116template <class TLabel>
117class ImageLabelInfo
118{
119public:
120 ImageLabelInfo (const SharedReleaseObjectGuard &sguardSil, const TLabel &label)
121 : label_(label), samples_(std::make_shared<ImageCollection>(sguardSil))
122 {}
123
124 ImageLabelInfo (const ImageLabelInfo& other) = delete;
125 ImageLabelInfo& operator= (const ImageLabelInfo& other) = delete;
126 ImageLabelInfo(ImageLabelInfo&& other) = delete;
127 ImageLabelInfo& operator= (ImageLabelInfo&& other) = delete;
128 virtual ~ImageLabelInfo() = default;
129
130public:
131 using LabelType = TLabel;
132
133 ImageCollectionPtr Samples()
134 {
135 return samples_;
136 }
137
138 TLabel Label() const
139 {
140 return label_;
141 }
142
143private:
144 TLabel label_;
145 ImageCollectionPtr samples_;
146}; /* class ImageLabelInfo */
147
148} /* namespace Private */
149
151
154{
155protected:
156 explicit SampleImageList (ReleaseObjectGuard&& guardSil, const String &fileName = String())
157 : SampleList (std::move(guardSil), fileName)
158 {}
159
160public:
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), static_cast<int>(bottom));
174 }
175
177
182 {
183 auto datatype = CVB_CALL_CAPI(SilGetDataType(Handle()));
184
185 CExports::cvbdim_t left = 0, top = 0, width = 0, height = 0, originX = 0, originY = 0;
186 CVB_CALL_CAPI_CHECKED(SilGetFeatureWindow(datatype, left, top, width, height, originX, originY));
187
188 return Size2D<int>(static_cast<int>(width), static_cast<int>(height));
189 }
190
192
197 {
198 auto datatype = CVB_CALL_CAPI(SilGetDataType(Handle()));
199
200 CExports::cvbdim_t left = 0, top = 0, width = 0, height = 0, originX = 0, originY = 0;
201 CVB_CALL_CAPI_CHECKED(SilGetFeatureWindow(datatype, left, top, width, height, originX, originY));
202
203 return Point2D<int>(static_cast<int>(left), static_cast<int>(top));
204 }
205
207
212 {
213 auto datatype = CVB_CALL_CAPI(SilGetDataType(Handle()));
214
215 CExports::cvbdim_t left = 0, top = 0, width = 0, height = 0, originX = 0, originY = 0;
216 CVB_CALL_CAPI_CHECKED(SilGetFeatureWindow(datatype, left, top, width, height, originX, originY));
217
218 return Point2D<int>(static_cast<int>(originX), static_cast<int>(originY));
219 }
220
222
227 {
228 auto datatype = CVB_CALL_CAPI(SilGetDataType(Handle()));
229
230 CExports::cvbdim_t width = 0, height = 0, dimension = 0;
231 CExports::cvbdatatype_t type = 0;
232 CVB_CALL_CAPI_CHECKED(SilGetImageDataGeometry(datatype, width, height, dimension, type));
233
234 return Size2D<int>(static_cast<int>(width), static_cast<int>(height));
235 }
236
238
242 int ImageDimension() const
243 {
244 auto datatype = CVB_CALL_CAPI(SilGetDataType(Handle()));
245
246 CExports::cvbdim_t width = 0, height = 0, dimension = 0;
247 CExports::cvbdatatype_t type = 0;
248 CVB_CALL_CAPI_CHECKED(SilGetImageDataGeometry(datatype, width, height, dimension, type));
249
250 return static_cast<int>(dimension);
251 }
252
254
259 {
260 auto datatype = CVB_CALL_CAPI(SilGetDataType(Handle()));
261
262 CExports::cvbdim_t width = 0, height = 0, dimension = 0;
263 CExports::cvbdatatype_t type = 0;
264 CVB_CALL_CAPI_CHECKED(SilGetImageDataGeometry(datatype, width, height, dimension, type));
265
266 return DataType::FromNativeDescriptor (type);
267 }
268
270
280 {
281 auto datatype = CVB_CALL_CAPI(SilGetDataType(Handle()));
282
283 CVB_CALL_CAPI_CHECKED(SilSetFeatureWindow(datatype, location.X(), location.Y(), size.Width(), size.Height(), origin.X(), origin.Y()));
284 }
285
287
292 bool IsCompatible (const Image &img) const
293 {
294 if (img.PlanesCount () != ImageDimension())
295 {
296 return false;
297 }
298 if (!img.PlaneDataTypesIdentical())
299 {
300 return false;
301 }
302 if (img.Planes()[0].DataType() != ImageDataType())
303 {
304 return false;
305 }
306 auto imageSize = ImageSize ();
307 if (img.Width() < imageSize.Width())
308 {
309 return false;
310 }
311 if (img.Height() < imageSize.Height())
312 {
313 return false;
314 }
315 return true;
316 }
317
319
326 {
327 return Internal::DoBoolCallObjectOut<Image>([&](void* & resimg)
328 {
329 auto dataHandle = CVB_CALL_CAPI (SilGetData(Handle(), sampleIndex));
330 CVB_CALL_CAPI_CHECKED(SilGetImageData (dataHandle, resimg));
331 return CVB_CALL_CAPI(ShareObject (resimg));
332 });
333 }
334}; /* class SampleImageList */
335
336
337
338
341
342
343} /* namespace SampleDatabase */
344CVB_END_INLINE_NS
345} /* namespace Cvb */
Data type description for an image plane.
Definition: data_type.hpp:28
static DataType FromNativeDescriptor(int dataTypeDescriptor) noexcept
Construct a data type descriptor from one of the native library's descriptor values.
Definition: data_type.hpp:37
The Common Vision Blox image.
Definition: decl_image.hpp:45
int Width() const noexcept
Width of the image in pixels.
Definition: decl_image.hpp:288
int Height() const noexcept
Height of the image in pixels.
Definition: decl_image.hpp:281
int PlanesCount() const noexcept
Get the number of planes for this image.
Definition: decl_image.hpp:233
std::vector< ImagePlane > Planes() const noexcept
Access all planes for this image.
Definition: detail_image.hpp:72
bool PlaneDataTypesIdentical() const noexcept
Check if all planes have the same data type.
Definition: decl_image.hpp:240
void * Handle() const noexcept
Classic API image handle.
Definition: decl_image.hpp:226
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
Object describing the fringes that are available around images in a sample image list.
Definition: fringes.hpp:23
Collection of image samples.
Definition: sample_image_list.hpp:97
Collection of data samples.
Definition: sample_list.hpp:243
Classifier type that operates on images.
Definition: sample_image_list.hpp:154
int ImageDimension() const
Dimension (# of planes) of the images that can be added to the image list.
Definition: sample_image_list.hpp:242
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:279
Size2D< int > FeatureWindowSize() const
Size of the feature window.
Definition: sample_image_list.hpp:181
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:292
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:211
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:258
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:325
Size2D< int > ImageSize() const
Size of the images (in pixels) that can be added to the image list.
Definition: sample_image_list.hpp:226
Point2D< int > FeatureWindowLocation() const
Location of the feature window (position of left top corner inside the image).
Definition: sample_image_list.hpp:196
Base class for sample lists.
Definition: sample_list.hpp:364
void * Handle() const noexcept
Classic API SIL handle.
Definition: sample_list.hpp:413
T Height() const noexcept
Gets the vertical component of the size.
Definition: size_2d.hpp:79
T Width() const noexcept
Gets the horizontal component of the size.
Definition: size_2d.hpp:59
SampleExtractionMode
Possible approaches to the sample extraction in the "Add" methods of image list classes.
Definition: sample_image_list.hpp:28
@ TopLeftCorner
Position parameter is assumed to refer to the top left corner of the feature window.
@ OriginPosition
Position parameter is assumed to refer to the origin of the feature window.
Root namespace for the Image Manager interface.
Definition: c_barcode.h:24