Kinect背景删除

时间:2011-11-12 21:41:45

标签: kinect

我按照Robert Levy在此链接提供的代码:http://channel9.msdn.com/coding4fun/kinect/Display-Kinect-color-image-containing-only-players-aka-background-removal

我尝试将其实现到现有代码中,并且结果不一致。如果用户在程序启动时处于kinect的视野中,则会在某些时候删除背景。如果用户走进视野,它将无法接收它们。

    namespace KinectUserRecognition
    {
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }

            //Kinect Runtime
            Runtime kinect = Runtime.Kinects[0];

            PlanarImage colorImage;
            PlanarImage depthImage;
            bool isDepthImage;
            WriteableBitmap player1;


            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                isDepthImage = false;
                //UseDepthAndPlayerIndex and UseSkeletalTracking
                kinect.Initialize(RuntimeOptions.UseDepthAndPlayerIndex | RuntimeOptions.UseColor);// | RuntimeOptions.UseSkeletalTracking);

                //register for event
                kinect.VideoFrameReady += new EventHandler<ImageFrameReadyEventArgs>(nui_VideoFrameReady);
                kinect.DepthFrameReady += new EventHandler<ImageFrameReadyEventArgs>(nui_DepthFrameReady);

                //Video image type
                kinect.VideoStream.Open(ImageStreamType.Video, 2, ImageResolution.Resolution640x480, 
                    ImageType.Color);

                //DepthAndPlayerIndex ImageType
                kinect.DepthStream.Open(ImageStreamType.Depth, 2, ImageResolution.Resolution320x240,
                    ImageType.DepthAndPlayerIndex);

            }

            void nui_VideoFrameReady(object sender, ImageFrameReadyEventArgs e)
            {
                colorImage = e.ImageFrame.Image;
                image1.Source = BitmapSource.Create(colorImage.Width, colorImage.Height, 96, 96,
                    PixelFormats.Bgr32, null, colorImage.Bits, colorImage.Width * colorImage.BytesPerPixel);

                if (isDepthImage)
                {
                    player1 = GeneratePlayerImage(e.ImageFrame, 1);
                    image3.Source = player1;
                }
            }

            void nui_DepthFrameReady(object sender, ImageFrameReadyEventArgs e)
            {
                //Convert depth information for a pixel into color information
                byte[] ColoredBytes = GenerateColoredBytes(e.ImageFrame);

                depthImage = e.ImageFrame.Image;
                image2.Source = BitmapSource.Create(depthImage.Width, depthImage.Height, 96, 96, PixelFormats.Bgr32, null,
                    ColoredBytes, depthImage.Width * PixelFormats.Bgr32.BitsPerPixel / 8);

                isDepthImage = true;
            }

            private WriteableBitmap GeneratePlayerImage(ImageFrame imageFrame, int playerIndex)
            {
                int depthWidth = kinect.DepthStream.Width;
                int depthHeight = kinect.DepthStream.Height;

                WriteableBitmap target = new WriteableBitmap(depthWidth, depthHeight, 96, 96, PixelFormats.Bgra32, null);
                var depthRect = new System.Windows.Int32Rect(0, 0, depthWidth, depthHeight);

                byte[] color = imageFrame.Image.Bits;

                byte[] output = new byte[depthWidth * depthHeight * 4];

                //loop over each pixel in the depth image
                int outputIndex = 0;
                for (int depthY = 0, depthIndex = 0; depthY < depthHeight; depthY++)
                {
                    for(int depthX = 0; depthX < depthWidth; depthX++, depthIndex +=2)
                    {

                        short depthValue = (short)(depthImage.Bits[depthIndex] | (depthImage.Bits[depthIndex + 1] << 8));

                        int colorX, colorY;
                        kinect.NuiCamera.GetColorPixelCoordinatesFromDepthPixel(
                            imageFrame.Resolution,
                            imageFrame.ViewArea,
                            depthX, depthY, //depth coordinate
                            depthValue,     //depth value
                            out colorX, out colorY); //color coordinate

                        //ensure that the calculate color location is within the bounds of the image
                        colorX = Math.Max(0, Math.Min(colorX, imageFrame.Image.Width - 1));
                        colorY = Math.Max(0, Math.Min(colorY, imageFrame.Image.Height - 1));

                        output[outputIndex++] = color[(4 * (colorX + (colorY * imageFrame.Image.Width))) + 0];
                        output[outputIndex++] = color[(4 * (colorX + (colorY * imageFrame.Image.Width))) + 1];
                        output[outputIndex++] = color[(4 * (colorX + (colorY * imageFrame.Image.Width))) + 2];
                        output[outputIndex++] = GetPlayerIndex(depthImage.Bits[depthIndex]) == playerIndex ? (byte)255 : (byte)0;
                    }
                }
                target.WritePixels(depthRect, output, depthWidth * PixelFormats.Bgra32.BitsPerPixel / 8, 0);
                return target;
                //return output;
            }

            private static int GetPlayerIndex(byte firstFrame)
            {
                //returns 0 = no player, 1 = 1st player, 2 = 2nd player...
                //bitwise & on firstFrame
                return (int)firstFrame & 7;
            }
        }
}

-Edit 1 -

我认为我已经缩小了问题范围,但我不确定是否有办法解决问题。我假设在kinect的视野中只有一个人会从我的“GetPlayerIndex”方法返回一个值。不是这种情况。我希望为每个人制作一个单独的图像,并删除背景。我应该假设接收什么类型的值:

-Edit 2 -

从我的测试中我发现玩家索引的最大值为6,但我得到的索引并不一致。如果有办法知道将哪个玩家索引分配给骨架?例如,如果我是fov中唯一的人,那么有办法知道我的玩家指数总是1吗?

1 个答案:

答案 0 :(得分:2)

玩家指数不保证是任何东西。一旦它抓住一个骨架,索引将保持相同的骨架,直到它看不到它,但你不能假设第一个玩家将是1,第二个2,等等。

您需要做的是在player1 = GeneratePlayerImage(e.ImageFrame, 1);调用之前确定有效的框架索引,或者更改GeneratePlayerImage函数以查找索引。如果您只想移除背景并保留框架中所有人的像素不变,请更改此信息:

output[outputIndex++] = GetPlayerIndex(depthImage.Bits[depthIndex]) == playerIndex ? (byte)255 : (byte)0;

这将只检查任何玩家,而不是特定玩家:

output[outputIndex++] = GetPlayerIndex(depthImage.Bits[depthIndex]) != 0 ? (byte)255 : (byte)0;

我可以想到另外两种方式为特定玩家而不是所有玩家做这件事:

  1. 打开Kinect的Skeleton提要,并循环遍历骨架数组,它可以让您找到有效的索引。创建一个全局整数来保存此索引,然后使用此全局整数调用GeneratePlayerImage方法。
  2. 更改GeneratePlayerImage方法以检查每个像素中的玩家索引,如果找到一个,则使用该索引删除整个图像的背景(忽略它找到的任何其他索引)。