C # Kinect V2 Learning Notes (iv) Depth image

Source: Internet
Author: User

For a long time did not write a simple tutorial, not I lazy, OK, I admit a little lazy. But really busy, and recently arranged to change direction, you said I just entered the door, want to learn it, now to do some hardware, there is no way can only go brain.

The depth of the image today, very simple, the steps are still a few steps: 1. Get the body sense equipment; 2. Image frame initialization; 3. Frame description; 4. Frame trigger events; 5. Create a bitmap to display the captured image. Infrared color depth is not really much, especially color and depth, you will find that the code does not change the same.

Well, the basis is still to be finished, although the same, but repeat do to remember ripe, programmers are mostly in the process of doing a cycle, the next time I will insist on writing a bit of fun, not regularly put on some of my interesting small demo, will also try to write some other, such as my recent study of some of the hardware knowledge, When it is to give yourself a memory, a share of solving problems encountered.

Put the code:

Settings for variables:

Body Sensor Equipment
        private kinectsensor kinectdevice;

        Depth frame reading variable
        private depthframereader depthframereader;
        Depth Frame description
        private framedescription depthframedesription;

        Bitmap Object
        private WriteableBitmap depthbitmap;
        A rectangular frame for storing a frame depth image
        private int32rect depthbitmaprect;
        The depth of the pixel group is converted to the length of the rectangular box
        private int depthstride;
        Depth image pixel data
        private ushort[] depthpixeldata;

Main program code: (Just like color data, color changed to depth, storage format changed)

InitializeComponent ();

            Gets the default connection of the body sensor This.kinectdevice = Kinectsensor.getdefault ();
            Depth image variable Initialization this.depthframereader = This.kinectDevice.DepthFrameSource.OpenReader (); 
            Depth frame description, width and height this.depthframedesription = this.kinectDevice.DepthFrameSource.FrameDescription;

            Trigger Depth Frame event this.depthFrameReader.FrameArrived + = _depthframereader_framearrived; Bitmap initialization, width, height, 96.0 representation resolution, pixel format This.depthbitmap = new WriteableBitmap (This.depthFrameDesription.Width, this.dept
            Hframedesription.height, 96.0, 96.0, PIXELFORMATS.GRAY16, NULL); A rectangular frame this.depthbitmaprect = new Int32Rect (0, 0, This.depthBitmap.PixelWidth, This.depthBitmap.PixelHei) that holds the pixel of the image
            Ght);           Length of byte array holding depth image = frame width * Frame height this.depthpixeldata = new Ushort[this.depthframedesription.lengthinpixels]; Replace width * height, more convenient//step: Width * 2 bytes/Pixel this.depthstride = this.deptHframedesription.width * 2;

            Display image depthimage.source = This.depthbitmap; This.kinectDevice.Open ();

Handling events of the body sensor:

void _depthframereader_framearrived (object sender, Depthframearrivedeventargs e)
        {
            //Get a frame depth image
            using ( Depthframe depthframe = E.framereference.acquireframe ())
            {/
                /If obtain success if
                (Depthframe!= null)
                {
                    // Storing the depth frame data in a byte array
                    depthframe.copyframedatatoarray (this.depthpixeldata);

                    Writes the pixel of an array to a rectangular box, rendering the bitmap.
                    this.depthBitmap.WritePixels (This.depthbitmaprect, This.depthpixeldata, this.depthstride,0);}}
        
The effect is as follows:

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.