2011-10-07 110 views
7

我試圖裁剪視頻RGB的矩形區域。首先我找到了頭部關節的座標,並用這個座標在RGB視頻上繪製了一個矩形。現在,我想在另一個視頻中顯示位於第一張圖像中的位置的圖像。任何幫助都會很棒。Kinect作物圖像

視頻RGB顯示在「RGBvideo」圖像控件中。 我想要在「faceImage」中顯示的裁剪圖像圖像控制

我在線搜索但找不到解決方案。我很迷惑。

太感謝你了

回答

11

歡迎堆棧溢出,請不要多次問同樣的問題。使用Kinect等不太流行的標籤,人們可能需要一些時間來回答(標籤只有79個關注者)。

爲了簡單起見,我將假設您想要裁剪出一組大小的圖像(例如,原始800x600中的60x60像素)。在你的VideoFrameReady方法中,你從事件參數中獲得PlanarImage。這個PlanarImage有位字段,其中包含圖像的所有RGB數據。用一點數學算法,就可以剪出一小部分數據,並將其用作較小的圖像。

// update video feeds 
void nui_VideoFrameReady(object sender, ImageFrameReadyEventArgs e) 
{ 
    PlanarImage image = e.ImageFrame.Image; 

    // Large video feed 
    video.Source = BitmapSource.Create(image.Width, image.Height, 96, 96, PixelFormats.Bgr32, null, image.Bits, image.Width * image.BytesPerPixel); 

    // X and Y coordinates of the smaller image, and the width and height of smaller image 
    int xCoord = 100, yCoord = 150, width = 60, height = 60; 

    // Create an array to copy data into 
    byte[] bytes = new byte[width * height * image.BytesPerPixel]; 

    // Copy over the relevant bytes 
    for (int i = 0; i < height; i++) 
    { 
     for (int j = 0; j < width * image.BytesPerPixel; j++) 
     { 
      bytes[i * (width * image.BytesPerPixel) + j] = image.Bits[(i + yCoord) * (image.Width * image.BytesPerPixel) + (j + xCoord * image.BytesPerPixel)]; 
     } 
    } 

    // Create the smaller image 
    smallVideo.Source = BitmapSource.Create(width, height, 96, 96, PixelFormats.Bgr32, null, bytes, width * image.BytesPerPixel); 
} 

請確保你理解的代碼,而不是僅僅複製/粘貼它。兩個for循環用於基本數組複製,並考慮每個像素的字節數(BGR32爲4)。然後使用原始數據的小部分來創建新的BitmapSource。您需要根據需要更改寬度/高度,並根據頭部跟蹤確定X和Y座標。

+1

如何接受別人的問題的答案? ;) – Dinushan

+0

謝謝你的幫助!但這隻會保持一個位置。所以如果我想跟蹤我的頭部,當我的頭部移動時,小視頻也會移動以適應我的頭部進入圖像控制。怎麼做?無論如何,你給了我很大的幫助! – user981924

+0

@ user981924:你需要做的是使xCoord和yCoord變量全局化。然後,在SkeletonFrameReady事件中,根據頭部的位置更改這些變量。只要確保將值保留在範圍內(例如,如果xCoord或yCoord小於0,則可能會發生OutOfBoundsException異常)。 – Coeffect