simple-read
Overview
This example demonstrates how to get the distance (depth) value from the camera for a specific point.
Expect Output
…
Distance is 1171 mm
Distance is 1150 mm
Distance is 1163 mm
Distance is 1188 mm
Distance is 1214 mm
…
Tutorial
Import openni package
using System;
using System.Threading;
using OpenNIWrapper;
Initialize SDK
OpenNI.Initialize();
Connect to camera
OpenNI.OnDeviceConnected += OpenNiOnDeviceConnected;
OpenNI.OnDeviceDisconnected += OpenNiOnDeviceDisconnected;
device = Device.Open(Device.AnyDevice);
Create depth stream and start it
VideoStream depthStream = device.CreateVideoStream(Device.SensorType.Depth);
depthStream.Start();
VideoMode mode = depthStream.VideoMode;
Read frame from camera and retrieve the depth data
if (OpenNI.WaitForStream(depthStream) == OpenNI.Status.Ok)
{
VideoFrameRef frame = depthStream.ReadFrame();
}
Depth data is stored in order, from top left to bottom right. We calculate the index of the center point of the frame according to the width and height. Then get the value of it.
ushort* pDepth = (ushort*)frame.Data.ToPointer();
int middleIndex = (frame.FrameSize.Height + 1) * (frame.FrameSize.Width / 2);
Console.WriteLine("Distance is {0} mm", pDepth[middleIndex]);
Full code
Last updated