예제 #1
0
 public void Reset()
 {
     //default box size
     box = new Shapes.Rectangle(0, 0, 0)
     {
         Width           = 200, Height = 200,
         videoProcessing = this,
         pointMode       = Shapes.PointMode.ScreenUnzoomed
     };
     // Draws
     s.DrawCross       = (IsUpCamera()) ? false : true;
     s.DrawDashedCross = false;
     // Finds:
     s.FindCircles    = false;
     s.FindRectangles = false;
     s.FindComponent  = false;
     s.TakeSnapshot   = false;
     s.TestAlgorithm  = false;
     s.Draw_Snapshot  = false;
     s.DrawBox        = false;
     s.FindFiducial   = false;
     s.Draw1mmGrid    = false;
     MarkA.Clear();
     MarkB.Clear();
     Arrows.Clear();
 }
예제 #2
0
        public static List <Shapes.Rectangle> FindRectangles(VideoProcessing vp, Bitmap frame)
        {
            List <Shapes.Rectangle> rects = new List <Shapes.Rectangle>();

            using (Image <Bgr, Byte> img = new Image <Bgr, byte>(frame)) {
                //  double cannyThresholdLinking = 120.0;
                //  double cannyThreshold = 180.0;

                //Convert the image to grayscale and filter out the noise
                Image <Gray, Byte> gray = img.Convert <Gray, Byte>().PyrDown().PyrUp();
                //Image<Gray, Byte> cannyEdges = gray.Canny(cannyThreshold, cannyThresholdLinking);

                using (MemStorage storage = new MemStorage()) { //allocate storage for contour approximation
                    Contour <System.Drawing.Point> contours = gray.FindContours(CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, RETR_TYPE.CV_RETR_LIST, storage);
                    for (; contours != null; contours = contours.HNext)
                    {
                        Contour <System.Drawing.Point> currentContour = contours.ApproxPoly(contours.Perimeter * 0.05, storage);

                        if (currentContour.Area > 250)     //only consider contours with area greater than 250
                        {
                            if (currentContour.Total == 4) //The contour has 4 vertices.
                            {
                                #region determine if all the angles in the contour are within [80, 100] degree
                                bool isRectangle             = true;
                                System.Drawing.Point[] pts   = currentContour.ToArray();
                                LineSegment2D[]        edges = PointCollection.PolyLine(pts, true);

                                for (int i = 0; i < edges.Length; i++)
                                {
                                    double angle = Math.Abs(edges[(i + 1) % edges.Length].GetExteriorAngleDegree(edges[i]));
                                    if (angle < 80 || angle > 100)
                                    {
                                        isRectangle = false;
                                        break;
                                    }
                                }
                                #endregion

                                if (isRectangle)
                                {
                                    var box            = currentContour.GetMinAreaRect();
                                    Shapes.Rectangle r = new Shapes.Rectangle(box.center.X, box.center.Y, box.angle);
                                    r.Height = box.size.Height;
                                    r.Width  = box.size.Width;
                                    rects.Add(r);
                                }
                            }
                        }
                    }
                }
            }
            //ensure we dont have massive rectangles
            rects = rects.Where(x => (x.Height < .8 * vp.FrameSize.Height && x.Width < .8 * vp.FrameSize.Width)).ToList();
            SetVideoProcessing(rects, vp);
            return(rects);
        }
예제 #3
0
        public static PointF[] ToRotatedRectangle(Shapes.Rectangle s)
        {
            s.ToRawResolution();
            PointF[] p = new PointF[5];
            p[0].X = (float)s.Left; p[0].Y = (float)s.Top;
            p[1].X = (float)s.Right; p[1].Y = (float)s.Top;
            p[2].X = (float)s.Right; p[2].Y = (float)s.Bottom;
            p[3].X = (float)s.Left; p[3].Y = (float)s.Bottom;
            p[4].X = (float)s.Left; p[4].Y = (float)s.Top;

            // roate about center
            double       angle  = s.A * Math.PI / 180d;
            PartLocation center = new PartLocation(s);

            for (int i = 0; i < p.Length; i++)
            {
                PartLocation pp = new PartLocation(p[i]) - center; //shift to zero
                pp   = pp.Rotate(angle) + center;
                p[i] = pp.ToPointF();
            }

            return(p);
        }
예제 #4
0
 public void Reset()
 {
     //default box size
     box = new Shapes.Rectangle(0, 0, 0) {
         Width = 200, Height = 200,
         videoProcessing=this,
         pointMode = Shapes.PointMode.ScreenUnzoomed
     };
     // Draws
     s.DrawCross = (IsUpCamera()) ? false : true;
     s.DrawDashedCross = false;
     // Finds:
     s.FindCircles = false;
     s.FindRectangles = false;
     s.FindComponent = false;
     s.TakeSnapshot = false;
     s.TestAlgorithm = false;
     s.Draw_Snapshot = false;
     s.DrawBox = false;
     s.FindFiducial = false;
     s.Draw1mmGrid = false;
     MarkA.Clear();
     MarkB.Clear();
     Arrows.Clear();
 }
예제 #5
0
 private static void DrawRectangle(ref Bitmap image, Shapes.Rectangle rectangle)
 {
     using (Graphics g = Graphics.FromImage(image)) {
         g.DrawLines(new Pen(Color.Red), ToRotatedRectangle(rectangle));
     }
 }