private async void Window_Loaded(object sender, RoutedEventArgs e)
        {
            while (true)
            {
                // Get Capture Frame
                using (K4A.Capture capture = await Task.Run(() => { return(this.device.GetCapture()); }))
                {
                    // Enque Capture
                    tracker.EnqueueCapture(capture);

                    // Pop Result
                    using (K4ABT.Frame frame = tracker.PopResult())
                        // Get Color Image
                        using (K4A.Image color_image = frame.Capture.Color)
                        {
                            // Get Color Buffer and Write Bitmap
                            byte[] color_buffer = color_image.Memory.ToArray();
                            color_bitmap.WritePixels(color_rect, color_buffer, color_stride, 0, 0);

                            // Clear All Ellipse from Canvas
                            Canvas_Body.Children.Clear();

                            // Draw Skeleton
                            for (uint body_index = 0; body_index < frame.NumberOfBodies; body_index++)
                            {
                                // Get Skeleton and ID
                                K4ABT.Skeleton skeleton = frame.GetBodySkeleton(body_index);
                                uint           id       = frame.GetBodyId(body_index);

                                // Draw Joints
                                for (int joint_index = 0; joint_index < (int)K4ABT.JointId.Count; joint_index++)
                                {
                                    // Get Joint and Position
                                    K4ABT.Joint joint    = skeleton.GetJoint(joint_index);
                                    Vector2?    position = calibration.TransformTo2D(joint.Position, K4A.CalibrationDeviceType.Depth, K4A.CalibrationDeviceType.Color);

                                    if (!position.HasValue)
                                    {
                                        continue;
                                    }

                                    // Create Ellipse
                                    const int       radius       = 10;
                                    SolidColorBrush stroke_color = new SolidColorBrush(colors[id % colors.Length]);
                                    SolidColorBrush fill_color   = new SolidColorBrush((joint.ConfidenceLevel >= K4ABT.JointConfidenceLevel.Medium) ? colors[id % colors.Length] : Colors.Transparent);
                                    Ellipse         ellipse      = new Ellipse()
                                    {
                                        Width = radius, Height = radius, StrokeThickness = 1, Stroke = stroke_color, Fill = fill_color
                                    };

                                    // Add Ellipse to Canvas
                                    Canvas.SetLeft(ellipse, position.Value.X - (radius / 2));
                                    Canvas.SetTop(ellipse, position.Value.Y - (radius / 2));
                                    Canvas_Body.Children.Add(ellipse);
                                }
                            }
                        }
                }
            }
        }
Exemplo n.º 2
0
        private async void Window_Loaded(object sender, RoutedEventArgs e)
        {
            while (true)
            {
                // Get Capture Frame
                using (K4A.Capture capture = await Task.Run(() => { return(this.device.GetCapture()); }))
                {
                    // Enqueue Tracker
                    tracker.EnqueueCapture(capture);

                    // Pop Result
                    using (K4ABT.Frame frame = tracker.PopResult())
                        // Get Color Image
                        using (K4A.Image color_image = frame.Capture.Color)
                        {
                            // Get Color Buffer and Write Bitmap
                            byte[] color_buffer = color_image.Memory.ToArray();
                            color_bitmap.WritePixels(color_rect, color_buffer, color_stride, 0, 0);

                            // Transform Body Index Map to Color Camera
                            (K4A.Image _, K4A.Image body_index_map)reult =
                                transformation.DepthImageToColorCameraCustom(
                                    frame.Capture.Depth,
                                    frame.BodyIndexMap,
                                    K4A.TransformationInterpolationType.Nearest,
                                    K4ABT.Frame.BodyIndexMapBackground
                                    );

                            //Get Body Index Map Buffer
                            byte[] body_index_map_buffer = reult.body_index_map.Memory.ToArray();

                            // Draw Body Index Map and Write Bitmap
                            Parallel.For(0, body_index_map_buffer.Length, i =>
                            {
                                uint index = (uint)body_index_map_buffer[i];
                                if (index != K4ABT.Frame.BodyIndexMapBackground)
                                {
                                    Color color = colors[index % colors.Length];
                                    colorized_body_index_map_buffer[i * 4 + 0] = color.B;
                                    colorized_body_index_map_buffer[i * 4 + 1] = color.G;
                                    colorized_body_index_map_buffer[i * 4 + 2] = color.R;
                                    colorized_body_index_map_buffer[i * 4 + 3] = color.A;
                                }
                                else
                                {
                                    colorized_body_index_map_buffer[i * 4 + 0] = 0;
                                    colorized_body_index_map_buffer[i * 4 + 1] = 0;
                                    colorized_body_index_map_buffer[i * 4 + 2] = 0;
                                    colorized_body_index_map_buffer[i * 4 + 3] = 255;
                                }
                            });
                            body_index_map_bitmap.WritePixels(body_index_map_rect, colorized_body_index_map_buffer, body_index_map_stride, 0, 0);

                            // Clear Body Index Map
                            reult.body_index_map.Dispose();
                        }
                }
            }
        }