CVB++ 15.0
Loading...
Searching...
No Matches
minos_filter.hpp
1#pragma once
2
3#include "../_cexports/c_minos.h"
4
5#include "search_result.hpp"
6
7#include "../global.hpp"
8#include "../image.hpp"
9
10#include <memory>
11#include <cmath>
12
13namespace Cvb
14{
15 CVB_BEGIN_INLINE_NS
16
18
22 namespace Minos
23 {
24
26
33 enum class KernelSize
34 {
43 };
44
46 enum class FilterOrder
47 {
52 };
53
54 namespace Private
55 {
56 inline void VerifyFilterInput(const Image &image)
57 {
58 if (image.Plane(0).DataType().BitsPerPixel() != 8 || image.Plane(0).DataType().IsSignedInteger())
59 {
60 throw std::invalid_argument("Minos filters can be applied only on 8-bit images");
61 }
62 }
63 } /* namespace Private */
64
66
76 {
77 Private::VerifyFilterInput(image);
78 return Internal::DoBoolCallObjectOut<Image>(
79 [&](void *&resimg) { return CVB_CALL_CAPI(FilterLaplace(image.Handle(), resimg)); });
80 }
81
83
92 {
93 Private::VerifyFilterInput(image);
94 return Internal::DoBoolCallObjectOut<Image>(
95 [&](void *&resimg) { return CVB_CALL_CAPI(FilterSharpen(image.Handle(), resimg)); });
96 }
97
99
106 {
107 Private::VerifyFilterInput(image);
108 return Internal::DoBoolCallObjectOut<Image>(
109 [&](void *&resimg) { return CVB_CALL_CAPI(FilterDilate(image.Handle(), resimg)); });
110 }
111
113
119 inline std::unique_ptr<Image> Erode(const Image &image)
120 {
121 Private::VerifyFilterInput(image);
122 return Internal::DoBoolCallObjectOut<Image>(
123 [&](void *&resimg) { return CVB_CALL_CAPI(FilterErode(image.Handle(), resimg)); });
124 }
125
127
139 inline std::unique_ptr<Image> ButterworthHighPass(const Image &image, double gain, int offset, double cutOff,
140 FilterOrder order)
141 {
142 Private::VerifyFilterInput(image);
143 return Internal::DoBoolCallObjectOut<Image>([&](void *&resimg) {
144 return CVB_CALL_CAPI(ButterWorth(image.Handle(), static_cast<short>(1), gain, offset,
145 static_cast<CExports::cvbval_t>(order), cutOff, resimg));
146 });
147 }
148
150
160 inline std::unique_ptr<Image> ButterworthLowPass(const Image &image, double cutOff, FilterOrder order)
161 {
162 Private::VerifyFilterInput(image);
163 return Internal::DoBoolCallObjectOut<Image>([&](void *&resimg) {
164 return CVB_CALL_CAPI(ButterWorth(image.Handle(), static_cast<short>(0), 1.0, 0,
165 static_cast<CExports::cvbval_t>(order), cutOff, resimg));
166 });
167 }
168
170
179 inline std::unique_ptr<Image> LowPass(const Image &image, KernelSize kernelSize)
180 {
181 Private::VerifyFilterInput(image);
182 return Internal::DoBoolCallObjectOut<Image>([&](void *&resimg) {
183 switch (kernelSize)
184 {
186 return CVB_CALL_CAPI(FilterLow2x2(image.Handle(), resimg));
188 return CVB_CALL_CAPI(FilterLow3x3(image.Handle(), resimg));
190 return CVB_CALL_CAPI(FilterLow5x5(image.Handle(), resimg));
191 default:
192 throw std::invalid_argument("unsupported low pass filter kernel size");
193 }
194 });
195 }
196
198
207 inline std::unique_ptr<Image> Edge(const Image &image, KernelSize kernelSize)
208 {
209 Private::VerifyFilterInput(image);
210 return Internal::DoBoolCallObjectOut<Image>([&](void *&resimg) {
211 switch (kernelSize)
212 {
214 return CVB_CALL_CAPI(FilterEdge2x2(image.Handle(), resimg));
216 return CVB_CALL_CAPI(FilterEdge3x3(image.Handle(), resimg));
217 default:
218 throw std::invalid_argument("unsupported edge filter kernel size");
219 }
220 });
221 }
222
224
233 inline std::unique_ptr<Image> Pyramid(const Image &image, KernelSize kernelSize)
234 {
235 Private::VerifyFilterInput(image);
236 return Internal::DoBoolCallObjectOut<Image>([&](void *&resimg) {
237 switch (kernelSize)
238 {
240 return CVB_CALL_CAPI(FilterPyramid3x3(image.Handle(), resimg));
242 return CVB_CALL_CAPI(FilterPyramid4x4(image.Handle(), resimg));
244 return CVB_CALL_CAPI(FilterPyramid5x5(image.Handle(), resimg));
245 default:
246 throw std::invalid_argument("unsupported pyramid filter kernel size");
247 }
248 });
249 }
250
252
271 template <class RANGE>
272 inline typename TypedRange<std::unique_ptr<Image>, double, RANGE>::type
273 UserFilter(const Image &image, KernelSize kernelSize, const RANGE &kernel)
274 {
275 Private::VerifyFilterInput(image);
276 size_t kernelWidthHeight = static_cast<size_t>(kernelSize);
277 auto kernelRange = MakeRangeAdapter<double>(kernel, kernelWidthHeight * kernelWidthHeight);
278 CExports::TFilterDef filterDef = {};
279 return Internal::DoBoolCallObjectOut<Image>([&](void *&resimg) {
280 switch (kernelSize)
281 {
283 filterDef.c0000 = std::lround(kernel[0] * 1000.0);
284 filterDef.c0R00 = std::lround(kernel[1] * 1000.0);
285 filterDef.c000B = std::lround(kernel[2] * 1000.0);
286 filterDef.c0R0B = std::lround(kernel[3] * 1000.0);
287 return CVB_CALL_CAPI(FilterUser2x2(image.Handle(), filterDef, resimg));
288
290 filterDef.c0L0T = std::lround(kernel[0] * 1000.0);
291 filterDef.c000T = std::lround(kernel[1] * 1000.0);
292 filterDef.c0R0T = std::lround(kernel[2] * 1000.0);
293 filterDef.c0L00 = std::lround(kernel[3] * 1000.0);
294 filterDef.c0000 = std::lround(kernel[4] * 1000.0);
295 filterDef.c0R00 = std::lround(kernel[5] * 1000.0);
296 filterDef.c0L0B = std::lround(kernel[6] * 1000.0);
297 filterDef.c000B = std::lround(kernel[7] * 1000.0);
298 filterDef.c0R0B = std::lround(kernel[8] * 1000.0);
299 return CVB_CALL_CAPI(FilterUser3x3(image.Handle(), filterDef, resimg));
300
302 filterDef.cLLTT = std::lround(kernel[0] * 1000.0);
303 filterDef.c0LTT = std::lround(kernel[1] * 1000.0);
304 filterDef.c00TT = std::lround(kernel[2] * 1000.0);
305 filterDef.c0RTT = std::lround(kernel[3] * 1000.0);
306 filterDef.cRRTT = std::lround(kernel[4] * 1000.0);
307 filterDef.cLL0T = std::lround(kernel[5] * 1000.0);
308 filterDef.c0L0T = std::lround(kernel[6] * 1000.0);
309 filterDef.c000T = std::lround(kernel[7] * 1000.0);
310 filterDef.c0R0T = std::lround(kernel[8] * 1000.0);
311 filterDef.cRR0T = std::lround(kernel[9] * 1000.0);
312 filterDef.cLL00 = std::lround(kernel[10] * 1000.0);
313 filterDef.c0L00 = std::lround(kernel[11] * 1000.0);
314 filterDef.c0000 = std::lround(kernel[12] * 1000.0);
315 filterDef.c0R00 = std::lround(kernel[13] * 1000.0);
316 filterDef.cRR00 = std::lround(kernel[14] * 1000.0);
317 filterDef.cLL0B = std::lround(kernel[15] * 1000.0);
318 filterDef.c0L0B = std::lround(kernel[16] * 1000.0);
319 filterDef.c000B = std::lround(kernel[17] * 1000.0);
320 filterDef.c0R0B = std::lround(kernel[18] * 1000.0);
321 filterDef.cRR0B = std::lround(kernel[19] * 1000.0);
322 filterDef.cLLBB = std::lround(kernel[20] * 1000.0);
323 filterDef.c0LBB = std::lround(kernel[21] * 1000.0);
324 filterDef.c00BB = std::lround(kernel[22] * 1000.0);
325 filterDef.c0RBB = std::lround(kernel[23] * 1000.0);
326 filterDef.cRRBB = std::lround(kernel[24] * 1000.0);
327 return CVB_CALL_CAPI(FilterUser5x5(image.Handle(), filterDef, resimg));
328
329 default:
330 throw std::invalid_argument("unsupported user filter kernel size");
331 }
332 });
333 }
334
335 } /* namespace Minos */
336 CVB_END_INLINE_NS
337} /* namespace Cvb */
int BitsPerPixel() const noexcept
Number of actually valid bits per pixel.
Definition data_type.hpp:322
bool IsSignedInteger() const noexcept
Gets whether the pixels of the plane have signed integer values.
Definition data_type.hpp:354
The Common Vision Blox image.
Definition decl_image.hpp:45
ImagePlane Plane(int plane) const
Indexed access to the individual plane information.
Definition detail_image.hpp:58
void * Handle() const noexcept
Classic API image handle.
Definition decl_image.hpp:232
class DataType DataType() const noexcept override
Data type descriptor for this array.
Definition detail_image_plane.hpp:337
Namespace for the Minos package.
Definition classifier.hpp:29
std::unique_ptr< Image > Pyramid(const Image &image, KernelSize kernelSize)
Apply a pyramid filter to the input image.
Definition minos_filter.hpp:233
std::unique_ptr< Image > Dilate(const Image &image)
Apply a 3x3 dilation filter to the input image.
Definition minos_filter.hpp:105
std::unique_ptr< Image > Edge(const Image &image, KernelSize kernelSize)
Apply an edge filter to the input image.
Definition minos_filter.hpp:207
FilterOrder
Order of the ButterworthLowPass or ButterworthHighPass filter to be applied.
Definition minos_filter.hpp:47
@ Order2nd
2nd order filter.
Definition minos_filter.hpp:51
@ Order1st
1st order filter.
Definition minos_filter.hpp:49
std::unique_ptr< Image > ButterworthHighPass(const Image &image, double gain, int offset, double cutOff, FilterOrder order)
Apply a Butterworth high pass filter to the image.
Definition minos_filter.hpp:139
std::unique_ptr< Image > LowPass(const Image &image, KernelSize kernelSize)
Apply a low pass filter to the input image.
Definition minos_filter.hpp:179
KernelSize
Available kernel sizes for the filter functions exported by the Minos library.
Definition minos_filter.hpp:34
@ Kernel4x4
Kernel with 4x4 elements.
Definition minos_filter.hpp:40
@ Kernel5x5
Kernel with 5x5 elements.
Definition minos_filter.hpp:42
@ Kernel2x2
Kernel with 2x2 elements.
Definition minos_filter.hpp:36
@ Kernel3x3
Kernel with 3x3 elements.
Definition minos_filter.hpp:38
std::unique_ptr< Image > Erode(const Image &image)
Apply a 3x3 erosion filter to the input image.
Definition minos_filter.hpp:119
std::unique_ptr< Image > ButterworthLowPass(const Image &image, double cutOff, FilterOrder order)
Apply a Butterworth low pass filter to the image.
Definition minos_filter.hpp:160
std::unique_ptr< Image > Sharpen(const Image &image)
Apply a 3x3 sharpen filter to the input image.
Definition minos_filter.hpp:91
std::unique_ptr< Image > Laplace(const Image &image)
Apply a 3x3 Laplace filter to the input image.
Definition minos_filter.hpp:75
TypedRange< std::unique_ptr< Image >, double, RANGE >::type UserFilter(const Image &image, KernelSize kernelSize, const RANGE &kernel)
Apply a user-defined filter to the input image.
Definition minos_filter.hpp:273
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
T lround(T... args)