The previous article describes the environment configuration for the Kinect development, and this article and the next article will introduce the basics of Kinect development and build the foundation for an in-depth study of the Kinect for Windows SDK.
Each Kinect application has some basic elements. The application must detect and discover the Kinect sensor that is linked to the device. Before you can use these sensors, you must initialize them, and once the initialization is successful, the data will be generated and our program will be able to process the data. Finally, when the application shuts down, these sensors must be reasonably released.
The first part of this article will describe how to detect the initialization of several release sensors, which is a very basic topic, but is important for applications based on the Kinect development. Once initialized, the Kinect's various sensors can generate data. Our program can read these streams of data. The Kinect-generated data flow class is similar to the IO data stream below the System.IO namespace.
The second section details the basics of the data flow and shows how to use Colorimagestream from the Kinect to get the data produced by the color camera. Data streams enable the production of pixel based data, making it possible to produce color images like cameras or basic photographs. This data can be handled in a variety of interesting ways.
This article is an essential part of the entire Kinect SDK development, and after that, it is helpful to familiarize yourself with other parts of the SDK.
1. Kinect sensor
The first object to be used by an application based on the Kinect development is the Kinectsensor object, which directly represents the Kinect hardware device. The Kinectsensor object is the source of data that we want to capture, including color image data, depth data, and skeleton tracking data. This article will cover colorimagestream in more detail, and the following articles will discuss Depthimagestream and skeletonstream in more detail.
The most common way to get data from Kinectsensor is by listening to a series of events for that object. Each data stream has a corresponding event, which triggers a change in time when the modified data stream is available. Each data stream is in frames (frame). For example, Colorimagestream triggers a Colorframeready event when a new data is obtained. When discussing each specific sensor data stream we will discuss these events in detail.
Each data stream (Color,depth,skeleton) is displayed as a data point in a different coordinate system, which we can see clearly in the discussion later. It is a common practice to convert point data from one data stream to another, and later in this article we will discuss how and why this conversion is necessary. The Kinectsensor object has some column methods that can be used to convert data to the data lattice, they are mapdepthtocolorimagepoint, Mapdepthtoskeletonpoint and Mapskeletonpointtodepth. Before we can get the Kinect data, we must first discover the connected Kinect devices. The Kinect device is found to be simple, but there are also areas that need attention.
1.1 Found connected Kinect devices
The Kinectobject object does not have a public constructor, and the application cannot create it directly. Instead, the object is created by the SDK when it detects a connected Kinect device. When a Kinect device is connected to the computer, the application should be notified or alerted. The Kinectseneor object has a static property kinectsensors, which is a Kinectsensorcollection collection that inherits from ReadOnlyCollection, The ReadOnlyCollection collection is simple, he has only one indexer and an event called statuschanged.
Use the indexer in the collection to get the Kinectsensor object. The number of elements in the collection is the number of Kinect devices. That is, a single computer can connect multiple Kinect devices to get data in different directions. Applications can use multiple Kinect devices to gain multiple data, and the number of Kinect limitations is limited only by computer configuration. Each Kinect device requires a USB cable to connect to the computer because each Kinect transmits data through USB. In addition, more Kinect devices require more CPU and memory consumption.
Find the Kinect device can be found by simply traversing the collection, but not all devices in the Kinectsensor collection can be used directly, so the Kinectsensor object has a status property, an enumeration type that identifies the status of the current Kinect device. The status of the sensor and its meaning are listed in the following table:
The Kinectsensor object can be initialized only if the device is in connected state. The state of the sensor may change during the entire lifecycle of the application, which means that the application we develop must monitor the connection status of the device and take appropriate steps to improve the user experience when the device connection status changes. For example, if the Kinect USB cable is unplugged from the computer, the sensor's connection status becomes disconnected, and typically the application should pause in this case and prompt the user to insert the Kinect device into the computer. The application should not assume that the Kinect device is available at the outset, nor should it assume that the Kinect device will be connected to the computer all the time the entire program is running.
Next, you first create a WPF application to show how to discover and get the status of the Kinect sensor. First build a WPF project and add Microsoft.Kinect.dll. Write the following code in the MainWindows.xaml.cs:
Public partial class Mainwindow:window {//Private Kinectsensor object private kinectsensor Kinect;
Public Kinectsensor Kinect {get {return this.kinect}
set {//if the sensor with assignment is not the same as if (This.kinect!=value) {//If the current sensor object is not NULL if (this.kinect!=null) {//uninitailize current object This.kinec
T=null; //If the incoming object is not empty and the state is a connection state if (Value!=null&&value).
status==kinectstatus.connected) {this.kinect=value;
Public MainWindow () {InitializeComponent ()}}}; This.
Loaded + = (s, e) => discoverkinectsensor (); This.
Unloaded + = (s, e) => this.kinect = null; private void Discoverkinectsensor () {KinectSensor.KinectSensors.StatusChanged + = Kinectsensors_sta
tuschanged; This.
Kinect = KinectSensor.KinectSensors.FirstOrDefault (x => x.status = = kinectstatus.connected); } private void Kinectsensors_statuschanged (object sender, Statuschangedeventargs e) {switch (E.statu s) {case KinectStatus.Connected:if (this.kinect = = null) this.ki
Nect = E.sensor;
Break Case KinectStatus.Disconnected:if (This.kinect = = e.sensor) {this.ki
Nect = null;
This.kinect = KinectSensor.KinectSensors.FirstOrDefault (x => x.status = = kinectstatus.connected);
if (This.kinect = = null) {//TODO: Notification used for Kinect unplugged}
} break; TODO: Handling status in other cases}}}