public static RectF Offset(this RectI rect, float widthOffsetPercent, float topOffsetPercent, float bottomOffsetPercent, float maxWidth, float maxHeight)
        {
            int width  = rect.Right - rect.Left;
            int height = rect.Bottom - rect.Top;

            float widthDifference = ((width / 100f) * widthOffsetPercent) * 100;

            float offsetTop    = rect.Top - ((height / 100f) * topOffsetPercent) * 100;
            float offsetLeft   = rect.Left - (widthDifference / 2);
            float offsetWidth  = width + widthDifference;
            float offsetHeight = height + (((height / 100f) * bottomOffsetPercent) * 100);

            return(new RectF()
            {
                Y = offsetTop > maxHeight ? maxHeight : offsetTop,
                X = offsetLeft > maxWidth ? maxWidth : offsetLeft,
                Height = offsetTop + offsetHeight > maxHeight ? maxHeight - offsetTop : offsetHeight,
                Width = offsetLeft + offsetWidth > maxWidth ? maxWidth - offsetLeft : offsetWidth
            });
        }
Ejemplo n.º 2
0
 public bool Equals(RectI obj)
 {
     return(Left.Equals(obj.Left) && Top.Equals(obj.Top) && Right.Equals(obj.Right) && Bottom.Equals(obj.Bottom));
 }
Ejemplo n.º 3
0
 public bool Equals(RectI obj)
 {
     return Left.Equals(obj.Left) && Top.Equals(obj.Top) && Right.Equals(obj.Right) && Bottom.Equals(obj.Bottom);
 }
        private void RecordData(RectI faceRegion, FaceFrame faceFrame)
        {
            //record the R, G, B, IR values from the Face Region pixels
            using (var irFrame = faceFrame.InfraredFrameReference.AcquireFrame())
            {
                using (var depthFrame = faceFrame.DepthFrameReference.AcquireFrame())
                {
                    using (var colorFrame = faceFrame.ColorFrameReference.AcquireFrame())
                    {
                        if ((null == irFrame) || (null == colorFrame) || (null == depthFrame)) return;

                        DepthSpacePoint[] depthSpacePoints = new DepthSpacePoint[colorFrame.FrameDescription.Height * colorFrame.FrameDescription.Width];

                        FrameDescription depthFrameDescription = depthFrame.FrameDescription;

                        // Access the depth frame data directly via LockImageBuffer to avoid making a copy
                        using (KinectBuffer depthFrameData = depthFrame.LockImageBuffer())
                        {
                            this.m_CoordMapper.MapColorFrameToDepthSpaceUsingIntPtr(
                                depthFrameData.UnderlyingBuffer,
                                depthFrameData.Size,
                                depthSpacePoints);
                        }

                        //Get the pixels
                        m_colorBitmap.Lock();
                        unsafe
                        {
                            byte* pixels = (byte*)m_colorBitmap.BackBuffer;

                            var startPixel = faceRegion.Left * faceRegion.Top;
                            var endPixel = faceRegion.Right * faceRegion.Bottom;

                            float alpha = 0;
                            float red = 0;
                            float green = 0;
                            float blue = 0;
                            float ir = 0;

                            ushort[] irFrameData = new ushort[irFrame.FrameDescription.Height * irFrame.FrameDescription.Width];
                            irFrame.CopyFrameDataToArray(irFrameData);

                            //Now get the Red, Green, Blue Pixels for the region
                            for (int i = startPixel; i < endPixel; i += 4)
                            {
                                //var pixel = pixels[i];
                                int irIndex = (int)depthSpacePoints[i].X * (int)depthSpacePoints[i].Y;

                                blue += pixels[i]; // << 24;
                                green += pixels[i + 1]; // << 16;
                                red += pixels[i + 2];// << 8;
                                alpha += pixels[i + 3];
                                if (irIndex < irFrameData.Length)
                                    ir += irFrameData[irIndex];
                                else
                                    ir += 0;
                            }
                            float size = Math.Abs(startPixel - endPixel);

                            float avg_alpha = alpha / size;
                            float avg_red = red / size;
                            float avg_green = green / size;
                            float avg_blue = blue / size;
                            float avg_ir = ir / size;

                            double std_alpha = 0;
                            double std_red = 0;
                            double std_green = 0;
                            double std_blue = 0;
                            double std_ir = 0;

                            double var_alpha = 0;
                            double var_red = 0;
                            double var_green = 0;
                            double var_blue = 0;
                            double var_ir = 0;

                            //Now calculate standard deviation
                            for (int i = startPixel; i < endPixel; i += 4)
                            {
                                //var pixel = pixels[i];
                                var_blue = (double)(pixels[i] - avg_blue);
                                std_blue += Math.Pow(var_blue, 2.0);

                                var_green = (pixels[i + 1] - avg_green);
                                std_green += Math.Pow(var_green, 2);

                                var_red = (pixels[i + 2] - avg_red);
                                std_red += Math.Pow(var_red, 2);

                                var_alpha = (pixels[i + 3] - avg_alpha);
                                std_alpha += Math.Pow(var_alpha, 2);

                                int irIndex = (int)depthSpacePoints[i].X * (int)depthSpacePoints[i].Y;
                                if (irIndex < irFrameData.Length)
                                    var_ir = irFrameData[irIndex] - avg_ir;
                                else
                                    var_ir = avg_ir;

                                std_ir += Math.Pow(var_ir, 2);

                            }

                            std_alpha = Math.Sqrt(std_alpha / size);
                            std_red = Math.Sqrt(std_red / size);
                            std_green = Math.Sqrt(std_green / size);
                            std_blue = Math.Sqrt(std_blue / size);
                            std_ir = Math.Sqrt(std_ir / size);

                            double prime_alpha = 0;
                            double prime_red = 0;
                            double prime_green = 0;
                            double prime_blue = 0;
                            double prime_ir = 0;

                            //Now calculate standard deviation
                            for (int i = startPixel; i < endPixel; i += 4)
                            {
                                //var pixel = pixels[i];
                                var_blue = (double)(pixels[i] - avg_blue);
                                prime_blue += var_blue / std_blue;

                                var_green = (pixels[i + 1] - avg_green);
                                prime_green += var_green / std_green;

                                var_red = (pixels[i + 2] - avg_red);
                                prime_red += var_red / std_red;

                                var_alpha = (pixels[i + 3] - avg_alpha);
                                prime_alpha += var_alpha / std_alpha;

                                int irIndex = (int)depthSpacePoints[i].X * (int)depthSpacePoints[i].Y;
                                if (irIndex < irFrameData.Length)
                                    var_ir = irFrameData[irIndex] - avg_ir;
                                else
                                    var_ir = avg_ir;

                                prime_ir += var_ir / std_ir;
                            }

                            double norm_alpha = prime_alpha / size;
                            double norm_red = prime_red / size;
                            double norm_blue = prime_blue / size;
                            double norm_green = prime_green / size;
                            double norm_ir = prime_ir / size;

                            Plot(norm_red, norm_green, norm_blue, norm_ir);

                            jadeCalculation.Write(
                                m_secondsElapsed.ElapsedMilliseconds,
                                norm_alpha, norm_red, norm_green, norm_blue, norm_ir);

                        }
                        m_colorBitmap.Unlock();
                    }
                }
            }
        }
        private void M_FaceReader_FrameArrived(object sender, Microsoft.Kinect.Face.FaceFrameArrivedEventArgs e)
        {
            var frameRef = e.FrameReference;
            using (var faceFrame = frameRef.AcquireFrame())
            {
                if (faceFrame != null)
                {
                    //get the Color Region
                    if (faceFrame.FaceFrameResult != null)
                    {
                        m_drawingRegion = faceFrame.FaceFrameResult.FaceBoundingBoxInColorSpace;
                        var faceRegion = new RectI();
                        faceRegion.Top = Math.Abs(m_drawingRegion.Top - 36 );
                        faceRegion.Bottom = Math.Abs(m_drawingRegion.Bottom - 12);
                        faceRegion.Left = Math.Abs(m_drawingRegion.Left + 26);
                        faceRegion.Right = Math.Abs(m_drawingRegion.Right - 20);
                        DrawBox(faceRegion);

                        //Take the new region and record ColorFrame Data
                        if (m_timerStarted)
                        {
                            RecordData(faceRegion, faceFrame);
                            lblColorFeeds.Text = "Please be still taking measurements...";
                        }
                        else
                        {
                            lblColorFeeds.Text = "Face Found, Click the Calculate button to start taking measurements...";
                            btnCalculateRate.IsEnabled = true;
                        }
                    }
                }
            }
        }
        private void DrawBox(RectI region)
        {
            if (null == region) return;

            var newLeft = region.Left;
            var newTop = region.Top;
            var newRight = region.Right;
            var newBottom = region.Bottom;

            var width = Math.Abs( (newRight+4) - (newLeft));
            var height = Math.Abs(newBottom - newTop);

            m_colorBitmap.Lock();
            unsafe {
                byte* pixels = (byte*)m_colorBitmap.BackBuffer;
                for (int i = 0; i < width; i++)
                {
                    for (int j = 0; j < height; j++)
                    {
                        var leftEdge = ((newTop + j) * m_colorBitmap.PixelWidth * 4) + (newLeft);
                        var rightEdge = ((newTop + j) * m_colorBitmap.PixelWidth * 4) + (newRight*4);
                        var index = ((newTop + j) * m_colorBitmap.PixelWidth * 4) + ((newLeft * 4 ) + 4*i);
                        if ((index >= leftEdge) && (index <= rightEdge))
                        {

                           //valid pixels
                            //Blue
                            pixels[index] = 255;

                        //Green
                        //pixels[index + 1] = 255;// (byte)((int)pixels[index + 1] - 100);

                        //Red
                        //pixels[index + 2] = 255;// (byte)((int)pixels[index + 1] + 10);

                        //Alpha - no effect right now
                           // pixels[index + 3] = 50;
                        }
                    }

                }
            }
            m_colorBitmap.Unlock();
        }