This program for their own writing, reference materials, including Microsoft Official examples and foreign language materials, do some of their own optimization. I have not learned the image processing, explain if there is a problem, I implore you to correct me.
Background code:
usingSystem.ComponentModel;usingSystem.Windows;usingSystem.Windows.Media;usingSystem.Windows.Media.Imaging;usingMicrosoft.kinect;namespacekinectv2{/// <summary> ///MainWindow.xaml/// </summary> Public Partial classMainwindow:window {#regionDefining variables//Kinect Devicekinectsensor Kinect; //Color Frame ReadColorframereader Colorframereader; //a description of the nature of a frame imageframedescription Colorframedesc; //enumeration type, defining the format of color frames (6 total)//This specifies that the color frame format is bgra format: 4 bytes Stores one pixel, including blue,green,red,alpha (that is, pixel luminance value) .Colorimageformat ColorFormat =Colorimageformat.bgra; //Create a bitmap for displaying an imageWriteableBitmap Colorbitmap; //all information for storing a single frame of color image (byte type, one byte per element) byte[] colorbuffer; //the step size of a color image (physical meaning: here represents the number of bytes per row of a frame image) (function: The step of the image is required to render the bitmap) intColorstride; //rectangular canvas for storing color images (function: canvas location and size required for rendering bitmaps)Int32Rect Colorrect; #endregion PublicMainWindow () {InitializeComponent (); #regionInitialize variables, assign values//get the default Kinect sensorKinect =Kinectsensor.getdefault (); //Start SensorKinect. Open (); //determining the data type of a colored frameColorframedesc =Kinect. Colorframesource.createframedescription (ColorFormat); //creates and returns a new Read ObjectColorframereader =Kinect. Colorframesource.openreader (); //Trigger Color Frame EventColorframereader.framearrived + =colorframereader_framearrived; //Color Bitmap Initialization (96,96 for resolution, i.e. 96 pixels per inch)Colorbitmap =NewWriteableBitmap (Colorframedesc.width, Colorframedesc.height, the, the, Pixelformats.bgra32,NULL); //Image Step InitializationColorstride = Colorframedesc.width * (int) Colorframedesc.bytesperpixel;//(int) Colorframedesc.bytesperpixel = 4, 4 bytes per pixel//Canvas Rectangle Box InitializationColorrect =NewInt32Rect (0,0, Colorframedesc.width, colorframedesc.height); //calculates the total number of bytes required to store all pixels of a single frame of imageColorbuffer =New byte[Colorstride *Colorframedesc.height]; //color bitmap and UI interface picture control associationsImagecolor.source = Colorbitmap;//that is, in real time, the captured color frame information is depicted in the Imagecolor control #endregion } #regionHandling color frame events from sensors//Color Frame Triggering event voidColorframereader_framearrived (Objectsender, Colorframearrivedeventargs e) { //Update the color frame and then paint the color frameUpdatecolorframe (e); Drawcolorframe (); } Private voidUpdatecolorframe (Colorframearrivedeventargs e) {//get a frame color image using(varColorframe =E.framereference.acquireframe ()) { if(Colorframe = =NULL) { return; } //put a frame of color image data in ' ColorFormat ' format to ' Colorbuffer 'Colorframe.copyconvertedframedatatoarray (Colorbuffer, ColorFormat); } } Private voidDrawcolorframe () {//updates the captured color image data into the bitmapColorbitmap.writepixels (Colorrect, Colorbuffer, Colorstride,0); } #endregion //program window Shutdown Event Private voidWindow_closing (Objectsender, CancelEventArgs e) { //Releasing color frame resources if(Colorframereader! =NULL) {colorframereader.dispose (); Colorframereader=NULL; } //turn off the Kinect sensor if(Kinect! =NULL) {Kinect. Close (); Kinect=NULL; } } }}
XAML Interface code:
<window x:class="Kinectv2.mainwindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="Http://schemas.microsoft.com/winfx/2006/xaml"Closing="window_closing"> <grid width=" +"height="424"> <image x:name="imagedepth"/> </Grid></Window>
A color image of the Kinect V2 Basic tutorial