Example #1
0
        /// <summary>
        /// Handles the depth/color/body index frame data arriving from the sensor
        /// </summary>
        /// <param name="sender">object sending the event</param>
        /// <param name="e">event arguments</param>
        private void Reader_MultiSourceFrameArrived(object sender, MultiSourceFrameArrivedEventArgs e)
        {
            if (this.runningCalibration)
            {
                //Console.WriteLine("runningCalibration");
                if (this.depthFrameCaptured)
                {
                    //Console.WriteLine("depthFrameCaptured");
                    if (this.depthFrameFiltered)
                    {
                        //Console.WriteLine("this.depthFrameFiltered");
                        if (this.depthScreenshotTaken)
                        {
                            //Console.WriteLine("this.depthScreenshotTaken");

                            if (this.horizontalProjectingDone && this.verticalProjectingDone)
                            {
                                this.StatusText = "this.horizontalProjectingDone && this.verticalProjectingDone";
                                if (this.calculatingStarted)
                                {
                                    this.StatusText = "calculatingFinished";
                                }
                                else
                                {
                                    this.StatusText = "calculating";
                                    this.startCalculating();
                                }
                            }
                            else
                            {
                                //this.StatusText = "projecting Patterns";
                                this.writeImageToFirstImageBuffer = true;
                            }
                            //}

                            /*else {
                             *  this.StatusText = "projecting Patterns";
                             *  this.writeImageToFirstImageBuffer = true;
                             * }*/
                        }
                        else
                        {
                            this.StatusText = "making Screenshot of DepthMap";
                            this.makeScreenshotOfDepthMap();
                        }
                    }
                    else
                    {
                        this.StatusText = "filtering Depth";
                        this.filterDepth2(5);
                    }
                }
            }

            int depthWidth  = 0;
            int depthHeight = 0;

            DepthFrame     depthFrame     = null;
            ColorFrame     colorFrame     = null;
            BodyIndexFrame bodyIndexFrame = null;
            bool           isBitmapLocked = false;

            MultiSourceFrame multiSourceFrame = e.FrameReference.AcquireFrame();

            // If the Frame has expired by the time we process this event, return.
            if (multiSourceFrame == null)
            {
                return;
            }

            // We use a try/finally to ensure that we clean up before we exit the function.
            // This includes calling Dispose on any Frame objects that we may have and unlocking the bitmap back buffer.
            try
            {
                depthFrame     = multiSourceFrame.DepthFrameReference.AcquireFrame();
                colorFrame     = multiSourceFrame.ColorFrameReference.AcquireFrame();
                bodyIndexFrame = multiSourceFrame.BodyIndexFrameReference.AcquireFrame();

                // If any frame has expired by the time we process this event, return.
                // The "finally" statement will Dispose any that are not null.
                if ((depthFrame == null) || (colorFrame == null) || (bodyIndexFrame == null))
                {
                    return;
                }

                // Process Depth
                FrameDescription depthFrameDescription = depthFrame.FrameDescription;

                depthWidth  = depthFrameDescription.Width;
                depthHeight = depthFrameDescription.Height;

                // Access the depth frame data directly via LockImageBuffer to avoid making a copy
                using (KinectBuffer depthFrameData = depthFrame.LockImageBuffer())
                {
                    this.coordinateMapper.MapColorFrameToDepthSpaceUsingIntPtr(
                        depthFrameData.UnderlyingBuffer,
                        depthFrameData.Size,
                        this.colorMappedToDepthPoints);
                    if (this.captureDepthFrame)
                    {
                        unsafe {
                            depthFrame.CopyFrameDataToArray(this.globalDepth);
                            //for (int i = 2000; i < 2020;i++ )
                            //{
                            this.minDepthRange = depthFrame.DepthMinReliableDistance;
                            this.maxDepthRange = depthFrame.DepthMaxReliableDistance;
                            //Console.WriteLine("DEPTHFRAME: {0}", frameData[512*424-1]);
                            //}
                        }

                        this.captureDepthFrame  = false;
                        this.depthFrameCaptured = true;
                    }
                }

                // We're done with the DepthFrame
                depthFrame.Dispose();
                depthFrame = null;

                // Process Color

                // Lock the bitmap for writing
                this.bitmap.Lock();
                isBitmapLocked = true;

                colorFrame.CopyConvertedFrameDataToIntPtr(this.bitmap.BackBuffer, this.bitmapBackBufferSize, ColorImageFormat.Bgra);

                if (this.projectingStarted && !this.verticalProjectingDone)
                {
                    Boolean newPattern           = false;
                    bool    timeToTakeScreenshot = false;
                    if (this.frameCounter == 10)
                    {
                        timeToTakeScreenshot = true;
                    }
                    if (this.frameCounter >= 15)
                    {
                        newPattern = true;
                        this.projectionImageCounter++;
                        this.frameCounter = 0;
                    }
                    else
                    {
                        this.frameCounter++;
                    }

                    if (timeToTakeScreenshot)
                    {
                        //int size = this.verticalImages.Count();
                        this.bitmap.Unlock();
                        this.screenshot(this.projectionImageCounter, "horizontal");
                        this.bitmap.Lock();
                        //this.verticalImages[size]
                        timeToTakeScreenshot = false;
                    }

                    if (newPattern)
                    {
                        ProjectablePattern pp = new ProjectablePattern(this.projectorWidth, this.projectorHeight, true, this.projectionImageCounter, this.myProjector);
                        bool success          = pp.projectPattern();
                        if (!success)
                        {
                            this.frameCounter           = 0;
                            this.projectionImageCounter = 0;
                            this.verticalProjectingDone = true;
                        }
                        else
                        {
                            this.horizontalImages.Add(pp);
                        }
                    }
                }



                if (this.projectingStarted && this.verticalProjectingDone && !this.horizontalProjectingDone)
                {
                    Boolean newPattern           = false;
                    bool    timeToTakeScreenshot = false;
                    if (this.frameCounter == 10)
                    {
                        timeToTakeScreenshot = true;
                    }
                    if (this.frameCounter >= 15)
                    {
                        newPattern = true;
                        this.projectionImageCounter++;
                        this.frameCounter = 0;
                    }
                    else
                    {
                        this.frameCounter++;
                    }

                    if (timeToTakeScreenshot)
                    {
                        //int size = this.verticalImages.Count();
                        this.bitmap.Unlock();
                        this.screenshot(this.projectionImageCounter, "vertical");
                        this.bitmap.Lock();
                        //this.verticalImages[size]
                        timeToTakeScreenshot = false;
                    }

                    if (newPattern)
                    {
                        ProjectablePattern pp = new ProjectablePattern(this.projectorWidth, this.projectorHeight, false, this.projectionImageCounter, this.myProjector);
                        bool success          = pp.projectPattern();
                        if (!success)
                        {
                            this.horizontalProjectingDone = true;
                            this.projectButton.IsEnabled  = false;
                        }
                        else
                        {
                            this.verticalImages.Add(pp);
                        }
                    }
                }


                if (this.writeImageToFirstImageBuffer)
                {
                    colorFrame.CopyConvertedFrameDataToIntPtr(this.firstImage.BackBuffer, this.bitmapBackBufferSize, ColorImageFormat.Bgra);
                    this.writeImageToFirstImageBuffer = false;
                    this.projectingStarted            = true;
                }

                // We're done with the ColorFrame
                colorFrame.Dispose();
                colorFrame = null;

                // We'll access the body index data directly to avoid a copy
                using (KinectBuffer bodyIndexData = bodyIndexFrame.LockImageBuffer())
                {
                    unsafe
                    {
                        byte *bodyIndexDataPointer = (byte *)bodyIndexData.UnderlyingBuffer;

                        int colorMappedToDepthPointCount = this.colorMappedToDepthPoints.Length;

                        fixed(DepthSpacePoint *colorMappedToDepthPointsPointer = this.colorMappedToDepthPoints)
                        {
                            // Treat the color data as 4-byte pixels
                            uint *bitmapPixelsPointer     = (uint *)this.bitmap.BackBuffer;
                            uint *firstImagePixelsPointer = (uint *)this.firstImage.BackBuffer;

                            // Loop over each row and column of the color image
                            // Zero out any pixels that don't correspond to a body index
                            for (int colorIndex = 0; colorIndex < colorMappedToDepthPointCount; ++colorIndex)
                            {
                                float colorMappedToDepthX = colorMappedToDepthPointsPointer[colorIndex].X;
                                float colorMappedToDepthY = colorMappedToDepthPointsPointer[colorIndex].Y;

                                if (colorIndex == 600)
                                {
                                    // The sentinel value is -inf, -inf, meaning that no depth pixel corresponds to this color pixel.
                                    if (!float.IsNegativeInfinity(colorMappedToDepthX) &&
                                        !float.IsNegativeInfinity(colorMappedToDepthY))
                                    {
                                        /*Console.WriteLine("colorMappedToDepthX: {0}, colorMappedToDepthY {1} ,colorIndex: {2}", colorMappedToDepthX, colorMappedToDepthY, colorIndex);*/
                                        // Make sure the depth pixel maps to a valid point in color space
                                        int depthX = (int)(colorMappedToDepthX + 0.5f);
                                        int depthY = (int)(colorMappedToDepthY + 0.5f);

                                        // If the point is not valid, there is no body index there.
                                        if ((depthX >= 0) && (depthX < depthWidth) && (depthY >= 0) && (depthY < depthHeight))
                                        {
                                            int depthIndex = (depthY * depthWidth) + depthX;

                                            // If we are tracking a body for the current pixel, do not zero out the pixel
                                            if (bodyIndexDataPointer[depthIndex] != 0xff)
                                            {
                                                continue;
                                            }
                                        }
                                    }
                                }//(colorIndex<200)

                                //bitmapPixelsPointer[colorIndex] = 0;
                            }
                        }

                        this.bitmap.AddDirtyRect(new Int32Rect(0, 0, this.bitmap.PixelWidth, this.bitmap.PixelHeight));
                    }
                }
            }
            finally
            {
                if (isBitmapLocked)
                {
                    this.bitmap.Unlock();
                }

                if (depthFrame != null)
                {
                    depthFrame.Dispose();
                }

                if (colorFrame != null)
                {
                    colorFrame.Dispose();
                }

                if (bodyIndexFrame != null)
                {
                    bodyIndexFrame.Dispose();
                }
            }
        }
Example #2
0
        private List <List <System.Windows.Point> > filterLightedPixels2(BitmapSource bitmapSource, List <List <System.Windows.Point> > inputList, ProjectablePattern image, List <System.Windows.Point> projector2d)
        {
            List <List <System.Windows.Point> > myResult = new List <List <System.Windows.Point> >();
            Bitmap myBitmap = this.getBitmap(bitmapSource);

            for (int a = 0; a < projector2d.Count; a++)
            {
                if (image.pixelIsWhite(((int)projector2d[a].X), ((int)projector2d[a].Y)) && image.getDistanceOfPixelToBorder(((int)projector2d[a].X), ((int)projector2d[a].Y)) >= 1)
                {
                    myResult.Add(this.filterColorWithUnsafe(myBitmap, inputList[a]));
                }
                else
                {
                    myResult.Add(inputList[a]);
                }
            }
            myBitmap.Dispose();

            return(myResult);
        }