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{
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
115 : handle_(nullptr),
116 calculationPreference_ (preference),
117 lastOffset_ (aoi.Location ())
118 {
119 auto h = CVB_CALL_CAPI(CreateImageMoments(static_cast<CExports::TMomentsCalculation>(preference)));
120 if (!h)
121 {
122 Utilities::SystemInfo::ThrowLastError ();
123 }
124 handle_.Reset(h);
125
126 CVB_CALL_CAPI_CHECKED (CalculateMoments(imagePlane.Parent().Handle(), imagePlane.Plane(),
127 aoi.Left(), aoi.Top(), aoi.Right(), aoi.Bottom(), h));
128 }
129
131
137 : ImageMoments (imagePlane, imagePlane.Parent ().Bounds (), preference)
138 { }
139
141 ImageMoments (ImageMoments&&) noexcept = default;
142
144 ImageMoments& operator=(ImageMoments&&) noexcept = default;
145
146public:
148
155 static std::unique_ptr<ImageMoments> Calculate (const ImagePlane & imagePlane, Rect<int> aoi, MomentsCalculation preference = MomentsCalculation::FavorNone)
156 {
157 return std::make_unique<ImageMoments>(imagePlane, aoi, preference);
158 }
159
161
168 {
169 return std::make_unique<ImageMoments>(imagePlane, preference);
170 }
171
173
179 {
180 return calculationPreference_;
181 }
182
184
190 void * Handle() const noexcept
191 {
192 return handle_.Handle();
193 }
194
195
196
198
206 double SpatialMoment (MomentsOrder xOrder, MomentsOrder yOrder, MomentsNormalization normalization, Point2D<int> offset)
207 {
208 double moment;
209 CVB_CALL_CAPI_CHECKED (GetSpatialImageMoment(Handle(), static_cast<int>(xOrder), static_cast<int>(yOrder),
210 offset.X(), offset.Y(), normalization == MomentsNormalization::Normalized, moment));
211 return moment;
212 }
213
215
225 double SpatialMoment (MomentsOrder xOrder, MomentsOrder yOrder, MomentsNormalization normalization)
226 {
227 return SpatialMoment (xOrder, yOrder, normalization, lastOffset_);
228 }
229
231
239 double CentralMoment (MomentsOrder xOrder, MomentsOrder yOrder, MomentsNormalization normalization)
240 {
241 double moment;
242 CVB_CALL_CAPI_CHECKED (GetCentralImageMoment(Handle(), static_cast<int>(xOrder), static_cast<int>(yOrder),
243 normalization == MomentsNormalization::Normalized, moment));
244 return moment;
245 }
246
248
254 {
255 double m1, m2, m3, m4, m5, m6, m7;
256 CVB_CALL_CAPI_CHECKED (GetHuImageMoments(Handle(), m1, m2, m3, m4, m5, m6, m7));
257 typedef struct HuMoments HuMomentsType;
258 return HuMomentsType{m1, m2, m3, m4, m5, m6, m7};
259 }
260
261private:
262 HandleGuard<ImageMoments> handle_;
263 MomentsCalculation calculationPreference_;
264 Point2D<int> lastOffset_;
265}; /* ImageMoments */
266
269
270} /* namespace Moments */
271
275
279
280} /* namespace Foundation */
281CVB_END_INLINE_NS
282} /* namespace Cvb */
283
284#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:114
ImageMoments(const ImagePlane &imagePlane, MomentsCalculation preference=MomentsCalculation::FavorNone)
Calculate the moments on the input imagePlane.
Definition: moments.hpp:136
MomentsCalculation CalculationPreference() const noexcept
The preference of speed versus accuracy chosen when this object was created.
Definition: moments.hpp:178
static std::unique_ptr< ImageMoments > Calculate(const ImagePlane &imagePlane, MomentsCalculation preference=MomentsCalculation::FavorNone)
Calculate the moments on the input imagePlane.
Definition: moments.hpp:167
HuMoments HuMoments()
Get the seven Hu moments of the image.
Definition: moments.hpp:253
ImageMoments(ImageMoments &&) noexcept=default
Move constructor.
double SpatialMoment(MomentsOrder xOrder, MomentsOrder yOrder, MomentsNormalization normalization)
Get one of the spatial image moments.
Definition: moments.hpp:225
double SpatialMoment(MomentsOrder xOrder, MomentsOrder yOrder, MomentsNormalization normalization, Point2D< int > offset)
Get one of the spatial image moments.
Definition: moments.hpp:206
void * Handle() const noexcept
Classic API IMGMOMENTS handle.
Definition: moments.hpp:190
double CentralMoment(MomentsOrder xOrder, MomentsOrder yOrder, MomentsNormalization normalization)
Get one of the central image moments.
Definition: moments.hpp:239
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:155
void * Handle() const noexcept
Classic API image handle.
Definition: decl_image.hpp:226
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:167
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:268
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