/// <summary>
        /// Map PointDepth3D to PointSkeleton3D
        /// </summary>
        /// <param name="depthImageFormat"></param>
        /// <param name="pointDepth3D"></param>
        /// <returns></returns>
        public PointSkeleton3D MapDepthPointToSketelonPoint(DepthImageFormat depthImageFormat, PointDepth3D pointDepth3D)
        {
            DepthImagePoint point = new DepthImagePoint();
            point.X = pointDepth3D.X;
            point.Y = pointDepth3D.Y;
            point.Depth = pointDepth3D.Depth;

            return new PointSkeleton3D(mapper.MapDepthPointToSkeletonPoint(depthImageFormat, point));
        }
Exemplo n.º 2
0
        public Hand(PointDepth3D[] pixel3D, PointDepth3D trackPalm, int RWidth = 100, int RHeight = 100)
        {
            this.pixelDepth3D = pixel3D;
            this.kinectTrackPalm = trackPalm;
            this.kinectPalmDetph = trackPalm.Depth;
            this.rectWidth = RWidth;
            this.rectHeight = RHeight;

            contourPixels = new List<Point2D>();
            interiorPixels = new List<Point2D>();
        }
Exemplo n.º 3
0
        public Hand(PointDepth3D[] pixel3D, PointDepth3D trackPalm, int RWidth = 100, int RHeight = 100)
        {
            this.pixelDepth3D    = pixel3D;
            this.kinectTrackPalm = trackPalm;
            this.kinectPalmDetph = trackPalm.Depth;
            this.rectWidth       = RWidth;
            this.rectHeight      = RHeight;

            contourPixels  = new List <Point2D>();
            interiorPixels = new List <Point2D>();
        }
        /// <summary>
        /// Depth Frame Ready
        /// According to the Hand position tracked by Kinect, extract small Rectangular pixels matrix for hand tracking
        /// Extract pixels for each frame, each pixel in matrix has the X,Y,Depth information
        /// Construct Hand instance, do hand tracking, draw with returning positions of fingertips and palm
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void sensor_DepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
        {
            if (!skeletonReadyFlag)
            {
                return;
            }
            using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
            {
                if (depthFrame != null)
                {
                    depthFrame.CopyDepthImagePixelDataTo(this.depthPixels);

                    CoordinateMapperPlus mapper      = new CoordinateMapperPlus(this.sensor);
                    PointDepth3D         palmDepth3D = mapper.MapSkeletonPointToDepthPoint(rightHand, this.sensor.DepthStream.Format);

                    int index = palmDepth3D.Y * DepthFrameWidth + palmDepth3D.X;
                    if (index < 0 || index > this.depthPixels.Length)
                    {
                        return;
                    }
                    short currDepth = depthPixels[index].Depth;

                    int indexColor = 0;
                    int rectSize   = 0;
                    for (int i = 0; i < this.DepthFrameWidth; i++)
                    {
                        for (int j = 0; j < this.DepthFrameHeight; j++)
                        {
                            indexColor = (j * this.DepthFrameWidth + i) * 4;
                            if (KinectUtil.isInTrackRegion(i, j, palmDepth3D.X, palmDepth3D.Y))
                            {
                                int indexDepthPixels = j * this.DepthFrameWidth + i;
                                rectDepth3D[rectSize++] = new PointDepth3D(i, j, depthPixels[indexDepthPixels].Depth);

                                //Draw depth pixel according to different distance
                                this.depthColor[indexColor + BLUE]  = 255;
                                this.depthColor[indexColor + GREEN] = 0;
                                this.depthColor[indexColor + RED]   = 0;
                                this.depthColor[indexColor + AlPHA] = 0;

                                if (depthPixels[indexDepthPixels].Depth >= currDepth - 100 &&
                                    depthPixels[indexDepthPixels].Depth <= currDepth + 100)
                                {
                                    this.depthColor[indexColor + BLUE]  = 255;
                                    this.depthColor[indexColor + GREEN] = 255;
                                    this.depthColor[indexColor + RED]   = 255;
                                    this.depthColor[indexColor + AlPHA] = 0;
                                }
                            }
                            else
                            {
                                this.depthColor[indexColor + BLUE]  = 0;
                                this.depthColor[indexColor + GREEN] = 0;
                                this.depthColor[indexColor + RED]   = 0;
                                this.depthColor[indexColor + AlPHA] = 0;
                            }
                        }
                    }
                    #region
                    //for (int i = 0,depthColorIndex = 0; i < this.depthPixels.Length; i++, depthColorIndex += 4)
                    //{
                    //    if (pointRight.X >= (int)(i % this.sensor.DepthStream.FrameWidth) - 50 &&
                    //        pointRight.X <= (int)(i % this.sensor.DepthStream.FrameWidth) + 50 &&
                    //        pointRight.Y >= (int)(i / this.sensor.DepthStream.FrameWidth) - 40 &&
                    //        pointRight.Y <= (int)(i / this.sensor.DepthStream.FrameWidth) + 60)
                    //    {
                    //        this.depthColor[depthColorIndex + BLUE] = 255;
                    //        this.depthColor[depthColorIndex + GREEN] = 0;
                    //        this.depthColor[depthColorIndex + RED] = 0;
                    //        this.depthColor[depthColorIndex + AlPHA] = 0;
                    //        if (depthPixels[i].Depth >= nearDepth - 100 && depthPixels[i].Depth <= nearDepth + 100)
                    //        {
                    //            this.depthColor[depthColorIndex + BLUE] = 255;
                    //            this.depthColor[depthColorIndex + GREEN] = 255;
                    //            this.depthColor[depthColorIndex + RED] = 255;
                    //            this.depthColor[depthColorIndex + AlPHA] = 0;
                    //        }
                    //    }
                    //    else
                    //    {
                    //        this.depthColor[depthColorIndex + BLUE] = 0;
                    //        this.depthColor[depthColorIndex + GREEN] = 0;
                    //        this.depthColor[depthColorIndex + RED] = 0;
                    //        this.depthColor[depthColorIndex + AlPHA] = 0;

                    //    }
                    //}
                    #endregion

                    //Construct Hand instance for tracker, then do hand tracking
                    if (rectSize == rectWidth * rectHeight)
                    {
                        detector.RightHand = new Hand(rectDepth3D, palmDepth3D, rectWidth, rectHeight);
                        detector.DetectAndSmooth(timeStamp);

                        //Fingers.Identify(this.Sketelon3DFingerTips,timeStamp);
                        //Fingers.Identify2(this.Sketelon3DFingerTips,rightHand, rightWrist);

                        Fingers.Identify3(this.Sketelon3DFingerTips, timeStamp);
                        timeStamp = (++timeStamp) % KinectUtil.LOOP_TIMES;
                        foreach (var element in detector.Depth3DFingerTips)
                        {
                            int k = (element.Y * DepthFrameWidth + element.X) * 4;
                            for (int i = k - 20; i < k + 20; i += 4)
                            {
                                depthColor[i + BLUE]  = 0;
                                depthColor[i + GREEN] = 0;
                                depthColor[i + RED]   = 255;
                            }
                        }
                    }
                    this.depthBitmap.WritePixels(
                        new Int32Rect(0, 0, this.depthBitmap.PixelWidth, this.depthBitmap.PixelHeight),
                        this.depthColor,
                        this.depthBitmap.PixelWidth * sizeof(int),
                        0);

                    readyEvent.afterDepthReady();
                }
            }
        }
Exemplo n.º 5
0
 public PointDepth3D(PointDepth3D point)
 {
     this.X = point.X;
     this.Y = point.Y;
     this.Depth = point.Depth;
 }
Exemplo n.º 6
0
        /// <summary>
        /// Depth Frame Ready
        /// According to the Hand position tracked by Kinect, extract small Rectangular pixels matrix for hand tracking
        /// Extract pixels for each frame, each pixel in matrix has the X,Y,Depth information
        /// Construct Hand instance, do hand tracking, draw with returning positions of fingertips and palm
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void sensor_DepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
        {
            if (!skeletonReadyFlag)
                return;
            using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
            {
                if (depthFrame != null)
                {
                    depthFrame.CopyDepthImagePixelDataTo(this.depthPixels);

                    CoordinateMapperPlus mapper = new CoordinateMapperPlus(this.sensor);
                    PointDepth3D palmDepth3D = mapper.MapSkeletonPointToDepthPoint(rightHand, this.sensor.DepthStream.Format);

                    int index = palmDepth3D.Y * DepthFrameWidth + palmDepth3D.X;
                    if (index < 0 || index > this.depthPixels.Length)
                        return;
                    short currDepth = depthPixels[index].Depth;

                    int indexColor = 0;
                    int rectSize = 0;
                    for (int i = 0; i < this.DepthFrameWidth; i++)
                    {
                        for (int j = 0; j < this.DepthFrameHeight; j++)
                        {
                            indexColor = (j * this.DepthFrameWidth + i) * 4;
                            if (KinectUtil.isInTrackRegion(i, j, palmDepth3D.X, palmDepth3D.Y))
                            {
                                int indexDepthPixels = j * this.DepthFrameWidth + i;
                                rectDepth3D[rectSize++] = new PointDepth3D(i, j, depthPixels[indexDepthPixels].Depth);

                                //Draw depth pixel according to different distance
                                this.depthColor[indexColor + BLUE] = 255;
                                this.depthColor[indexColor + GREEN] = 0;
                                this.depthColor[indexColor + RED] = 0;
                                this.depthColor[indexColor + AlPHA] = 0;

                                if (depthPixels[indexDepthPixels].Depth >= currDepth - 100
                                    && depthPixels[indexDepthPixels].Depth <= currDepth + 100)
                                {
                                    this.depthColor[indexColor + BLUE] = 255;
                                    this.depthColor[indexColor + GREEN] = 255;
                                    this.depthColor[indexColor + RED] = 255;
                                    this.depthColor[indexColor + AlPHA] = 0;
                                }
                            }
                            else
                            {
                                this.depthColor[indexColor + BLUE] = 0;
                                this.depthColor[indexColor + GREEN] = 0;
                                this.depthColor[indexColor + RED] = 0;
                                this.depthColor[indexColor + AlPHA] = 0;
                            }
                        }
                    }
                    #region
                    //for (int i = 0,depthColorIndex = 0; i < this.depthPixels.Length; i++, depthColorIndex += 4)
                    //{
                    //    if (pointRight.X >= (int)(i % this.sensor.DepthStream.FrameWidth) - 50 &&
                    //        pointRight.X <= (int)(i % this.sensor.DepthStream.FrameWidth) + 50 &&
                    //        pointRight.Y >= (int)(i / this.sensor.DepthStream.FrameWidth) - 40 &&
                    //        pointRight.Y <= (int)(i / this.sensor.DepthStream.FrameWidth) + 60)
                    //    {
                    //        this.depthColor[depthColorIndex + BLUE] = 255;
                    //        this.depthColor[depthColorIndex + GREEN] = 0;
                    //        this.depthColor[depthColorIndex + RED] = 0;
                    //        this.depthColor[depthColorIndex + AlPHA] = 0;
                    //        if (depthPixels[i].Depth >= nearDepth - 100 && depthPixels[i].Depth <= nearDepth + 100)
                    //        {
                    //            this.depthColor[depthColorIndex + BLUE] = 255;
                    //            this.depthColor[depthColorIndex + GREEN] = 255;
                    //            this.depthColor[depthColorIndex + RED] = 255;
                    //            this.depthColor[depthColorIndex + AlPHA] = 0;
                    //        }
                    //    }
                    //    else
                    //    {
                    //        this.depthColor[depthColorIndex + BLUE] = 0;
                    //        this.depthColor[depthColorIndex + GREEN] = 0;
                    //        this.depthColor[depthColorIndex + RED] = 0;
                    //        this.depthColor[depthColorIndex + AlPHA] = 0;

                    //    }
                    //}
                    #endregion

                    //Construct Hand instance for tracker, then do hand tracking
                    if (rectSize == rectWidth * rectHeight)
                    {
                        detector.RightHand = new Hand(rectDepth3D, palmDepth3D, rectWidth, rectHeight);
                        detector.DetectAndSmooth(timeStamp);

                        //Fingers.Identify(this.Sketelon3DFingerTips,timeStamp);
                        //Fingers.Identify2(this.Sketelon3DFingerTips,rightHand, rightWrist);

                        Fingers.Identify3(this.Sketelon3DFingerTips,timeStamp);
                        timeStamp = (++timeStamp) % KinectUtil.LOOP_TIMES;
                        foreach (var element in detector.Depth3DFingerTips)
                        {
                            int k = (element.Y * DepthFrameWidth + element.X) * 4;
                            for (int i = k - 20; i < k + 20; i += 4)
                            {
                                depthColor[i + BLUE] = 0;
                                depthColor[i + GREEN] = 0;
                                depthColor[i + RED] = 255;
                            }
                        }
                    }
                    this.depthBitmap.WritePixels(
                         new Int32Rect(0, 0, this.depthBitmap.PixelWidth, this.depthBitmap.PixelHeight),
                         this.depthColor,
                         this.depthBitmap.PixelWidth * sizeof(int),
                         0);

                    readyEvent.afterDepthReady();
                }
            }
        }
Exemplo n.º 7
0
 public PointDepth3D(PointDepth3D point)
 {
     this.X     = point.X;
     this.Y     = point.Y;
     this.Depth = point.Depth;
 }
Exemplo n.º 8
0
        /// <summary>
        /// Depth Slice, generate the valid matrix
        /// </summary>
        /// <param name="pixel3D"></param>
        /// <param name="Depth"></param>
        /// <returns>bool[][] valid</returns>
        private bool[][] generateValidMatrix(PointDepth3D[] pixel3D, float Depth, int dalitionErosionMask)
        {
            bool[][] valid = new bool[rectWidth][];
            for (int i = 0; i < valid.Length; i++)
            {
                valid[i] = new bool[rectHeight];
            }
            for (int i = 0; i < valid.Length; i++)
            {
                for (int j = 0; j < valid[i].Length; j++)
                {
                    int index = j * rectWidth + i;
                    if (pixel3D[index].Depth <= Depth + 100 && pixel3D[index].Depth >= Depth - 100)
                    {
                        valid[i][j] = true;
                    }
                    else
                    {
                        valid[i][j] = false;
                    }
                }
            }
            //Noisy Reduction
            if (dalitionErosionMask > 0)
            {
                valid = Denoise.Dilation(valid, dalitionErosionMask);
                valid = Denoise.Erosion(valid, dalitionErosionMask);
            }
            // Mark as not valid the borders of the matrix to improve the efficiency in some methods
            int m;
            // First row
            for (int j = 0; j < valid[0].Length; ++j)
                valid[0][j] = false;
            // Last row
            m = valid.Length - 1;
            for (int j = 0; j < valid[0].Length; ++j)
                valid[m][j] = false;
            // First column
            for (int i = 0; i < valid.Length; ++i)
                valid[i][0] = false;
            // Last column
            m = valid[0].Length - 1;
            for (int i = 0; i < valid.Length; ++i)
                valid[i][m] = false;

            return valid;
        }
        /// <summary>
        /// Map PointDepth3D to PointSkeleton3D
        /// </summary>
        /// <param name="depthImageFormat"></param>
        /// <param name="pointDepth3D"></param>
        /// <returns></returns>
        public PointSkeleton3D MapDepthPointToSketelonPoint(DepthImageFormat depthImageFormat, PointDepth3D pointDepth3D)
        {
            DepthImagePoint point = new DepthImagePoint();

            point.X     = pointDepth3D.X;
            point.Y     = pointDepth3D.Y;
            point.Depth = pointDepth3D.Depth;

            return(new PointSkeleton3D(mapper.MapDepthPointToSkeletonPoint(depthImageFormat, point)));
        }