CVB++ 15.0
classifier_factory.hpp
1#pragma once
2
3#include "../angle.hpp"
4#include "../value_range.hpp"
5#include "../point_2d.hpp"
6#include "../_detail/detail_pixel_list.hpp"
7#include "../image.hpp"
8
9#include "shapefinder2.hpp"
10#include "classifier.hpp"
11
12
13namespace Cvb
14{
15CVB_BEGIN_INLINE_NS
16
17
18
19namespace ShapeFinder2
20{
21
22
24
27{
28public:
29
31
34 ClassifierFactory() noexcept = default;
35
36
37
39
46 std::vector<int> GradientHistogram(const ImagePlane & plane, Rect<int> aoi, ShapeFinder2::GradientType gradient) const
47 {
48 if ((plane.DataType().BitsPerPixel() != 8) || (!plane.DataType().IsUnsignedInteger()))
49 std::rethrow_exception(CvbException::FromCvbResult(ErrorCodes::CVB_INVALIDDATATYPE, "invalid plane datatype"));
50
52 CExports::SFGradHistogram(plane.Parent().Handle(), plane.Plane(), aoi.Left(), aoi.Top(), aoi.Right(), aoi.Bottom(), static_cast<CExports::TSFGradientType>(gradient), retval.data());
53 return std::vector<int>(retval.begin(), retval.end());
54 }
55
57
62 {
64 }
65
67
72 {
73 return rotationRange_;
74 }
75
77
82 {
83 if (value.Min() < RotationRangeMax().Min() || value.Max() > RotationRangeMax().Max())
84 throw std::out_of_range("rotation range must be in [-180.0, 180.0]");
85 rotationRange_ = value;
86 }
87
89
94 {
95 return ValueRange<double>(0.66, 1.50);
96 }
97
99
104 {
105 return scaleRange_;
106 }
107
109
114 {
115 if (value.Min() < ScaleRangeMax().Min() || value.Max() > ScaleRangeMax().Max())
116 throw std::out_of_range("scale range must be in [0.66, 1.50]");
117 scaleRange_ = value;
118 }
119
121
125 int FeatureCount() const noexcept // Function exists twice
126 {
127 return featureCount_;
128 }
129
131
135 void SetFeatureCount(int value)
136 {
137 using namespace std::string_literals;
138 if (value < FeatureCountMin)
139 throw std::out_of_range(("feature count must be >= "s + std::to_string(FeatureCountMin)).c_str());
140 featureCount_ = value;
141 }
142
144
148 int ContrastThreshold() const noexcept
149 {
150 return contrastThreshold_;
151 }
152
154
158 void SetContrastThreshold(int value)
159 {
160 using namespace std::string_literals;
161 if (value < ContrastThresholdMin)
162 throw std::out_of_range(("contrast threshold must be >= "s + std::to_string(FeatureCountMin)).c_str());
163 contrastThreshold_ = value;
164 }
165
167
171 int MaxCoarseLayerScale() const noexcept
172 {
173 return maxCoarseScale_;
174 }
175
177
182 {
183 if (value < -1)
184 throw std::out_of_range("maximum coarse layer scale must be >= -1");
185 maxCoarseScale_ = value;
186 }
187
189
194 {
195 return contrastMode_;
196 }
197
199
204 {
205 contrastMode_ = mode;
206 }
207
209
213 int ProfileSize() const noexcept
214 {
215 return profileSize_;
216 }
217
219
223 void SetProfileSize(int value)
224 {
225 if (value < 1)
226 throw std::out_of_range("profile size must be >= 1");
227 if ((value % 2) == 0)
228 throw std::invalid_argument("profile size must be %2 != 0");
229 profileSize_ = value;
230 }
231
233
237 int ProfileDelta() const noexcept
238 {
239 return profileDelta_;
240 }
241
243
247 void SetProfileDelta(int value)
248 {
249 if (value < 1)
250 throw std::out_of_range("profile delta must be >= 1");
251 profileDelta_ = value;
252 }
253
255
263 std::unique_ptr<Classifier> Learn(const ImagePlane & plane, Point2D<double> position, Rect<int> teachWindow, const std::vector<Point2D<int>>& dontCarePoints) const
264 {
265 return Learn(plane, position, Angle(), 0.0, teachWindow, dontCarePoints);
266 }
267
269
276 std::unique_ptr<Classifier> Learn(const ImagePlane & plane, Point2D<double> position, Rect<int> teachWindow) const
277 {
278 return Learn(plane, position, Angle(), 0.0, teachWindow);
279 }
280
282
291 std::unique_ptr<Classifier> Learn(const ImagePlane & plane, Point2D<double> position, Angle angleOffset, double scaleFactor, Rect<int> teachWindow) const
292 {
293 CExports::PIXELLIST dontCarePointsNative = nullptr;
294
295 if (plane.Parent().Handle() != nullptr && plane.DataType().HasOverlayBit())
296 {
297 CExports::CreatePixelListFromOverlay(plane.Parent().Handle(), plane.Plane(), 0, 0, plane.Parent().Width() - 1, plane.Parent().Height() - 1, dontCarePointsNative);
298 if (dontCarePointsNative)
299 {
300 auto identity = Matrix2D::Identity();
301 CExports::TransformPixelListMatrix(dontCarePointsNative, *reinterpret_cast<CExports::TMatrix*>(&identity), -position.X(), -position.Y());
302 }
303
304 }
305
306 return Learn(plane, position, angleOffset, scaleFactor, teachWindow, dontCarePointsNative);
307 }
308
310
320 std::unique_ptr<Classifier> Learn(const ImagePlane & plane, Point2D<double> position, Angle angleOffset, double scaleFactor, Rect<int> teachWindow, const std::vector<Point2D<int>>& dontCarePoints) const
321 {
322 return Learn(plane, position, angleOffset, scaleFactor, teachWindow, Point2DToPixelList(dontCarePoints)->Handle());
323 }
324
325private:
326
327
328
329 static ValueRange<Angle> RotationRangeDefault() noexcept
330 {
332 }
333
334 static ValueRange<double> ScaleRangeDefault() noexcept
335 {
336 return ValueRange<double>(1.0, 1.0);
337 }
338
339 std::unique_ptr<Classifier> Learn(const ImagePlane & plane, Point2D<double> position, Angle angleOffset, double scaleFactor, Rect<int> teachWindow, CExports::PIXELLIST dontCareList ) const
340 {
341 CExports::TSymmetryParams symmetries = { 0 };
342 symmetries.ContrastMode = (int)contrastMode_;
343 symmetries.A0 = RotationRange().Min().Deg();
344 symmetries.A1 = RotationRange().Max().Deg();
345 symmetries.R0 = ScaleRange().Min();
346 symmetries.R1 = ScaleRange().Max();
347
348 int maxCoarseScale = MaxCoarseLayerScale();
349 if (maxCoarseScale < 0)
350 maxCoarseScale = 16;
351
352 CExports::SF sf = nullptr;
353 if (ProfileSize() > 1)
354 {
355 sf = CExports::CreateSF2ExEx(plane.Parent().Handle(), plane.Plane(), (CExports::cvbdim_t)position.X(), (CExports::cvbdim_t)position.Y(), angleOffset.Rad(), scaleFactor
356 , teachWindow.Left(), teachWindow.Top(), teachWindow.Right(), teachWindow.Bottom(), ContrastThreshold(), FeatureCount()
357 , maxCoarseScale, symmetries, dontCareList, true, ProfileSize(), ProfileDelta());
358 }
359 else
360 {
361 sf = CExports::CreateSF2Ex(plane.Parent().Handle(), plane.Plane(), (CExports::cvbdim_t)position.X(), (CExports::cvbdim_t)position.Y(), angleOffset.Rad(), scaleFactor
362 , teachWindow.Left(), teachWindow.Top(), teachWindow.Right(), teachWindow.Bottom(), ContrastThreshold(), FeatureCount()
363 , maxCoarseScale, symmetries, dontCareList);
364 }
365
366 if (sf == nullptr)
367 Utilities::SystemInfo::ThrowLastError();
368 else
369 return Classifier::FromHandle(HandleGuard<Classifier>(sf));
370
371 return nullptr;
372 }
373
374
375 static std::unique_ptr<Internal::PixelList> Point2DToPixelList(const std::vector<Point2D<int>>& points)
376 {
378 for (std::size_t i = 0; i < points.size(); i++)
379 {
380 tmp.push_back(static_cast<Point2D<double>>(points.at(i)));
381 }
382 return Internal::PixelList::FromPoints (tmp);
383 }
384
385
386public:
387 static const int FeatureCountMin = 10;
388 static const int ContrastThresholdMin = 1;
389
390private:
391
392 static const int FeatureCountDefault = 100;
393 static const int ContrastThresholdDefault = 25;
394 static const int MaxCoarseLayerDefault = 8;
395
396 int featureCount_ = FeatureCountDefault;
397 int contrastThreshold_ = ContrastThresholdDefault;
398 int maxCoarseScale_ = MaxCoarseLayerDefault;
399 int profileSize_ = 1;
400 int profileDelta_ = 1;
401
402 Cvb::ValueRange<Cvb::Angle> rotationRange_ = RotationRangeDefault();
403 Cvb::ValueRange<double> scaleRange_ = ScaleRangeDefault();
405
406};
407
408}
409
410using ShapeFinder2::ClassifierFactory;
411
412CVB_END_INLINE_NS
413
414}
415
416
Object for convenient and type - safe handling of angles.
Definition: angle.hpp:19
static Angle FromDegrees(double deg, bool trim=false) noexcept
Create an angle in degrees.
Definition: angle.hpp:30
Image plane information container.
Definition: decl_image_plane.hpp:33
static Matrix2D Identity() noexcept
The identity element.
Definition: matrix_2d.hpp:24
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
Rectangle object.
Definition: rect.hpp:26
ShapeFinder2 classifier factory object.
Definition: classifier_factory.hpp:27
void SetFeatureCount(int value)
Minimum number of features the result classifier should have.
Definition: classifier_factory.hpp:135
std::unique_ptr< Classifier > Learn(const ImagePlane &plane, Point2D< double > position, Rect< int > teachWindow) const
Create a ShapeFinder2 classifier from plane 0 of the input image.
Definition: classifier_factory.hpp:276
int ContrastThreshold() const noexcept
Minimum contrast a feature must have to enter into the classifier.
Definition: classifier_factory.hpp:148
std::vector< int > GradientHistogram(const ImagePlane &plane, Rect< int > aoi, ShapeFinder2::GradientType gradient) const
Calculate the gradient histogram.
Definition: classifier_factory.hpp:46
ValueRange< double > ScaleRange() const noexcept
Range of scales, that the classifier should be able to cover.
Definition: classifier_factory.hpp:103
void SetScaleRange(ValueRange< double > value)
Range of scales, that the classifier should be able to cover.
Definition: classifier_factory.hpp:113
void SetContrastThreshold(int value)
Minimum contrast a feature must have to enter into the classifier.
Definition: classifier_factory.hpp:158
ValueRange< Angle > RotationRangeMax() const noexcept
The maximum range of rotations (in degrees), that may be set on the learner.
Definition: classifier_factory.hpp:61
int ProfileDelta() const noexcept
Distance (in pixels) between adjacent profile points.
Definition: classifier_factory.hpp:237
std::unique_ptr< Classifier > Learn(const ImagePlane &plane, Point2D< double > position, Angle angleOffset, double scaleFactor, Rect< int > teachWindow) const
Create a ShapeFinder2 classifier from the input image.
Definition: classifier_factory.hpp:291
int MaxCoarseLayerScale() const noexcept
Maximum exponent of the scale factor between the coarse layer and the image.
Definition: classifier_factory.hpp:171
std::unique_ptr< Classifier > Learn(const ImagePlane &plane, Point2D< double > position, Rect< int > teachWindow, const std::vector< Point2D< int > > &dontCarePoints) const
Creates a ShapeFinder2 classifier from plane 0 of the input image.
Definition: classifier_factory.hpp:263
int FeatureCount() const noexcept
Minimum number of features the result classifier should have.
Definition: classifier_factory.hpp:125
ClassifierFactory() noexcept=default
The object that holds methods for generating ShapeFinder classifier factory.
ValueRange< double > ScaleRangeMax() const noexcept
The maximum range of scales, that may be set on the learner.
Definition: classifier_factory.hpp:93
void SetMaxCoarseLayerScale(int value)
Maximum exponent of the scale factor between the coarse layer and the image.
Definition: classifier_factory.hpp:181
ValueRange< Angle > RotationRange() const noexcept
Range of rotations, that the classifier should be able to cover.
Definition: classifier_factory.hpp:71
void SetProfileDelta(int value)
Distance (in pixels) between adjacent profile points.
Definition: classifier_factory.hpp:247
void SetRotationRange(ValueRange< Angle > value)
Range of rotations, that the classifier should be able to cover.
Definition: classifier_factory.hpp:81
void SetContrastMode(Cvb::ShapeFinder2::ContrastMode mode) noexcept
Contrast mode to be used for feature extraction.
Definition: classifier_factory.hpp:203
int ProfileSize() const noexcept
Profile size gives the number of profile points to be used for correlation around each feature.
Definition: classifier_factory.hpp:213
Cvb::ShapeFinder2::ContrastMode ContrastMode() const noexcept
Contrast mode to be used for feature extraction.
Definition: classifier_factory.hpp:193
void SetProfileSize(int value)
Profile size gives the number of profile points to be used for correlation around each feature.
Definition: classifier_factory.hpp:223
std::unique_ptr< Classifier > Learn(const ImagePlane &plane, Point2D< double > position, Angle angleOffset, double scaleFactor, Rect< int > teachWindow, const std::vector< Point2D< int > > &dontCarePoints) const
Create a ShapeFinder2 classifier from the input image.
Definition: classifier_factory.hpp:320
static std::unique_ptr< Classifier > FromHandle(HandleGuard< Classifier > &&guard)
Creates a classifier from a classic API handle.
Definition: classifier.hpp:49
Container for range definitions.
Definition: value_range.hpp:17
T Min() const noexcept
Gets the minimum value.
Definition: value_range.hpp:50
T Max() const noexcept
Gets the maximum value.
Definition: value_range.hpp:72
const int CVB_INVALIDDATATYPE
Invalid data type.
Definition: exception.hpp:78
GradientType
Type of Gradient used for feature extraction.
Definition: shapefinder2.hpp:59
ContrastMode
Normal contrast features.
Definition: shapefinder2.hpp:37
@ Normal
Normal contrast features.
Root namespace for the Image Manager interface.
Definition: c_barcode.h:24
Angle Max(Angle a, Angle b) noexcept
Returns the bigger of two angles.
Definition: angle.hpp:504
Angle Min(Angle a, Angle b) noexcept
Returns the smaller of two angles.
Definition: angle.hpp:521