feat(synth): articulated depth + player-index + color/IR rasterizer (wave 4; todo 7 done)
ci / Linux C++ core (cmake + ctest) (push) Canceled after 0s
ci / Python tracker (pytest) (push) Canceled after 0s

This commit is contained in:
kinect-bot committed 2026-11-25 04:33:33 -05:00
1 parent 0df2702c24
commit 3fef71b020
7 files changed
+602 -6

No files matched your search

+1 -1
View File
@@ -176,7 +176,7 @@ Your next move: run the default-on high-accuracy review (in progress), then exec
Commit: Y | feat(mapping): nui intrinsics, 33->20 map, holt smoothing
Recommended task executor category: deep-low - math-heavy but fully settleable from the cited contracts.
- [ ] 7. Synthetic depth + player-index + color rasterizer
- [x] 7. Synthetic depth + player-index + color rasterizer
What to do / Must NOT do: In `core/synth/`, turn the tracker's mask + 3D pose (via todo 6) into Kinect image frames: a packed depth buffer (`(mm&0x1FFF)<<3 | player&0x7`) at the resolution the host requests, body pixels = articulated metric depth (bone capsules at their z, silhouette-masked; billboard-gradient default per `.omo/design/nui-contracts.md` §2.3), background = 0; player-index = skeletonSlot+1 on body pixels; color = webcam passthrough (BGRA at requested res); optional IR. Largest-connected-component + morphological close + temporal hold for a stable blob. Must NOT: hardcode 640x480 - synthesize at the requested `eResolution`; leave body depth at 0 or out of ~[800,4000]mm (reads as no player).
Closes: GAP-2 (synthesis half)
Parallelization: Wave 4 | Blocked by: 5,6 (soft-gated by the crux test - spike 3 if NiTE2 available, else spike 4 Windows MS NUI: if synthetic depth is NOT skeletonizable, HALT and re-plan the depth model before building this todo) | Blocks: 8,14,15
+2
View File
@@ -13,6 +13,7 @@ set(CMAKE_CXX_EXTENSIONS OFF)
add_library(libnui_core STATIC
core/contract/contract.cpp
core/mapping/mapping.cpp
core/synth/mask_stabilizer.cpp
core/synth/synth.cpp
)
# Keep the on-disk name libnui_core.a (not liblibnui_core.a).
@@ -48,6 +49,7 @@ if(NUI_CORE_BUILD_TESTS)
tests/test_core.cpp
tests/test_contract.cpp
tests/test_mapping.cpp
tests/test_synth.cpp
)
target_link_libraries(libnui_core_tests PRIVATE libnui_core GTest::gtest_main)
+160
View File
@@ -0,0 +1,160 @@
#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
+51
View File
@@ -0,0 +1,51 @@
#ifndef NUI_CORE_SYNTH_MASK_STABILIZER_H
#define NUI_CORE_SYNTH_MASK_STABILIZER_H
#include <cstddef>
#include <cstdint>
#include <vector>
// Segmentation-mask cleanup: resize-to-requested-resolution, morphological
// close, largest-connected-component, and a short temporal hold so a
// fragmented/flickering mask still yields one stable human-shaped blob
// (nui-contracts.md §2.3, §2.5).
namespace nui::synth {
struct ImageSize {
uint32_t width;
uint32_t height;
};
struct SegmentationMask {
ImageSize size;
std::vector<float> confidence;
};
struct BinaryMask {
ImageSize size;
std::vector<uint8_t> pixels;
};
std::size_t pixel_count(ImageSize size);
bool has_body(const BinaryMask& mask);
class MaskStabilizer {
public:
explicit MaskStabilizer(std::size_t temporal_hold_frames = 2);
// Thresholds and resizes `mask` to `requested_size`, applies a
// morphological close + largest-connected-component, and, if that yields
// no body, holds the last stable mask for up to `temporal_hold_frames`
// frames before falling through to background.
BinaryMask update(const SegmentationMask& mask, ImageSize requested_size);
private:
std::size_t temporal_hold_frames_;
std::size_t hold_frames_remaining_ = 0;
BinaryMask last_stable_{};
};
} // namespace nui::synth
#endif // NUI_CORE_SYNTH_MASK_STABILIZER_H
+158 -1
View File
@@ -1,9 +1,166 @@
#include "synth/synth.h"
#include <algorithm>
#include <array>
#include <cmath>
#include "mapping/mapping.h"
namespace nui::synth {
namespace {
constexpr uint16_t kMinBodyDepthMm = 800;
constexpr uint16_t kMaxBodyDepthMm = 4000;
struct ProjectedJoint {
float x;
float y;
float depth_mm;
};
std::array<ProjectedJoint, contract::kJointCount> project_joints(
const contract::SkeletonFrame& skeleton,
ImageSize size) {
std::array<ProjectedJoint, contract::kJointCount> projected{};
for (std::size_t index = 0; index < projected.size(); ++index) {
const auto point = mapping::skeleton_to_depth_image(
skeleton.joints[index].position,
static_cast<int>(size.width),
static_cast<int>(size.height));
projected[index] = {
static_cast<float>(std::min(point.x, static_cast<int>(size.width) - 1)),
static_cast<float>(std::min(point.y, static_cast<int>(size.height) - 1)),
static_cast<float>(std::clamp(
NuiDepthPixelToDepth(point.packed_depth),
kMinBodyDepthMm,
kMaxBodyDepthMm)),
};
}
return projected;
}
uint16_t interpolated_depth(
uint32_t x,
uint32_t y,
const std::array<ProjectedJoint, contract::kJointCount>& joints) {
float weighted_depth = 0.0f;
float total_weight = 0.0f;
for (const auto& joint : joints) {
const float delta_x = static_cast<float>(x) - joint.x;
const float delta_y = static_cast<float>(y) - joint.y;
const float weight = 1.0f / (delta_x * delta_x + delta_y * delta_y + 1.0f);
weighted_depth += joint.depth_mm * weight;
total_weight += weight;
}
return static_cast<uint16_t>(std::clamp(
static_cast<int>(std::lround(weighted_depth / total_weight)),
static_cast<int>(kMinBodyDepthMm),
static_cast<int>(kMaxBodyDepthMm)));
}
} // namespace
uint16_t pack_depth(uint16_t depth_mm, uint8_t player_index) {
return static_cast<uint16_t>(((depth_mm & 0x1FFF) << 3) | (player_index & 0x7));
return static_cast<uint16_t>(
((depth_mm & 0x1FFF) << 3) | (player_index & 0x7));
}
uint16_t NuiDepthPixelToDepth(uint16_t packed_depth) {
return static_cast<uint16_t>(packed_depth >> 3);
}
uint8_t NuiDepthPixelToPlayerIndex(uint16_t packed_depth) {
return static_cast<uint8_t>(packed_depth & 0x7);
}
DepthRasterizer::DepthRasterizer(std::size_t temporal_hold_frames)
: stabilizer_(temporal_hold_frames) {}
DepthFrame DepthRasterizer::rasterize(
const SegmentationMask& mask,
const contract::SkeletonFrame& skeleton,
DepthRasterRequest request) {
stable_mask_ = stabilizer_.update(mask, request.size);
DepthFrame frame{
request.size,
request.size.width * static_cast<uint32_t>(sizeof(uint16_t)),
std::vector<uint16_t>(pixel_count(request.size), 0),
};
if (!has_body(stable_mask_) ||
request.size.width == 0 || request.size.height == 0) {
return frame;
}
const auto joints = project_joints(skeleton, request.size);
const uint8_t player_index =
static_cast<uint8_t>(std::min<uint8_t>(request.skeleton_slot, 5) + 1);
for (uint32_t y = 0; y < request.size.height; ++y) {
for (uint32_t x = 0; x < request.size.width; ++x) {
const std::size_t index =
static_cast<std::size_t>(y) * request.size.width + x;
if (stable_mask_.pixels[index] != 0) {
frame.pixels[index] =
pack_depth(interpolated_depth(x, y, joints), player_index);
}
}
}
return frame;
}
const BinaryMask& DepthRasterizer::stable_mask() const {
return stable_mask_;
}
BgraFrame passthrough_color(
const BgraFrame& source,
ImageSize requested_size) {
BgraFrame result{
requested_size,
requested_size.width * 4,
std::vector<uint8_t>(pixel_count(requested_size) * 4, 0),
};
if (source.size.width == 0 || source.size.height == 0 ||
source.pixels.size() != pixel_count(source.size) * 4 ||
requested_size.width == 0 || requested_size.height == 0) {
return result;
}
for (uint32_t y = 0; y < requested_size.height; ++y) {
const uint32_t source_y =
static_cast<uint64_t>(y) * source.size.height / requested_size.height;
for (uint32_t x = 0; x < requested_size.width; ++x) {
const uint32_t source_x =
static_cast<uint64_t>(x) * source.size.width / requested_size.width;
const std::size_t source_index =
(static_cast<std::size_t>(source_y) * source.size.width + source_x) * 4;
const std::size_t target_index =
(static_cast<std::size_t>(y) * requested_size.width + x) * 4;
std::copy_n(
source.pixels.begin() + static_cast<std::ptrdiff_t>(source_index),
4,
result.pixels.begin() + static_cast<std::ptrdiff_t>(target_index));
}
}
return result;
}
InfraredFrame synthesize_infrared(
const BgraFrame& source,
ImageSize requested_size) {
const auto color = passthrough_color(source, requested_size);
InfraredFrame result{
requested_size,
requested_size.width * static_cast<uint32_t>(sizeof(uint16_t)),
std::vector<uint16_t>(pixel_count(requested_size), 0),
};
for (std::size_t index = 0; index < result.pixels.size(); ++index) {
const std::size_t color_index = index * 4;
const uint32_t luminance =
77u * color.pixels[color_index + 2] +
150u * color.pixels[color_index + 1] +
29u * color.pixels[color_index];
result.pixels[index] = static_cast<uint16_t>(luminance);
}
return result;
}
} // namespace nui::synth
+53 -4
View File
@@ -1,16 +1,65 @@
#ifndef NUI_CORE_SYNTH_H
#define NUI_CORE_SYNTH_H
#include <cstddef>
#include <cstdint>
#include <vector>
// Placeholder for the packed depth + player-index + color rasterizer (plan todo 7).
#include "contract/contract.h"
#include "synth/mask_stabilizer.h"
namespace nui::synth {
// Packs a depth value (mm) and player index into the Kinect v1 depth word layout:
// (mm & 0x1FFF) << 3 | (player & 0x7). Provided now so the library has a real symbol;
// the full rasterizer arrives in todo 7.
struct DepthRasterRequest {
ImageSize size;
uint8_t skeleton_slot;
};
struct DepthFrame {
ImageSize size;
uint32_t pitch;
std::vector<uint16_t> pixels;
};
struct BgraFrame {
ImageSize size;
uint32_t pitch;
std::vector<uint8_t> pixels;
};
struct InfraredFrame {
ImageSize size;
uint32_t pitch;
std::vector<uint16_t> pixels;
};
uint16_t pack_depth(uint16_t depth_mm, uint8_t player_index);
uint16_t NuiDepthPixelToDepth(uint16_t packed_depth);
uint8_t NuiDepthPixelToPlayerIndex(uint16_t packed_depth);
class DepthRasterizer {
public:
explicit DepthRasterizer(std::size_t temporal_hold_frames = 2);
DepthFrame rasterize(
const SegmentationMask& mask,
const contract::SkeletonFrame& skeleton,
DepthRasterRequest request);
const BinaryMask& stable_mask() const;
private:
MaskStabilizer stabilizer_;
BinaryMask stable_mask_{};
};
BgraFrame passthrough_color(
const BgraFrame& source,
ImageSize requested_size);
InfraredFrame synthesize_infrared(
const BgraFrame& source,
ImageSize requested_size);
} // namespace nui::synth
+177
View File
@@ -0,0 +1,177 @@
#include <gtest/gtest.h>
#include <array>
#include <cstddef>
#include <cstdint>
#include <queue>
#include <vector>
#include "mapping/mapping.h"
#include "synth/synth.h"
namespace {
using nui::contract::SkeletonFrame;
using nui::synth::BinaryMask;
using nui::synth::ImageSize;
using nui::synth::SegmentationMask;
SkeletonFrame fixture_pose(ImageSize size) {
SkeletonFrame pose{};
const std::array<std::pair<int, int>, nui::contract::kJointCount> pixels{{
{10, 6}, {12, 7}, {14, 6}, {16, 5}, {8, 7},
{9, 9}, {10, 11}, {11, 12}, {18, 7}, {19, 9},
{20, 11}, {21, 12}, {12, 13}, {11, 15}, {10, 18},
{9, 20}, {16, 13}, {17, 15}, {18, 18}, {19, 20},
}};
for (std::size_t index = 0; index < pixels.size(); ++index) {
pose.joints[index].position = nui::mapping::depth_image_to_skeleton(
{pixels[index].first, pixels[index].second,
static_cast<uint16_t>((1800 + index * 20) << 3)},
static_cast<int>(size.width),
static_cast<int>(size.height));
pose.joints[index].position.w = 1.0f;
}
return pose;
}
SegmentationMask filled_mask(ImageSize size) {
return {size, std::vector<float>(
static_cast<std::size_t>(size.width) * size.height, 1.0f)};
}
std::size_t count_components(const BinaryMask& mask) {
std::vector<uint8_t> visited(mask.pixels.size(), 0);
std::size_t components = 0;
for (std::size_t start = 0; start < mask.pixels.size(); ++start) {
if (mask.pixels[start] == 0 || visited[start] != 0) {
continue;
}
++components;
std::queue<std::size_t> pending;
pending.push(start);
visited[start] = 1;
while (!pending.empty()) {
const std::size_t current = pending.front();
pending.pop();
const int x = static_cast<int>(current % mask.size.width);
const int y = static_cast<int>(current / mask.size.width);
for (int delta_y = -1; delta_y <= 1; ++delta_y) {
for (int delta_x = -1; delta_x <= 1; ++delta_x) {
const int neighbor_x = x + delta_x;
const int neighbor_y = y + delta_y;
if (neighbor_x < 0 || neighbor_y < 0 ||
neighbor_x >= static_cast<int>(mask.size.width) ||
neighbor_y >= static_cast<int>(mask.size.height)) {
continue;
}
const std::size_t neighbor =
static_cast<std::size_t>(neighbor_y) * mask.size.width +
static_cast<std::size_t>(neighbor_x);
if (mask.pixels[neighbor] != 0 && visited[neighbor] == 0) {
visited[neighbor] = 1;
pending.push(neighbor);
}
}
}
}
}
return components;
}
} // namespace
TEST(synth, PackAndUnpackDepthAndPlayerIndexRoundTrip) {
const auto packed = nui::synth::pack_depth(2500, 3);
EXPECT_EQ(packed, 0x4E23);
EXPECT_EQ(nui::synth::NuiDepthPixelToDepth(packed), 2500);
EXPECT_EQ(nui::synth::NuiDepthPixelToPlayerIndex(packed), 3);
}
TEST(synth, RasterizedBodyUsesRequestedResolutionAndPlayerSlot) {
const ImageSize size{32, 24};
nui::synth::DepthRasterizer rasterizer;
const auto frame = rasterizer.rasterize(
filled_mask(size), fixture_pose(size), {size, 2});
ASSERT_EQ(frame.size.width, size.width);
ASSERT_EQ(frame.size.height, size.height);
ASSERT_EQ(frame.pitch, size.width * sizeof(uint16_t));
for (const auto pixel : frame.pixels) {
EXPECT_GE(nui::synth::NuiDepthPixelToDepth(pixel), 800);
EXPECT_LE(nui::synth::NuiDepthPixelToDepth(pixel), 4000);
EXPECT_EQ(nui::synth::NuiDepthPixelToPlayerIndex(pixel), 3);
}
}
TEST(synth, ProjectedJointsLandInsideTheDepthSilhouette) {
const ImageSize size{32, 24};
const auto pose = fixture_pose(size);
nui::synth::DepthRasterizer rasterizer;
const auto frame = rasterizer.rasterize(filled_mask(size), pose, {size, 0});
for (const auto& joint : pose.joints) {
const auto projected = nui::mapping::skeleton_to_depth_image(
joint.position, static_cast<int>(size.width), static_cast<int>(size.height));
const std::size_t index =
static_cast<std::size_t>(projected.y) * size.width + projected.x;
ASSERT_LT(index, frame.pixels.size());
EXPECT_NE(frame.pixels[index], 0);
}
}
TEST(synth, CleanupKeepsOneBlobAndTemporallyHoldsIt) {
const ImageSize size{16, 12};
SegmentationMask fragmented{size, std::vector<float>(192, 0.0f)};
for (uint32_t y = 3; y < 9; ++y) {
for (uint32_t x = 4; x < 11; ++x) {
fragmented.confidence[static_cast<std::size_t>(y) * size.width + x] = 1.0f;
}
}
fragmented.confidence[static_cast<std::size_t>(6) * size.width + 7] = 0.0f;
fragmented.confidence[0] = 1.0f;
fragmented.confidence[191] = 1.0f;
nui::synth::DepthRasterizer rasterizer(1);
const auto filled = rasterizer.rasterize(
fragmented, fixture_pose(size), {size, 0});
const auto held = rasterizer.rasterize(
{size, std::vector<float>(192, 0.0f)}, fixture_pose(size), {size, 0});
EXPECT_EQ(count_components(rasterizer.stable_mask()), 1u);
EXPECT_NE(filled.pixels[static_cast<std::size_t>(6) * size.width + 7], 0);
EXPECT_EQ(filled.pixels.front(), 0);
EXPECT_NE(held.pixels[static_cast<std::size_t>(6) * size.width + 7], 0);
}
TEST(synth, BackgroundRemainsZeroAndColorAndInfraredResize) {
// The depth body sits two pixels in from every edge so morphological
// closing's zero-padded boundary cannot erode it away; the corner pixel
// stays untouched background.
const ImageSize depth_size{8, 8};
SegmentationMask mask{depth_size, std::vector<float>(64, 0.0f)};
for (uint32_t y = 3; y < 5; ++y) {
for (uint32_t x = 3; x < 5; ++x) {
mask.confidence[static_cast<std::size_t>(y) * depth_size.width + x] = 1.0f;
}
}
nui::synth::DepthRasterizer rasterizer;
const auto depth =
rasterizer.rasterize(mask, fixture_pose(depth_size), {depth_size, 0});
const std::size_t body_index = 4u * depth_size.width + 4u;
EXPECT_EQ(depth.pixels.front(), 0);
EXPECT_NE(depth.pixels[body_index], 0);
const ImageSize size{4, 2};
const nui::synth::BgraFrame source{
{2, 1}, 8, {10, 20, 30, 255, 40, 50, 60, 255}};
const auto color = nui::synth::passthrough_color(source, size);
const auto infrared = nui::synth::synthesize_infrared(source, size);
EXPECT_EQ(color.pixels[0], 10);
EXPECT_EQ(color.pixels[12], 40);
EXPECT_EQ(infrared.pixels[0], 5600);
EXPECT_EQ(infrared.pitch, size.width * sizeof(uint16_t));
}