CVB++ 14.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, height, dimension;
43 CExports::cvbdatatype_t type;
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, top, width, height, originX, originY;
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
92
94class ImageCollection : public SampleCollection<std::shared_ptr<Image>>
95{
96public:
97 ImageCollection (const SharedReleaseObjectGuard &sguardSil)
99 {}
100
101 ImageCollection (const ImageCollection&) = default;
102 ImageCollection& operator= (const ImageCollection&) = default;
103
104 virtual ~ImageCollection ()
105 {}
106
107protected:
108 std::shared_ptr<Image> GetData (CExports::TSILDATA dataHandle) const override
109 {
110 return GetImage (dataHandle);
111 }
112}; /* class ImageCollection */
113
114namespace Private
115{
116
117template <class TLabel>
118class ImageLabelInfo
119{
120public:
121 ImageLabelInfo (const SharedReleaseObjectGuard &sguardSil, const TLabel &label)
122 : label_(label), samples_(sguardSil)
123 {}
124
125 ImageLabelInfo (const ImageLabelInfo&) = default;
126 ImageLabelInfo& operator= (const ImageLabelInfo&) = default;
127
128 ~ImageLabelInfo () {};
129
130public:
131 using LabelType = TLabel;
132
133 ImageCollection Samples() const
134 {
135 return samples_;
136 }
137
138 TLabel Label() const
139 {
140 return label_;
141 }
142
143private:
144 TLabel label_;
145 ImageCollection samples_;
146}; /* class ImageLabelInfo */
147
148} /* namespace Private */
149
151
154{
155protected:
156 SampleImageList (ReleaseObjectGuard&& guardSil, const String &fileName = String())
157 : SampleList (std::move(guardSil), fileName)
158 {}
159
160 SampleImageList (SampleImageList&&) noexcept = default;
161
162 SampleImageList& operator=(SampleImageList&&) noexcept = default;
163
164 virtual ~SampleImageList () = default;
165
166public:
168
172 class Fringes Fringes() const
173 {
174 auto datatype = CVB_CALL_CAPI(SilGetDataType(Handle()));
175
176 CExports::cvbdim_t left, top, right, bottom;
177 CVB_CALL_CAPI_CHECKED(SilGetFringes(datatype, left, top, right, bottom));
178
179 return SampleDatabase::Fringes(static_cast<int>(left), static_cast<int>(top), static_cast<int>(right), static_cast<int>(bottom));
180 }
181
183
188 {
189 auto datatype = CVB_CALL_CAPI(SilGetDataType(Handle()));
190
191 CExports::cvbdim_t left, top, width, height, originX, originY;
192 CVB_CALL_CAPI_CHECKED(SilGetFeatureWindow(datatype, left, top, width, height, originX, originY));
193
194 return Size2D<int>(static_cast<int>(width), static_cast<int>(height));
195 }
196
198
203 {
204 auto datatype = CVB_CALL_CAPI(SilGetDataType(Handle()));
205
206 CExports::cvbdim_t left, top, width, height, originX, originY;
207 CVB_CALL_CAPI_CHECKED(SilGetFeatureWindow(datatype, left, top, width, height, originX, originY));
208
209 return Point2D<int>(static_cast<int>(left), static_cast<int>(top));
210 }
211
213
218 {
219 auto datatype = CVB_CALL_CAPI(SilGetDataType(Handle()));
220
221 CExports::cvbdim_t left, top, width, height, originX, originY;
222 CVB_CALL_CAPI_CHECKED(SilGetFeatureWindow(datatype, left, top, width, height, originX, originY));
223
224 return Point2D<int>(static_cast<int>(originX), static_cast<int>(originY));
225 }
226
228
233 {
234 auto datatype = CVB_CALL_CAPI(SilGetDataType(Handle()));
235
236 CExports::cvbdim_t width, height, dimension;
237 CExports::cvbdatatype_t type;
238 CVB_CALL_CAPI_CHECKED(SilGetImageDataGeometry(datatype, width, height, dimension, type));
239
240 return Size2D<int>(static_cast<int>(width), static_cast<int>(height));
241 }
242
244
248 int ImageDimension() const
249 {
250 auto datatype = CVB_CALL_CAPI(SilGetDataType(Handle()));
251
252 CExports::cvbdim_t width, height, dimension;
253 CExports::cvbdatatype_t type;
254 CVB_CALL_CAPI_CHECKED(SilGetImageDataGeometry(datatype, width, height, dimension, type));
255
256 return static_cast<int>(dimension);
257 }
258
260
265 {
266 auto datatype = CVB_CALL_CAPI(SilGetDataType(Handle()));
267
268 CExports::cvbdim_t width, height, dimension;
269 CExports::cvbdatatype_t type;
270 CVB_CALL_CAPI_CHECKED(SilGetImageDataGeometry(datatype, width, height, dimension, type));
271
272 return DataType::FromNativeDescriptor (type);
273 }
274
276
286 {
287 auto datatype = CVB_CALL_CAPI(SilGetDataType(Handle()));
288
289 CVB_CALL_CAPI_CHECKED(SilSetFeatureWindow(datatype, location.X(), location.Y(), size.Width(), size.Height(), origin.X(), origin.Y()));
290 }
291
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 {
335 auto dataHandle = CVB_CALL_CAPI (SilGetData(Handle(), sampleIndex));
336 CVB_CALL_CAPI_CHECKED(SilGetImageData (dataHandle, resimg));
337 return CVB_CALL_CAPI(ShareObject (resimg));
338 });
339 }
340}; /* class SampleImageList */
341
344
345
346} /* namespace SampleDatabase */
347CVB_END_INLINE_NS
348} /* 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:285
int Height() const noexcept
Height of the image in pixels.
Definition: decl_image.hpp:278
int PlanesCount() const noexcept
Get the number of planes for this image.
Definition: decl_image.hpp:230
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:237
void * Handle() const noexcept
Classic API image handle.
Definition: decl_image.hpp:223
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:95
Collection of data samples.
Definition: sample_list.hpp:240
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:248
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:285
Size2D< int > FeatureWindowSize() const
Size of the feature window.
Definition: sample_image_list.hpp:187
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:217
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:264
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:232
Point2D< int > FeatureWindowLocation() const
Location of the feature window (position of left top corner inside the image).
Definition: sample_image_list.hpp:202
Base class for sample lists.
Definition: sample_list.hpp:360
void * Handle() const noexcept
Classic API SIL handle.
Definition: sample_list.hpp:403
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