src/CoreGeometry/include/CoreGeometry/masks.hpp
src/CoreGeometry/include/CoreGeometry/masks.hpp
Classes
| Name | |
|---|---|
| class | Mask2D |
Functions
| Name | |
|---|---|
| std::pair< Point2D< uint32_t >, Point2D< uint32_t > > | get_bounding_box(Mask2D const & mask) |
| std::vector< Point2D< uint32_t > > | get_mask_outline(Mask2D const & mask) Compute the outline of a mask by finding extremal points. |
| std::vector< Point2D< uint32_t > > | generate_ellipse_pixels(float center_x, float center_y, float radius_x, float radius_y) Generate mask pixels within an elliptical region. |
| std::vector< Point2D< uint32_t > > | generate_rotated_ellipse_pixels(float center_x, float center_y, float semi_major, float semi_minor, float angle_rad) Generate mask pixels within a rotated elliptical region. |
| std::vector< Point2D< uint32_t > > | generate_rectangle_pixels(float center_x, float center_y, float width, float height) Generate mask pixels within a rectangular region. |
| std::vector< Point2D< uint32_t > > | clipPixelsToImage(std::vector< Point2D< uint32_t > > pixels, int image_width, int image_height) Remove pixels that fall outside image bounds. |
| Mask2D | combine_masks(Mask2D const & mask1, Mask2D const & mask2) Combine two masks using union operation (no duplicate pixels). |
| Mask2D | subtract_masks(Mask2D const & mask1, Mask2D const & mask2) Subtract mask2 from mask1 (remove intersecting pixels). |
| Mask2D | generate_outline_mask(Mask2D const & mask, int thickness =1, uint32_t image_width =UINT32_MAX, uint32_t image_height =UINT32_MAX) Generate an outline mask from an existing mask. |
| std::vector< Point2D< uint32_t > > | extract_line_pixels(std::vector< uint8_t > const & binary_img, ImageSize const image_size) |
Functions Documentation
function get_bounding_box
std::pair< Point2D< uint32_t >, Point2D< uint32_t > > get_bounding_box(
Mask2D const & mask
)function get_mask_outline
std::vector< Point2D< uint32_t > > get_mask_outline(
Mask2D const & mask
)Compute the outline of a mask by finding extremal points.
Parameters:
- mask The input mask (vector of Point2D)
Return: Line2D containing the outline points
Note: If mask is empty or has only one point, returns empty outline
This function creates an outline by finding:
- For each unique x coordinate, the maximum y value
- For each unique y coordinate, the maximum x value These extremal points are then connected to form the boundary outline.
function generate_ellipse_pixels
std::vector< Point2D< uint32_t > > generate_ellipse_pixels(
float center_x,
float center_y,
float radius_x,
float radius_y
)Generate mask pixels within an elliptical region.
Parameters:
- center_x X coordinate of the ellipse center
- center_y Y coordinate of the ellipse center
- radius_x Radius in the X direction
- radius_y Radius in the Y direction
Return: Vector of Point2D containing all pixels within the ellipse
Note:
- Only returns pixels with non-negative coordinates
- Uses the standard ellipse equation: (dx/radius_x)² + (dy/radius_y)² ≤ 1
Generates all pixels that fall within an elliptical region centered at the given point. When radius_x == radius_y, this creates a perfect circle. Input coordinates are rounded to nearest integers for pixel precision.
function generate_rotated_ellipse_pixels
std::vector< Point2D< uint32_t > > generate_rotated_ellipse_pixels(
float center_x,
float center_y,
float semi_major,
float semi_minor,
float angle_rad
)Generate mask pixels within a rotated elliptical region.
Parameters:
- center_x X coordinate of the ellipse center
- center_y Y coordinate of the ellipse center
- semi_major Semi-major axis length
- semi_minor Semi-minor axis length
- angle_rad Rotation angle in radians
Return: Vector of Point2D containing all pixels within the rotated ellipse
Note: Only returns pixels with non-negative coordinates
Generates all pixels within an ellipse defined by center, semi-axes, and rotation angle. Uses bounding-box scanning with inverse rotation to test pixel membership.
function generate_rectangle_pixels
std::vector< Point2D< uint32_t > > generate_rectangle_pixels(
float center_x,
float center_y,
float width,
float height
)Generate mask pixels within a rectangular region.
Parameters:
- center_x X coordinate of the rectangle center
- center_y Y coordinate of the rectangle center
- width Width of the rectangle
- height Height of the rectangle
Return: Vector of Point2D containing all pixels within the rectangle
Note: Only returns pixels with non-negative coordinates
Generates all integer-coordinate pixels within a rectangle defined by center and dimensions.
function clipPixelsToImage
std::vector< Point2D< uint32_t > > clipPixelsToImage(
std::vector< Point2D< uint32_t > > pixels,
int image_width,
int image_height
)Remove pixels that fall outside image bounds.
Parameters:
- pixels The pixels to clip (taken by value for move efficiency)
- image_width Width of the image in pixels
- image_height Height of the image in pixels
Return: Filtered vector containing only in-bounds pixels
Filters a pixel vector to keep only pixels within [0, image_width) x [0, image_height). Since pixel coordinates are unsigned, only the upper bounds need checking.
function combine_masks
Mask2D combine_masks(
Mask2D const & mask1,
Mask2D const & mask2
)Combine two masks using union operation (no duplicate pixels).
Parameters:
- mask1 First mask
- mask2 Second mask to union with the first
Return: New mask containing union of both input masks
Takes two masks and returns a new mask containing all pixels from both masks, with duplicates removed. Pixels are considered duplicates if they have the same rounded integer coordinates.
function subtract_masks
Mask2D subtract_masks(
Mask2D const & mask1,
Mask2D const & mask2
)Subtract mask2 from mask1 (remove intersecting pixels).
Parameters:
- mask1 Base mask to subtract from
- mask2 Mask containing pixels to remove from mask1
Return: New mask with mask2 pixels removed from mask1
Takes two masks and returns a new mask containing only pixels from mask1 that are NOT present in mask2. Pixels are considered equal if they have the same rounded integer coordinates.
function generate_outline_mask
Mask2D generate_outline_mask(
Mask2D const & mask,
int thickness =1,
uint32_t image_width =UINT32_MAX,
uint32_t image_height =UINT32_MAX
)Generate an outline mask from an existing mask.
Parameters:
- mask The input mask to generate outline from
- thickness The thickness of the outline in pixels (default: 1)
- image_width Width of the image bounds for edge detection
- image_height Height of the image bounds for edge detection
Return: New mask containing only the outline pixels
Creates a new mask that represents the outline/border of the input mask. The outline is created by including pixels that are on the edge of the mask (pixels that have at least one neighbor that is not in the original mask).
function extract_line_pixels
std::vector< Point2D< uint32_t > > extract_line_pixels(
std::vector< uint8_t > const & binary_img,
ImageSize const image_size
)Source code
#ifndef DATAMANAGER_MASKS_HPP
#define DATAMANAGER_MASKS_HPP
#include "CoreGeometry/ImageSize.hpp"
#include "CoreGeometry/points.hpp"
#include <cstdint>
#include <vector>
class Mask2D {
public:
Mask2D() = default;
explicit Mask2D(std::vector<Point2D<uint32_t>> points)
: points_(std::move(points)) {}
Mask2D(std::initializer_list<Point2D<uint32_t>> points)
: points_(points) {}
Mask2D(std::vector<uint32_t> const & x, std::vector<uint32_t> const & y);
Mask2D(std::vector<float> const & x, std::vector<float> const & y);
size_t size() const {
return points_.size();
}
Point2D<uint32_t> front() const {
return points_.front();
}
bool empty() const {
return points_.empty();
}
void push_back(Point2D<uint32_t> const & point) {
points_.push_back(point);
}
void reserve(size_t capacity) {
points_.reserve(capacity);
}
Point2D<uint32_t> back() const {
return points_.back();
}
Point2D<uint32_t> operator[](size_t index) const {
return points_[index];
}
std::vector<Point2D<uint32_t>>::iterator begin() {
return points_.begin();
}
std::vector<Point2D<uint32_t>>::iterator end() {
return points_.end();
}
std::vector<Point2D<uint32_t>>::const_iterator begin() const {
return points_.begin();
}
std::vector<Point2D<uint32_t>>::const_iterator end() const {
return points_.end();
}
void erase(std::vector<Point2D<uint32_t>>::iterator it) {
points_.erase(it);
}
void erase(std::vector<Point2D<uint32_t>>::iterator first, std::vector<Point2D<uint32_t>>::iterator last) {
points_.erase(first, last);
}
std::vector<Point2D<uint32_t>> const & points() const {
return points_;
}
private:
std::vector<Point2D<uint32_t>> points_;
};
std::pair<Point2D<uint32_t>, Point2D<uint32_t>> get_bounding_box(Mask2D const & mask);
std::vector<Point2D<uint32_t>> get_mask_outline(Mask2D const & mask);
std::vector<Point2D<uint32_t>> generate_ellipse_pixels(float center_x, float center_y, float radius_x, float radius_y);
std::vector<Point2D<uint32_t>> generate_rotated_ellipse_pixels(
float center_x, float center_y,
float semi_major, float semi_minor,
float angle_rad);
std::vector<Point2D<uint32_t>> generate_rectangle_pixels(
float center_x, float center_y,
float width, float height);
std::vector<Point2D<uint32_t>> clipPixelsToImage(
std::vector<Point2D<uint32_t>> pixels,
int image_width,
int image_height);
Mask2D combine_masks(Mask2D const & mask1, Mask2D const & mask2);
Mask2D subtract_masks(Mask2D const & mask1, Mask2D const & mask2);
Mask2D generate_outline_mask(Mask2D const & mask, int thickness = 1, uint32_t image_width = UINT32_MAX, uint32_t image_height = UINT32_MAX);
std::vector<Point2D<uint32_t>> extract_line_pixels(
std::vector<uint8_t> const & binary_img,
ImageSize const image_size);
#endif// DATAMANAGER_MASKS_HPPUpdated on 2026-11-13 at 20:25:42 +0000