Files
kinect/tests/test_synth.cpp
T
kinect-bot 3fef71b020
ci / Linux C++ core (cmake + ctest) (push) Canceled after 0s
ci / Python tracker (pytest) (push) Canceled after 0s
feat(synth): articulated depth + player-index + color/IR rasterizer (wave 4; todo 7 done)
2026-11-25 04:33:33 -05:00

178 lines
6.3 KiB
C++

#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));
}