CVB++ 15.1
Loading...
Searching...
No Matches
moments.hpp
1#pragma once
2
3#if defined _WIN32
4
5# include "../_cexports/c_foundation.h"
6
7# include "../global.hpp"
8# include "../image.hpp"
9# include "../exception.hpp"
10# include "../point_2d.hpp"
11# include "../rect.hpp"
12
13# include <memory>
14
15namespace Cvb
16{
17 CVB_BEGIN_INLINE_NS
18
19 namespace Foundation
20 {
21 namespace Moments
22 {
23 class ImageMoments;
24 }
25 } // namespace Foundation
26
27 template <>
28 inline HandleGuard<Foundation::Moments::ImageMoments>::HandleGuard(void *handle) noexcept
29 : HandleGuard<Foundation::Moments::ImageMoments>(handle, [](void *h) { CVB_CALL_CAPI(ReleaseObject(h)); })
30 {
31 }
32
33 namespace Foundation
34 {
35
37
41 namespace Moments
42 {
43
46 {
48 FavorNone = CExports::MC_FavorNone,
50 FavorSpeed = CExports::MC_FavorSpeed,
52 FavorAccuracy = CExports::MC_FavorAccuracy
53 };
54
56 enum class MomentsOrder
57 {
59 Order0 = 0,
61 Order1 = 1,
63 Order2 = 2,
66 };
67
76
78
80 struct HuMoments
81 {
83 double M1;
84
86 double M2;
87
89 double M3;
90
92 double M4;
93
95 double M5;
96
98 double M6;
99
101 double M7;
102 }; /* HuMoments */
103
105
108 {
109 public:
111
117 ImageMoments(const ImagePlane &imagePlane, Rect<int> aoi,
119 : handle_(nullptr)
120 , calculationPreference_(preference)
121 , lastOffset_(aoi.Location())
122 {
123 auto h = CVB_CALL_CAPI(CreateImageMoments(static_cast<CExports::TMomentsCalculation>(preference)));
124 if (!h)
125 {
126 Utilities::SystemInfo::ThrowLastError();
127 }
128 handle_.Reset(h);
129
130 CVB_CALL_CAPI_CHECKED(CalculateMoments(imagePlane.Parent().Handle(), imagePlane.Plane(), aoi.Left(),
131 aoi.Top(), aoi.Right(), aoi.Bottom(), h));
132 }
133
135
141 : ImageMoments(imagePlane, imagePlane.Parent().Bounds(), preference)
142 {
143 }
144
146 ImageMoments(ImageMoments &&) noexcept = default;
147
149 ImageMoments &operator=(ImageMoments &&) noexcept = default;
150
151 ~ImageMoments() = default;
152 ImageMoments() = delete;
153 ImageMoments(const ImageMoments &) = delete;
154 ImageMoments &operator=(const ImageMoments &) = delete;
155
157
164 static std::unique_ptr<ImageMoments> Calculate(const ImagePlane &imagePlane, Rect<int> aoi,
166 {
167 return std::make_unique<ImageMoments>(imagePlane, aoi, preference);
168 }
169
171
179 {
180 return std::make_unique<ImageMoments>(imagePlane, preference);
181 }
182
184
190 {
191 return calculationPreference_;
192 }
193
195
201 void *Handle() const noexcept
202 {
203 return handle_.Handle();
204 }
205
207
215 double SpatialMoment(MomentsOrder xOrder, MomentsOrder yOrder, MomentsNormalization normalization,
216 Point2D<int> offset)
217 {
218 double moment = 0;
219 CVB_CALL_CAPI_CHECKED(GetSpatialImageMoment(Handle(), static_cast<int>(xOrder), static_cast<int>(yOrder),
220 offset.X(), offset.Y(),
221 normalization == MomentsNormalization::Normalized, moment));
222 return moment;
223 }
224
226
236 double SpatialMoment(MomentsOrder xOrder, MomentsOrder yOrder, MomentsNormalization normalization)
237 {
238 return SpatialMoment(xOrder, yOrder, normalization, lastOffset_);
239 }
240
242
250 double CentralMoment(MomentsOrder xOrder, MomentsOrder yOrder, MomentsNormalization normalization)
251 {
252 double moment = 0;
253 CVB_CALL_CAPI_CHECKED(GetCentralImageMoment(Handle(), static_cast<int>(xOrder), static_cast<int>(yOrder),
254 normalization == MomentsNormalization::Normalized, moment));
255 return moment;
256 }
257
259
265 {
266 double m1 = 0, m2 = 0, m3 = 0, m4 = 0, m5 = 0, m6 = 0, m7 = 0;
267 CVB_CALL_CAPI_CHECKED(GetHuImageMoments(Handle(), m1, m2, m3, m4, m5, m6, m7));
268 typedef struct HuMoments HuMomentsType;
269 return HuMomentsType{m1, m2, m3, m4, m5, m6, m7};
270 }
271
272 private:
273 HandleGuard<ImageMoments> handle_;
274 MomentsCalculation calculationPreference_;
275 Point2D<int> lastOffset_;
276 }; /* ImageMoments */
277
280
281 } /* namespace Moments */
282
286
287 using Moments::HuMoments;
290
291 } /* namespace Foundation */
292 CVB_END_INLINE_NS
293} /* namespace Cvb */
294
295#endif
ImageMoments(const ImagePlane &imagePlane, Rect< int > aoi, MomentsCalculation preference=MomentsCalculation::FavorNone)
Calculate the moments on the input ImagePlane and area of interest.
Definition moments.hpp:117
State object for calculating various image moments.
Definition moments.hpp:108
ImageMoments(const ImagePlane &imagePlane, Rect< int > aoi, MomentsCalculation preference=MomentsCalculation::FavorNone)
Calculate the moments on the input ImagePlane and area of interest.
Definition moments.hpp:117
ImageMoments(const ImagePlane &imagePlane, MomentsCalculation preference=MomentsCalculation::FavorNone)
Calculate the moments on the input imagePlane.
Definition moments.hpp:140
MomentsCalculation CalculationPreference() const noexcept
The preference of speed versus accuracy chosen when this object was created.
Definition moments.hpp:189
static std::unique_ptr< ImageMoments > Calculate(const ImagePlane &imagePlane, MomentsCalculation preference=MomentsCalculation::FavorNone)
Calculate the moments on the input imagePlane.
Definition moments.hpp:177
HuMoments HuMoments()
Get the seven Hu moments of the image.
Definition moments.hpp:264
ImageMoments(ImageMoments &&) noexcept=default
Move constructor.
double SpatialMoment(MomentsOrder xOrder, MomentsOrder yOrder, MomentsNormalization normalization)
Get one of the spatial image moments.
Definition moments.hpp:236
double SpatialMoment(MomentsOrder xOrder, MomentsOrder yOrder, MomentsNormalization normalization, Point2D< int > offset)
Get one of the spatial image moments.
Definition moments.hpp:215
void * Handle() const noexcept
Classic API IMGMOMENTS handle.
Definition moments.hpp:201
double CentralMoment(MomentsOrder xOrder, MomentsOrder yOrder, MomentsNormalization normalization)
Get one of the central image moments.
Definition moments.hpp:250
static std::unique_ptr< ImageMoments > Calculate(const ImagePlane &imagePlane, Rect< int > aoi, MomentsCalculation preference=MomentsCalculation::FavorNone)
Calculate the moments on the input imagePlane and aoi.
Definition moments.hpp:164
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
T X() const noexcept
Gets the x-component of the point.
Definition point_2d.hpp:84
T Y() const noexcept
Gets the y-component of the point.
Definition point_2d.hpp:104
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
cvbres_t CalculateMoments(IMG ImgIn, long Index, long left, long top, long right, long bottom, IMGMOMENTS Moments)
IMGMOMENTS CreateImageMoments(TMomentsCalculation CalcMode)
cvbres_t GetSpatialImageMoment(IMGMOMENTS Moments, cvbval_t XOrder, cvbval_t YOrder, cvbdim_t OffsetX, cvbdim_t OffsetY, cvbbool_t Normalized, double &Moment)
cvbres_t GetHuImageMoments(IMGMOMENTS Moments, double &M1, double &M2, double &M3, double &M4, double &M5, double &M6, double &M7)
cvbres_t GetCentralImageMoment(IMGMOMENTS Moments, long XOrder, long YOrder, cvbbool_t Normalized, double &Moment)
cvbbool_t ReleaseObject(OBJ &Object)
Namespace for image moments related tools from the Foundation package.
Definition moments.hpp:22
MomentsOrder
Enumerated order for x and y central or spatial moments.
Definition moments.hpp:57
@ Order0
0th order
Definition moments.hpp:59
@ Order3
3rd order
Definition moments.hpp:65
@ Order1
1st order
Definition moments.hpp:61
@ Order2
2nd order
Definition moments.hpp:63
std::shared_ptr< ImageMoments > ImageMomentsPtr
Convenience shared pointer for ImageMoments.
Definition moments.hpp:279
MomentsCalculation
Choose between speed and accuracy during image moment calculation.
Definition moments.hpp:46
@ FavorNone
Favor neither speed nor accuracy.
Definition moments.hpp:48
@ FavorSpeed
Favor speed over accuracy during moments calculation.
Definition moments.hpp:50
@ FavorAccuracy
Favor accuracy over speed during moments calculation.
Definition moments.hpp:52
MomentsNormalization
Available normalization modes for moment calculations.
Definition moments.hpp:70
@ Normalized
Report moments normalized.
Definition moments.hpp:74
@ Unnormalized
Report moments unnormalized.
Definition moments.hpp:72
Namespace for the Foundation package.
Definition decl_metric_aqs12_calibration_piece.hpp:11
Root namespace for the Image Manager interface.
Definition version.hpp:11
Results of Hu Moment calculation.
Definition moments.hpp:81
double M2
Hu Moment M2.
Definition moments.hpp:86
double M5
Hu Moment M5.
Definition moments.hpp:95
double M7
Hu Moment M7.
Definition moments.hpp:101
double M1
Hu Moment M1.
Definition moments.hpp:83
double M6
Hu Moment M6.
Definition moments.hpp:98
double M4
Hu Moment M4.
Definition moments.hpp:92
double M3
Hu Moment M3.
Definition moments.hpp:89