MayaFlux 0.5.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Contours.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "Features.hpp"
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 normalised to [0, 1] relative to total image area (w * h)
16 * - Perimeter is normalised to [0, 1] relative to image diagonal
17 */
18
20
21/**
22 * @brief Extract outer contours from a binary mask.
23 *
24 * Uses the Suzuki-Abe border following algorithm (single pass).
25 * Only outer contours are extracted; holes are not traced.
26 * Each contour is a closed polygon; the last point connects back
27 * to the first.
28 *
29 * Contours with fewer than 3 points are discarded.
30 * Contours whose normalised area is below min_area are discarded after tracing.
31 * If max_contours is non-zero, only the largest max_contours contours by area
32 * are returned; excess contours are dropped before the result is returned.
33 *
34 * @param mask Single-channel float span, size must be w * h.
35 * @param w Image width in pixels.
36 * @param h Image height in pixels.
37 * @param min_area Minimum normalised area [0,1] to retain. Default 0 (no filter).
38 * @param max_contours Maximum number of contours to return. 0 means no limit.
39 * @return Vector of Contour, each with normalised points, area, and perimeter.
40 */
41[[nodiscard]] MAYAFLUX_API std::vector<Contour> find_contours(
42 std::span<const float> mask, uint32_t w, uint32_t h,
43 float min_area = 0.0F, uint32_t max_contours = 0);
44
45/**
46 * @brief Zero all pixels in @p pixels that fall outside @p contour.
47 *
48 * For each pixel in the buffer, reconstructs its normalised image-space
49 * position from its row/column index, the crop origin (@p origin_x,
50 * @p origin_y), and the pixel dimensions. Tests containment using the
51 * winding number algorithm against the contour's point polygon. Pixels
52 * outside the polygon have all channel values set to 0.
53 *
54 * The buffer is modified in place. Pixels inside the polygon are untouched.
55 *
56 * @param pixels Interleaved float buffer, size must be w * h * channels.
57 * @param w Buffer width in pixels.
58 * @param h Buffer height in pixels.
59 * @param channels Channels per pixel.
60 * @param contour Contour whose polygon defines the keep region.
61 * @param origin_x Normalised x coordinate of the buffer's left edge.
62 * @param origin_y Normalised y coordinate of the buffer's top edge.
63 * @param scale_x Normalised width covered by one pixel column.
64 * @param scale_y Normalised height covered by one pixel row.
65 */
66MAYAFLUX_API void apply_contour_mask(
67 std::span<float> pixels,
68 uint32_t w, uint32_t h,
69 uint32_t channels,
70 const Contour& contour,
71 float origin_x, float origin_y,
72 float scale_x, float scale_y);
73
74} // namespace MayaFlux::Kinesis::Vision
const std::vector< float > * pixels
Definition Decoder.cpp:65
uint32_t h
Definition InkPress.cpp:28
float min_area
uint32_t max_contours
std::vector< Contour > find_contours(std::span< const float > mask, uint32_t w, uint32_t h, float min_area, uint32_t max_contours)
Extract outer contours from a binary mask.
Definition Contours.cpp:70
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:211