161 lines
5.2 KiB
C++
161 lines
5.2 KiB
C++
#include "synth/mask_stabilizer.h"
|
|
|
|
#include <algorithm>
|
|
#include <queue>
|
|
#include <utility>
|
|
|
|
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<uint8_t>(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<uint32_t>(
|
|
static_cast<uint64_t>(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<uint32_t>(
|
|
static_cast<uint64_t>(x) * source.size.width / target.width),
|
|
source.size.width - 1);
|
|
result.pixels[static_cast<std::size_t>(y) * target.width + x] =
|
|
source.confidence[
|
|
static_cast<std::size_t>(source_y) * source.size.width +
|
|
source_x] > kMaskThreshold;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
BinaryMask morph(const BinaryMask& source, bool dilate) {
|
|
BinaryMask result{
|
|
source.size, std::vector<uint8_t>(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<int>(x) + offset_x;
|
|
const int neighbor_y = static_cast<int>(y) + offset_y;
|
|
const bool in_bounds =
|
|
neighbor_x >= 0 && neighbor_y >= 0 &&
|
|
neighbor_x < static_cast<int>(source.size.width) &&
|
|
neighbor_y < static_cast<int>(source.size.height);
|
|
const bool neighbor =
|
|
in_bounds
|
|
? source.pixels[
|
|
static_cast<std::size_t>(neighbor_y) *
|
|
source.size.width +
|
|
static_cast<std::size_t>(neighbor_x)] != 0
|
|
: !dilate;
|
|
value = dilate ? value || neighbor : value && neighbor;
|
|
}
|
|
}
|
|
result.pixels[static_cast<std::size_t>(y) * source.size.width + x] =
|
|
value;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
BinaryMask largest_component(const BinaryMask& source) {
|
|
BinaryMask result{
|
|
source.size, std::vector<uint8_t>(source.pixels.size(), 0)};
|
|
std::vector<uint8_t> visited(source.pixels.size(), 0);
|
|
std::vector<std::size_t> largest;
|
|
|
|
for (std::size_t start = 0; start < source.pixels.size(); ++start) {
|
|
if (source.pixels[start] == 0 || visited[start] != 0) {
|
|
continue;
|
|
}
|
|
std::vector<std::size_t> component;
|
|
std::queue<std::size_t> 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<int>(current % source.size.width);
|
|
const int y = static_cast<int>(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<int>(source.size.width) ||
|
|
neighbor_y >= static_cast<int>(source.size.height)) {
|
|
continue;
|
|
}
|
|
const std::size_t neighbor =
|
|
static_cast<std::size_t>(neighbor_y) * source.size.width +
|
|
static_cast<std::size_t>(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<std::size_t>(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
|