コード例 #1
0
        public void synchronize(
            DepthImageFrame depthFrame,
            ColorImageFrame colorFrame,
            SkeletonFrame skletonFrame,
            Boolean isPauseMode
            )
        {
            IsPauseMode = isPauseMode;
            colorFrame.CopyPixelDataTo(_colorByte);

            //Console.WriteLine("max depth: "+depthFrame.MaxDepth);
            depthFrame.CopyDepthImagePixelDataTo(_depthPixels);

            _sensor.CoordinateMapper.MapColorFrameToDepthFrame(
                ColorImageFormat.RgbResolution640x480Fps30,
                DepthImageFormat.Resolution640x480Fps30,
                _depthPixels,
                _depthPoint
                );

            for (int i = 0; i < _pixelDepthDataLength; i++)
            {
                _depthShort[i] = (short)_depthPoint[i].Depth;
                _depthByte[i] = (byte)(_depthPoint[i].Depth*0.064-1);
            }

            skletonFrame.CopySkeletonDataTo(totalSkeleton);
            Skeleton firstSkeleton = (from trackskeleton in totalSkeleton
                                      where trackskeleton.TrackingState == SkeletonTrackingState.
                                      Tracked
                                      select trackskeleton).FirstOrDefault();

            _isCreation = true;
            if (firstSkeleton != null)
            {
                if (firstSkeleton.Joints[JointType.Spine].TrackingState == JointTrackingState.Tracked)
                {
                    IsSkeletonDetected = true;
                    UserSkeleton[SkeletonDataType.RIGHT_HAND] =
                        ScalePosition(firstSkeleton.Joints[JointType.HandRight].Position);
                    UserSkeleton[SkeletonDataType.LEFT_HAND] =
                        ScalePosition(firstSkeleton.Joints[JointType.HandLeft].Position);
                    UserSkeleton[SkeletonDataType.SPINE] =
                        ScalePosition(firstSkeleton.Joints[JointType.Spine].Position);
                    return;
                }
            }
            IsSkeletonDetected = false;
            _isCreation = false;
        }
コード例 #2
0
		public void Record(DepthImageFrame frame)
		{
			writer.Write((int) FrameType.Depth);

			var timeSpan = DateTime.Now.Subtract(referenceTime);
			referenceTime = DateTime.Now;
			writer.Write((long) timeSpan.TotalMilliseconds);
			writer.Write(frame.BytesPerPixel);
			writer.Write((int) frame.Format);
			writer.Write(frame.Width);
			writer.Write(frame.Height);

			writer.Write(frame.FrameNumber);

			var shorts = new short[frame.PixelDataLength];
            //frame.CopyPixelDataTo(shorts);

            frame.CopyDepthImagePixelDataTo(this._tmpDepthPixels);
            _sensor.CoordinateMapper.MapColorFrameToDepthFrame(
                ColorImageFormat.RgbResolution640x480Fps30,
                DepthImageFormat.Resolution640x480Fps30,
                this._tmpDepthPixels,
                this._tmpDepthPoints
                );

            for (int i = 0; i < shorts.Length; i++)
            {
                shorts[i] = (short)this._tmpDepthPoints[i].Depth;
            }

			
            try
            {
                writer.Write(shorts.Length);
                foreach (var s in shorts)
                    writer.Write(s);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
			
		}
コード例 #3
0
ファイル: Game1.cs プロジェクト: RIT-Tool-Time/Cascade
        void depthFrameSetUp(DepthImageFrame frame, DepthImagePixel[] depthPixels, Color[] depthArray, RenderTarget2D depthTarget)
        {
            depthFramyBusy = true;
            using (frame)
            {
                if (frame != null)
                {
                    //Console.WriteLine("Has frame");
                    frame.CopyDepthImagePixelDataTo(depthPixels);

                    for (int i = 0; i < depthPixels.Length; i++)
                    {

                        int b = (depthPixels[i].Depth >= frame.MinDepth && depthPixels[i].Depth <= frame.MaxDepth && depthPixels[i].IsKnownDepth) ? depthPixels[i].Depth : 0;
                        if (depthPixels[i].Depth >= frame.MaxDepth)
                        {
                            b = frame.MaxDepth;
                        }
                        float f = (float)((float)b - frame.MinDepth) / (float)(frame.MaxDepth - frame.MinDepth);

                        depthArray[i] = new Color(f, f, f, 1);
                    }

                    depthTarget.SetData(depthArray);
                    DepthImagePoint[] dip = new DepthImagePoint[kinect.ColorStream.FrameWidth * kinect.ColorStream.FrameHeight];
                    kinect.CoordinateMapper.MapColorFrameToDepthFrame(kinect.ColorStream.Format,
                        kinect.DepthStream.Format, depthPixels, dip);
                    for (int i = 0; i < depthCoordArray.Length; i++)
                    {
                        depthCoordArray[i] = new Color((float)dip[i].X / (float)kinect.ColorStream.FrameWidth, (float)dip[i].Y / (float)kinect.ColorStream.FrameHeight, 0, 1);
                    }
                    depthCoordMap.SetData(depthCoordArray);

                }
                else
                {

                }
            }
            depthFramyBusy = false;
        }
        private void ReconhecerDistancia(DepthImageFrame quadro, byte[] bytesImagem, int distanciaMaxima)
        {
            if (quadro == null || bytesImagem == null) return;

            using (quadro)
            {
                DepthImagePixel[] imagemProfundidade = new DepthImagePixel[quadro.PixelDataLength];
                quadro.CopyDepthImagePixelDataTo(imagemProfundidade);

                for (int indice = 0; indice < imagemProfundidade.Length; indice++)
                {
                    if (imagemProfundidade[indice].Depth < distanciaMaxima)
                    {
                        int indiceImageCores = indice * 4;
                        byte maiorValorCor = Math.Max(bytesImagem[indiceImageCores], Math.Max(bytesImagem[indiceImageCores + 1], bytesImagem[indiceImageCores + 2]));

                        bytesImagem[indiceImageCores] = maiorValorCor;
                        bytesImagem[indiceImageCores + 1] = maiorValorCor;
                        bytesImagem[indiceImageCores + 2] = maiorValorCor;
                    }
                }
            }
        }
コード例 #5
0
ファイル: TDepthFrame.cs プロジェクト: Styrna/TKinect
        public TDepthFrame(DepthImageFrame sensorFrame)
        {
            //TODO This can be done better
            var depthImagePixels = new DepthImagePixel[sensorFrame.PixelDataLength];
            sensorFrame.CopyDepthImagePixelDataTo(depthImagePixels);

            var depthData = new short[sensorFrame.PixelDataLength];

            for (int i = 0; i < sensorFrame.PixelDataLength; i++)
                depthData[i] = depthImagePixels[i].Depth;

            DepthData = depthData;

            PixelDataLength = sensorFrame.PixelDataLength;
            BytesPerPixel = sensorFrame.BytesPerPixel;
            FrameNumber = sensorFrame.FrameNumber;
            Width = sensorFrame.Width;
            Height = sensorFrame.Height;
            Timestamp = sensorFrame.Timestamp;

            MinDepth = sensorFrame.MinDepth;
            MaxDepth = sensorFrame.MaxDepth;
        }
コード例 #6
0
 private BitmapSource ReconhecerHumanos(DepthImageFrame quadro)
 {
     if (quadro == null) return null;
     using (quadro)
     {
         DepthImagePixel[] imagemProfundidade =
         new DepthImagePixel[quadro.PixelDataLength];
         quadro.CopyDepthImagePixelDataTo(imagemProfundidade);
         byte[] bytesImagem = new byte[imagemProfundidade.Length * 4];
         for (int indice = 0; indice < bytesImagem.Length; indice += 4)
         {
             if (imagemProfundidade[indice / 4].PlayerIndex != 0)
             {
                 bytesImagem[indice + 1] = 255;
             }
         }
         return BitmapSource.Create(quadro.Width, quadro.Height,
         96, 96, PixelFormats.Bgr32, null, bytesImagem,
         quadro.Width * 4);
     } 
 }
コード例 #7
0
ファイル: Core.cs プロジェクト: hcilab-um/STim
        private double CalculateBlockPercentage(DepthImageFrame depthFrame)
        {
            DepthImagePixel[] depthPixels;

              depthPixels = new DepthImagePixel[KinectSensor.DepthStream.FramePixelDataLength];
              depthFrame.CopyDepthImagePixelDataTo(depthPixels);
              int closePixel = 0;
              short constrain = (short)(STimSettings.CloseZoneConstrain);
              for (int i = 0; i < depthPixels.Length; ++i)
              {
            closePixel += (depthPixels[i].Depth <= constrain ? 1 : 0);
              }
              double rawPercent = (double)(closePixel * 100) / (double)depthPixels.Length;
              return DepthPercentF.ProcessNewPercentageData(rawPercent);
        }
コード例 #8
0
ファイル: MainWindow.xaml.cs プロジェクト: NebulaX/doraemon
 // Returns depth at a pixel position from a depth frame
 short getDepthAtPoint(int[] position, DepthImageFrame frame)
 {
     if (frame != null)
     {
         frame.CopyDepthImagePixelDataTo(this.depthPixels);
         int x =  depthPixels.Length;
         int pix = 640 * (position[1]) + position[0];
         short depth = depthPixels[pix].Depth;
         return depth;
     }
     else
     {
         return 0;
     }
 }
コード例 #9
0
        void GetROI(Skeleton user, DepthImageFrame depthFrame , ColorImageFrame color_frame = null)
        {

            
            // Map skeleton to Depth 
            DepthImagePoint rightHandPoint = 
                _sensor.CoordinateMapper.MapSkeletonPointToDepthPoint(user.Joints[JointType.HandRight].Position, DepthImageFormat.Resolution640x480Fps30);

            DepthImagePoint rightWristPoint =
                _sensor.CoordinateMapper.MapSkeletonPointToDepthPoint(user.Joints[JointType.WristRight].Position, DepthImageFormat.Resolution640x480Fps30);

            int hand_depth = (rightHandPoint.Depth>rightWristPoint.Depth)?rightHandPoint.Depth:rightWristPoint.Depth+10; // hand depth used for segmenting out the hand


            //*********************************** Map The depth Image to color Image to align the color image************************************************************************

            DepthImagePixel[] depthImagePixels = new DepthImagePixel[depthFrame.PixelDataLength];
            depthFrame.CopyDepthImagePixelDataTo(depthImagePixels);

            short[] rawDepthData = new short[depthFrame.PixelDataLength];
            depthFrame.CopyPixelDataTo(rawDepthData);

            ColorImagePoint[] mapped_depth_locations = new ColorImagePoint[depthFrame.PixelDataLength];

            _sensor.CoordinateMapper.MapDepthFrameToColorFrame(DepthImageFormat.Resolution640x480Fps30, depthImagePixels, ColorImageFormat.RgbResolution640x480Fps30, mapped_depth_locations);
            byte[] aligned_colorPixels = new byte[color_frame.PixelDataLength];  // creating a byte array for storing the aligned pixel values

            byte[] original_colorPixels = new byte[color_frame.PixelDataLength];
            color_frame.CopyPixelDataTo(original_colorPixels);
            int aligned_image_index = 0;
            //int hand_baseindex = rightHandPoint.Y*640 + rightHandPoint.X;
            for (int i = 0; i < mapped_depth_locations.Length; i++)
            {
                
                int depth = rawDepthData[i] >> DepthImageFrame.PlayerIndexBitmaskWidth;
                //Console.WriteLine(depth);
                ColorImagePoint point = mapped_depth_locations[i];
                
                if ((point.X >= 0 && point.X < 640) && (point.Y >= 0 && point.Y < 480))
                {
                    int baseIndex = (point.Y * 640 + point.X) * 4;
                    if (depth < hand_depth && depth != -1)
                    {
                        
                        aligned_colorPixels[aligned_image_index] = original_colorPixels[baseIndex];
                        aligned_colorPixels[aligned_image_index + 1] = original_colorPixels[baseIndex + 1];
                        aligned_colorPixels[aligned_image_index + 2] = original_colorPixels[baseIndex + 2];
                        aligned_colorPixels[aligned_image_index + 3] = 0;
                    }
                    else
                    {
                        aligned_colorPixels[aligned_image_index] = 0;
                        aligned_colorPixels[aligned_image_index + 1] = 0;
                        aligned_colorPixels[aligned_image_index + 2] = 0;
                        aligned_colorPixels[aligned_image_index + 3] = 0;
                    }
                } 
                aligned_image_index = aligned_image_index + 4;
               


                // *************************** Now modify the contents of this aligned_colorBitmap using the depth information ***************************************************
              


            }


            //***********************************************************************************************************************************************************************



            


            int threshold = 20;
            
            int hand_length = 3 * Math.Max(Math.Abs(rightHandPoint.X - rightWristPoint.X), Math.Abs(rightHandPoint.Y - rightWristPoint.Y));

          //  int hand_length = (int)Math.Sqrt((rightHandPoint.X - rightWristPoint.X) ^ 2 + (rightHandPoint.Y - rightWristPoint.Y) ^ 2);
           
            int hand_length_old = hand_length;
            //****************************Low pass filter for hand_length*********************************

            if (Math.Abs(hand_length - hand_length_old) > threshold)
                hand_length = hand_length_old;

            //************************************************************************************************

           // Console.WriteLine(hand_length);
            int top_left_X_depth = rightHandPoint.X - hand_length;
            int top_left_Y_depth = rightHandPoint.Y - hand_length;
            int top_left_Z_depth = rightHandPoint.Depth;


            top_left_X_depth = (top_left_X_depth<0)? 0 : top_left_X_depth;
            top_left_Y_depth = (top_left_Y_depth<0)? 0 : top_left_Y_depth;

            DepthImagePoint top_left = new DepthImagePoint();
            top_left.X = top_left_X_depth;
            top_left.Y = top_left_Y_depth;
            top_left.Depth = rightHandPoint.Depth;

            int bottom_right_X_depth = rightHandPoint.X + hand_length;
            int bottom_right_Y_depth = rightHandPoint.Y + hand_length;
            int bottom_right_Z_depth = rightHandPoint.Depth ;

            bottom_right_X_depth = (bottom_right_X_depth>640)? 600 : bottom_right_X_depth;
            bottom_right_Y_depth = (bottom_right_Y_depth>480)? 400 : bottom_right_Y_depth;

            DepthImagePoint bottom_right = new DepthImagePoint();
            bottom_right.X = bottom_right_X_depth;
            bottom_right.Y = bottom_right_Y_depth;
            bottom_right.Depth = bottom_right_Z_depth;

            Canvas.SetLeft(right_hand_pointer, top_left.X - right_hand_pointer.Width / 2);
            Canvas.SetTop(right_hand_pointer, top_left.Y - right_hand_pointer.Height / 2);

           


            Canvas.SetLeft(left_hand_pointer, bottom_right.X - left_hand_pointer.Width / 2);
            Canvas.SetTop(left_hand_pointer, bottom_right.Y - left_hand_pointer.Height / 2);

          border_rect.Width = 2*hand_length;
          border_rect.Height = 2*hand_length;

          Canvas.SetLeft(border_rect, top_left.X);
          Canvas.SetTop(border_rect, top_left.Y);


          aligned_colorPixelsToBitmap(aligned_colorPixels, color_frame, (int)top_left.X, (int)top_left.Y, (int)border_rect.Width, (int)border_rect.Height);   

           
        
        }
コード例 #10
0
ファイル: ThereminGame.cs プロジェクト: niallwingham/theremin
        private void ProcessDepthAndSkeletonFrames(DepthImageFrame depthFrame, SkeletonFrame skeletonFrame)
        {
            depthFrame.CopyDepthImagePixelDataTo(depthFrameData);
            coordinateMapper.MapDepthFrameToSkeletonFrame(DepthFormat, depthFrameData, skeletonPointData);

            skeletonFrame.CopySkeletonDataTo(skeletonFrameData);
            Skeleton skeleton = skeletonFrameData.FirstOrDefault(s => s.TrackingState == SkeletonTrackingState.Tracked);
            if (skeleton != null)
            {
                hipCenter = skeleton.Joints[JointType.HipCenter];
                handLeft = skeleton.Joints[JointType.HandLeft];
                handRight = skeleton.Joints[JointType.HandRight];
            }

            if (handLeft.TrackingState != JointTrackingState.NotTracked && handRight.TrackingState != JointTrackingState.NotTracked)
            {
                LocateHands();
                IdentifyGestures(LeftHand);
            }
        }
コード例 #11
0
        int find_code(ColorImageFrame colorFrame, DepthImageFrame depthFrame)
        {
            ZXing.Kinect.BarcodeReader reader = new ZXing.Kinect.BarcodeReader();
            if (colorFrame != null)
            {
                //Decode the colorFrame
                var result = reader.Decode(colorFrame);
                if (result != null)
                {
                    string val = result.Text;
                    int code_num = Convert.ToInt32(val);
                    double center_x = result.ResultPoints[0].X + 0.5 * (result.ResultPoints[2].X - result.ResultPoints[0].X);
                    double center_y = result.ResultPoints[0].Y + 0.5 * (result.ResultPoints[2].Y - result.ResultPoints[0].Y);

                    code_size = new Point((result.ResultPoints[2].X - result.ResultPoints[0].X), (result.ResultPoints[2].Y - result.ResultPoints[0].Y));

                    // Must mirror the coordinate here -- the depth frame comes in mirrored.
                    center_x = 640 - center_x;

                    // Map the color frame onto the depth frame
                    DepthImagePixel[] depthPixel = new DepthImagePixel[depthFrame.PixelDataLength];
                    depthFrame.CopyDepthImagePixelDataTo(depthPixel);
                    DepthImagePoint[] depthImagePoints = new DepthImagePoint[sensor.DepthStream.FramePixelDataLength];
                    sensor.CoordinateMapper.MapColorFrameToDepthFrame(sensor.ColorStream.Format, sensor.DepthStream.Format, depthPixel, depthImagePoints);

                    // Get the point in the depth frame at the center of the barcode
                    int center_point_color_index = (int)center_y * 640 + (int)center_x;
                    DepthImagePoint converted_depth_point = depthImagePoints[center_point_color_index];
                    Point p = new Point(converted_depth_point.X, converted_depth_point.Y);
                    code_points[code_num] = p;

                    Console.WriteLine("Found code " + code_num + " at (" + center_x + ", " + center_y + ") in color coordinates.");
                    Console.WriteLine("Translated to (" + p.X + ", " + p.Y + ") in depth coordinates.");
                    return code_num;
                }
            }

            return -1;
        }
 public void incomingDepthFrame(DepthImageFrame _df)
 {
         _df.CopyDepthImagePixelDataTo(_depthPixels);
 }
        private void handleDepthImageFrame(DepthImageFrame depthFrame)
        {
            using (depthFrame)
            {
                if (depthFrame != null)
                {
                    DepthImagePixel[] depthPixels = new DepthImagePixel[depthFrame.PixelDataLength];
                    depthFrame.CopyDepthImagePixelDataTo(depthPixels);

                    ThreadPool.QueueUserWorkItem(new WaitCallback(o => DepthFrameCallback(depthFrame.Timestamp, depthFrame.FrameNumber, depthPixels)));
                }
            }
        }
コード例 #14
0
ファイル: CamDisplay.xaml.cs プロジェクト: juliusba/QuadKin
        private void drawWhiteOnBlack(DepthImageFrame depthFrame)
        {
            depthFrame.CopyDepthImagePixelDataTo(this.depthPixels);

            // Get the min and max reliable depth for the current players
            //int minDepth = depthPixels[0].Depth;
            //int maxDepth = depthPixels[0].Depth;
            //double avgDepth = 0;
            //int playerPixelCount = 0;

            //for (int i = 0; i < this.depthPixels.Length; ++i)
            //{
            //    if (depthPixels[i].PlayerIndex == 1)
            //    {
            //        short depth = this.depthPixels[i].Depth;
            //        minDepth = Math.Min(depth, minDepth);
            //        maxDepth = Math.Max(depth, maxDepth);
            //        avgDepth += depth;
            //        playerPixelCount++;
            //    }
            //}

            //avgDepth /= playerPixelCount;

            // Convert the depth to RGB
            int colorPixelIndex = 0;
            for (int i = 0; i < this.depthPixels.Length; ++i)
            {
                if (depthPixels[i].PlayerIndex != 0)
                {
                    this.depthColorPixels[colorPixelIndex++] = (byte)255;
                    this.depthColorPixels[colorPixelIndex++] = (byte)255;
                    this.depthColorPixels[colorPixelIndex++] = (byte)255;
                    this.depthColorPixels[colorPixelIndex++] = (byte) this.depthAlpha;

                    //short depth = this.depthPixels[i].Depth;

                    //// Write out blue byte
                    //this.colorPixels[colorPixelIndex++] = (byte)Math.Min(depth - avgDepth, 0);
                    //// Write out green byte
                    //this.colorPixels[colorPixelIndex++] = (byte)maxDepth;
                    //// Write out red byte
                    //this.colorPixels[colorPixelIndex++] = (byte)Math.Min(avgDepth - depth, 0);
                    //// Write alpha if Bgra, else unused...
                    //++colorPixelIndex;
                }
                else
                {

                    byte intensity = (byte)0;

                    // Write out blue byte
                    this.depthColorPixels[colorPixelIndex++] = intensity;
                    // Write out green byte
                    this.depthColorPixels[colorPixelIndex++] = intensity;
                    // Write out red byte
                    this.depthColorPixels[colorPixelIndex++] = intensity;
                    // Write alpha if Bgra, else unused...
                    this.depthColorPixels[colorPixelIndex++] = (byte) this.depthAlpha;
                }
            }

            this.depthColorBitmap.WritePixels(
                new Int32Rect(0, 0, this.depthColorBitmap.PixelWidth, this.depthColorBitmap.PixelHeight),
                this.depthColorPixels,
                this.depthColorBitmap.PixelWidth * sizeof(int),
                0);
        }
コード例 #15
0
        private void ReconhecerDistancia(DepthImageFrame quadro, byte[] bytesImagem, int maxDistancia)
        {
            if (quadro == null || bytesImagem == null)
                return;
            
                using (quadro)
                {
                    DepthImagePixel[] imagemProfundidade = new DepthImagePixel[quadro.PixelDataLength];
                    quadro.CopyDepthImagePixelDataTo(imagemProfundidade);

                    DepthImagePoint[] pontosImagemProfundidade = new DepthImagePoint[640 * 480];

                    Kinect.CoordinateMapper
                            .MapColorFrameToDepthFrame(Kinect.ColorStream.Format,
                                                       Kinect.DepthStream.Format, imagemProfundidade,
                                                       pontosImagemProfundidade);

                    for (int i = 0; i < pontosImagemProfundidade.Length; i++)
                    {
                        var point = pontosImagemProfundidade[i];
                        if (point.Depth < maxDistancia && KinectSensor.IsKnownPoint(point))
                        {
                            var pixelDataIndex = i * 4;
                            byte maiorValorCor =
                            Math.Max(bytesImagem[pixelDataIndex],
                            Math.Max(bytesImagem[pixelDataIndex + 1],
                            bytesImagem[pixelDataIndex + 2]));
                            bytesImagem[pixelDataIndex] = maiorValorCor;
                            bytesImagem[pixelDataIndex + 1] = maiorValorCor;
                            bytesImagem[pixelDataIndex + 2] = maiorValorCor;
                        }
                    }
                }
        }
        private void FuncoesProfundidade(DepthImageFrame quadro, byte[] bytesImagem, int distanciaMaxima)
        {
            if (quadro == null || bytesImagem == null) return;

            using (quadro)
            {
                DepthImagePixel[] imagemProfundidade = new DepthImagePixel[quadro.PixelDataLength];
                quadro.CopyDepthImagePixelDataTo(imagemProfundidade);

                if (btnDesenhar.IsChecked)
                    fluxoInteracao.ProcessDepth(imagemProfundidade, quadro.Timestamp);
                else if (btnEscalaCinza.IsChecked)
                    ReconhecerProfundidade(bytesImagem, distanciaMaxima, imagemProfundidade);
            }
        }
コード例 #17
0
        public BitmapSource WriteToBitmap( DepthImageFrame frame )
        {
            // Mostly from: http://msdn.microsoft.com/en-us/library/jj131029.aspx

            if ( ( null == _depthPixels ) || ( _depthPixels.Length != frame.PixelDataLength ) )
            {
                this._depthPixels = new DepthImagePixel[_kinectSensor.DepthStream.FramePixelDataLength];
                this._colorPixels = new byte[_kinectSensor.DepthStream.FramePixelDataLength * sizeof( int )];
            }

            frame.CopyDepthImagePixelDataTo( _depthPixels );

            if( null == _writeableBitmap || _writeableBitmap.Format != PixelFormats.Bgra32 )
            {
                this._writeableBitmap = new WriteableBitmap(
                    _kinectSensor.DepthStream.FrameWidth,
                    _kinectSensor.DepthStream.FrameHeight,
                    96.0,
                    96.0,
                    PixelFormats.Bgr32,
                    null
                    );
            }

            // Get the min and max reliable depth for the current frame
            int minDepth = frame.MinDepth;
            int maxDepth = frame.MaxDepth;

            // Convert the depth to RGB
            int colorPixelIndex = 0;
            for ( int i = 0; i < this._depthPixels.Length; ++i )
            {
                // Get the depth for this pixel
                short depth = _depthPixels[i].Depth;

                // To convert to a byte, we're discarding the most-significant
                // rather than least-significant bits.
                // We're preserving detail, although the intensity will "wrap."
                // Values outside the reliable depth range are mapped to 0 (black).

                // Note: Using conditionals in this loop could degrade performance.
                // Consider using a lookup table instead when writing production code.
                // See the KinectDepthViewer class used by the KinectExplorer sample
                // for a lookup table example.
                byte intensity = (byte)( depth >= minDepth && depth <= maxDepth ? depth : 0 );

                // Write out blue byte
                this._colorPixels[colorPixelIndex++] = intensity;

                // Write out green byte
                this._colorPixels[colorPixelIndex++] = intensity;

                // Write out red byte
                this._colorPixels[colorPixelIndex++] = intensity;

                // We're outputting BGR, the last byte in the 32 bits is unused so skip it
                // If we were outputting BGRA, we would write alpha here.
                ++colorPixelIndex;
            }

            this._writeableBitmap.WritePixels(
                new Int32Rect( 0, 0, this._writeableBitmap.PixelWidth, this._writeableBitmap.PixelHeight ),
                this._colorPixels,
                this._writeableBitmap.PixelWidth * sizeof( int ),
            0 );

            return _writeableBitmap;
        }
コード例 #18
0
ファイル: Form1.cs プロジェクト: nerndt/iRobotKinect
        private Bitmap DepthImageFrameToBitmap(DepthImageFrame depthFrame)
        {
            DepthImagePixel[] depthPixels = new DepthImagePixel[depthFrame.PixelDataLength];
            byte[] colorPixels = new byte[depthFrame.PixelDataLength * 4];
            depthFrame.CopyDepthImagePixelDataTo(depthPixels);

            // Get the min and max reliable depth for the current frame
            int minDepth = depthFrame.MinDepth;
            int maxDepth = depthFrame.MaxDepth;

            // Convert the depth to RGB
            int colorPixelIndex = 0;
            for (int i = 0; i < depthPixels.Length; ++i)
            {
                // Get the depth for this pixel
                short depth = depthPixels[i].Depth;

                // To convert to a byte, we're discarding the most-significant
                // rather than least-significant bits.
                // We're preserving detail, although the intensity will "wrap."
                // Values outside the reliable depth range are mapped to 0 (black).

                // NOTE: Using conditionals in this loop could degrade performance.
                // Consider using a lookup table instead when writing production code.
                // See the KinectDepthViewer class used by the KinectExplorer sample
                // for a lookup table example.
                byte intensity = (byte)(depth >= minDepth && depth <= maxDepth ? depth : 0);

                // Write out blue byte
                colorPixels[colorPixelIndex++] = intensity;

                // Write out green byte
                colorPixels[colorPixelIndex++] = intensity;

                // Write out red byte
                colorPixels[colorPixelIndex++] = intensity;

                // We're outputting BGR, the last byte in the 32 bits is unused so skip it
                // If we were outputting BGRA, we would write alpha here.
                ++colorPixelIndex;
            }
            Bitmap bitmapFrame = ArrayToBitmap(colorPixels, depthFrame.Width, depthFrame.Height, PixelFormat.Format32bppRgb);
            return bitmapFrame;
        }
コード例 #19
0
ファイル: MainWindow.xaml.cs プロジェクト: Cocotus/kinect
        private byte[] GenerateColoredBytes(DepthImageFrame depthFrame)
        {
            DepthImagePixel[] depthPoints = new DepthImagePixel[depthFrame.PixelDataLength];
            depthFrame.CopyDepthImagePixelDataTo(depthPoints); 
            

            //use depthFrame to create the image to display on-screen
            //depthFrame contains color information for all pixels in image
            //Height x Width x 4 (Red, Green, Blue, empty byte)


            //Bgr32  - Blue, Green, Red, empty byte
            //Bgra32 - Blue, Green, Red, transparency 
            //You must set transparency for Bgra as .NET defaults a byte to 0 = fully transparent

            //hardcoded locations to Blue, Green, Red (BGR) index positions       
     
            
            //loop through all distances
            //pick a RGB color based on distance 

            _allPlayers.Clear();

            for (int depthIndex = 0, colorIndex = 0; 
                depthIndex < depthPoints.Length && colorIndex < _pixels.Length; 
                depthIndex++, colorIndex += 4)
            {

                //gets the depth value
                int depth = depthPoints[depthIndex].Depth;

                // 900 mm or 2.95'
                if (depth <= 900)
                {
                    //we are very close
                    _pixels[colorIndex + BlueIndex] = 255;
                    _pixels[colorIndex + GreenIndex] = 0;
                    _pixels[colorIndex + RedIndex] = 0;

                }
                // 900 mm - 2K+ mm or 2.95' - 6.56'
                else if (depth > 900 && depth <= 2000)
                {
                    //we are a bit further away
                    _pixels[colorIndex + BlueIndex] = 0;
                    _pixels[colorIndex + GreenIndex] = 255;
                    _pixels[colorIndex + RedIndex] = 0;
                }
                // 2K+ mm or 6.56'+
                else if (depth > 2000)
                {
                    //we are the farthest
                    _pixels[colorIndex + BlueIndex] = 0;
                    _pixels[colorIndex + GreenIndex] = 0;
                    _pixels[colorIndex + RedIndex] = 255;
                }

                // get the player (requires skeleton tracking enabled for values)
                int player = depthPoints[depthIndex].PlayerIndex;

                //Color all players "yellow"
                if (player > 0)
                {
                    _allPlayers.Add(depthPoints[depthIndex]);
                    _pixels[colorIndex + BlueIndex] = 18;
                    _pixels[colorIndex + GreenIndex] = 250;
                    _pixels[colorIndex + RedIndex] = 255;
                }
            }

            if (_allPlayers.Count > 0)
            {
                var avg = (from p in _allPlayers select Convert.ToDouble(p.Depth)).Average();
                timer.Stop();

                //Convert to feet and display
                //Calculate speed by change in distance over change in time
                //Subtract oldAvg from avg to get the displacement, then use the timer's
                //ElapsedMilliseconds variable to divide by the change in time
                var feet = ConvertToFeet(Math.Abs(avg - oldAvg));
                var speed = feet / (timer.ElapsedMilliseconds / 1000.0);
                txtDistance.Text = String.Format("{0:0.##} ft/s", speed);
                oldAvg = avg;
                timer.Reset();
                timer.Start();
            }

            return _pixels;
        }