예제 #1
0
        //Show initial image
        private async Task <BitmapSource> ShowInitImage(Capture capture, Transformation transform)
        {
            BitmapSource initImageBitmap = null;
            // Create a BitmapSource for the unmodified color image.
            // Creating the BitmapSource is slow, so do it asynchronously on another thread
            Task <BitmapSource> createColorBitmapTask = new Task <BitmapSource>(() =>
            {
                BitmapSource source = capture.Color.CreateBitmapSource();

                // Allow the bitmap to move threads
                source.Freeze();
                return(source);
            });

            // Compute the colorized output bitmap on a thread pool thread
            Task <BitmapSource> createDepthBitmapTask = new Task <BitmapSource>(() =>
            {
                int colorWidth  = this.KinectDevice.GetCalibration().ColorCameraCalibration.ResolutionWidth;
                int colorHeight = this.KinectDevice.GetCalibration().ColorCameraCalibration.ResolutionHeight;
                // Allocate image buffers for us to manipulate
                var transformedDepth = new Microsoft.Azure.Kinect.Sensor.Image(ImageFormat.Depth16, colorWidth, colorHeight);
                var outputColorImage = new Microsoft.Azure.Kinect.Sensor.Image(ImageFormat.ColorBGRA32, colorWidth, colorHeight);
                // Transform the depth image to the perspective of the color camera
                transform.DepthImageToColorCamera(capture, transformedDepth);

                // Get Span<T> references to the pixel buffers for fast pixel access.
                Span <ushort> depthBuffer     = transformedDepth.GetPixels <ushort>().Span;
                Span <BGRA> colorBuffer       = capture.Color.GetPixels <BGRA>().Span;
                Span <BGRA> outputColorBuffer = outputColorImage.GetPixels <BGRA>().Span;

                // Create an output color image with data from the depth image
                for (int i = 0; i < colorBuffer.Length; i++)
                {
                    // The output image will be the same as the input color image,
                    // but colorized with Red where there is no depth data, and Green
                    // where there is depth data at more than 1.5 meters
                    outputColorBuffer[i] = colorBuffer[i];

                    if (depthBuffer[i] == 0)
                    {
                        outputColorBuffer[i].R = 255;
                    }
                    else if (depthBuffer[i] > 1500)
                    {
                        outputColorBuffer[i].G = 255;
                    }
                }

                BitmapSource source = outputColorImage.CreateBitmapSource();

                // Allow the bitmap to move threads
                source.Freeze();

                return(source);
            });

            if (this.sysState.DeviceDepthMode)
            {
                createDepthBitmapTask.Start();
                initImageBitmap = await createDepthBitmapTask.ConfigureAwait(false);
            }
            else
            {
                createColorBitmapTask.Start();
                initImageBitmap = await createColorBitmapTask.ConfigureAwait(false);
            }

            return(initImageBitmap);
        }