CVB++ 14.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 enum class ScanDirection
26 {
28 X = CExports::CVFScanDirection::CVFSD_X,
30 Y = CExports::CVFScanDirection::CVFSD_Y
31 };
32
35 {
39 Use
40 };
41
43
46 {
47 public:
49 : maxIterations_(50)
50 , polyDegree_(3)
51 , tolerance_(1e-6)
52 , scanDirection_(ScanDirection::Y)
53 , preDefinedPixelSizeMode_(PreDefinedPixelSizeMode::DoNotUse)
54 , pixelSize_(1)
55 {
56 }
57
59
70 std::uint32_t maxIterations, std::uint32_t polyDegree, double tolerance,
72 Foundation::CalibrationLineScan::PreDefinedPixelSizeMode preDefinedPixelSizeMode, double pixelSize) noexcept
73 : maxIterations_(maxIterations)
74 , polyDegree_(polyDegree)
75 , tolerance_(tolerance)
76 , scanDirection_(scanDirection)
77 , preDefinedPixelSizeMode_(preDefinedPixelSizeMode)
78 , pixelSize_(pixelSize)
79 {
80 }
81
82 ~LineScanCalibrationConfiguration() noexcept = default;
85 LineScanCalibrationConfiguration &operator=(const LineScanCalibrationConfiguration &) noexcept = default;
87
89
96 std::uint32_t MaxIterations() const noexcept
97 {
98 return maxIterations_;
99 }
100
102
108 void SetMaxIterations(std::uint32_t maxIterations) noexcept
109 {
110 maxIterations_ = maxIterations;
111 }
112
114
120 std::uint32_t PolyDegree() const noexcept
121 {
122 return polyDegree_;
123 }
124
126
132 void SetPolyDegree(std::uint32_t polyDegree) noexcept
133 {
134 polyDegree_ = polyDegree;
135 }
136
138
144 double Tolerance() const noexcept
145 {
146 return tolerance_;
147 }
148
150
155 void SetTolerance(double tolerance) noexcept
156 {
157 tolerance_ = tolerance;
158 }
159
161
166 {
167 return scanDirection_;
168 }
169
171
177 {
178 scanDirection_ = scanDirection;
179 }
180
182
190 {
191 return preDefinedPixelSizeMode_;
192 }
193
195
202 {
203 preDefinedPixelSizeMode_ = preDefinedPixelSize;
204 }
205
207
214 double PixelSize() const noexcept
215 {
216 return pixelSize_;
217 }
218
220
225 void SetPixelSize(double pixelSize) noexcept
226 {
227 pixelSize_ = pixelSize;
228 }
229
230 private:
231 std::uint32_t maxIterations_;
232 std::uint32_t polyDegree_;
233 double tolerance_;
236 double pixelSize_;
237 };
238
240
259 CalibrationPatternContrast contrast, int minContrast,
260 const ValueRange<double> &pointSizeRange, const ScanDirection &scanDirection)
261 {
263 Internal::DoResCall([&]() {
264 return CVB_CALL_CAPI(CVFGet2PointsForCalibrationOfMovement(
265 imagePlane.Parent().Handle(), static_cast<CExports::cvbdim_t>(imagePlane.Plane()), reinterpret_cast<const CExports::TArea&>(aoi),
266 static_cast<CExports::TCalibrationPatternContrast>(contrast), minContrast,
267 pointSizeRange.Min(), pointSizeRange.Max(), static_cast<CExports::CVFScanDirection>(scanDirection),
268 reinterpret_cast<CExports::CVIPointD&>(points.first),
269 reinterpret_cast<CExports::CVIPointD&>(points.second));
270 }));
271 return points;
272 }
273
274 class LineScanCalibrator;
277
279
281 {
283 const Point2D<double>& calibrationPoint2,
284 double referenceDistanceCalibrationPoints,
285 const EdgeDetectionResult& edgeDetectionResult,
286 double referenceWidthStripes,
287 const LineScanCalibrationConfiguration& configuration);
288
289 public:
290 EdgeDetectionResult() = default;
291
292 EdgeDetectionResult(std::vector<int>&& scanLineIndices, std::vector<double>&& edgeIndices, int numProfiles, int numEdgesPerProfile)
293 : scanLineIndices_(std::move(scanLineIndices))
294 , edgeIndices_(std::move(edgeIndices))
295 , numProfiles_(numProfiles)
296 , numEdgesPerProfile_(numEdgesPerProfile)
297 {
298 }
299
301
306 {
307 return scanLineIndices_;
308 }
309
311
316 {
317 return edgeIndices_;
318 }
319
321
325 int NumProfiles() const noexcept
326 {
327 return numProfiles_;
328 }
329
331
335 int NumEdgesPerProfile() const noexcept
336 {
337 return numEdgesPerProfile_;
338 }
339
340 private:
341
342 std::vector<int> scanLineIndices_;
343 std::vector<double> edgeIndices_;
344 int numProfiles_;
345 int numEdgesPerProfile_;
346 };
347
349
365 inline EdgeDetectionResult DetectEdgesOfStripeTarget(const Image &imageStripes, const Area2D &aoi, int numStripes,
366 const ScanDirection &scanDirection, double threshold)
367 {
368 std::vector<int> scanLineIndices(imageStripes.Width());
369 std::vector<double> edgeIndices(static_cast<size_t>(imageStripes.Width()) * (static_cast<size_t>(numStripes) + 1));
370 size_t numProfiles = 0;
371 size_t numEdgesPerProfile = 0;
372 Internal::DoResCall([&]() {
373 auto left = aoi.P0().X();
374 auto top = aoi.P0().Y();
375 auto right = aoi.P3().X();
376 auto bottom = aoi.P3().Y();
377 return CVB_CALL_CAPI(
378 CVFDetectEdgesOfStripeTarget(imageStripes.Handle(), static_cast<CExports::cvbdim_t>(left),
379 static_cast<CExports::cvbdim_t>(top), static_cast<CExports::cvbdim_t>(right),
380 static_cast<CExports::cvbdim_t>(bottom), numStripes,
381 static_cast<CExports::CVFScanDirection>(scanDirection), threshold,
382 scanLineIndices.data(), edgeIndices.data(), numProfiles, numEdgesPerProfile);
383 }));
384 return { std::move(scanLineIndices), std::move(edgeIndices), static_cast<int>(numProfiles), static_cast<int>(numEdgesPerProfile) };
385 }
386
388
390 {
392 const Point2D<double>& calibrationPoint2,
393 double referenceDistanceCalibrationPoints,
394 const EdgeDetectionResult& edgeDetectionResult,
395 double referenceWidthStripes,
396 const LineScanCalibrationConfiguration& configuration);
397
398 private:
399
400 struct PrivateTag {};
401
402 public:
403
404 LineScanCalibrator(const NonLinearTransformationPtr& transformation, double pixelSize, double meanError, double standardDeviation, PrivateTag)
405 : transformation_(transformation)
406 , pixelSize_(pixelSize)
407 , meanError_(meanError)
408 , standardDeviation_(standardDeviation)
409 {
410 }
411
413
417 NonLinearTransformationPtr Transformation() const noexcept
418 {
419 return transformation_;
420 }
421
423
429 double PixelSize() const noexcept
430 {
431 return pixelSize_;
432 }
433
435
441 double MeanError() const noexcept
442 {
443 return meanError_;
444 }
445
447
451 double StandardDeviation() const noexcept
452 {
453 return standardDeviation_;
454 }
455
456 private:
457
458 NonLinearTransformationPtr transformation_;
459 double pixelSize_;
460 double meanError_;
461 double standardDeviation_;
462 };
463
464
465
467
497 const Point2D<double> &calibrationPoint2,
498 double referenceDistanceCalibrationPoints,
499 const EdgeDetectionResult &edgeDetectionResult,
500 double referenceWidthStripes,
501 const LineScanCalibrationConfiguration &configuration)
502 {
503 void *transformation = nullptr;
504 double pixelSize = 0;
505 double meanError = 0;
506 double standardDeviation = 0;
507 Internal::DoResCall([&]() {
508 return CVB_CALL_CAPI(CVFCreateLineScanCalibration(*reinterpret_cast<const CExports::CVIPointD*>(&calibrationPoint1),
509 *reinterpret_cast<const CExports::CVIPointD*>(&calibrationPoint2),
510 referenceDistanceCalibrationPoints,
511 const_cast<int*>(reinterpret_cast<const int*>(edgeDetectionResult.scanLineIndices_.data())),
512 const_cast<double*>(reinterpret_cast<const double*>(edgeDetectionResult.edgeIndices_.data())),
513 edgeDetectionResult.numProfiles_,
514 edgeDetectionResult.numEdgesPerProfile_,
515 referenceWidthStripes,
516 *reinterpret_cast<const CExports::CVFLineScanCalibrationConfiguration*>(&configuration),
517 transformation, pixelSize, meanError, standardDeviation);
518 }));
519 return std::make_unique<LineScanCalibrator>(NonLinearTransformation::FromHandle(HandleGuard<NonLinearTransformation>(transformation)), pixelSize,
520 meanError, standardDeviation, LineScanCalibrator::PrivateTag{});
521 }
522
523
524
525 } // namespace CalibrationLineScan
526
536
537 } // namespace Foundation
538 CVB_END_INLINE_NS
539} // namespace Cvb
540
541#endif
Structure that represents an area of interest in the image.
Definition: area_2d.hpp:21
A set of parameters that stores the result from an edge detection.
Definition: calibration_line_scan.hpp:281
int NumProfiles() const noexcept
Gets the number of scan-line where edges are correctly detected.
Definition: calibration_line_scan.hpp:325
int NumEdgesPerProfile() const noexcept
Gets the number of edges per unit profile.
Definition: calibration_line_scan.hpp:335
std::vector< double > EdgeIndices() const noexcept
Gets the indices of edges for each scan-line.
Definition: calibration_line_scan.hpp:315
std::vector< int > ScanLineIndices() const noexcept
Gets the indices of the scan-lines.
Definition: calibration_line_scan.hpp:305
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 line-scan cameras.
Definition: calibration_line_scan.hpp:496
A set of parameters, which is used to configure line-scan calibration.
Definition: calibration_line_scan.hpp:46
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:132
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:96
void SetScanDirection(Foundation::CalibrationLineScan::ScanDirection scanDirection) noexcept
Sets the scanning direction of camera.
Definition: calibration_line_scan.hpp:176
double Tolerance() const noexcept
Gets the value for early stopping criteria.
Definition: calibration_line_scan.hpp:144
void SetTolerance(double tolerance) noexcept
Sets the value for early stopping criteria.
Definition: calibration_line_scan.hpp:155
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:108
Foundation::CalibrationLineScan::ScanDirection ScanDirection() const noexcept
Gets the scanning direction of camera.
Definition: calibration_line_scan.hpp:165
void SetPreDefinedPixelSizeMode(Foundation::CalibrationLineScan::PreDefinedPixelSizeMode preDefinedPixelSize) noexcept
Sets the decision if predefined pixel size needs to be used.
Definition: calibration_line_scan.hpp:200
double PixelSize() const noexcept
Gets the pixel size of transformed image.
Definition: calibration_line_scan.hpp:214
Foundation::CalibrationLineScan::PreDefinedPixelSizeMode PreDefinedPixelSizeMode() const noexcept
Gets the decision if the calibrator uses predefined pixel size.
Definition: calibration_line_scan.hpp:189
void SetPixelSize(double pixelSize) noexcept
Sets the pixel size of transformed image.
Definition: calibration_line_scan.hpp:225
LineScanCalibrationConfiguration(std::uint32_t maxIterations, std::uint32_t polyDegree, double tolerance, Foundation::CalibrationLineScan::ScanDirection scanDirection, Foundation::CalibrationLineScan::PreDefinedPixelSizeMode preDefinedPixelSizeMode, double pixelSize) noexcept
Constructor for line-scan calibration configuration.
Definition: calibration_line_scan.hpp:69
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:120
A set of parameters that is required by the following data transformation to get the final result.
Definition: calibration_line_scan.hpp:390
double PixelSize() const noexcept
Gets the pixel size after transformation.
Definition: calibration_line_scan.hpp:429
double MeanError() const noexcept
Gets the mean difference between stripe width in the transformed image and reference.
Definition: calibration_line_scan.hpp:441
NonLinearTransformationPtr Transformation() const noexcept
Gets the transformation.
Definition: calibration_line_scan.hpp:417
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 line-scan cameras.
Definition: calibration_line_scan.hpp:496
double StandardDeviation() const noexcept
Gets the standard deviation of the mean error.
Definition: calibration_line_scan.hpp:451
static std::unique_ptr< NonLinearTransformation > FromHandle(HandleGuard< NonLinearTransformation > &&guard)
Creates transformation from a classic API handle.
Definition: transform_2d.hpp:666
The Common Vision Blox image.
Definition: decl_image.hpp:45
int Width() const noexcept
Width of the image in pixels.
Definition: decl_image.hpp:285
void * Handle() const noexcept
Classic API image handle.
Definition: decl_image.hpp:223
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:169
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 Area2D &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:365
PreDefinedPixelSizeMode
Specifies if the predefined pixel resolution is used for the metric calibration.
Definition: calibration_line_scan.hpp:35
@ 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 line-scan cameras.
Definition: calibration_line_scan.hpp:496
std::pair< Point2D< double >, Point2D< double > > CalculateTwoPointsForCalibrationOfMovement(const ImagePlane &imagePlane, const Area2D &aoi, 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 line-scan cameras.
Definition: calibration_line_scan.hpp:258
ScanDirection
Specifies the scanning direction.
Definition: calibration_line_scan.hpp:26
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...