Color images in the basic tutorial of Kinect V2,

Source: Internet
Author: User

Color images in the basic tutorial of Kinect V2,

This program is written by myself. The reference materials include official Microsoft examples and foreign documents, and I made some optimizations. If you have any questions, please correct them.

Background code:

Using System. componentModel; using System. windows; using System. windows. media; using System. windows. media. imaging; using Microsoft. kinect; namespace KinectV2 {// <summary> // MainWindow. xaml /// </summary> public partial class MainWindow: Window {# region definition variable // KinectSensor kinect of the kinect device; // color frame reads ColorFrameReader colorFrameReader; // FrameDescription colorFrameDesc; // The Enumeration type defines the color frame format (6 in total) // The color frame format is Bgra: a pixel is stored in 4 bytes, including blue, green, red, and alpha (that is, the pixel Brightness Value) ColorImageFormat colorFormat = ColorImageFormat. bgra; // create a bitmap WriteableBitmap colorBitmap for displaying the image; // all information of a color image (byte type, each element occupies one byte) byte [] colorBuffer; // The step size of a color image (physical meaning: the number of bytes required for each row of an image) (Role: Provide the image step size when rendering a bitmap) int colorStride; // The rectangle canvas for storing color images (function: Provide the canvas position and size when rendering the bitmap) Int32Rect colorRect; # endregion public MainWindow () {InitializeComponent (); # Initialize and assign values to the region variable // obtain the default kinect sensor kinect = KinectSensor. getDefault (); // start the sensor kinect. open (); // determine the color frame data type colorFrameDesc = kinect. colorFrameSource. createFrameDescription (colorFormat); // create and return a new read object colorFrameReader = kinect. colorFrameSource. openReader (); // triggers the colorFrameReader of the color frame event. frameArrived + = colorFrameReader_FrameArrived; // color bitmap initialization (96,96 indicates resolution, I .e. 96 pixels/inch) colorBitmap = new WriteableBitmap (ColorFrameDesc. width, colorFrameDesc. height, 96, 96, PixelFormats. bgra32, null); // image step size initialization colorStride = colorFrameDesc. width * (int) colorFrameDesc. bytesPerPixel; // (int) colorFrameDesc. bytesPerPixel = 4, each pixel occupies 4 bytes // colorRect = new Int32Rect (0, 0, colorFrameDesc) initialized in the canvas rectangle. width, colorFrameDesc. height); // calculate the total number of bytes required to store all pixels of an image. colorBuffer = new byte [colorStride * colorFrameDesc. height]; // color bitmap and UI The image control is associated with ImageColor. source = colorBitmap; // describe the acquired color frame information in the ImageColor control in real time # endregion} # region processes the color frame event from the sensor // The color frame trigger event void colorFrameReader_FrameArrived (object sender, colorFrameArrivedEventArgs e) {// update the color frame, and then depict the color frame UpdateColorFrame (e); DrawColorFrame ();} private void UpdateColorFrame (ColorFrameArrivedEventArgs e) {// obtain a color image using (var colorFrame = e. frameReference. acquireFrame () {if (colo RFrame = null) {return;} // copy a frame of color image data in 'colorformat' format to 'colorbuffer' colorFrame. copyConvertedFrameDataToArray (colorBuffer, colorFormat) ;}} private void DrawColorFrame () {// update the acquired color image data to the colorBitmap in place. writePixels (colorRect, colorBuffer, colorStride, 0) ;}# endregion // program window close event private void Window_Closing (object sender, CancelEventArgs e) {// release the color frame resource if (colorFrameReader! = Null) {colorFrameReader. Dispose (); colorFrameReader = null;} // disable 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="512" Height="424">        <Image x:Name="ImageDepth"  />    </Grid></Window>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.