Files
kinect/fallback/linux-openni2/tools/openni2_depth_consumer.cpp
T
2026-11-25 05:09:31 -05:00

100 lines
3.0 KiB
C++

#include <OpenNI.h>
#include <cstdint>
#include <cstdio>
namespace {
int fail(const char* step) {
std::fprintf(stderr, "%s failed: %s\n", step, openni::OpenNI::getExtendedError());
return 1;
}
}
int main() {
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");
}
openni::VideoFrameRef frame;
const openni::Status read_status = depth.readFrame(&frame);
int result = 0;
if (read_status != openni::STATUS_OK || !frame.isValid()) {
std::fprintf(stderr, "readFrame failed or invalid frame\n");
result = 1;
} else {
const int width = frame.getWidth();
const int height = frame.getHeight();
const auto* pixels = static_cast<const openni::DepthPixel*>(frame.getData());
const int pixel_count = width * height;
long long sum = 0;
int in_band_count = 0;
int non_zero_count = 0;
uint16_t sample_min = 0xFFFF;
uint16_t sample_max = 0;
for (int i = 0; i < pixel_count; ++i) {
const uint16_t value = pixels[i];
if (value != 0) {
++non_zero_count;
sum += value;
if (value < sample_min) sample_min = value;
if (value > sample_max) sample_max = value;
if (value >= 800 && value <= 4000) {
++in_band_count;
}
}
}
std::printf("device_uri=%s\n", device.getDeviceInfo().getUri());
std::printf("frame_width=%d frame_height=%d\n", width, height);
std::printf("non_zero_pixels=%d in_band_pixels=%d total_pixels=%d\n",
non_zero_count, in_band_count, pixel_count);
if (non_zero_count > 0) {
std::printf("depth_min_mm=%u depth_max_mm=%u depth_mean_mm=%lld\n",
sample_min, sample_max, sum / non_zero_count);
std::printf("sample_pixel[%d,%d]=%u mm\n", width / 2, height / 2,
pixels[(height / 2) * width + width / 2]);
}
const bool has_body = non_zero_count > 0;
const bool body_in_band = non_zero_count > 0 && in_band_count == non_zero_count;
const bool correct_geometry = width == 320 && height == 240;
if (!has_body || !body_in_band || !correct_geometry) {
std::fprintf(stderr,
"FAIL: has_body=%d body_in_band=%d correct_geometry=%d\n",
has_body, body_in_band, correct_geometry);
result = 1;
} else {
std::printf("PASS: real OpenNI2 consumer read in-band synthetic depth\n");
}
}
frame.release();
depth.stop();
depth.destroy();
device.close();
openni::OpenNI::shutdown();
return result;
}