CVB++ 15.0
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() = default;
336
337 EdgeDetectionResult(std::vector<int> &&scanLineIndices, std::vector<double> &&edgeIndices, int numProfiles,
338 int numEdgesPerProfile)
339 : scanLineIndices_(std::move(scanLineIndices))
340 , edgeIndices_(std::move(edgeIndices))
341 , numProfiles_(numProfiles)
342 , numEdgesPerProfile_(numEdgesPerProfile)
343 {
344 }
345
347
356 {
357 return scanLineIndices_;
358 }
359
361
395 {
396 return edgeIndices_;
397 }
398
400
407 int NumProfiles() const noexcept
408 {
409 return numProfiles_;
410 }
411
413
417 int NumEdgesPerProfile() const noexcept
418 {
419 return numEdgesPerProfile_;
420 }
421
422 private:
423 std::vector<int> scanLineIndices_;
424 std::vector<double> edgeIndices_;
425 int numProfiles_;
426 int numEdgesPerProfile_;
427 };
428
430
450 inline EdgeDetectionResult DetectEdgesOfStripeTarget(const Image &imageStripes, const Rect<int> &aoi,
451 int numStripes, const ScanDirection &scanDirection,
452 double threshold)
453 {
454 std::vector<int> scanLineIndices(imageStripes.Width());
455 std::vector<double> edgeIndices(static_cast<size_t>(imageStripes.Width())
456 * (static_cast<size_t>(numStripes) + 1));
457 size_t numProfiles = 0;
458 size_t numEdgesPerProfile = 0;
459 Internal::DoResCall([&]() {
460 return CVB_CALL_CAPI(
461 CVFDetectEdgesOfStripeTarget(imageStripes.Handle(), static_cast<CExports::cvbdim_t>(aoi.Left()),
462 static_cast<CExports::cvbdim_t>(aoi.Top()), static_cast<CExports::cvbdim_t>(aoi.Right()),
463 static_cast<CExports::cvbdim_t>(aoi.Bottom()), numStripes,
464 static_cast<CExports::CVFScanDirection>(scanDirection), threshold,
465 scanLineIndices.data(), edgeIndices.data(), numProfiles, numEdgesPerProfile);
466 }));
467 return {std::move(scanLineIndices), std::move(edgeIndices), static_cast<int>(numProfiles),
468 static_cast<int>(numEdgesPerProfile)};
469 }
470
472
473 class LineScanCalibrator final
474 {
476 CreateLineScanCalibration(const Point2D<double> &calibrationPoint1, const Point2D<double> &calibrationPoint2,
477 double referenceDistanceCalibrationPoints,
478 const EdgeDetectionResult &edgeDetectionResult, double referenceWidthStripes,
479 const LineScanCalibrationConfiguration &configuration);
480
481 private:
482 struct PrivateTag
483 {
484 };
485
486 public:
487 LineScanCalibrator(const NonLinearTransformationPtr &transformation, double pixelSize, double meanError,
488 double standardDeviation, PrivateTag)
489 : transformation_(transformation)
490 , pixelSize_(pixelSize)
491 , meanError_(meanError)
492 , standardDeviation_(standardDeviation)
493 {
494 }
495
497
534 {
535 return transformation_;
536 }
537
539
546 double PixelSize() const noexcept
547 {
548 return pixelSize_;
549 }
550
552
558 double MeanError() const noexcept
559 {
560 return meanError_;
561 }
562
564
568 double StandardDeviation() const noexcept
569 {
570 return standardDeviation_;
571 }
572
573 private:
574 NonLinearTransformationPtr transformation_;
575 double pixelSize_;
576 double meanError_;
577 double standardDeviation_;
578 };
579
581
618 CreateLineScanCalibration(const Point2D<double> &calibrationPoint1, const Point2D<double> &calibrationPoint2,
619 double referenceDistanceCalibrationPoints,
620 const EdgeDetectionResult &edgeDetectionResult, double referenceWidthStripes,
621 const LineScanCalibrationConfiguration &configuration)
622 {
623 void *transformation = nullptr;
624 double pixelSize = 0;
625 double meanError = 0;
626 double standardDeviation = 0;
627 Internal::DoResCall([&]() {
628 return CVB_CALL_CAPI(CVFCreateLineScanCalibration(*reinterpret_cast<const CExports::CVIPointD*>(&calibrationPoint1),
629 *reinterpret_cast<const CExports::CVIPointD*>(&calibrationPoint2),
630 referenceDistanceCalibrationPoints,
631 const_cast<int*>(reinterpret_cast<const int*>(edgeDetectionResult.scanLineIndices_.data())),
632 const_cast<double*>(reinterpret_cast<const double*>(edgeDetectionResult.edgeIndices_.data())),
633 edgeDetectionResult.numProfiles_,
634 edgeDetectionResult.numEdgesPerProfile_,
635 referenceWidthStripes,
636 *reinterpret_cast<const CExports::CVFLineScanCalibrationConfiguration*>(&configuration),
637 transformation, pixelSize, meanError, standardDeviation);
638 }));
639 return std::make_unique<LineScanCalibrator>(
640 NonLinearTransformation::FromHandle(HandleGuard<NonLinearTransformation>(transformation)), pixelSize,
641 meanError, standardDeviation, LineScanCalibrator::PrivateTag{});
642 }
643
644 } // namespace CalibrationLineScan
645
655
656 } // namespace Foundation
657 CVB_END_INLINE_NS
658} // namespace Cvb
659
660#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:407
int NumEdgesPerProfile() const noexcept
Gets the number of edges per profile (scan line).
Definition calibration_line_scan.hpp:417
std::vector< double > EdgeIndices() const noexcept
Gets the indices of edges for each scan line.
Definition calibration_line_scan.hpp:394
std::vector< int > ScanLineIndices() const noexcept
Gets the indices of the scan lines.
Definition calibration_line_scan.hpp:355
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:618
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:474
double PixelSize() const noexcept
Gets the pixel size after transformation.
Definition calibration_line_scan.hpp:546
double MeanError() const noexcept
Gets the mean difference between stripe width in the transformed image and reference.
Definition calibration_line_scan.hpp:558
NonLinearTransformationPtr Transformation() const noexcept
Gets the transformation.
Definition calibration_line_scan.hpp:533
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:618
double StandardDeviation() const noexcept
Gets the standard deviation of the mean error.
Definition calibration_line_scan.hpp:568
static std::unique_ptr< NonLinearTransformation > FromHandle(HandleGuard< NonLinearTransformation > &&guard)
Creates transformation from a classic API handle.
Definition transform_2d.hpp:712
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:29
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
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:450
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:618
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:1061
Namespace for the Foundation package.
Definition decl_metric_aqs12_calibration_piece.hpp:11
Root namespace for the Image Manager interface.
Definition version.hpp:11