CVB++ 15.0
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
22 namespace CalibrationLineScan
23 {
25
29 enum class ScanDirection
30 {
32
35 X = CExports::CVFScanDirection::CVFSD_X,
37
40 Y = CExports::CVFScanDirection::CVFSD_Y
41 };
42
45 {
49 Use
50 };
51
53
56 {
57 public:
59 : maxIterations_(50)
60 , polyDegree_(3)
61 , tolerance_(1e-6)
62 , scanDirection_(ScanDirection::Y)
63 , preDefinedPixelSizeMode_(PreDefinedPixelSizeMode::DoNotUse)
64 , pixelSize_(1)
65 {
66 }
67
69
82 std::uint32_t maxIterations, std::uint32_t polyDegree, double tolerance,
83 ScanDirection scanDirection,
84 PreDefinedPixelSizeMode preDefinedPixelSizeMode, double pixelSize) noexcept
85 : maxIterations_(maxIterations)
86 , polyDegree_(polyDegree)
87 , tolerance_(tolerance)
88 , scanDirection_(scanDirection)
89 , preDefinedPixelSizeMode_(preDefinedPixelSizeMode)
90 , pixelSize_(pixelSize)
91 {
92 }
93
94 ~LineScanCalibrationConfiguration() noexcept = default;
97 LineScanCalibrationConfiguration &operator=(const LineScanCalibrationConfiguration &) noexcept = default;
99
101
108 std::uint32_t MaxIterations() const noexcept
109 {
110 return maxIterations_;
111 }
112
114
123 void SetMaxIterations(std::uint32_t maxIterations) noexcept
124 {
125 maxIterations_ = maxIterations;
126 }
127
129
137 std::uint32_t PolyDegree() const noexcept
138 {
139 return polyDegree_;
140 }
141
143
151 void SetPolyDegree(std::uint32_t polyDegree) noexcept
152 {
153 polyDegree_ = polyDegree;
154 }
155
157
163 double Tolerance() const noexcept
164 {
165 return tolerance_;
166 }
167
169
176 void SetTolerance(double tolerance) noexcept
177 {
178 tolerance_ = tolerance;
179 }
180
182
187 {
188 return scanDirection_;
189 }
190
192
198 {
199 scanDirection_ = scanDirection;
200 }
201
203
210 {
211 return preDefinedPixelSizeMode_;
212 }
213
215
225 {
226 preDefinedPixelSizeMode_ = preDefinedPixelSize;
227 }
228
230
239 double PixelSize() const noexcept
240 {
241 return pixelSize_;
242 }
243
245
254 void SetPixelSize(double pixelSize) noexcept
255 {
256 pixelSize_ = pixelSize;
257 }
258
259 private:
260 std::uint32_t maxIterations_;
261 std::uint32_t polyDegree_;
262 double tolerance_;
265 double pixelSize_;
266 };
267
269
293 const ValueRange<double> &pointSizeRange, const ScanDirection &scanDirection)
294 {
296 Internal::DoResCall([&]() {
297 return CVB_CALL_CAPI(CVFGet2PointsForCalibrationOfMovement(
298 imagePlane.Parent().Handle(), static_cast<CExports::cvbdim_t>(imagePlane.Plane()), reinterpret_cast<const CExports::TArea&>(aoi),
299 static_cast<CExports::TCalibrationPatternContrast>(contrast), minContrast,
300 pointSizeRange.Min(), pointSizeRange.Max(), static_cast<CExports::CVFScanDirection>(scanDirection),
301 reinterpret_cast<CExports::CVIPointD&>(points.first),
302 reinterpret_cast<CExports::CVIPointD&>(points.second));
303 }));
304 return points;
305 }
306
307 class LineScanCalibrator;
310
312
322 {
324 const Point2D<double>& calibrationPoint2,
325 double referenceDistanceCalibrationPoints,
326 const EdgeDetectionResult& edgeDetectionResult,
327 double referenceWidthStripes,
328 const LineScanCalibrationConfiguration& configuration);
329
330 public:
331 EdgeDetectionResult() = default;
332
333 EdgeDetectionResult(std::vector<int>&& scanLineIndices, std::vector<double>&& edgeIndices, int numProfiles, int numEdgesPerProfile)
334 : scanLineIndices_(std::move(scanLineIndices))
335 , edgeIndices_(std::move(edgeIndices))
336 , numProfiles_(numProfiles)
337 , numEdgesPerProfile_(numEdgesPerProfile)
338 {
339 }
340
342
351 {
352 return scanLineIndices_;
353 }
354
356
390 {
391 return edgeIndices_;
392 }
393
395
402 int NumProfiles() const noexcept
403 {
404 return numProfiles_;
405 }
406
408
412 int NumEdgesPerProfile() const noexcept
413 {
414 return numEdgesPerProfile_;
415 }
416
417 private:
418
419 std::vector<int> scanLineIndices_;
420 std::vector<double> edgeIndices_;
421 int numProfiles_;
422 int numEdgesPerProfile_;
423 };
424
426
446 inline EdgeDetectionResult DetectEdgesOfStripeTarget(const Image &imageStripes, const Rect<int> &aoi, int numStripes,
447 const ScanDirection &scanDirection, double threshold)
448 {
449 std::vector<int> scanLineIndices(imageStripes.Width());
450 std::vector<double> edgeIndices(static_cast<size_t>(imageStripes.Width()) * (static_cast<size_t>(numStripes) + 1));
451 size_t numProfiles = 0;
452 size_t numEdgesPerProfile = 0;
453 Internal::DoResCall([&]() {
454 return CVB_CALL_CAPI(
455 CVFDetectEdgesOfStripeTarget(imageStripes.Handle(), static_cast<CExports::cvbdim_t>(aoi.Left()),
456 static_cast<CExports::cvbdim_t>(aoi.Top()), static_cast<CExports::cvbdim_t>(aoi.Right()),
457 static_cast<CExports::cvbdim_t>(aoi.Bottom()), numStripes,
458 static_cast<CExports::CVFScanDirection>(scanDirection), threshold,
459 scanLineIndices.data(), edgeIndices.data(), numProfiles, numEdgesPerProfile);
460 }));
461 return { std::move(scanLineIndices), std::move(edgeIndices), static_cast<int>(numProfiles), static_cast<int>(numEdgesPerProfile) };
462 }
463
465
467 {
469 const Point2D<double>& calibrationPoint2,
470 double referenceDistanceCalibrationPoints,
471 const EdgeDetectionResult& edgeDetectionResult,
472 double referenceWidthStripes,
473 const LineScanCalibrationConfiguration& configuration);
474
475 private:
476
477 struct PrivateTag {};
478
479 public:
480
481 LineScanCalibrator(const NonLinearTransformationPtr& transformation, double pixelSize, double meanError, double standardDeviation, PrivateTag)
482 : transformation_(transformation)
483 , pixelSize_(pixelSize)
484 , meanError_(meanError)
485 , standardDeviation_(standardDeviation)
486 {
487 }
488
490
526 NonLinearTransformationPtr Transformation() const noexcept
527 {
528 return transformation_;
529 }
530
532
539 double PixelSize() const noexcept
540 {
541 return pixelSize_;
542 }
543
545
551 double MeanError() const noexcept
552 {
553 return meanError_;
554 }
555
557
561 double StandardDeviation() const noexcept
562 {
563 return standardDeviation_;
564 }
565
566 private:
567
568 NonLinearTransformationPtr transformation_;
569 double pixelSize_;
570 double meanError_;
571 double standardDeviation_;
572 };
573
574
575
577
612 const Point2D<double> &calibrationPoint2,
613 double referenceDistanceCalibrationPoints,
614 const EdgeDetectionResult &edgeDetectionResult,
615 double referenceWidthStripes,
616 const LineScanCalibrationConfiguration &configuration)
617 {
618 void *transformation = nullptr;
619 double pixelSize = 0;
620 double meanError = 0;
621 double standardDeviation = 0;
622 Internal::DoResCall([&]() {
623 return CVB_CALL_CAPI(CVFCreateLineScanCalibration(*reinterpret_cast<const CExports::CVIPointD*>(&calibrationPoint1),
624 *reinterpret_cast<const CExports::CVIPointD*>(&calibrationPoint2),
625 referenceDistanceCalibrationPoints,
626 const_cast<int*>(reinterpret_cast<const int*>(edgeDetectionResult.scanLineIndices_.data())),
627 const_cast<double*>(reinterpret_cast<const double*>(edgeDetectionResult.edgeIndices_.data())),
628 edgeDetectionResult.numProfiles_,
629 edgeDetectionResult.numEdgesPerProfile_,
630 referenceWidthStripes,
631 *reinterpret_cast<const CExports::CVFLineScanCalibrationConfiguration*>(&configuration),
632 transformation, pixelSize, meanError, standardDeviation);
633 }));
634 return std::make_unique<LineScanCalibrator>(NonLinearTransformation::FromHandle(HandleGuard<NonLinearTransformation>(transformation)), pixelSize,
635 meanError, standardDeviation, LineScanCalibrator::PrivateTag{});
636 }
637
638
639
640 } // namespace CalibrationLineScan
641
651
652 } // namespace Foundation
653 CVB_END_INLINE_NS
654} // namespace Cvb
655
656#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:322
int NumProfiles() const noexcept
Gets the number of scan lines where edges are correctly detected.
Definition: calibration_line_scan.hpp:402
int NumEdgesPerProfile() const noexcept
Gets the number of edges per profile (scan line).
Definition: calibration_line_scan.hpp:412
std::vector< double > EdgeIndices() const noexcept
Gets the indices of edges for each scan line.
Definition: calibration_line_scan.hpp:389
std::vector< int > ScanLineIndices() const noexcept
Gets the indices of the scan lines.
Definition: calibration_line_scan.hpp:350
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:611
A set of parameters, which is used to configure linescan calibration calculated with function CreateL...
Definition: calibration_line_scan.hpp:56
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:151
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:81
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:108
void SetScanDirection(Foundation::CalibrationLineScan::ScanDirection scanDirection) noexcept
Sets the scanning direction of camera.
Definition: calibration_line_scan.hpp:197
double Tolerance() const noexcept
Gets the value for early stopping criteria.
Definition: calibration_line_scan.hpp:163
ScanDirection ScanDirection() const noexcept
Gets the scanning direction of camera.
Definition: calibration_line_scan.hpp:186
void SetTolerance(double tolerance) noexcept
Sets the value for early stopping criteria.
Definition: calibration_line_scan.hpp:176
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:123
void SetPreDefinedPixelSizeMode(Foundation::CalibrationLineScan::PreDefinedPixelSizeMode preDefinedPixelSize) noexcept
Sets the decision if predefined pixel size needs to be used.
Definition: calibration_line_scan.hpp:223
double PixelSize() const noexcept
Gets the pixel size of transformed image.
Definition: calibration_line_scan.hpp:239
void SetPixelSize(double pixelSize) noexcept
Sets the pixel size of transformed image.
Definition: calibration_line_scan.hpp:254
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:137
PreDefinedPixelSizeMode PreDefinedPixelSizeMode() const noexcept
Gets the decision if the calibrator uses predefined pixel size.
Definition: calibration_line_scan.hpp:209
Result of the linescan calibration executed by the CreateLineScanCalibration function.
Definition: calibration_line_scan.hpp:467
double PixelSize() const noexcept
Gets the pixel size after transformation.
Definition: calibration_line_scan.hpp:539
double MeanError() const noexcept
Gets the mean difference between stripe width in the transformed image and reference.
Definition: calibration_line_scan.hpp:551
NonLinearTransformationPtr Transformation() const noexcept
Gets the transformation.
Definition: calibration_line_scan.hpp:526
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:611
double StandardDeviation() const noexcept
Gets the standard deviation of the mean error.
Definition: calibration_line_scan.hpp:561
static std::unique_ptr< NonLinearTransformation > FromHandle(HandleGuard< NonLinearTransformation > &&guard)
Creates transformation from a classic API handle.
Definition: transform_2d.hpp:668
The Common Vision Blox image.
Definition: decl_image.hpp:45
int Width() const noexcept
Width of the image in pixels.
Definition: decl_image.hpp:288
void * Handle() const noexcept
Classic API image handle.
Definition: decl_image.hpp:226
Image plane information container.
Definition: decl_image_plane.hpp:33
int Plane() const noexcept
Plane index in the image, to which this plane refers to.
Definition: decl_image_plane.hpp:167
const Image & Parent() const noexcept
Image to which this plane descriptor refers to.
Definition: detail_image_plane.hpp:87
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
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:446
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:291
PreDefinedPixelSizeMode
Specifies if the predefined pixel resolution is used for the metric calibration.
Definition: calibration_line_scan.hpp:45
@ DoNotUse
Predefined pixel resolution will not be used.
@ Use
Predefined pixel resolution will be used.
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:611
ScanDirection
Specifies the scanning direction.
Definition: calibration_line_scan.hpp:30
CalibrationPatternContrast
Definition of the contrast of the pattern used for automatic calibration.
Definition: transform_2d.hpp:351
Root namespace for the Image Manager interface.
Definition: c_barcode.h:24
@ X
Sensor pixel values are mirrored in X (or denoted by u), so that the columns of the range map will be...
@ Y
Sensor pixel values are mirrored in Y (or denoted by v), so that the range map pixel values will be f...