CVB++ 15.0
Loading...
Searching...
No Matches
undistortion.hpp
1#pragma once
2
3#include "_cexports/c_core_2d.h"
4#include "_cexports/c_img.h"
5#include "global.hpp"
6#include "image.hpp"
7#include "point_2d.hpp"
8#include "size_2d.hpp"
9#include "core_2d.hpp"
10
11namespace Cvb
12{
13 CVB_BEGIN_INLINE_NS
14
16
17 template <>
18 inline HandleGuard<UndistPointFilter>::HandleGuard(void *handle) noexcept
19 : HandleGuard<UndistPointFilter>(handle, [](void *handle) { CVB_CALL_CAPI(ReleaseObject(handle)); })
20 {
21 }
22
24
25 template <>
26 inline HandleGuard<UndistImageFilter>::HandleGuard(void *handle) noexcept
27 : HandleGuard<UndistImageFilter>(handle, [](void *handle) { CVB_CALL_CAPI(ReleaseObject(handle)); })
28 {
29 }
30
39 class UndistPointFilter final
40 {
41 public:
43
49 {
50 const std::string ascii_data = Cvb::Internal::CastToAscii(calibrationData);
51 return Internal::DoResCallObjectOut<UndistPointFilter>(
52 [&](void *&handle) { return CVB_CALL_CAPI(CVC2DCreateUndistortionPointFilter(ascii_data.c_str(), handle)); });
53 }
54
55 static std::unique_ptr<UndistPointFilter> FromHandle(HandleGuard<UndistPointFilter> &&guard)
56 {
57 if (!guard.Handle())
58 throw std::runtime_error("handle must not be null");
59
60 return std::make_unique<UndistPointFilter>(std::move(guard));
61 }
62
63 explicit UndistPointFilter(HandleGuard<UndistPointFilter> &&guard) noexcept
64 : handle_(std::move(guard))
65 {
66 }
67
68 UndistPointFilter(const UndistPointFilter &other) = delete;
69 UndistPointFilter &operator=(const UndistPointFilter &other) = delete;
70 UndistPointFilter(UndistPointFilter &&other) = delete;
71 UndistPointFilter &operator=(UndistPointFilter &&other) = delete;
72
73 ~UndistPointFilter() = default;
74
75 void *Handle() const noexcept
76 {
77 return handle_.Handle();
78 }
79
81
87 {
88 // Convert Point2D to CVIPointD
89 std::vector<Cvb::CExports::CVIPointD> distorted_points_CVI;
90 for (const auto &c : distortedPoints)
91 {
92 Cvb::CExports::CVIPointD pt;
93 pt.X = c.X();
94 pt.Y = c.Y();
95 distorted_points_CVI.push_back(pt);
96 }
97
98 const int N = static_cast<int>(distortedPoints.size());
99 std::vector<Cvb::CExports::CVIPointD> undistorted_points_CVI(N);
100 CVB_CALL_CAPI_CHECKED(
101 CVC2DUndistortionPointFilterRun(Handle(), distorted_points_CVI.data(), N, undistorted_points_CVI.data()));
102
103 // Convert CVIPointD to Point2D
104 std::vector<Point2D<double>> undistorted_points;
105 for (const auto &c : undistorted_points_CVI)
106 undistorted_points.push_back(Point2D<double>(c.X, c.Y));
107
108 return undistorted_points;
109 }
110
111 private:
112 HandleGuard<UndistPointFilter> handle_;
113 };
114
125 class UndistImageFilter final
126 {
127 public:
129
138 Create(const Cvb::String &calibrationData, const Size2D<int> &imageSize, bool crop,
140 {
141 Cvb::CExports::cvbdim_t outwidth, outheight;
142 const std::string ascii_data = Cvb::Internal::CastToAscii(calibrationData);
143
144 auto res = Internal::DoResCallObjectOut<UndistImageFilter>([&](void *&handle) {
145 return CVB_CALL_CAPI(CVC2DCreateUndistortionFilter(
146 ascii_data.c_str(), imageSize.Width(), imageSize.Height(), crop,
147 static_cast<Cvb::CExports::CVC2DInterpolationMode>(interpolationMode), handle, outwidth, outheight));
148 });
149
150 res->outSize_ = Size2D<int>(outwidth, outheight);
151
152 return res;
153 }
154
155 static std::unique_ptr<UndistImageFilter> FromHandle(HandleGuard<UndistImageFilter> &&guard)
156 {
157 if (!guard.Handle())
158 throw std::runtime_error("handle must not be null");
159
160 return std::make_unique<UndistImageFilter>(std::move(guard));
161 }
162
163 explicit UndistImageFilter(HandleGuard<UndistImageFilter> &&guard) noexcept
164 : handle_(std::move(guard))
165 {
166 }
167
168 UndistImageFilter(const UndistImageFilter &other) = delete;
169 UndistImageFilter &operator=(const UndistImageFilter &other) = delete;
170 UndistImageFilter(UndistImageFilter &&other) = delete;
171 UndistImageFilter &operator=(UndistImageFilter &&other) = delete;
172
173 ~UndistImageFilter() = default;
174
175 void *Handle() const noexcept
176 {
177 return handle_.Handle();
178 }
179
181
184 const Size2D<int> &OutSize() const
185 {
186 return outSize_;
187 }
188
190
196 {
197 if (!image.PlaneDataTypesIdentical())
198 throw std::runtime_error("plane data types must be identical");
199
200 auto imageOut =
201 Image::Create(outSize_.Width(), outSize_.Height(), image.PlanesCount(), image.Plane(0).DataType());
202 Undistort(image, *imageOut);
203 return imageOut;
204 }
205
207
212 void Undistort(const Image &imageIn, Image &imageOut) const
213 {
214 auto outHandle = imageOut.Handle();
215 CVB_CALL_CAPI_CHECKED(CVC2DUndistortionFilterRun(
216 Handle(), static_cast<Cvb::CExports::CVC2DUndistThreads>(Cvb::CExports::CVC2DUndistThreadsSingle),
217 imageIn.Handle(), outHandle));
218 }
219
220 private:
221 HandleGuard<UndistImageFilter> handle_;
222 Size2D<int> outSize_;
223 };
224
225 CVB_END_INLINE_NS
226} // namespace Cvb
The Common Vision Blox image.
Definition decl_image.hpp:50
int PlanesCount() const noexcept
Get the number of planes for this image.
Definition decl_image.hpp:247
ImagePlane Plane(int plane) const
Indexed access to the individual plane information.
Definition detail_image.hpp:58
bool PlaneDataTypesIdentical() const noexcept
Check if all planes have the same data type.
Definition decl_image.hpp:257
void * Handle() const noexcept
Classic API image handle.
Definition decl_image.hpp:237
static std::unique_ptr< Image > Create(Size2D< int > size, int numPlanes=1, DataType dataType=DataType::Int8BppUnsigned())
Creates an uninitialized image with the given parameters.
Definition decl_image.hpp:75
class DataType DataType() const noexcept override
Data type descriptor for this array.
Definition detail_image_plane.hpp:337
Multi-purpose 2D vector class.
Definition point_2d.hpp:20
Stores a pair of numbers that represents the width and the height of a subject, typically a rectangle...
Definition size_2d.hpp:20
T Height() const noexcept
Gets the vertical component of the size.
Definition size_2d.hpp:77
T Width() const noexcept
Gets the horizontal component of the size.
Definition size_2d.hpp:57
A filter for undistorting images.
Definition undistortion.hpp:126
static std::unique_ptr< UndistImageFilter > Create(const Cvb::String &calibrationData, const Size2D< int > &imageSize, bool crop, UndistInterpolationMode interpolationMode=UndistInterpolationMode::Bilinear)
Creates an image undistortion filter for a given calibration configuration.
Definition undistortion.hpp:138
std::unique_ptr< Image > Undistort(const Image &image) const
Undistort an image.
Definition undistortion.hpp:195
void Undistort(const Image &imageIn, Image &imageOut) const
Undistort an image.
Definition undistortion.hpp:212
const Size2D< int > & OutSize() const
Gives the dimensions of the undistorted images.
Definition undistortion.hpp:184
A filter for undistorting 2D-point sets.
Definition undistortion.hpp:40
std::vector< Point2D< double > > Undistort(const std::vector< Point2D< double > > &distortedPoints)
Undistort a set of 2D points.
Definition undistortion.hpp:86
static std::unique_ptr< UndistPointFilter > Create(const Cvb::String &calibrationData)
Creates a point undistortion filter for a given calibration configuration.
Definition undistortion.hpp:48
cvbbool_t ReleaseObject(OBJ &Object)
T move(T... args)
Root namespace for the Image Manager interface.
Definition version.hpp:11
std::string String
String for wide characters or unicode characters.
Definition string.hpp:49
UndistInterpolationMode
Interpolation modes for image undistortion.
Definition core_2d.hpp:15
@ Bilinear
Bilinear interpolation.
Definition core_2d.hpp:17