CVB++ 15.0
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 public:
153
160 static std::unique_ptr<ImageMoments> Calculate(const ImagePlane &imagePlane, Rect<int> aoi,
162 {
163 return std::make_unique<ImageMoments>(imagePlane, aoi, preference);
164 }
165
167
175 {
176 return std::make_unique<ImageMoments>(imagePlane, preference);
177 }
178
180
186 {
187 return calculationPreference_;
188 }
189
191
197 void *Handle() const noexcept
198 {
199 return handle_.Handle();
200 }
201
203
211 double SpatialMoment(MomentsOrder xOrder, MomentsOrder yOrder, MomentsNormalization normalization,
212 Point2D<int> offset)
213 {
214 double moment;
215 CVB_CALL_CAPI_CHECKED(GetSpatialImageMoment(Handle(), static_cast<int>(xOrder), static_cast<int>(yOrder),
216 offset.X(), offset.Y(),
217 normalization == MomentsNormalization::Normalized, moment));
218 return moment;
219 }
220
222
232 double SpatialMoment(MomentsOrder xOrder, MomentsOrder yOrder, MomentsNormalization normalization)
233 {
234 return SpatialMoment(xOrder, yOrder, normalization, lastOffset_);
235 }
236
238
246 double CentralMoment(MomentsOrder xOrder, MomentsOrder yOrder, MomentsNormalization normalization)
247 {
248 double moment;
249 CVB_CALL_CAPI_CHECKED(GetCentralImageMoment(Handle(), static_cast<int>(xOrder), static_cast<int>(yOrder),
250 normalization == MomentsNormalization::Normalized, moment));
251 return moment;
252 }
253
255
261 {
262 double m1, m2, m3, m4, m5, m6, m7;
263 CVB_CALL_CAPI_CHECKED(GetHuImageMoments(Handle(), m1, m2, m3, m4, m5, m6, m7));
264 typedef struct HuMoments HuMomentsType;
265 return HuMomentsType{m1, m2, m3, m4, m5, m6, m7};
266 }
267
268 private:
269 HandleGuard<ImageMoments> handle_;
270 MomentsCalculation calculationPreference_;
271 Point2D<int> lastOffset_;
272 }; /* ImageMoments */
273
276
277 } /* namespace Moments */
278
282
283 using Moments::HuMoments;
286
287 } /* namespace Foundation */
288 CVB_END_INLINE_NS
289} /* namespace Cvb */
290
291#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:185
static std::unique_ptr< ImageMoments > Calculate(const ImagePlane &imagePlane, MomentsCalculation preference=MomentsCalculation::FavorNone)
Calculate the moments on the input imagePlane.
Definition moments.hpp:173
HuMoments HuMoments()
Get the seven Hu moments of the image.
Definition moments.hpp:260
ImageMoments(ImageMoments &&) noexcept=default
Move constructor.
double SpatialMoment(MomentsOrder xOrder, MomentsOrder yOrder, MomentsNormalization normalization)
Get one of the spatial image moments.
Definition moments.hpp:232
double SpatialMoment(MomentsOrder xOrder, MomentsOrder yOrder, MomentsNormalization normalization, Point2D< int > offset)
Get one of the spatial image moments.
Definition moments.hpp:211
void * Handle() const noexcept
Classic API IMGMOMENTS handle.
Definition moments.hpp:197
double CentralMoment(MomentsOrder xOrder, MomentsOrder yOrder, MomentsNormalization normalization)
Get one of the central image moments.
Definition moments.hpp:246
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:160
void * Handle() const noexcept
Classic API image handle.
Definition decl_image.hpp:232
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
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
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:275
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 c_bayer_to_rgb.h:17
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