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
12namespace Cvb
13{
14 CVB_BEGIN_INLINE_NS
15
16 namespace ShapeFinder2
17 {
18
20
23 {
24 public:
26
29 ClassifierFactory() noexcept = default;
30
32
39 std::vector<int> GradientHistogram(const ImagePlane &plane, Rect<int> aoi,
40 ShapeFinder2::GradientType gradient) const
41 {
42 if ((plane.DataType().BitsPerPixel() != 8) || (!plane.DataType().IsUnsignedInteger()))
44 CvbException::FromCvbResult(ErrorCodes::CVB_INVALIDDATATYPE, "invalid plane datatype"));
45
47 CExports::SFGradHistogram(plane.Parent().Handle(), plane.Plane(), aoi.Left(), aoi.Top(), aoi.Right(),
48 aoi.Bottom(), static_cast<CExports::TSFGradientType>(gradient), retval.data());
49 return std::vector<int>(retval.begin(), retval.end());
50 }
51
53
58 {
60 }
61
63
68 {
69 return rotationRange_;
70 }
71
73
78 {
79 if (value.Min() < RotationRangeMax().Min() || value.Max() > RotationRangeMax().Max())
80 throw std::out_of_range("rotation range must be in [-180.0, 180.0]");
81 rotationRange_ = value;
82 }
83
85
90 {
91 return ValueRange<double>(0.66, 1.50);
92 }
93
95
100 {
101 return scaleRange_;
102 }
103
105
110 {
111 if (value.Min() < ScaleRangeMax().Min() || value.Max() > ScaleRangeMax().Max())
112 throw std::out_of_range("scale range must be in [0.66, 1.50]");
113 scaleRange_ = value;
114 }
115
117
121 int FeatureCount() const noexcept // Function exists twice
122 {
123 return featureCount_;
124 }
125
127
131 void SetFeatureCount(int value)
132 {
133 using namespace std::string_literals;
134 if (value < FeatureCountMin)
135 throw std::out_of_range(("feature count must be >= "s + std::to_string(FeatureCountMin)).c_str());
136 featureCount_ = value;
137 }
138
140
144 int ContrastThreshold() const noexcept
145 {
146 return contrastThreshold_;
147 }
148
150
154 void SetContrastThreshold(int value)
155 {
156 using namespace std::string_literals;
157 if (value < ContrastThresholdMin)
158 throw std::out_of_range(("contrast threshold must be >= "s + std::to_string(FeatureCountMin)).c_str());
159 contrastThreshold_ = value;
160 }
161
163
167 int MaxCoarseLayerScale() const noexcept
168 {
169 return maxCoarseScale_;
170 }
171
173
178 {
179 if (value < -1)
180 throw std::out_of_range("maximum coarse layer scale must be >= -1");
181 maxCoarseScale_ = value;
182 }
183
185
190 {
191 return contrastMode_;
192 }
193
195
200 {
201 contrastMode_ = mode;
202 }
203
205
209 int ProfileSize() const noexcept
210 {
211 return profileSize_;
212 }
213
215
219 void SetProfileSize(int value)
220 {
221 if (value < 1)
222 throw std::out_of_range("profile size must be >= 1");
223 if ((value % 2) == 0)
224 throw std::invalid_argument("profile size must be %2 != 0");
225 profileSize_ = value;
226 }
227
229
233 int ProfileDelta() const noexcept
234 {
235 return profileDelta_;
236 }
237
239
243 void SetProfileDelta(int value)
244 {
245 if (value < 1)
246 throw std::out_of_range("profile delta must be >= 1");
247 profileDelta_ = value;
248 }
249
251
261 const std::vector<Point2D<int>> &dontCarePoints) const
262 {
263 return Learn(plane, position, Angle(), 0.0, teachWindow, dontCarePoints);
264 }
265
267
274 std::unique_ptr<Classifier> Learn(const ImagePlane &plane, Point2D<int> position, Rect<int> teachWindow) const
275 {
276 return Learn(plane, position, Angle(), 0.0, teachWindow);
277 }
278
280
289 std::unique_ptr<Classifier> Learn(const ImagePlane &plane, Point2D<int> position, Angle angleOffset,
290 double scaleFactor, Rect<int> teachWindow) const
291 {
292 CExports::PIXELLIST dontCarePointsNative = nullptr;
293
294 if (plane.Parent().Handle() != nullptr && plane.DataType().HasOverlayBit())
295 {
296 CExports::CreatePixelListFromOverlay(plane.Parent().Handle(), plane.Plane(), 0, 0, plane.Parent().Width() - 1,
297 plane.Parent().Height() - 1, dontCarePointsNative);
298 if (dontCarePointsNative)
299 {
300 auto identity = Matrix2D::Identity();
301 CExports::TransformPixelListMatrix(dontCarePointsNative, *reinterpret_cast<CExports::TMatrix *>(&identity),
302 -position.X(), -position.Y());
303 }
304 }
305
306 return Learn(plane, position, angleOffset, scaleFactor, teachWindow, dontCarePointsNative);
307 }
308
310
321 std::unique_ptr<Classifier> Learn(const ImagePlane &plane, Point2D<int> position, Angle angleOffset,
322 double scaleFactor, Rect<int> teachWindow,
323 const std::vector<Point2D<int>> &dontCarePoints) const
324 {
325 return Learn(plane, position, angleOffset, scaleFactor, teachWindow,
326 Point2DToPixelList(dontCarePoints)->Handle());
327 }
328
329 private:
330 static ValueRange<Angle> RotationRangeDefault() noexcept
331 {
333 }
334
335 static ValueRange<double> ScaleRangeDefault() noexcept
336 {
337 return ValueRange<double>(1.0, 1.0);
338 }
339
340 std::unique_ptr<Classifier> Learn(const ImagePlane &plane, Point2D<int> position, Angle angleOffset,
341 double scaleFactor, Rect<int> teachWindow,
342 CExports::PIXELLIST dontCareList) const
343 {
344 CExports::TSymmetryParams symmetries = {0};
345 symmetries.ContrastMode = (int)contrastMode_;
346 symmetries.A0 = RotationRange().Min().Deg();
347 symmetries.A1 = RotationRange().Max().Deg();
348 symmetries.R0 = ScaleRange().Min();
349 symmetries.R1 = ScaleRange().Max();
350
351 int maxCoarseScale = MaxCoarseLayerScale();
352 if (maxCoarseScale < 0)
353 maxCoarseScale = 16;
354
355 CExports::SF sf = nullptr;
356 if (ProfileSize() > 1)
357 {
358 sf = CExports::CreateSF2ExEx(plane.Parent().Handle(), plane.Plane(), position.X(), position.Y(),
359 angleOffset.Rad(), scaleFactor, teachWindow.Left(), teachWindow.Top(),
360 teachWindow.Right(), teachWindow.Bottom(), ContrastThreshold(), FeatureCount(),
361 maxCoarseScale, symmetries, dontCareList, true, ProfileSize(), ProfileDelta());
362 }
363 else
364 {
365 sf = CExports::CreateSF2Ex(plane.Parent().Handle(), plane.Plane(), position.X(), position.Y(),
366 angleOffset.Rad(), scaleFactor, teachWindow.Left(), teachWindow.Top(),
367 teachWindow.Right(), teachWindow.Bottom(), ContrastThreshold(), FeatureCount(),
368 maxCoarseScale, symmetries, dontCareList);
369 }
370
371 if (sf == nullptr)
372 Utilities::SystemInfo::ThrowLastError();
373 else
374 return Classifier::FromHandle(HandleGuard<Classifier>(sf));
375
376 return nullptr;
377 }
378
379 static std::unique_ptr<Internal::PixelList> Point2DToPixelList(const std::vector<Point2D<int>> &points)
380 {
381 std::vector<Point2D<double>> tmp;
382 for (std::size_t i = 0; i < points.size(); i++)
383 {
384 tmp.push_back(static_cast<Point2D<double>>(points.at(i)));
385 }
386 return Internal::PixelList::FromPoints(tmp);
387 }
388
389 public:
390 static const int FeatureCountMin = 10;
391 static const int ContrastThresholdMin = 1;
392
393 private:
394 static const int FeatureCountDefault = 100;
395 static const int ContrastThresholdDefault = 25;
396 static const int MaxCoarseLayerDefault = 8;
397
398 int featureCount_ = FeatureCountDefault;
399 int contrastThreshold_ = ContrastThresholdDefault;
400 int maxCoarseScale_ = MaxCoarseLayerDefault;
401 int profileSize_ = 1;
402 int profileDelta_ = 1;
403
404 Cvb::ValueRange<Cvb::Angle> rotationRange_ = RotationRangeDefault();
405 Cvb::ValueRange<double> scaleRange_ = ScaleRangeDefault();
407 };
408
409 } // namespace ShapeFinder2
410
412
413 CVB_END_INLINE_NS
414
415} // namespace Cvb
Object for convenient and type - safe handling of angles.
Definition angle.hpp:16
static Angle FromDegrees(double deg, bool trim=false) noexcept
Create an angle in degrees.
Definition angle.hpp:25
ClassifierFactory() noexcept=default
The object that holds methods for generating ShapeFinder classifier factory.
bool HasOverlayBit() const noexcept
Gets whether bit 0 of the pixels of the plane are being used as an overlay indicator bit.
Definition data_type.hpp:384
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
void * Handle() const noexcept
Classic API image handle.
Definition decl_image.hpp:232
Image plane information container.
Definition decl_image_plane.hpp:29
class DataType DataType() const noexcept override
Data type descriptor for this array.
Definition detail_image_plane.hpp:337
int Plane() const noexcept
Plane index in the image, to which this plane refers to.
Definition decl_image_plane.hpp:147
const Image & Parent() const noexcept
Image to which this plane descriptor refers to.
Definition detail_image_plane.hpp:87
static Matrix2D Identity() noexcept
The identity element.
Definition matrix_2d.hpp:23
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
Rectangle object.
Definition rect.hpp:24
ShapeFinder2 classifier factory object.
Definition classifier_factory.hpp:23
void SetFeatureCount(int value)
Minimum number of features the result classifier should have.
Definition classifier_factory.hpp:131
std::unique_ptr< Classifier > Learn(const ImagePlane &plane, Point2D< int > 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:321
int ContrastThreshold() const noexcept
Minimum contrast a feature must have to enter into the classifier.
Definition classifier_factory.hpp:144
std::vector< int > GradientHistogram(const ImagePlane &plane, Rect< int > aoi, ShapeFinder2::GradientType gradient) const
Calculate the gradient histogram.
Definition classifier_factory.hpp:39
ValueRange< double > ScaleRange() const noexcept
Range of scales, that the classifier should be able to cover.
Definition classifier_factory.hpp:99
void SetScaleRange(ValueRange< double > value)
Range of scales, that the classifier should be able to cover.
Definition classifier_factory.hpp:109
void SetContrastThreshold(int value)
Minimum contrast a feature must have to enter into the classifier.
Definition classifier_factory.hpp:154
ValueRange< Angle > RotationRangeMax() const noexcept
The maximum range of rotations (in degrees), that may be set on the learner.
Definition classifier_factory.hpp:57
int ProfileDelta() const noexcept
Distance (in pixels) between adjacent profile points.
Definition classifier_factory.hpp:233
std::unique_ptr< Classifier > Learn(const ImagePlane &plane, Point2D< int > 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:260
std::unique_ptr< Classifier > Learn(const ImagePlane &plane, Point2D< int > position, Rect< int > teachWindow) const
Create a ShapeFinder2 classifier from plane 0 of the input image.
Definition classifier_factory.hpp:274
int MaxCoarseLayerScale() const noexcept
Maximum exponent of the scale factor between the coarse layer and the image.
Definition classifier_factory.hpp:167
int FeatureCount() const noexcept
Minimum number of features the result classifier should have.
Definition classifier_factory.hpp:121
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:89
void SetMaxCoarseLayerScale(int value)
Maximum exponent of the scale factor between the coarse layer and the image.
Definition classifier_factory.hpp:177
ValueRange< Angle > RotationRange() const noexcept
Range of rotations, that the classifier should be able to cover.
Definition classifier_factory.hpp:67
void SetProfileDelta(int value)
Distance (in pixels) between adjacent profile points.
Definition classifier_factory.hpp:243
void SetRotationRange(ValueRange< Angle > value)
Range of rotations, that the classifier should be able to cover.
Definition classifier_factory.hpp:77
void SetContrastMode(Cvb::ShapeFinder2::ContrastMode mode) noexcept
Contrast mode to be used for feature extraction.
Definition classifier_factory.hpp:199
int ProfileSize() const noexcept
Profile size gives the number of profile points to be used for correlation around each feature.
Definition classifier_factory.hpp:209
Cvb::ShapeFinder2::ContrastMode ContrastMode() const noexcept
Contrast mode to be used for feature extraction.
Definition classifier_factory.hpp:189
void SetProfileSize(int value)
Profile size gives the number of profile points to be used for correlation around each feature.
Definition classifier_factory.hpp:219
std::unique_ptr< Classifier > Learn(const ImagePlane &plane, Point2D< int > position, Angle angleOffset, double scaleFactor, Rect< int > teachWindow) const
Create a ShapeFinder2 classifier from the input image.
Definition classifier_factory.hpp:289
static std::unique_ptr< Classifier > FromHandle(HandleGuard< Classifier > &&guard)
Creates a classifier from a classic API handle.
Definition classifier.hpp:46
Container for range definitions.
Definition value_range.hpp:17
T Min() const noexcept
Gets the minimum value.
Definition value_range.hpp:47
T Max() const noexcept
Gets the maximum value.
Definition value_range.hpp:69
const int CVB_INVALIDDATATYPE
Invalid data type.
Definition exception.hpp:77
Namespace for the ShapeFinder2 package.
Definition classifier.hpp:30
GradientType
Type of Gradient used for feature extraction.
Definition shapefinder2.hpp:56
ContrastMode
Normal contrast features.
Definition shapefinder2.hpp:32
@ Normal
Normal contrast features.
Definition shapefinder2.hpp:34
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
Angle Max(Angle a, Angle b) noexcept
Returns the bigger of two angles.
Definition angle.hpp:495
Angle Min(Angle a, Angle b) noexcept
Returns the smaller of two angles.
Definition angle.hpp:512
T rethrow_exception(T... args)
T to_string(T... args)