MayaFlux 0.5.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Gradient.hpp
Go to the documentation of this file.
1#pragma once
2
3/**
4 * @file Gradient.hpp
5 * @brief Gradient and edge detection on normalised float image spans.
6 *
7 * Pure functions. No MayaFlux type dependencies.
8 *
9 * ## Conventions
10 * - All inputs are single-channel normalised float in [0, 1]
11 * - w and h are pixel dimensions; caller ensures span size == w * h
12 * - Border handling is clamp-to-edge throughout
13 * - Gradient angle is in radians in [-pi, pi], measured from +X axis
14 * - Parallelism handled internally via Parallel::par_unseq
15 */
16
18
19/**
20 * @brief Gradient maps produced by Sobel and Scharr operators.
21 */
23 std::vector<float> dx; ///< Horizontal gradient component.
24 std::vector<float> dy; ///< Vertical gradient component.
25 std::vector<float> magnitude; ///< Gradient magnitude, normalised to [0, 1].
26 std::vector<float> angle; ///< Gradient angle in radians, [-pi, pi].
27};
28
29// ============================================================================
30// Gradient operators
31// ============================================================================
32
33/**
34 * @brief Sobel gradient operator.
35 *
36 * 3x3 Sobel kernels applied separably. dx and dy are in [-1, 1].
37 * Magnitude is normalised by the theoretical maximum (4 * 255 / 255 = 4
38 * for uint8 sources, but since input is already [0,1] the max magnitude
39 * is sqrt(2) which is divided out). Angle via atan2(dy, dx).
40 *
41 * @param gray Single-channel float span, size must be w * h.
42 * @param w Image width in pixels.
43 * @param h Image height in pixels.
44 * @return GradientResult with all four maps populated.
45 */
46[[nodiscard]] MAYAFLUX_API GradientResult sobel(
47 std::span<const float> gray, uint32_t w, uint32_t h);
48
49/**
50 * @brief Sobel gradient writing dx and dy into caller-supplied buffers.
51 *
52 * Does not compute magnitude or angle. @p tmp is used as the horizontal
53 * filter pass scratch. All buffers must be size >= w * h and must not alias.
54 *
55 * @param gray Input span, size w * h.
56 * @param dx Output horizontal gradient, size >= w * h.
57 * @param dy Output vertical gradient, size >= w * h.
58 * @param tmp Scratch span for separable filter pass, size >= w * h.
59 * @param w Image width in pixels.
60 * @param h Image height in pixels.
61 */
62MAYAFLUX_API void sobel(
63 std::span<const float> gray,
64 std::span<float> dx,
65 std::span<float> dy,
66 std::span<float> tmp,
67 uint32_t w, uint32_t h);
68
69/**
70 * @brief Scharr gradient operator.
71 *
72 * 3x3 Scharr kernels. Better rotational symmetry than Sobel.
73 * Same output contract as sobel().
74 *
75 * @param gray Single-channel float span, size must be w * h.
76 * @param w Image width in pixels.
77 * @param h Image height in pixels.
78 * @return GradientResult with all four maps populated.
79 */
80[[nodiscard]] MAYAFLUX_API GradientResult scharr(
81 std::span<const float> gray, uint32_t w, uint32_t h);
82
83/**
84 * @brief Scharr gradient writing dx and dy into caller-supplied buffers.
85 *
86 * Same contract as the void sobel overload.
87 */
88MAYAFLUX_API void scharr(
89 std::span<const float> gray,
90 std::span<float> dx,
91 std::span<float> dy,
92 std::span<float> tmp,
93 uint32_t w, uint32_t h);
94
95// ============================================================================
96// Edge detection
97// ============================================================================
98
99/**
100 * @brief Canny edge detector.
101 *
102 * Pipeline: gaussian_blur(sigma) -> sobel -> non-maximum suppression ->
103 * double threshold -> hysteresis. Output is binary: 1.0f on edges, 0.0f
104 * elsewhere.
105 *
106 * @param gray Single-channel float span, size must be w * h.
107 * @param w Image width in pixels.
108 * @param h Image height in pixels.
109 * @param sigma Gaussian pre-blur sigma. 0.0f skips blur.
110 * @param low_threshold Hysteresis low threshold in [0, 1].
111 * @param high_threshold Hysteresis high threshold in [0, 1].
112 * @return Binary float vector of length w * h.
113 */
114[[nodiscard]] MAYAFLUX_API std::vector<float> canny(
115 std::span<const float> gray, uint32_t w, uint32_t h,
116 float sigma, float low_threshold, float high_threshold);
117
118/**
119 * @brief Canny edge detector writing into a caller-supplied buffer.
120 *
121 * @param gray Input span, size w * h.
122 * @param dst Output span, size >= w * h. Binary: 1.0f on edges.
123 * @param w Image width in pixels.
124 * @param h Image height in pixels.
125 * @param sigma Gaussian pre-blur sigma. 0.0f skips blur.
126 * @param low_threshold Hysteresis low threshold in [0, 1].
127 * @param high_threshold Hysteresis high threshold in [0, 1].
128 */
129MAYAFLUX_API void canny(
130 std::span<const float> gray,
131 std::span<float> dst,
132 uint32_t w, uint32_t h,
133 float sigma, float low_threshold, float high_threshold);
134
135} // namespace MayaFlux::Kinesis::Vision
uint32_t h
Definition InkPress.cpp:28
float sigma
std::vector< float > canny(std::span< const float > gray, uint32_t w, uint32_t h, float sigma, float low_threshold, float high_threshold)
Canny edge detector.
Definition Gradient.cpp:132
void sobel(std::span< const float > gray, std::span< float > dx, std::span< float > dy, std::span< float > tmp, uint32_t w, uint32_t h)
Sobel gradient writing dx and dy into caller-supplied buffers.
Definition Gradient.cpp:66
void scharr(std::span< const float > gray, std::span< float > dx, std::span< float > dy, std::span< float > tmp, uint32_t w, uint32_t h)
Scharr gradient writing dx and dy into caller-supplied buffers.
Definition Gradient.cpp:99
std::vector< float > angle
Gradient angle in radians, [-pi, pi].
Definition Gradient.hpp:26
std::vector< float > magnitude
Gradient magnitude, normalised to [0, 1].
Definition Gradient.hpp:25
std::vector< float > dx
Horizontal gradient component.
Definition Gradient.hpp:23
std::vector< float > dy
Vertical gradient component.
Definition Gradient.hpp:24
Gradient maps produced by Sobel and Scharr operators.
Definition Gradient.hpp:22