Getting started with the Kinect for Windows SDK Development (iii) basic knowledge

Source: Internet
Author: User

1. Performance improvement

In the code above, for each color image frame, a new bitmap object is created. Because the Kinect video camera has a default acquisition frequency of 30 per second, the application creates 30 bitmap objects per second, generating 30 of bitmap memory creation, object initialization, and pixel data filling operations. These objects will soon become garbage waiting for the garbage collector to recycle. It may not be obvious to a program with a small amount of data, but when the volume of data is large, its drawbacks become apparent.

The improved approach is to use the WriteableBitmap object. It is located under the System.Windows.Media.Imaging namespace, which is used to handle pixel data that needs to be updated frequently. When creating a WriteableBitmap, the application needs to specify its height, width, and format so that the memory can be created at once for the WriteableBitmap, and the pixel can be updated only as needed.

Using writeablebitmap code to change places is small. In the following code, you first define three new member variables, one is the actual WriteableBitmap object, and the other two are used to update the pixel data. The size of each image is constant, so you only have to calculate it once when you create the WriteableBitmap.

The bold part of the Initializekinect method is the changed code. Creates the WriteableBitmap object, prepares to receive the pixel data, the image scope also calculates. When WriteableBitmap is initialized, the UI element (an image object named Colorimageelement) is also bound. At this point, there is no pixel data in the WriteableBitmap, so the UI is empty.

Private WriteableBitmap Colorimagebitmap;
Private Int32Rect Colorimagebitmaprect;
private int colorimagestride;
Private byte[] Colorimagepixeldata;
    
if (kinectsensor!= null)
{   
    colorimagestream colorstream=kinectsensor.colorstream;
    Colorstream.enable ();
    This.colorimagebitmap = new WriteableBitmap (Colorstream.framewidth, Colorstream.frameheight,
                                                                    96, 96, Pixelformats.bgr32, null);
    This.colorimagebitmaprect = new Int32Rect (0, 0, colorstream.framewidth, colorstream.frameheight);
    This.colorimagestride = Colorstream.framewidth * Colorstream.framebytesperpixel;
    Colorimageelement.source = This.colorimagebitmap;
    
    Kinectsensor.colorframeready + = Kinectsensor_colorframeready;
    Kinectsensor.start ();
}

Another change that needs to be made is the code that responds to the Colorframeready event. The following figure. First delete the code that previously created the bitmap section. Call the WriteableBitmap object's WritePixels method to update the image. Methods use the rectangular range of the image, the array of code pixel data, the stride of the image, and the offset (offset). The offsets are typically set to 0.

private void Kinect_colorframeready (object sender, Colorimageframereadyeventargs e)
{
   using (colorimageframe frame = E.opencolorimageframe ())
  {
     if (frame!= null)
     {
        byte[] pixeldata = new Byte[frame. Pixeldatalength];
        Frame. Copypixeldatato (pixeldata);
        This.colorImageBitmap.WritePixels (This.colorimagebitmaprect, Pixeldata, this.colorimagestride, 0);}}

The Kinect application should use the WriteableBitmap object to display the frame image, whether it is displaying colorimagestream data or displaying depthimagestream data. In the best case, the color data stream produces 30 frames per second of color images, which means a greater consumption of memory resources. WriteableBitmap can reduce this memory consumption, reduce the need to update the impact of memory-generating and recycling operations. After all, the display of frame data in the application is not the most important function of the application, so it is necessary to reduce the internal image storage consumption in this aspect.

2. Simple image processing

Each frame Colorimageframe returns the original pixel data in the form of a byte sequence. The application must create an image with this data. This means that we can do some processing of the raw data and then show it. Let's take a look at how to do some simple processing of the raw data you get.

void Kinectsensor_colorframeready (object sender, Colorimageframereadyeventargs e)
{
    using (colorimageframe frame = E.opencolorimageframe ())
    {
        if (frame!= null)
        {
            byte[] pixeldata = new Byte[frame. Pixeldatalength];
            Frame. Copypixeldatato (pixeldata);
            for (int i = 0; i < pixeldata.length i + = frame. Bytesperpixel)
            {
               Pixeldata[i] = 0x00;//Blue
               pixeldata[i + 1] = 0x00;//Green
             }
           This.colorImageBitMap.WritePixels (This.colorimagebitmaprect, pixeldata,this.colorimagestride,0);
        }
    }

Related Article

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.