Ejemplo n.º 1
0
        private DetectedDoor DoorFromRec(Rectangle rec, Size imageSize)
        {
            Rectangle boundingBox = rec;
            int       centerX     = (boundingBox.Left + boundingBox.Right) / 2;
            int       centerY     = (boundingBox.Bottom + boundingBox.Top) / 2;

            // Get the angle relative to the bottom center of the image
            int    centerXOffset = centerX - (imageSize.Width / 2);
            double angle         = -Math.Atan2(-centerY, centerXOffset) - 2 * Math.PI / 4;
            double distance      = GetEstimatedDistance(rec, imageSize);

            DetectedDoor.DetectionConfidence confidence = GetConfidence(boundingBox, imageSize);

            return(new DetectedDoor(boundingBox, imageSize, confidence, angle, distance, DetectedDoor.DetectMethod.HAAR));
        }
        private DetectedDoor DoorFromContour(Contour <Point> contour, ushort[] depthValue, Size imageSize)
        {
            Rectangle boundingBox = contour.BoundingRectangle;
            int       left        = boundingBox.Left;
            int       right       = boundingBox.Right;
            int       top         = boundingBox.Top;
            int       bottom      = boundingBox.Bottom;
            int       centerX     = (boundingBox.Left + boundingBox.Right) / 2;
            int       centerY     = (boundingBox.Bottom + boundingBox.Top) / 2;

            // Get the angle relative to the bottom center of the image
            int    centerXOffset = centerX - (imageSize.Width / 2);
            double angle         = -Math.Atan2(-centerY, centerXOffset) - Math.PI / 2;

            DetectedDoor.DetectionConfidence confidence = GetConfidence(boundingBox, imageSize);
            Rectangle box       = contour.BoundingRectangle;
            int       maxHeight = 424;
            int       maxWidth  = 512;

            //calculate average depth at left right and middle
            int averDepthLeft, averDepthRight, averDepthMiddle;
            int sum          = 0;
            int pixelCounter = 0;

            for (int i = -8; i <= -2; i++)
            {
                int x = left + i;
                for (int y = top; y < bottom; y++)
                {
                    sum += GetDepth(depthValue, maxWidth * y + x);
                    pixelCounter++;
                }
            }
            averDepthLeft = sum / pixelCounter;
            sum           = pixelCounter = 0;
            for (int i = 2; i <= 8; i++)
            {
                int x = right + i;
                for (int y = top; y < bottom; y++)
                {
                    sum += GetDepth(depthValue, maxWidth * y + x);
                    pixelCounter++;
                }
            }

            averDepthRight = sum / pixelCounter;
            sum            = pixelCounter = 0;
            for (int i = -3; i <= 3; i++)
            {
                int x = (right + left) / 2 + i;
                for (int y = top; y < bottom; y++)
                {
                    sum += GetDepth(depthValue, maxWidth * y + x);
                    pixelCounter++;
                }
            }
            averDepthMiddle = sum / pixelCounter;

            //if (averDepthMiddle < averDepthLeft - 20 && averDepthMiddle < averDepthRight - 20)
            return(new DetectedDoor(boundingBox, imageSize, confidence, angle, (averDepthLeft + averDepthRight) / 2, DetectedDoor.DetectMethod.DEPTH));
        }
        public List <DetectedDoor> GetDoors(object data)
        {
            DFrame depthFrame = data as DFrame;

            if (depthFrame == null)
            {
                return(null);
            }
            List <DetectedDoor> doors = new List <DetectedDoor>();

            //Image<Bgr, Byte> smoothed = img.SmoothGaussian(15);
            //Image<Gray, Byte> threshold = FilterColor(smoothed);
            //threshold = SimplifyImage(threshold, 100);

            // new method

            //new method end
            using (MemStorage stor = new MemStorage())
            {
                Bitmap bmp = depthFrame.GetDoorNaviBitmap();
                //Bitmap bmp2 = bmp.Clone(new Rectangle(0, 0, bmp.Width, bmp.Height), bmp.PixelFormat);
                //Image<Bgr, Byte> img = new Image<Bgr, Byte>(bmp2);
                Image <Gray, Byte> grey;
                try
                {
                    grey = new Image <Gray, Byte>(bmp);
                }
                catch (Exception)
                {
                    return(null);
                }

                //CvInvoke.cvInRangeS(img.Ptr, new MCvScalar(0.0, 0.0, 0.0), new MCvScalar(250.0, 250.0, 250.0), grey.Ptr);

                //new method
                CvInvoke.cvErode(grey.Ptr, grey.Ptr, (IntPtr)null, 1);
                CvInvoke.cvDilate(grey.Ptr, grey.Ptr, (IntPtr)null, 2);
                Image <Gray, Byte> cannyEdges = grey.Canny(180, 120);
                LineSegment2D[]    lines      = cannyEdges.HoughLinesBinary(
                    1,              //Distance resolution in pixel-related units
                    Math.PI / 45.0, //Angle resolution measured in radians.
                    20,             //threshold
                    5,              //min Line width
                    10              //gap between lines
                    )[0];           //Get the lines from the first channel

                LineSegment2D[] container = null;

                container = FilterAndMergeVerticalLines(lines, 1);
                container = FilterShortLines(container, 100);
                container = FindVerticalDoorFrameLines(container, 500, grey);

                if (container != null)
                {
                    int       boxX      = container[0].P1.X;
                    int       boxY      = container[0].P1.Y;
                    int       boxWidth  = container[1].P1.X - boxX;
                    int       boxHeight = container[1].P2.Y - boxY;
                    Rectangle newBox    = new Rectangle(boxX, boxY, boxWidth, boxHeight);
                    int       centerX   = (newBox.Left + newBox.Right) / 2;
                    int       centerY   = (newBox.Bottom + newBox.Top) / 2;
                    DetectedDoor.DetectionConfidence confidence = GetConfidence(newBox, grey.Size);
                    int          centerXOffset = centerX - (grey.Size.Width / 2);
                    double       angle         = -Math.Atan2(-centerY, centerXOffset) - Math.PI / 2;
                    double       distance      = GetEstimatedDistance(newBox, grey.Size);
                    DetectedDoor newDoor       = new DetectedDoor(newBox, grey.Size, confidence, angle, distance, DetectedDoor.DetectMethod.DEPTH);
                    doors.Add(newDoor);
                }
                //new method end

                /** old method
                 *
                 * //CvInvoke.cvErode(grey.Ptr, grey.Ptr, (IntPtr)null, 4);
                 * //CvInvoke.cvDilate(grey.Ptr, grey.Ptr, (IntPtr)null, 2);
                 *
                 * //Contour<System.Drawing.Point> contours = grey.FindContours(
                 * //   Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_TC89_L1,
                 * //   Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_EXTERNAL);
                 *
                 * ////grey = new Image<Gray, byte>(grey.Width, grey.Height, new Gray(0));
                 * //for (; contours != null; contours = contours.HNext)
                 * //{
                 * //    Contour<System.Drawing.Point> contour = contours.ApproxPoly(contours.Perimeter * 0.08, stor);
                 * //    if (contour.Area < 80000 && contour.Area > 3000 && contour.Total == 4) //hard code for now, will update later
                 * //    {
                 * //        DetectedDoor newDoor = DoorFromContour(contour, depthFrame.get_DepthData(), grey.Size);
                 * //        if(newDoor != null)
                 * //            doors.Add(newDoor);
                 * //    }
                 * //}
                 *
                 **/
            }
            return(doors);
        }
        public List <DetectedDoor> GetDoors(object data)
        {
            IFrame infraredFrame = data as IFrame;

            if (infraredFrame == null)
            {
                return(null);
            }
            List <DetectedDoor> doors = new List <DetectedDoor>();

            //Image<Bgr, Byte> smoothed = img.SmoothGaussian(15);
            //Image<Gray, Byte> threshold = FilterColor(smoothed);
            //threshold = SimplifyImage(threshold, 100);

            // new method

            //new method end
            using (MemStorage stor = new MemStorage())
            {
                Bitmap bmp = infraredFrame.GetBMP();
                //Bitmap bmp2 = bmp.Clone(new Rectangle(0, 0, bmp.Width, bmp.Height), bmp.PixelFormat);
                //Image<Bgr, Byte> img = new Image<Bgr, Byte>(bmp2);
                Image <Gray, Byte> grey;
                try
                {
                    grey = new Image <Gray, Byte>(bmp);
                }
                catch (Exception)
                {
                    return(null);
                }

                //CvInvoke.cvInRangeS(img.Ptr, new MCvScalar(0.0, 0.0, 0.0), new MCvScalar(250.0, 250.0, 250.0), grey.Ptr);

                //new method
                CvInvoke.cvErode(grey.Ptr, grey.Ptr, (IntPtr)null, 1);
                CvInvoke.cvDilate(grey.Ptr, grey.Ptr, (IntPtr)null, 2);
                Image <Gray, Byte> cannyEdges = grey.Canny(180, 120);
                LineSegment2D[]    lines      = cannyEdges.HoughLinesBinary(
                    1,              //Distance resolution in pixel-related units
                    Math.PI / 45.0, //Angle resolution measured in radians.
                    20,             //threshold
                    5,              //min Line width
                    10              //gap between lines
                    )[0];           //Get the lines from the first channel

                LineSegment2D[] container = null;

                container = FilterAndMergeVerticalLines(lines, 1);
                container = FilterShortLines(container, 100);
                container = FindVerticalDoorFrameLines(container, 500, grey);

                if (container != null)
                {
                    int       boxX      = container[0].P1.X;
                    int       boxY      = container[0].P1.Y;
                    int       boxWidth  = container[1].P1.X - boxX;
                    int       boxHeight = container[1].P2.Y - boxY;
                    Rectangle newBox    = new Rectangle(boxX, boxY, boxWidth, boxHeight);
                    int       centerX   = (newBox.Left + newBox.Right) / 2;
                    int       centerY   = (newBox.Bottom + newBox.Top) / 2;
                    DetectedDoor.DetectionConfidence confidence = GetConfidence(newBox, grey.Size);
                    int          centerXOffset = centerX - (grey.Size.Width / 2);
                    double       angle         = -Math.Atan2(-centerY, centerXOffset) - Math.PI / 2;
                    DetectedDoor newDoor       = new DetectedDoor(newBox, grey.Size, confidence, angle, 0, DetectedDoor.DetectMethod.INFRARED);
                    doors.Add(newDoor);
                }
            }
            return(doors);
        }