MayaFlux 0.5.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Vision/Morphology.hpp
Go to the documentation of this file.
1#pragma once
2
3/**
4 * @file Morphology.hpp
5 * @brief Binary morphological operations on normalised float masks.
6 *
7 * Pure functions. No MayaFlux type dependencies.
8 *
9 * ## Conventions
10 * - Input masks are single-channel float where 1.0f is foreground
11 * and 0.0f is background. Values are treated as binary: >= 0.5f
12 * is foreground, < 0.5f is background.
13 * - Output is strictly binary: 1.0f or 0.0f
14 * - w and h are pixel dimensions; caller ensures span size == w * h
15 * - radius is the structuring element half-size in pixels. A radius
16 * of 1 produces a 3x3 square structuring element.
17 * - Border handling is clamp-to-edge throughout
18 * - Parallelism handled internally via Parallel::par_unseq
19 */
20
22
23// ============================================================================
24// Primitive operations
25// ============================================================================
26
27/**
28 * @brief Morphological erosion with square structuring element.
29 *
30 * A pixel is 1.0f only if all pixels within radius are foreground.
31 *
32 * @param mask Single-channel float span, size must be w * h.
33 * @param w Image width in pixels.
34 * @param h Image height in pixels.
35 * @param radius Structuring element half-size in pixels.
36 * @return Eroded binary float vector of length w * h.
37 */
38[[nodiscard]] MAYAFLUX_API std::vector<float> erode(
39 std::span<const float> mask, uint32_t w, uint32_t h, uint32_t radius);
40
41/** @brief Erosion writing into @p dst. Same contract as the returning overload. */
42MAYAFLUX_API void erode(
43 std::span<const float> mask, std::span<float> dst,
44 uint32_t w, uint32_t h, uint32_t radius);
45
46/**
47 * @brief Morphological dilation with square structuring element.
48 *
49 * A pixel is 1.0f if any pixel within radius is foreground.
50 *
51 * @param mask Single-channel float span, size must be w * h.
52 * @param w Image width in pixels.
53 * @param h Image height in pixels.
54 * @param radius Structuring element half-size in pixels.
55 * @return Dilated binary float vector of length w * h.
56 */
57[[nodiscard]] MAYAFLUX_API std::vector<float> dilate(
58 std::span<const float> mask, uint32_t w, uint32_t h, uint32_t radius);
59
60/** @brief Dilation writing into @p dst. */
61MAYAFLUX_API void dilate(
62 std::span<const float> mask, std::span<float> dst,
63 uint32_t w, uint32_t h, uint32_t radius);
64
65// ============================================================================
66// Compound operations
67// ============================================================================
68
69/**
70 * @brief Morphological opening: erode then dilate.
71 *
72 * Removes small foreground regions and smooths boundaries.
73 *
74 * @param mask Single-channel float span, size must be w * h.
75 * @param w Image width in pixels.
76 * @param h Image height in pixels.
77 * @param radius Structuring element half-size in pixels.
78 * @return Opened binary float vector of length w * h.
79 */
80[[nodiscard]] MAYAFLUX_API std::vector<float> open(
81 std::span<const float> mask, uint32_t w, uint32_t h, uint32_t radius);
82
83/**
84 * @brief Morphological opening writing into @p dst.
85 *
86 * @p tmp is used for the intermediate erode result. Size >= w * h, must not
87 * alias @p mask or @p dst.
88 */
89MAYAFLUX_API void open(
90 std::span<const float> mask, std::span<float> tmp, std::span<float> dst,
91 uint32_t w, uint32_t h, uint32_t radius);
92
93/**
94 * @brief Morphological closing: dilate then erode.
95 *
96 * Fills small holes and gaps in foreground regions.
97 *
98 * @param mask Single-channel float span, size must be w * h.
99 * @param w Image width in pixels.
100 * @param h Image height in pixels.
101 * @param radius Structuring element half-size in pixels.
102 * @return Closed binary float vector of length w * h.
103 */
104[[nodiscard]] MAYAFLUX_API std::vector<float> close(
105 std::span<const float> mask, uint32_t w, uint32_t h, uint32_t radius);
106
107/** @brief Morphological closing writing into @p dst. @p tmp same contract as open. */
108MAYAFLUX_API void close(
109 std::span<const float> mask, std::span<float> tmp, std::span<float> dst,
110 uint32_t w, uint32_t h, uint32_t radius);
111
112/**
113 * @brief Morphological gradient: dilate minus erode.
114 *
115 * Produces the outline of foreground regions.
116 *
117 * @param mask Single-channel float span, size must be w * h.
118 * @param w Image width in pixels.
119 * @param h Image height in pixels.
120 * @param radius Structuring element half-size in pixels.
121 * @return Binary float vector of length w * h.
122 */
123[[nodiscard]] MAYAFLUX_API std::vector<float> morph_gradient(
124 std::span<const float> mask, uint32_t w, uint32_t h, uint32_t radius);
125
126/**
127 * @brief Morphological gradient (dilate - erode) writing into @p dst.
128 *
129 * @p tmp used for the dilate intermediate. Eigen subtraction for final step.
130 */
131MAYAFLUX_API void morph_gradient(
132 std::span<const float> mask, std::span<float> tmp, std::span<float> dst,
133 uint32_t w, uint32_t h, uint32_t radius);
134
135} // namespace MayaFlux::Kinesis::Vision
uint32_t h
Definition InkPress.cpp:28
uint32_t radius
void erode(std::span< const float > mask, std::span< float > dst, uint32_t w, uint32_t h, uint32_t radius)
Erosion writing into dst.
void close(std::span< const float > mask, std::span< float > tmp, std::span< float > dst, uint32_t w, uint32_t h, uint32_t radius)
Morphological closing writing into dst.
void dilate(std::span< const float > mask, std::span< float > dst, uint32_t w, uint32_t h, uint32_t radius)
Dilation writing into dst.
void morph_gradient(std::span< const float > mask, std::span< float > tmp, std::span< float > dst, uint32_t w, uint32_t h, uint32_t radius)
Morphological gradient (dilate - erode) writing into dst.
void open(std::span< const float > mask, std::span< float > tmp, std::span< float > dst, uint32_t w, uint32_t h, uint32_t radius)
Morphological opening writing into dst.