CVB++ 14.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{
17CVB_BEGIN_INLINE_NS
18
19
20namespace Foundation { namespace Moments { class ImageMoments; } }
21
22template <>
23inline HandleGuard<Foundation::Moments::ImageMoments>::HandleGuard(void * handle) noexcept
24 : HandleGuard<Foundation::Moments::ImageMoments>(handle, [](void* h) { CVB_CALL_CAPI(ReleaseObject(h)); })
25{
26}
27
28
29namespace Foundation
30{
31
33
37namespace Moments
38{
39
42{
44 FavorNone = CExports::MC_FavorNone,
46 FavorSpeed = CExports::MC_FavorSpeed,
48 FavorAccuracy = CExports::MC_FavorAccuracy
49};
50
51
53enum class MomentsOrder
54{
56 Order0 = 0,
58 Order1 = 1,
60 Order2 = 2,
62 Order3 = 3
63};
64
67{
71 Normalized
72};
73
75
78{
80double M1;
81
83double M2;
84
86double M3;
87
89double M4;
90
92double M5;
93
95double M6;
96
98double M7;
99}; /* HuMoments */
100
102
105{
106public:
108
116 : handle_(nullptr),
117 calculationPreference_ (preference),
118 lastOffset_ (aoi.Location ())
119 {
120 auto h = CVB_CALL_CAPI(CreateImageMoments(static_cast<CExports::TMomentsCalculation>(preference)));
121 if (!h)
122 {
123 Utilities::SystemInfo::ThrowLastError ();
124 }
125 handle_.Reset(h);
126
127 CVB_CALL_CAPI_CHECKED (CalculateMoments(imagePlane.Parent().Handle(), imagePlane.Plane(),
128 aoi.Left(), aoi.Top(), aoi.Right(), aoi.Bottom(), h));
129 }
130
132
139 : ImageMoments (imagePlane, imagePlane.Parent ().Bounds (), preference)
140 { }
141
143 ImageMoments (ImageMoments&&) noexcept = default;
144
146 ImageMoments& operator=(ImageMoments&&) noexcept = default;
147
148public:
150
157 static std::unique_ptr<ImageMoments> Calculate (const ImagePlane & imagePlane, Rect<int> aoi, MomentsCalculation preference = MomentsCalculation::FavorNone)
158 {
159 return std::unique_ptr<ImageMoments>(new ImageMoments(imagePlane, aoi, preference));
160 }
161
163
170 {
171 return std::unique_ptr<ImageMoments>(new ImageMoments(imagePlane, preference));
172 }
173
175
181 {
182 return calculationPreference_;
183 }
184
186
192 void * Handle() const noexcept
193 {
194 return handle_.Handle();
195 }
196
197
198
200
208 double SpatialMoment (MomentsOrder xOrder, MomentsOrder yOrder, MomentsNormalization normalization, Point2D<int> offset)
209 {
210 double moment;
211 CVB_CALL_CAPI_CHECKED (GetSpatialImageMoment(Handle(), static_cast<int>(xOrder), static_cast<int>(yOrder),
212 offset.X(), offset.Y(), normalization == MomentsNormalization::Normalized, moment));
213 return moment;
214 }
215
217
227 double SpatialMoment (MomentsOrder xOrder, MomentsOrder yOrder, MomentsNormalization normalization)
228 {
229 return SpatialMoment (xOrder, yOrder, normalization, lastOffset_);
230 }
231
233
241 double CentralMoment (MomentsOrder xOrder, MomentsOrder yOrder, MomentsNormalization normalization)
242 {
243 double moment;
244 CVB_CALL_CAPI_CHECKED (GetCentralImageMoment(Handle(), static_cast<int>(xOrder), static_cast<int>(yOrder),
245 normalization == MomentsNormalization::Normalized, moment));
246 return moment;
247 }
248
250
256 {
257 double m1, m2, m3, m4, m5, m6, m7;
258 CVB_CALL_CAPI_CHECKED (GetHuImageMoments(Handle(), m1, m2, m3, m4, m5, m6, m7));
259 typedef struct HuMoments HuMomentsType;
260 return HuMomentsType{m1, m2, m3, m4, m5, m6, m7};
261 }
262
263private:
264 HandleGuard<ImageMoments> handle_;
265 MomentsCalculation calculationPreference_;
266 Point2D<int> lastOffset_;
267}; /* ImageMoments */
268
271
272} /* namespace Moments */
273
277
281
282} /* namespace Foundation */
283CVB_END_INLINE_NS
284} /* namespace Cvb */
285
286#endif
State object for calculating various image moments.
Definition: moments.hpp:105
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:115
ImageMoments(const ImagePlane &imagePlane, MomentsCalculation preference=MomentsCalculation::FavorNone)
Calculate the moments on the input imagePlane.
Definition: moments.hpp:138
MomentsCalculation CalculationPreference() const noexcept
The preference of speed versus accuracy chosen when this object was created.
Definition: moments.hpp:180
static std::unique_ptr< ImageMoments > Calculate(const ImagePlane &imagePlane, MomentsCalculation preference=MomentsCalculation::FavorNone)
Calculate the moments on the input imagePlane.
Definition: moments.hpp:169
HuMoments HuMoments()
Get the seven Hu moments of the image.
Definition: moments.hpp:255
ImageMoments(ImageMoments &&) noexcept=default
Move constructor.
double SpatialMoment(MomentsOrder xOrder, MomentsOrder yOrder, MomentsNormalization normalization)
Get one of the spatial image moments.
Definition: moments.hpp:227
double SpatialMoment(MomentsOrder xOrder, MomentsOrder yOrder, MomentsNormalization normalization, Point2D< int > offset)
Get one of the spatial image moments.
Definition: moments.hpp:208
void * Handle() const noexcept
Classic API IMGMOMENTS handle.
Definition: moments.hpp:192
double CentralMoment(MomentsOrder xOrder, MomentsOrder yOrder, MomentsNormalization normalization)
Get one of the central image moments.
Definition: moments.hpp:241
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:157
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 X() const noexcept
Gets the x-component of the point.
Definition: point_2d.hpp:86
T Y() const noexcept
Gets the y-component of the point.
Definition: point_2d.hpp:106
MomentsOrder
Enumerated order for x and y central or spatial moments.
Definition: moments.hpp:54
std::shared_ptr< ImageMoments > ImageMomentsPtr
Convenience shared pointer for ImageMoments.
Definition: moments.hpp:270
MomentsCalculation
Choose between speed and accuracy during image moment calculation.
Definition: moments.hpp:42
@ FavorNone
Favor neither speed nor accuracy.
@ FavorSpeed
Favor speed over accuracy during moments calculation.
@ FavorAccuracy
Favor accuracy over speed during moments calculation.
MomentsNormalization
Available normalization modes for moment calculations.
Definition: moments.hpp:67
@ Unnormalized
Report moments unnormalized.
Root namespace for the Image Manager interface.
Definition: c_barcode.h:24
Results of Hu Moment calculation.
Definition: moments.hpp:78
double M2
Hu Moment M2.
Definition: moments.hpp:83
double M5
Hu Moment M5.
Definition: moments.hpp:92
double M7
Hu Moment M7.
Definition: moments.hpp:98
double M1
Hu Moment M1.
Definition: moments.hpp:80
double M6
Hu Moment M6.
Definition: moments.hpp:95
double M4
Hu Moment M4.
Definition: moments.hpp:89
double M3
Hu Moment M3.
Definition: moments.hpp:86