Kinect for Windows SDK開發入門(三)基礎知識 下

來源:互聯網
上載者:User

1. 效能改進

上文的代碼中,對於每一個彩色映像幀,都會建立一個新的Bitmap對象。由於Kinect視頻網路攝影機預設採集頻率為每秒30幅,所以應用程式每秒會建立30個bitmap對象,產生30次的Bitmap記憶體建立,對象初始化,填充像素資料等操作。這些對象很快就會變成垃圾等待記憶體回收行程進行回收。對資料量小的程式來說可能影響不是很明顯,但當資料量很大時,其缺點就會顯現出來。

改進方法是使用WriteableBitmap對象。它位於System.Windows.Media.Imaging命名空間下面,該對象被用來處理需要頻繁更新的像素資料。當建立WriteableBitmap時,應用程式需要指定它的高度,寬度以及格式,以使得能夠一次性為WriteableBitmap建立好記憶體,以後只需根據需要更新像素即可。

使用WriteableBitmap代碼改動地方很小。下面的代碼中,首先定義三個新的成員變數,一個是實際的WriteableBitmap對象,另外兩個用來更新像素資料。每一幅映像的大小都是不變的,因此在建立WriteableBitmap時只需計算一次即可。

InitializeKinect方法中加粗的部分是更改的代碼。建立WriteableBitmap對象,準備接收像素資料,映像的範圍同時也計算了。在初始化WriteableBitmap的時候,同時也綁定了UI元素(名為ColorImageElement的Image對象)。此時WriteableBitmap中沒有像素資料,所以UI上是空的。

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();}

還需要進行的一處改動是,對ColorFrameReady事件響應的代碼。如下圖。首先刪除之前建立Bitmap那部分的代碼。調用WriteableBitmap對象的WritePixels方法來更新映像。方法使用映像的矩形範圍,代碼像素資料的數組,映像的Stride,以及位移(offset).位移量通常設定為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);     }   }}

基於Kinect的應用程式在無論是在顯示ColorImageStream資料還是顯示DepthImageStream資料的時候,都應該使用WriteableBitmap對象來顯示幀影像。在最好的情況下,彩色資料流會每秒產生30幀彩色影像,這意味著對記憶體資源的消耗比較大。WriteableBitmap能夠減少這種記憶體消耗,減少需要更新影響帶來的記憶體開闢和回收操作。畢竟在應用中顯示幀資料不是應用程式的最主要功能,所以在這方面減少內像存消耗顯得很有必要。

2. 簡單的影像處理

每一幀ColorImageFrame都是以位元組序列的方式返回原始的像素資料。應用程式必須以這些資料建立映像。這意味這我們可以對這些未經處理資料進行一定的處理,然後再展示出來。下面來看看如何對擷取的未經處理資料進行一些簡單的處理。

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;//藍色               pixelData[i + 1] = 0x00;//綠色             }           this.colorImageBitMap.WritePixels(this.colorImageBitmapRect, pixelData,this.colorImageStride,0);        }    }}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.