CVB++ 15.1
Loading...
Searching...
No Matches
calibration_line_scan.hpp
1#pragma once
2
3#if defined _WIN32
4
5# include "../_cexports/c_foundation.h"
6# include "cvb/foundation/transform_2d.hpp"
7# include "cvb/image_plane.hpp"
8# include "cvb/point_2d.hpp"
9# include "cvb/value_range.hpp"
10
11namespace Cvb
12{
13 CVB_BEGIN_INLINE_NS
14
15 namespace Foundation
16 {
18
28 {
30
34 enum class ScanDirection
35 {
37
40 X = CExports::CVFScanDirection::CVFSD_X,
42
45 Y = CExports::CVFScanDirection::CVFSD_Y
46 };
47
56
59
61 class LineScanCalibrationConfiguration final
62 {
63 public:
64 LineScanCalibrationConfiguration() noexcept
65 : maxIterations_(50)
66 , polyDegree_(3)
67 , tolerance_(1e-6)
68 , scanDirection_(ScanDirection::Y)
69 , preDefinedPixelSizeMode_(PreDefinedPixelSizeMode::DoNotUse)
70 , pixelSize_(1)
71 {
72 }
73
75
87 LineScanCalibrationConfiguration(std::uint32_t maxIterations, std::uint32_t polyDegree, double tolerance,
88 ScanDirection scanDirection, PreDefinedPixelSizeMode preDefinedPixelSizeMode,
89 double pixelSize) noexcept
90 : maxIterations_(maxIterations)
91 , polyDegree_(polyDegree)
92 , tolerance_(tolerance)
93 , scanDirection_(scanDirection)
94 , preDefinedPixelSizeMode_(preDefinedPixelSizeMode)
95 , pixelSize_(pixelSize)
96 {
97 }
98
99 ~LineScanCalibrationConfiguration() noexcept = default;
102 LineScanCalibrationConfiguration &operator=(const LineScanCalibrationConfiguration &) noexcept = default;
104
106
113 std::uint32_t MaxIterations() const noexcept
114 {
115 return maxIterations_;
116 }
117
119
128 void SetMaxIterations(std::uint32_t maxIterations) noexcept
129 {
130 maxIterations_ = maxIterations;
131 }
132
134
142 std::uint32_t PolyDegree() const noexcept
143 {
144 return polyDegree_;
145 }
146
148
156 void SetPolyDegree(std::uint32_t polyDegree) noexcept
157 {
158 polyDegree_ = polyDegree;
159 }
160
162
168 double Tolerance() const noexcept
169 {
170 return tolerance_;
171 }
172
174
181 void SetTolerance(double tolerance) noexcept
182 {
183 tolerance_ = tolerance;
184 }
185
187
192 {
193 return scanDirection_;
194 }
195
197
203 {
204 scanDirection_ = scanDirection;
205 }
206
208
215 {
216 return preDefinedPixelSizeMode_;
217 }
218
220
230 {
231 preDefinedPixelSizeMode_ = preDefinedPixelSize;
232 }
233
235
244 double PixelSize() const noexcept
245 {
246 return pixelSize_;
247 }
248
250
259 void SetPixelSize(double pixelSize) noexcept
260 {
261 pixelSize_ = pixelSize;
262 }
263
264 private:
265 std::uint32_t maxIterations_;
266 std::uint32_t polyDegree_;
267 double tolerance_;
270 double pixelSize_;
271 };
272
274
297 const ImagePlane &imagePlane, const Area2D &aoi, Foundation::Transform2D::CalibrationPatternContrast contrast,
298 int minContrast, const ValueRange<double> &pointSizeRange, const ScanDirection &scanDirection)
299 {
301 Internal::DoResCall([&]() {
302 return CVB_CALL_CAPI(CVFGet2PointsForCalibrationOfMovement(
303 imagePlane.Parent().Handle(), static_cast<CExports::cvbdim_t>(imagePlane.Plane()), reinterpret_cast<const CExports::TArea&>(aoi),
304 static_cast<CExports::TCalibrationPatternContrast>(contrast), minContrast,
305 pointSizeRange.Min(), pointSizeRange.Max(), static_cast<CExports::CVFScanDirection>(scanDirection),
306 reinterpret_cast<CExports::CVIPointD&>(points.first),
307 reinterpret_cast<CExports::CVIPointD&>(points.second));
308 }));
309 return points;
310 }
311
312 class LineScanCalibrator;
315
317
326 class EdgeDetectionResult final
327 {
329 CreateLineScanCalibration(const Point2D<double> &calibrationPoint1, const Point2D<double> &calibrationPoint2,
330 double referenceDistanceCalibrationPoints,
331 const EdgeDetectionResult &edgeDetectionResult, double referenceWidthStripes,
332 const LineScanCalibrationConfiguration &configuration);
333
334 public:
335 EdgeDetectionResult()
336 : EdgeDetectionResult(std::vector<int>(), std::vector<double>(), 0, 0)
337 {
338 }
339
340 EdgeDetectionResult(std::vector<int> &&scanLineIndices, std::vector<double> &&edgeIndices, int numProfiles,
341 int numEdgesPerProfile)
342 : scanLineIndices_(std::move(scanLineIndices))
343 , edgeIndices_(std::move(edgeIndices))
344 , numProfiles_(numProfiles)
345 , numEdgesPerProfile_(numEdgesPerProfile)
346 {
347 }
348
350
359 {
360 return scanLineIndices_;
361 }
362
364
398 {
399 return edgeIndices_;
400 }
401
403
410 int NumProfiles() const noexcept
411 {
412 return numProfiles_;
413 }
414
416
420 int NumEdgesPerProfile() const noexcept
421 {
422 return numEdgesPerProfile_;
423 }
424
425 private:
426 std::vector<int> scanLineIndices_;
427 std::vector<double> edgeIndices_;
428 int numProfiles_;
429 int numEdgesPerProfile_;
430 };
431
433
453 inline EdgeDetectionResult DetectEdgesOfStripeTarget(const Image &imageStripes, const Rect<int> &aoi,
454 int numStripes, const ScanDirection &scanDirection,
455 double threshold)
456 {
457 std::vector<int> scanLineIndices(imageStripes.Width());
458 std::vector<double> edgeIndices(static_cast<size_t>(imageStripes.Width())
459 * (static_cast<size_t>(numStripes) + 1));
460 size_t numProfiles = 0;
461 size_t numEdgesPerProfile = 0;
462 Internal::DoResCall([&]() {
463 return CVB_CALL_CAPI(
464 CVFDetectEdgesOfStripeTarget(imageStripes.Handle(), static_cast<CExports::cvbdim_t>(aoi.Left()),
465 static_cast<CExports::cvbdim_t>(aoi.Top()), static_cast<CExports::cvbdim_t>(aoi.Right()),
466 static_cast<CExports::cvbdim_t>(aoi.Bottom()), numStripes,
467 static_cast<CExports::CVFScanDirection>(scanDirection), threshold,
468 scanLineIndices.data(), edgeIndices.data(), numProfiles, numEdgesPerProfile);
469 }));
470 return {std::move(scanLineIndices), std::move(edgeIndices), static_cast<int>(numProfiles),
471 static_cast<int>(numEdgesPerProfile)};
472 }
473
475
476 class LineScanCalibrator final
477 {
479 CreateLineScanCalibration(const Point2D<double> &calibrationPoint1, const Point2D<double> &calibrationPoint2,
480 double referenceDistanceCalibrationPoints,
481 const EdgeDetectionResult &edgeDetectionResult, double referenceWidthStripes,
482 const LineScanCalibrationConfiguration &configuration);
483
484 private:
485 struct PrivateTag
486 {
487 };
488
489 public:
490 LineScanCalibrator(const NonLinearTransformationPtr &transformation, double pixelSize, double meanError,
491 double standardDeviation, PrivateTag)
492 : transformation_(transformation)
493 , pixelSize_(pixelSize)
494 , meanError_(meanError)
495 , standardDeviation_(standardDeviation)
496 {
497 }
498
500
537 {
538 return transformation_;
539 }
540
542
549 double PixelSize() const noexcept
550 {
551 return pixelSize_;
552 }
553
555
561 double MeanError() const noexcept
562 {
563 return meanError_;
564 }
565
567
571 double StandardDeviation() const noexcept
572 {
573 return standardDeviation_;
574 }
575
576 private:
577 NonLinearTransformationPtr transformation_;
578 double pixelSize_;
579 double meanError_;
580 double standardDeviation_;
581 };
582
584
621 CreateLineScanCalibration(const Point2D<double> &calibrationPoint1, const Point2D<double> &calibrationPoint2,
622 double referenceDistanceCalibrationPoints,
623 const EdgeDetectionResult &edgeDetectionResult, double referenceWidthStripes,
624 const LineScanCalibrationConfiguration &configuration)
625 {
626 void *transformation = nullptr;
627 double pixelSize = 0;
628 double meanError = 0;
629 double standardDeviation = 0;
630 Internal::DoResCall([&]() {
631 return CVB_CALL_CAPI(CVFCreateLineScanCalibration(*reinterpret_cast<const CExports::CVIPointD*>(&calibrationPoint1),
632 *reinterpret_cast<const CExports::CVIPointD*>(&calibrationPoint2),
633 referenceDistanceCalibrationPoints,
634 const_cast<int*>(reinterpret_cast<const int*>(edgeDetectionResult.scanLineIndices_.data())),
635 const_cast<double*>(reinterpret_cast<const double*>(edgeDetectionResult.edgeIndices_.data())),
636 edgeDetectionResult.numProfiles_,
637 edgeDetectionResult.numEdgesPerProfile_,
638 referenceWidthStripes,
639 *reinterpret_cast<const CExports::CVFLineScanCalibrationConfiguration*>(&configuration),
640 transformation, pixelSize, meanError, standardDeviation);
641 }));
642 return std::make_unique<LineScanCalibrator>(
643 NonLinearTransformation::FromHandle(HandleGuard<NonLinearTransformation>(transformation)), pixelSize,
644 meanError, standardDeviation, LineScanCalibrator::PrivateTag{});
645 }
646
647 } // namespace CalibrationLineScan
648
658
659 } // namespace Foundation
660 CVB_END_INLINE_NS
661} // namespace Cvb
662
663#endif
Structure that represents an area of interest in the image.
Definition area_2d.hpp:21
Result of the function DetectEdgesOfStripeTarget.
Definition calibration_line_scan.hpp:327
int NumProfiles() const noexcept
Gets the number of scan lines where edges are correctly detected.
Definition calibration_line_scan.hpp:410
int NumEdgesPerProfile() const noexcept
Gets the number of edges per profile (scan line).
Definition calibration_line_scan.hpp:420
std::vector< double > EdgeIndices() const
Gets the indices of edges for each scan line.
Definition calibration_line_scan.hpp:397
std::vector< int > ScanLineIndices() const
Gets the indices of the scan lines.
Definition calibration_line_scan.hpp:358
friend std::unique_ptr< LineScanCalibrator > CreateLineScanCalibration(const Point2D< double > &calibrationPoint1, const Point2D< double > &calibrationPoint2, double referenceDistanceCalibrationPoints, const EdgeDetectionResult &edgeDetectionResult, double referenceWidthStripes, const LineScanCalibrationConfiguration &configuration)
Calibrates linescan cameras.
Definition calibration_line_scan.hpp:621
A set of parameters, which is used to configure linescan calibration calculated with function CreateL...
Definition calibration_line_scan.hpp:62
void SetPolyDegree(std::uint32_t polyDegree) noexcept
Sets a positive (or zero) integer value that defines the polynomial to be used.
Definition calibration_line_scan.hpp:156
LineScanCalibrationConfiguration(std::uint32_t maxIterations, std::uint32_t polyDegree, double tolerance, ScanDirection scanDirection, PreDefinedPixelSizeMode preDefinedPixelSizeMode, double pixelSize) noexcept
Constructor for linescan calibration configuration.
Definition calibration_line_scan.hpp:87
std::uint32_t MaxIterations() const noexcept
Gets the number of maximal iterations for all nonlinear solvers in the calibration algorithm.
Definition calibration_line_scan.hpp:113
void SetScanDirection(Foundation::CalibrationLineScan::ScanDirection scanDirection) noexcept
Sets the scanning direction of camera.
Definition calibration_line_scan.hpp:202
double Tolerance() const noexcept
Gets the value for early stopping criteria.
Definition calibration_line_scan.hpp:168
ScanDirection ScanDirection() const noexcept
Gets the scanning direction of camera.
Definition calibration_line_scan.hpp:191
void SetTolerance(double tolerance) noexcept
Sets the value for early stopping criteria.
Definition calibration_line_scan.hpp:181
void SetMaxIterations(std::uint32_t maxIterations) noexcept
Sets number of maximal iterations for all nonlinear solvers in the calibration algorithm.
Definition calibration_line_scan.hpp:128
void SetPreDefinedPixelSizeMode(Foundation::CalibrationLineScan::PreDefinedPixelSizeMode preDefinedPixelSize) noexcept
Sets the decision if predefined pixel size needs to be used.
Definition calibration_line_scan.hpp:228
double PixelSize() const noexcept
Gets the pixel size of transformed image.
Definition calibration_line_scan.hpp:244
void SetPixelSize(double pixelSize) noexcept
Sets the pixel size of transformed image.
Definition calibration_line_scan.hpp:259
std::uint32_t PolyDegree() const noexcept
Gets a positive (or zero) integer value that defines the polynomial to be used.
Definition calibration_line_scan.hpp:142
PreDefinedPixelSizeMode PreDefinedPixelSizeMode() const noexcept
Gets the decision if the calibrator uses predefined pixel size.
Definition calibration_line_scan.hpp:214
Result of the linescan calibration executed by the CreateLineScanCalibration function.
Definition calibration_line_scan.hpp:477
double PixelSize() const noexcept
Gets the pixel size after transformation.
Definition calibration_line_scan.hpp:549
double MeanError() const noexcept
Gets the mean difference between stripe width in the transformed image and reference.
Definition calibration_line_scan.hpp:561
NonLinearTransformationPtr Transformation() const noexcept
Gets the transformation.
Definition calibration_line_scan.hpp:536
friend std::unique_ptr< LineScanCalibrator > CreateLineScanCalibration(const Point2D< double > &calibrationPoint1, const Point2D< double > &calibrationPoint2, double referenceDistanceCalibrationPoints, const EdgeDetectionResult &edgeDetectionResult, double referenceWidthStripes, const LineScanCalibrationConfiguration &configuration)
Calibrates linescan cameras.
Definition calibration_line_scan.hpp:621
double StandardDeviation() const noexcept
Gets the standard deviation of the mean error.
Definition calibration_line_scan.hpp:571
static std::unique_ptr< NonLinearTransformation > FromHandle(HandleGuard< NonLinearTransformation > &&guard)
Creates transformation from a classic API handle.
Definition transform_2d.hpp:716
The Common Vision Blox image.
Definition decl_image.hpp:50
int Width() const noexcept
Width of the image in pixels.
Definition decl_image.hpp:309
void * Handle() const noexcept
Classic API image handle.
Definition decl_image.hpp:237
Image plane information container.
Definition decl_image_plane.hpp:31
int Plane() const noexcept
Plane index in the image, to which this plane refers to.
Definition decl_image_plane.hpp:149
const Image & Parent() const noexcept
Image to which this plane descriptor refers to.
Definition detail_image_plane.hpp:87
Multi-purpose 2D vector class.
Definition point_2d.hpp:20
Rectangle object.
Definition rect.hpp:24
T Bottom() const noexcept
Gets bottom row of the rectangle (still inside the rectangle).
Definition rect.hpp:144
T Top() const noexcept
Gets first row of the rectangle.
Definition rect.hpp:104
T Right() const noexcept
Gets rightmost column of the rectangle (still inside the rectangle).
Definition rect.hpp:124
T Left() const noexcept
Gets first column of the rectangle.
Definition rect.hpp:84
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
cvbres_t CVFGet2PointsForCalibrationOfMovement(IMG Image, cvbdim_t Index, const TArea &Aoi, TCalibrationPatternContrast Contrast, cvbval_t MinContrast, double MinPointSize, double MaxPointSize, CVFScanDirection ScanDirection, CVIPointD &FirstPoint, CVIPointD &SecondPoint)
cvbres_t CVFCreateLineScanCalibration(CVIPointD CalibrationPoint1, CVIPointD CalibrationPoint2, double ReferenceDistanceCalibrationPoints, int *ScanLineIndices, double *EdgeIndices, size_t NumScanLines, size_t NumEdgesPerScanLine, double ReferenceWidthStripes, const CVFLineScanCalibrationConfiguration &Configuration, NLTRANSFORMATION &Transformation, double &PixelSize, double &MeanError, double &StdDev)
cvbres_t CVFDetectEdgesOfStripeTarget(IMG ImageStripes, cvbdim_t Left, cvbdim_t Top, cvbdim_t Right, cvbdim_t Bottom, size_t NumStripes, CVFScanDirection ScanDirection, double Threshold, int *ScanLineIndices, double *EdgeIndices, size_t &NumScanLines, size_t &NumEdgesPerScanLine)
T move(T... args)
Namespace for collection of calibration linescan functions from the Foundation package.
Definition calibration_line_scan.hpp:28
EdgeDetectionResult DetectEdgesOfStripeTarget(const Image &imageStripes, const Rect< int > &aoi, int numStripes, const ScanDirection &scanDirection, double threshold)
This function detects edges from a calibration pattern with alternating black and white stripes.
Definition calibration_line_scan.hpp:453
std::pair< Point2D< double >, Point2D< double > > CalculateTwoPointsForCalibrationOfMovement(const ImagePlane &imagePlane, const Area2D &aoi, Foundation::Transform2D::CalibrationPatternContrast contrast, int minContrast, const ValueRange< double > &pointSizeRange, const ScanDirection &scanDirection)
Extracts two points which can be used for the calibration of the movement of linescan cameras.
Definition calibration_line_scan.hpp:296
std::shared_ptr< LineScanCalibrator > LineScanCalibratorPtr
Convenience shared pointer for LineScanCalibrator.
Definition calibration_line_scan.hpp:314
PreDefinedPixelSizeMode
Specifies if the predefined pixel resolution is used for the metric calibration.
Definition calibration_line_scan.hpp:50
@ DoNotUse
Predefined pixel resolution will not be used.
Definition calibration_line_scan.hpp:52
@ Use
Predefined pixel resolution will be used.
Definition calibration_line_scan.hpp:54
std::unique_ptr< LineScanCalibrator > CreateLineScanCalibration(const Point2D< double > &calibrationPoint1, const Point2D< double > &calibrationPoint2, double referenceDistanceCalibrationPoints, const EdgeDetectionResult &edgeDetectionResult, double referenceWidthStripes, const LineScanCalibrationConfiguration &configuration)
Calibrates linescan cameras.
Definition calibration_line_scan.hpp:621
ScanDirection
Specifies the scanning direction.
Definition calibration_line_scan.hpp:35
@ X
Scans along the X coordinate.
Definition calibration_line_scan.hpp:40
@ Y
Scans along the Y coordinate.
Definition calibration_line_scan.hpp:45
CalibrationPatternContrast
Definition of the contrast of the pattern used for automatic calibration.
Definition transform_2d.hpp:358
std::shared_ptr< NonLinearTransformation > NonLinearTransformationPtr
Convenience shared pointer for NonLinearTransformation.
Definition transform_2d.hpp:1065
Namespace for the Foundation package.
Definition decl_metric_aqs12_calibration_piece.hpp:11
Root namespace for the Image Manager interface.
Definition version.hpp:11