CVB++ 15.0
threshold.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 "../value_range.hpp"
11
12# include <vector>
13# include <memory>
14
15namespace Cvb
16{
17 CVB_BEGIN_INLINE_NS
18
19 namespace Foundation
20 {
21
23
27 namespace Threshold
28 {
29
32 {
34 LessThan = CExports::TH_Less,
36 LessThanOrEqual = CExports::TH_LessOrEqual,
38 Equal = CExports::TH_Equal,
40 GreaterOrEqual = CExports::TH_GreaterOrEqual,
42 Greater = CExports::TH_Greater
43 };
44
47 {
49 Mean = CExports::DTN_Mean,
52 MinMax = CExports::DTN_MinMax
53 };
54
57
71 inline std::unique_ptr<Image> StaticThreshold(const Image &image, const Image &thresholds,
72 StaticThresholding comparison)
73 {
74 return Internal::DoResCallObjectOut<Image>([&](void *&resimg) {
75 return CVB_CALL_CAPI(CreateImageThresholdImage(
76 image.Handle(), thresholds.Handle(), static_cast<CExports::TThresholdComparison>(comparison), resimg));
77 });
78 }
79
81
90 template <class RANGE>
91 inline typename TypedRange<std::unique_ptr<Image>, double, RANGE>::type
92 StaticThreshold(const Image &image, const RANGE &values, StaticThresholding comparison)
93 {
94 auto valuesRange = MakeRangeAdapter<double>(values, image.PlanesCount());
95
96 return Internal::DoResCallObjectOut<Image>([&](void *&resimg) {
97 /* Note: need to cast-away const of "values" parameter to match the C-API, the function does not modify it. */
98 return CVB_CALL_CAPI(CreateConstantThresholdImage(image.Handle(), const_cast<double *>(valuesRange.Data()),
99 static_cast<CExports::TThresholdComparison>(comparison),
100 resimg));
101 });
102 }
103
105
113 inline std::unique_ptr<Image> StaticThreshold(const Image &image, double value, StaticThresholding comparison)
114 {
115 return StaticThreshold(image, std::vector<double>(image.PlanesCount(), value), comparison);
116 }
117
119
139 inline std::unique_ptr<Image> DynamicThreshold(const Image &image, int windowSize, int threshold,
141 {
142 return Internal::DoResCallObjectOut<Image>([&](void *&resimg) {
143 return CVB_CALL_CAPI(CreateDynamicThresholdImageEx(
144 image.Handle(), static_cast<CExports::TDynamicThresholdNorm>(norm), windowSize, threshold, resimg));
145 });
146 }
147
149
162 template <class RANGE>
163 inline typename TypedRange<std::unique_ptr<Image>, double, RANGE>::type
164 StaticTransparentThreshold(const Image &image, const RANGE &thresholds, StaticThresholding comparison,
165 const RANGE &values)
166 {
167 auto planesCount = static_cast<std::size_t>(image.PlanesCount());
168 auto thresholdsRange = MakeRangeAdapter<double>(thresholds, planesCount);
169 auto valuesRange = MakeRangeAdapter<double>(values, planesCount);
170
171 return Internal::DoResCallObjectOut<Image>([&](void *&resimg) {
172 /* Note: need to cast-away const of "values" parameter to match the C-API, the function does not modify it. */
173 return CVB_CALL_CAPI(CreateTransparentThresholdImage(
174 image.Handle(), const_cast<double *>(thresholdsRange.Data()), const_cast<double *>(valuesRange.Data()),
175 static_cast<CExports::TThresholdComparison>(comparison), resimg));
176 });
177 }
178
180
193 inline std::unique_ptr<Image> StaticTransparentThreshold(const Image &image, double threshold,
194 StaticThresholding comparison, double value)
195 {
196 return StaticTransparentThreshold(image, std::vector<double>(image.PlanesCount(), threshold), comparison,
197 std::vector<double>(image.PlanesCount(), value));
198 }
199
201
213 template <class RANGE>
214 inline typename TypedRange<std::unique_ptr<Image>, double, RANGE>::type
215 StaticTransparentThreshold(const Image &image, const RANGE &thresholds, StaticThresholding comparison)
216 {
217 return StaticTransparentThreshold(image, thresholds, comparison, thresholds);
218 }
219
221
233 inline std::unique_ptr<Image> StaticTransparentThreshold(const Image &image, double threshold,
234 StaticThresholding comparison)
235 {
236 return StaticTransparentThreshold(image, std::vector<double>(image.PlanesCount(), threshold), comparison);
237 }
238
240
256 inline void RangeThresholdToDst(const ImagePlane &plane, ValueRange<int> range, Image &imageDst, Rect<int> aoi)
257 {
258 CExports::TDRect tmpRect = {static_cast<double>(aoi.Left()), static_cast<double>(aoi.Top()),
259 static_cast<double>(aoi.Right()), static_cast<double>(aoi.Bottom())};
260 if (CVB_CALL_CAPI(FBlobBinariseImage(plane.Parent().Handle(), &tmpRect, plane.Plane(), range.Min(), range.Max(),
261 imageDst.Handle()))
262 < 0)
263 Utilities::SystemInfo::ThrowLastError();
264 imageDst.RaisePixelContentChanged();
265 }
266
268
279 inline void RangeThresholdToDst(const ImagePlane &plane, ValueRange<int> range, Image &imageDst)
280 {
281 RangeThresholdToDst(plane, range, imageDst, plane.Parent().Bounds());
282 }
283
285
295 {
296 auto result = Image::Create(aoi.Size());
297 RangeThresholdToDst(plane, range, *result, aoi);
298 return result;
299 }
300
302
311 {
312 auto result = Image::Create(plane.Parent().Size());
313 RangeThresholdToDst(plane, range, *result);
314 return result;
315 }
316
317 } /* namespace Threshold */
318
321
325
328
329 } /* namespace Foundation */
330 CVB_END_INLINE_NS
331} /* namespace Cvb */
332
333#endif
The Common Vision Blox image.
Definition decl_image.hpp:50
void RaisePixelContentChanged() const
Inform clients listening to the pixel content changed event, that the image data has been completely ...
Definition decl_image.hpp:448
int PlanesCount() const noexcept
Get the number of planes for this image.
Definition decl_image.hpp:247
Rect< int > Bounds() const noexcept
Bounding rectangle of the image in pixels.
Definition decl_image.hpp:438
Size2D< int > Size() const noexcept
Size of the image in pixels.
Definition decl_image.hpp:428
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
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
Rectangle object.
Definition rect.hpp:24
Container for range definitions.
Definition value_range.hpp:17
T Min() const noexcept
Gets the minimum value.
Definition value_range.hpp:47
T Max() const noexcept
Gets the maximum value.
Definition value_range.hpp:69
cvbres_t FBlobBinariseImage(IMG Src, TDRect *Rect, long Index, long Low, long High, IMG Dst)
cvbres_t CreateImageThresholdImage(IMG ImgIn, IMG ImgThreshold, TThresholdComparison Comparison, IMG &ImgOut)
cvbres_t CreateTransparentThresholdImage(IMG ImgIn, double Thresholds[], double Values[], TThresholdComparison Comparison, IMG &ImgOut)
cvbres_t CreateDynamicThresholdImageEx(IMG ImgIn, TDynamicThresholdNorm Norm, long WindowSize, double Threshold, IMG &ImgOut)
cvbres_t CreateConstantThresholdImage(IMG ImgIn, double Values[], TThresholdComparison Comparison, IMG &ImgOut)
Namespace for collection of thresholding functions from the Foundation package.
Definition threshold.hpp:28
std::unique_ptr< Image > DynamicThreshold(const Image &image, int windowSize, int threshold, DynamicThresholdNorm norm=DynamicThresholdNorm::Mean)
This function performs dynamic thresholding on the input image.
Definition threshold.hpp:139
StaticThresholding
Comparisons for static thresholding.
Definition threshold.hpp:32
@ LessThanOrEqual
Less than or Equal.
Definition threshold.hpp:36
@ GreaterOrEqual
Greater or Equal.
Definition threshold.hpp:40
@ Greater
Greater.
Definition threshold.hpp:42
@ LessThan
Less than.
Definition threshold.hpp:34
@ Equal
Equal.
Definition threshold.hpp:38
void RangeThresholdToDst(const ImagePlane &plane, ValueRange< int > range, Image &imageDst, Rect< int > aoi)
A range based binarization algorithm to the given input.
Definition threshold.hpp:256
TypedRange< std::unique_ptr< Image >, double, RANGE >::type StaticTransparentThreshold(const Image &image, const RANGE &thresholds, StaticThresholding comparison, const RANGE &values)
Create a thresholded image using a transparency approach.
Definition threshold.hpp:164
std::unique_ptr< Image > RangeThreshold(const ImagePlane &plane, ValueRange< int > range, Rect< int > aoi)
A range based binarization algorithm to the given input.
Definition threshold.hpp:294
DynamicThresholdNorm
Norm calculation approaches for dynamic thresholding.
Definition threshold.hpp:47
@ Mean
Dynamic thresholding compares versus the mean value of the threshold mask (default).
Definition threshold.hpp:49
std::unique_ptr< Image > StaticThreshold(const Image &image, const Image &thresholds, StaticThresholding comparison)
This function performs thresholding of an input image with pixel-by-pixel thresholds stored in the pi...
Definition threshold.hpp:71
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