#include "synth/mask_stabilizer.h" #include #include #include namespace nui::synth { namespace { constexpr float kMaskThreshold = 0.5f; bool same_size(ImageSize first, ImageSize second) { return first.width == second.width && first.height == second.height; } BinaryMask threshold_and_resize( const SegmentationMask& source, ImageSize target) { BinaryMask result{target, std::vector(pixel_count(target), 0)}; if (source.size.width == 0 || source.size.height == 0 || source.confidence.size() != pixel_count(source.size) || target.width == 0 || target.height == 0) { return result; } for (uint32_t y = 0; y < target.height; ++y) { const uint32_t source_y = std::min( static_cast( static_cast(y) * source.size.height / target.height), source.size.height - 1); for (uint32_t x = 0; x < target.width; ++x) { const uint32_t source_x = std::min( static_cast( static_cast(x) * source.size.width / target.width), source.size.width - 1); result.pixels[static_cast(y) * target.width + x] = source.confidence[ static_cast(source_y) * source.size.width + source_x] > kMaskThreshold; } } return result; } BinaryMask morph(const BinaryMask& source, bool dilate) { BinaryMask result{ source.size, std::vector(source.pixels.size(), 0)}; for (uint32_t y = 0; y < source.size.height; ++y) { for (uint32_t x = 0; x < source.size.width; ++x) { bool value = !dilate; for (int offset_y = -1; offset_y <= 1; ++offset_y) { for (int offset_x = -1; offset_x <= 1; ++offset_x) { const int neighbor_x = static_cast(x) + offset_x; const int neighbor_y = static_cast(y) + offset_y; const bool in_bounds = neighbor_x >= 0 && neighbor_y >= 0 && neighbor_x < static_cast(source.size.width) && neighbor_y < static_cast(source.size.height); const bool neighbor = in_bounds ? source.pixels[ static_cast(neighbor_y) * source.size.width + static_cast(neighbor_x)] != 0 : !dilate; value = dilate ? value || neighbor : value && neighbor; } } result.pixels[static_cast(y) * source.size.width + x] = value; } } return result; } BinaryMask largest_component(const BinaryMask& source) { BinaryMask result{ source.size, std::vector(source.pixels.size(), 0)}; std::vector visited(source.pixels.size(), 0); std::vector largest; for (std::size_t start = 0; start < source.pixels.size(); ++start) { if (source.pixels[start] == 0 || visited[start] != 0) { continue; } std::vector component; std::queue pending; pending.push(start); visited[start] = 1; while (!pending.empty()) { const std::size_t current = pending.front(); pending.pop(); component.push_back(current); const int x = static_cast(current % source.size.width); const int y = static_cast(current / source.size.width); for (int offset_y = -1; offset_y <= 1; ++offset_y) { for (int offset_x = -1; offset_x <= 1; ++offset_x) { const int neighbor_x = x + offset_x; const int neighbor_y = y + offset_y; if (neighbor_x < 0 || neighbor_y < 0 || neighbor_x >= static_cast(source.size.width) || neighbor_y >= static_cast(source.size.height)) { continue; } const std::size_t neighbor = static_cast(neighbor_y) * source.size.width + static_cast(neighbor_x); if (source.pixels[neighbor] != 0 && visited[neighbor] == 0) { visited[neighbor] = 1; pending.push(neighbor); } } } } if (component.size() > largest.size()) { largest = std::move(component); } } for (const std::size_t index : largest) { result.pixels[index] = 1; } return result; } } // namespace std::size_t pixel_count(ImageSize size) { return static_cast(size.width) * size.height; } bool has_body(const BinaryMask& mask) { return std::any_of( mask.pixels.begin(), mask.pixels.end(), [](uint8_t pixel) { return pixel != 0; }); } MaskStabilizer::MaskStabilizer(std::size_t temporal_hold_frames) : temporal_hold_frames_(temporal_hold_frames) {} BinaryMask MaskStabilizer::update( const SegmentationMask& mask, ImageSize requested_size) { const auto closed = morph(morph(threshold_and_resize(mask, requested_size), true), false); auto cleaned = largest_component(closed); if (has_body(cleaned)) { last_stable_ = cleaned; hold_frames_remaining_ = temporal_hold_frames_; return cleaned; } if (hold_frames_remaining_ > 0 && same_size(last_stable_.size, requested_size)) { --hold_frames_remaining_; return last_stable_; } return cleaned; } } // namespace nui::synth