2011-10-07 115 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