MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Contours.hpp
Go to the documentation of this file.
1#pragma once
2
4
5/**
6 * @file Contours.hpp
7 * @brief Contour extraction from binary float masks.
8 *
9 * Pure functions. No MayaFlux type dependencies.
10 *
11 * ## Conventions
12 * - Input masks are single-channel float where >= 0.5f is foreground
13 * - Contour points are in normalised image coordinates [0, 1]
14 * - 8-connectivity is used for contour tracing
15 * - Area is pixel coverage normalised by total image area (w * h)
16 * - Perimeter is measured in pixels
17 */
18
20
21/**
22 * @brief Extract outer and hole contours from a binary mask.
23 *
24 * Labels 8-connected foreground components and traces their outer boundaries
25 * with Moore neighborhood following. Enclosed 4-connected background regions
26 * are traced as holes, with parent_label naming their enclosing foreground
27 * component. Background connected to an image edge is excluded.
28 * Outer contours use Contour::no_parent (0xFFFFFFFF) as their parent_label.
29 * Each contour closes from its last point to its first. Closure follows the
30 * starting directed edge so touching branches may revisit the start pixel.
31 *
32 * Area is (absolute pixel polygon area + half the pixel perimeter + 1) divided
33 * by w * h. Perimeter is the closed polygon length in pixels.
34 *
35 * Contours with fewer than 3 points are discarded.
36 * Contours whose normalised area is below min_area are discarded after tracing.
37 * A nonzero max_contours sorts by descending area and retains at most that
38 * many contours. Outer boundaries and holes share this limit.
39 * A nonzero max_points_per_contour stops each trace at that many points;
40 * measurements use the retained polygon, including its closing edge.
41 *
42 * @param mask Single-channel float span, size must be w * h.
43 * @param w Image width in pixels.
44 * @param h Image height in pixels.
45 * @param min_area Minimum normalised area [0,1] to retain. Default 0 (no filter).
46 * @param max_contours Maximum number of contours to return. 0 means no limit.
47 * @param max_points_per_contour Maximum trace length. 0 means no caller limit.
48 * @return Contours with normalised points and area, and pixel perimeter.
49 */
50[[nodiscard]] MAYAFLUX_API std::vector<Contour> find_contours(
51 std::span<const float> mask, uint32_t w, uint32_t h,
52 float min_area = 0.0F, uint32_t max_contours = 0,
53 uint32_t max_points_per_contour = 0);
54
55/**
56 * @brief Extract contours from an existing CPU component labeling.
57 *
58 * Applies the same tracing, filtering, and measurement rules as mask-based
59 * extraction without repeating connected-component labeling.
60 *
61 * @param components CPU labeling with a complete w * h label_map and compact
62 * foreground labels in 1..count; zero labels are background.
63 * @param w Image width in pixels.
64 * @param h Image height in pixels.
65 * @param min_area Minimum normalised area to retain.
66 * @param max_contours Maximum number of contours to return; zero is unlimited.
67 * @param max_points_per_contour Maximum trace length; zero is unlimited.
68 * @return Outer and hole contours with normalized points and pixel perimeter.
69 */
70[[nodiscard]] MAYAFLUX_API std::vector<Contour> find_contours(
71 const ComponentResult& components, uint32_t w, uint32_t h,
72 float min_area = 0.0F, uint32_t max_contours = 0,
73 uint32_t max_points_per_contour = 0);
74
75/**
76 * @brief Zero all pixels in @p pixels that fall outside @p contour.
77 *
78 * For each pixel in the buffer, reconstructs its normalised image-space
79 * position from its row/column index, the crop origin (@p origin_x,
80 * @p origin_y), and the pixel dimensions. Tests containment using the
81 * winding number algorithm against the contour's point polygon. Pixels
82 * outside the polygon have all channel values set to 0.
83 *
84 * The buffer is modified in place. Pixels inside the polygon are untouched.
85 *
86 * @param pixels Interleaved float buffer, size must be w * h * channels.
87 * @param w Buffer width in pixels.
88 * @param h Buffer height in pixels.
89 * @param channels Channels per pixel.
90 * @param contour Contour whose polygon defines the keep region.
91 * @param origin_x Normalised x coordinate of the buffer's left edge.
92 * @param origin_y Normalised y coordinate of the buffer's top edge.
93 * @param scale_x Normalised width covered by one pixel column.
94 * @param scale_y Normalised height covered by one pixel row.
95 */
96MAYAFLUX_API void apply_contour_mask(
97 std::span<float> pixels,
98 uint32_t w, uint32_t h,
99 uint32_t channels,
100 const Contour& contour,
101 float origin_x, float origin_y,
102 float scale_x, float scale_y);
103
104} // namespace MayaFlux::Kinesis::Vision
Connected component labelling on normalised float masks.
const std::vector< float > * pixels
Definition Decoder.cpp:48
uint32_t h
Definition InkPress.cpp:29
uint32_t channels
std::vector< Contour > find_contours(std::span< const float > mask, uint32_t w, uint32_t h, float min_area, uint32_t max_contours, uint32_t max_points_per_contour)
Extract outer and hole contours from a binary mask.
Definition Contours.cpp:111
void apply_contour_mask(std::span< float > pixels, uint32_t w, uint32_t h, uint32_t channels, const Contour &contour, float origin_x, float origin_y, float scale_x, float scale_y)
Zero all pixels in pixels that fall outside contour.
Definition Contours.cpp:226