Example #1
0
        void StartDepthHandler(Int32 apiVersion, KinectImageResolution resolution)
        {
            var bitmap = new Bitmap(resolution.Width(), resolution.Height());
            var buffer = new Int16[resolution.Width() * resolution.Height()];


            if (apiVersion == 1)
            {
                Action<KinectBaseImageFrame> UpdatePictureBox = frame =>
                {
                    Marshal.Copy(frame.Ptr, buffer, 0, buffer.Length);
                    bitmap.SetPixels(buffer.SelectMany(d =>
                    {
                        var depth = ((Int32)d) >> 3;
                        if (depth > _tmpMaxDepth)
                        {
                            _tmpMaxDepth = depth;
                        }

                        var player= ((Int32)d) & 0x7;
                        var color = ColorMap[player];

                        return new Byte[]{
                        (Byte)(color.R * depth / MaxDepth)
                        , (Byte)(color.G * depth / MaxDepth)
                        , (Byte)(color.B * depth / MaxDepth)
                        , 255
                    };
                    }).ToArray());

                    pictureBoxForDepth.Image = bitmap;
                    if (_tmpMaxDepth > MaxDepth)
                    {
                        MaxDepth = _tmpMaxDepth;
                    }
                };
                _depthHandler = _timerEvent
                    .Where(_ => _sensor.DepthImageStream != null)
                    .Subscribe(_ => Observable.Using(
                        () => _sensor.DepthImageStream.GetFrame()
                        , frame => Observable.Return(frame)
                        )
                        .Where(frame => frame != null)
                        .Subscribe(
                            frame => UpdatePictureBox(frame)
                            , ex => {} // Console.WriteLine("error")
                        )
                    )
                    ;
            }
            else if(apiVersion==2)
            {
                Action<IEnumerable<Int16>, Byte[]> UpdatePictureBox = (db, ib) =>
                {
                    bitmap.SetPixels(db.Zip(ib.Select(index => (SByte)index), (depth, player) =>
                    {
                        if (depth > _tmpMaxDepth)
                        {
                            _tmpMaxDepth = depth;
                        }

                        var color = ColorMap[player+1];

                        return new Byte[]{
                        (Byte)(color.R * depth / MaxDepth)
                        , (Byte)(color.G * depth / MaxDepth)
                        , (Byte)(color.B * depth / MaxDepth)
                        , 255
                        };
                    }).SelectMany(pixel => pixel).ToArray());
                    pictureBoxForDepth.Image = bitmap;
                    if (_tmpMaxDepth > MaxDepth)
                    {
                        MaxDepth = _tmpMaxDepth;
                    }
                };

                var depthImage = _timerEvent
                    .Where(_ => _sensor.DepthImageStream != null)
                    .SelectMany(_ => Observable.Using(
                        ()=>_sensor.DepthImageStream.GetFrame()
                        , frame=>Observable.Return(frame)
                     )
                     .Catch((COMException ex)=>Observable.Return((KinectBaseImageFrame)null))
                     .Where(frame=>frame!=null)
                     .Select(frame =>
                     {
                         Marshal.Copy(frame.Ptr, buffer, 0, buffer.Length);
                         return new { Buffer = buffer, Time = frame.Time };
                     }
                     ))
                    ;

                var indexBuffer=new Byte[resolution.Width()*resolution.Height()];
                var indexImage = _timerEvent
                    .Where(_ => _sensor.IndexImageStream != null)
                    .SelectMany(_ => Observable.Using(
                        () => _sensor.IndexImageStream.GetFrame()
                            , frame => Observable.Return(frame)
                                )
                     .Catch((COMException ex) => Observable.Return((KinectBaseImageFrame)null))
                     .Where(frame => frame != null)
                                .Select(frame =>
                                {
                                    Marshal.Copy(frame.Ptr, indexBuffer, 0, indexBuffer.Length);
                                    return new { Buffer = indexBuffer, Time = frame.Time };
                                }))
                                ;

                _depthHandler = depthImage.CombineLatest(indexImage,
                    (l, r) => new { Depth = l, Index = r })
                    .Where(pair => pair.Depth.Time == pair.Index.Time)
                    .Subscribe(
                        pair => UpdatePictureBox(pair.Depth.Buffer, pair.Index.Buffer)
                        );
                    ;
            }
            else
            {
                throw new ArgumentException("apiVersion");
            }

        }
Example #2
0
        void StartColorImageHandler(KinectImageResolution resolution)
        {
            var bitmap = new Bitmap(resolution.Width(), resolution.Height());
            var buffer=new Byte[resolution.Width() * resolution.Height() *4 ];
            Action<KinectBaseImageFrame> UpdatePictureBox = frame =>
            {
                frame.CopyTo(buffer);
                bitmap.SetPixels(buffer);
                pictureBoxForImage.Image = bitmap;
            };

            _colorImageHandler = _timerEvent
                .Where(_ => _sensor.ColorImageStream != null)
                .Subscribe(_ => Observable.Using(
                    () => _sensor.ColorImageStream.GetFrame()
                    , frame => Observable.Return(frame)
                    )
                    .Where(frame => frame != null)
                    .Subscribe(
                        frame => UpdatePictureBox(frame)
                        , ex => { } // Console.WriteLine("error")
                    )
                )
                ;
        }