#include #include #include #include #include #include namespace { int fail(const char* step) { std::fprintf(stderr, "%s failed: %s\n", step, openni::OpenNI::getExtendedError()); return 1; } struct FrameStats { int frame_index = 0; int width = 0; int height = 0; int non_zero_count = 0; int in_band_count = 0; uint16_t depth_min_mm = 0xFFFF; uint16_t depth_max_mm = 0; double centroid_x = 0.0; double centroid_y = 0.0; }; FrameStats analyze(const openni::VideoFrameRef& frame) { FrameStats stats; stats.frame_index = frame.getFrameIndex(); stats.width = frame.getWidth(); stats.height = frame.getHeight(); const auto* pixels = static_cast(frame.getData()); double sum_x = 0.0; double sum_y = 0.0; for (int y = 0; y < stats.height; ++y) { for (int x = 0; x < stats.width; ++x) { const uint16_t value = pixels[y * stats.width + x]; if (value == 0) { continue; } ++stats.non_zero_count; sum_x += x; sum_y += y; if (value < stats.depth_min_mm) { stats.depth_min_mm = value; } if (value > stats.depth_max_mm) { stats.depth_max_mm = value; } if (value >= 800 && value <= 4000) { ++stats.in_band_count; } } } if (stats.non_zero_count > 0) { stats.centroid_x = sum_x / stats.non_zero_count; stats.centroid_y = sum_y / stats.non_zero_count; } return stats; } } // namespace int main(int argc, char** argv) { const int frame_count = argc > 1 ? std::atoi(argv[1]) : 12; if (openni::OpenNI::initialize() != openni::STATUS_OK) { return fail("OpenNI::initialize"); } openni::Device device; if (device.open(openni::ANY_DEVICE) != openni::STATUS_OK) { openni::OpenNI::shutdown(); return fail("Device::open"); } openni::VideoStream depth; if (depth.create(device, openni::SENSOR_DEPTH) != openni::STATUS_OK) { device.close(); openni::OpenNI::shutdown(); return fail("VideoStream::create(SENSOR_DEPTH)"); } if (depth.start() != openni::STATUS_OK) { depth.destroy(); device.close(); openni::OpenNI::shutdown(); return fail("VideoStream::start"); } std::printf("device_uri=%s\n", device.getDeviceInfo().getUri()); std::vector history; int result = 0; for (int i = 0; i < frame_count; ++i) { openni::VideoFrameRef frame; const openni::Status read_status = depth.readFrame(&frame); if (read_status != openni::STATUS_OK || !frame.isValid()) { std::fprintf(stderr, "readFrame failed or invalid frame at index %d\n", i); result = 1; break; } const FrameStats stats = analyze(frame); std::printf( "frame[%02d] seq=%d size=%dx%d non_zero=%d in_band=%d depth_min_mm=%u depth_max_mm=%u " "centroid=(%.1f,%.1f)\n", i, stats.frame_index, stats.width, stats.height, stats.non_zero_count, stats.in_band_count, stats.depth_min_mm == 0xFFFF ? 0 : stats.depth_min_mm, stats.depth_max_mm, stats.centroid_x, stats.centroid_y); history.push_back(stats); frame.release(); } depth.stop(); depth.destroy(); device.close(); openni::OpenNI::shutdown(); if (result != 0) { return result; } const bool correct_geometry = !history.empty() && history.front().width == 320 && history.front().height == 240; bool every_frame_has_body = true; bool every_body_in_band = true; for (const auto& stats : history) { if (stats.non_zero_count == 0) { every_frame_has_body = false; } if (stats.non_zero_count > 0 && stats.in_band_count != stats.non_zero_count) { every_body_in_band = false; } } double max_centroid_displacement = 0.0; bool all_centroids_identical = true; for (std::size_t i = 1; i < history.size(); ++i) { const double dx = history[i].centroid_x - history[0].centroid_x; const double dy = history[i].centroid_y - history[0].centroid_y; const double displacement = std::sqrt(dx * dx + dy * dy); if (displacement > max_centroid_displacement) { max_centroid_displacement = displacement; } if (history[i].centroid_x != history[0].centroid_x || history[i].centroid_y != history[0].centroid_y) { all_centroids_identical = false; } } constexpr double kMinMotionPixels = 1.5; const bool motion_observed = !all_centroids_identical && max_centroid_displacement >= kMinMotionPixels; std::printf( "summary: frames=%zu every_frame_has_body=%d every_body_in_band=%d " "max_centroid_displacement_px=%.2f motion_observed=%d correct_geometry=%d\n", history.size(), every_frame_has_body, every_body_in_band, max_centroid_displacement, motion_observed, correct_geometry); if (!every_frame_has_body || !every_body_in_band || !motion_observed || !correct_geometry) { std::fprintf(stderr, "FAIL: every_frame_has_body=%d every_body_in_band=%d motion_observed=%d " "correct_geometry=%d\n", every_frame_has_body, every_body_in_band, motion_observed, correct_geometry); return 1; } std::printf( "PASS: real OpenNI2 consumer read %zu in-band depth frames with a moving silhouette " "(max centroid displacement %.2fpx)\n", history.size(), max_centroid_displacement); return 0; }