#include #include #include #include #include #include #include "mapping/mapping.h" #include "synth/synth.h" namespace { using nui::contract::SkeletonFrame; using nui::synth::BinaryMask; using nui::synth::has_body; using nui::synth::ImageSize; using nui::synth::MaskStabilizer; using nui::synth::SegmentationMask; SkeletonFrame fixture_pose(ImageSize size) { SkeletonFrame pose{}; const std::array, 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((1800 + index * 20) << 3)}, static_cast(size.width), static_cast(size.height)); pose.joints[index].position.w = 1.0f; } return pose; } SegmentationMask filled_mask(ImageSize size) { return {size, std::vector( static_cast(size.width) * size.height, 1.0f)}; } std::size_t count_components(const BinaryMask& mask) { std::vector 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 pending; pending.push(start); visited[start] = 1; while (!pending.empty()) { const std::size_t current = pending.front(); pending.pop(); const int x = static_cast(current % mask.size.width); const int y = static_cast(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(mask.size.width) || neighbor_y >= static_cast(mask.size.height)) { continue; } const std::size_t neighbor = static_cast(neighbor_y) * mask.size.width + static_cast(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(size.width), static_cast(size.height)); const std::size_t index = static_cast(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(192, 0.0f)}; for (uint32_t y = 3; y < 9; ++y) { for (uint32_t x = 4; x < 11; ++x) { fragmented.confidence[static_cast(y) * size.width + x] = 1.0f; } } fragmented.confidence[static_cast(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(192, 0.0f)}, fixture_pose(size), {size, 0}); EXPECT_EQ(count_components(rasterizer.stable_mask()), 1u); EXPECT_NE(filled.pixels[static_cast(6) * size.width + 7], 0); EXPECT_EQ(filled.pixels.front(), 0); EXPECT_NE(held.pixels[static_cast(6) * size.width + 7], 0); } TEST(synth, StabilizerSurvivesHeavySpeckleNoiseAndMultiFrameFlicker) { // Stress case for IS-8 (mask fragmentation/flicker under harsher conditions // than CleanupKeepsOneBlobAndTemporallyHoldsIt): a body silhouette peppered // with scattered single-pixel salt noise (its own tiny components, must not // merge into / enlarge the body's largest component) plus internal pepper // holes (must be closed), followed by TWO consecutive fully-empty frames // within the temporal hold window (must still hold the last stable blob), // then a hold-exhausting gap (must correctly fall through to background). const ImageSize size{20, 16}; const std::size_t total = static_cast(size.width) * size.height; SegmentationMask speckled{size, std::vector(total, 0.0f)}; for (uint32_t y = 2; y < 14; ++y) { for (uint32_t x = 3; x < 17; ++x) { speckled.confidence[static_cast(y) * size.width + x] = 1.0f; } } // Pepper: punch isolated holes inside the body. const std::array, 4> holes{{ {5, 4}, {9, 7}, {12, 10}, {15, 12}}}; for (const auto& [hx, hy] : holes) { speckled.confidence[static_cast(hy) * size.width + hx] = 0.0f; } // Salt: scattered single-pixel noise well outside the body, each isolated // (no two salt pixels touching, so each forms its own tiny component). const std::array, 6> salt{{ {0, 0}, {19, 0}, {0, 15}, {19, 15}, {1, 15}, {18, 0}}}; for (const auto& [sx, sy] : salt) { speckled.confidence[static_cast(sy) * size.width + sx] = 1.0f; } MaskStabilizer stabilizer(/*temporal_hold_frames=*/2); const BinaryMask cleaned = stabilizer.update(speckled, size); // The pepper holes are filled (morphological close) and the body stays one // component; the salt noise is discarded because largest-component keeps // only the (much larger) body blob. EXPECT_EQ(count_components(cleaned), 1u); for (const auto& [hx, hy] : holes) { EXPECT_NE(cleaned.pixels[static_cast(hy) * size.width + hx], 0) << "pepper hole at (" << hx << "," << hy << ") should be closed"; } for (const auto& [sx, sy] : salt) { EXPECT_EQ(cleaned.pixels[static_cast(sy) * size.width + sx], 0) << "salt speckle at (" << sx << "," << sy << ") should not survive largest-component"; } ASSERT_TRUE(has_body(cleaned)); const SegmentationMask empty{size, std::vector(total, 0.0f)}; const BinaryMask held_frame1 = stabilizer.update(empty, size); const BinaryMask held_frame2 = stabilizer.update(empty, size); const BinaryMask fallen_through = stabilizer.update(empty, size); // Two consecutive empty frames stay within the 2-frame hold: the stable // blob keeps being served, not a flicker to background. EXPECT_TRUE(has_body(held_frame1)); EXPECT_EQ(held_frame1.pixels, cleaned.pixels); EXPECT_TRUE(has_body(held_frame2)); EXPECT_EQ(held_frame2.pixels, cleaned.pixels); // The third consecutive empty frame exhausts the hold: falls through to a // safe, empty (no-player) background rather than holding forever. EXPECT_FALSE(has_body(fallen_through)); } 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(64, 0.0f)}; for (uint32_t y = 3; y < 5; ++y) { for (uint32_t x = 3; x < 5; ++x) { mask.confidence[static_cast(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)); }