public void ProcessImage(Emgu.CV.Image<Emgu.CV.Structure.Bgr, byte> image) {
      MCvBox2D mybox = new MCvBox2D(new System.Drawing.PointF(100, 100), new System.Drawing.Size(50, 30), 110);
      MCvBox2D mybox2 = new MCvBox2D(new System.Drawing.PointF(100, 100), new System.Drawing.Size(50, 30), 0);

      image.Draw(new Ellipse(mybox), new Bgr(0,0,255), 2);
      image.Draw(new Ellipse(mybox2), new Bgr(0, 255, 0), 2);
    }
    public virtual void ProcessImage(Emgu.CV.Image<Emgu.CV.Structure.Bgr, byte> image) {
      Emgu.CV.Image<Gray, byte> gray = image.Convert<Gray, byte>();
      gray._ThresholdBinary(new Gray(_threshold), new Gray(255.0));
      gray._Not();

      Parsley.Core.EllipseDetector ed = new Parsley.Core.EllipseDetector();
      ed.MinimumContourCount = _min_contour_count;

      List < Parsley.Core.DetectedEllipse > ellipses = 
        new List<Parsley.Core.DetectedEllipse>(ed.DetectEllipses(gray));

      List < Parsley.Core.DetectedEllipse > finals = 
        new List<Parsley.Core.DetectedEllipse>(
          ellipses.Where(e => { return e.Rating < _distance_threshold; })
        );

      finals.Sort(
        (a, b) => {
          double dista = a.Ellipse.MCvBox2D.center.X * a.Ellipse.MCvBox2D.center.X + a.Ellipse.MCvBox2D.center.Y * a.Ellipse.MCvBox2D.center.Y;
          double distb = b.Ellipse.MCvBox2D.center.X * b.Ellipse.MCvBox2D.center.X + b.Ellipse.MCvBox2D.center.Y * b.Ellipse.MCvBox2D.center.Y;
          return dista.CompareTo(distb);
        }
      );

      Bgr bgr = new Bgr(0, 255, 0);
      MCvFont f = new MCvFont(Emgu.CV.CvEnum.FONT.CV_FONT_HERSHEY_PLAIN, 0.8, 0.8);
      int count = 1;
      foreach (Parsley.Core.DetectedEllipse e in finals) {
        image.Draw(e.Ellipse, bgr, 2);
        image.Draw(count.ToString(), ref f, new System.Drawing.Point((int)e.Ellipse.MCvBox2D.center.X, (int)e.Ellipse.MCvBox2D.center.Y), bgr);
        count++;
      }
    }
Beispiel #3
0
    /// <summary>
    /// Draw a visual indication of the pattern coordinate frame
    /// </summary>
    /// <param name="img">Image to draw to</param>
    /// <param name="ecp">Extrinsic calibration</param>
    /// <param name="icp">Intrinsic calibration</param>
    public static void DrawCoordinateFrame(
      Emgu.CV.Image<Bgr, Byte> img,
      Emgu.CV.ExtrinsicCameraParameters ecp,
      Emgu.CV.IntrinsicCameraParameters icp) {
      float extension = img.Width / 10;
      PointF[] coords = Emgu.CV.CameraCalibration.ProjectPoints(
        new MCvPoint3D32f[] { 
          new MCvPoint3D32f(0, 0, 0),
          new MCvPoint3D32f(extension, 0, 0),
          new MCvPoint3D32f(0, extension, 0),
          new MCvPoint3D32f(0, 0, extension),
        },
        ecp, icp);

      img.Draw(new LineSegment2DF(coords[0], coords[1]), new Bgr(System.Drawing.Color.Red), 2);
      img.Draw(new LineSegment2DF(coords[0], coords[2]), new Bgr(System.Drawing.Color.Green), 2);
      img.Draw(new LineSegment2DF(coords[0], coords[3]), new Bgr(System.Drawing.Color.Blue), 2);
    }
    public void ProcessImage(Emgu.CV.Image<Emgu.CV.Structure.Bgr, byte> image) {
      Emgu.CV.Image<Gray, byte> gray = image.Convert<Gray, byte>();

      

      Emgu.CV.Image<Gray, byte> binary  = new Image<Gray,byte>(image.Size);
      CvInvoke.cvThreshold(gray, binary, 40, 255, THRESH.CV_THRESH_BINARY | THRESH.CV_THRESH_OTSU);
      binary._Not();
      Emgu.CV.Contour<System.Drawing.Point> contour_points = binary.FindContours();
      
      MemStorage storage = new MemStorage();
      Matrix<double> warp = new Matrix<double>(3, 3);

      while (contour_points != null) {
        Contour<Point> c = contour_points.ApproxPoly(contour_points.Perimeter * 0.05, storage);
        double p = c.Perimeter;
        if (c.Total == 4 && p > 300) {
          
          PointF[] src = new PointF[] { 
            new PointF(c[0].X, c[0].Y),
            new PointF(c[1].X, c[1].Y),
            new PointF(c[2].X, c[2].Y),
            new PointF(c[3].X, c[3].Y)};

          CvInvoke.cvGetPerspectiveTransform(src, _dest, warp);
          int flags = (int)INTER.CV_INTER_LINEAR + (int)WARP.CV_WARP_FILL_OUTLIERS;
          CvInvoke.cvWarpPerspective(gray, _roi, warp, flags, new MCvScalar(0));


          double min_error;
          Orientation orient;

          FindBestOrientation(out min_error, out orient);
          if (min_error < 0.4) {
            image.DrawPolyline(c.ToArray(), true, new Bgr(Color.Green), 2);
            System.Console.WriteLine(min_error + " " + orient);

            switch (orient) {
              case Orientation.Degrees0:
                image.Draw(new LineSegment2D(c[0], c[3]), new Bgr(System.Drawing.Color.Red), 2);
                break;
              case Orientation.Degrees90:
                image.Draw(new LineSegment2D(c[1], c[0]), new Bgr(System.Drawing.Color.Red), 2);
                break;
              case Orientation.Degrees180:
                image.Draw(new LineSegment2D(c[2], c[1]), new Bgr(System.Drawing.Color.Red), 2);
                break;
              case Orientation.Degrees270:
                image.Draw(new LineSegment2D(c[3], c[2]), new Bgr(System.Drawing.Color.Red), 2);
                break;
            }
            


          }

          // 0 degrees
        }
        contour_points = contour_points.HNext;
      }
    }
Beispiel #5
0
 override protected void OnFrame(Parsley.Core.BuildingBlocks.FrameGrabber fp, Emgu.CV.Image<Emgu.CV.Structure.Bgr, byte> img) {
   if (Context.ROIHandler.Last != Rectangle.Empty) {
     img.Draw(Context.ROIHandler.Last, new Emgu.CV.Structure.Bgr(Color.Green), 1);
   }
 }
Beispiel #6
0
        public void ProcessImage(Emgu.CV.Image<Emgu.CV.Structure.Bgr, byte> image)
        {
            Emgu.CV.Image<Gray, byte> gray = image.Convert<Gray, byte>();
            gray._ThresholdBinary(new Gray(_threshold), new Gray(255.0));
            gray._Not();

            //Emgu.CV.Contour<System.Drawing.Point> c = gray.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_CODE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_LIST);
            Emgu.CV.Contour<System.Drawing.Point> c = gray.FindContours();
            List<Ellipse> ellipses = new List<Ellipse>();

            while (c != null)
            {
                if (c.Count() >= _min_contour_count)
                {
                    System.Drawing.PointF[] mypoints = Array.ConvertAll(
                      c.ToArray<System.Drawing.Point>(),
                      value => new System.Drawing.PointF(value.X, value.Y)
                    );

                    Ellipse e = Emgu.CV.PointCollection.EllipseLeastSquareFitting(mypoints);
                    MCvBox2D box = e.MCvBox2D;
                    box.size.Height *= 0.5f;
                    box.size.Width *= 0.5f;
                    Ellipse final_ellipse = new Ellipse(box);

                    Matrix m = Matrix.Identity(3, 3);
                    m[0, 0] = Math.Cos(final_ellipse.MCvBox2D.angle);
                    m[0, 1] = -Math.Sin(final_ellipse.MCvBox2D.angle);
                    m[0, 2] = final_ellipse.MCvBox2D.center.X;
                    m[1, 0] = Math.Sin(final_ellipse.MCvBox2D.angle);
                    m[1, 1] = Math.Cos(final_ellipse.MCvBox2D.angle);
                    m[1, 2] = final_ellipse.MCvBox2D.center.Y;

                    Matrix inv = m.Inverse();
                    double rating = 0.0;
                    double a = final_ellipse.MCvBox2D.size.Width;
                    double b = final_ellipse.MCvBox2D.size.Height;

                    if (a < b)
                    {
                        double tmp = a;
                        a = b;
                        a = tmp;
                    }
                    foreach (System.Drawing.PointF p in mypoints)
                    {
                        Vector x = new Vector(new double[] { p.X, p.Y, 1 });
                        Matrix r = inv.Multiply(x.ToColumnMatrix());

                        rating += Math.Abs((Math.Pow(r[0, 0] / a, 2) + Math.Pow(r[1, 0] / b, 2)) - 1);
                    }
                    Console.WriteLine(rating);
                    if (rating < 50)
                    {
                        ellipses.Add(final_ellipse);
                    }

                }
                c = c.HNext;
            }

            ellipses.Sort(
              (a, b) =>
              {
                  double dista = a.MCvBox2D.center.X * a.MCvBox2D.center.X + a.MCvBox2D.center.Y * a.MCvBox2D.center.Y;
                  double distb = b.MCvBox2D.center.X * b.MCvBox2D.center.X + b.MCvBox2D.center.Y * b.MCvBox2D.center.Y;
                  return dista.CompareTo(distb);
              }
            );

            Bgr bgr = new Bgr(0, 255, 0);
            MCvFont f = new MCvFont(Emgu.CV.CvEnum.FONT.CV_FONT_HERSHEY_PLAIN, 0.8, 0.8);
            int count = 1;
            foreach (Ellipse e in ellipses)
            {
                image.Draw(e, bgr, 2);
                image.Draw(count.ToString(), ref f, new System.Drawing.Point((int)e.MCvBox2D.center.X, (int)e.MCvBox2D.center.Y), bgr);
                count++;
            }
        }
 public static void VisualizeError(
   Emgu.CV.Image<Bgr, Byte> img,
   Emgu.CV.ExtrinsicCameraParameters ecp,
   Emgu.CV.IntrinsicCameraParameters icp,
   double[] deviations,
   Vector[] isect_points) 
 {
   Bgr bgr = new Bgr(System.Drawing.Color.Green);
   MCvFont f = new MCvFont(Emgu.CV.CvEnum.FONT.CV_FONT_HERSHEY_PLAIN, 0.8, 0.8);
   foreach (Vector p in isect_points) {
     
     System.Drawing.PointF[] coords = Emgu.CV.CameraCalibration.ProjectPoints(
       new MCvPoint3D32f[] { p.ToEmguF() },
       ecp, icp
     );
     img.Draw(new CircleF(coords[0], 1), bgr, 1);
   }
 }
 /// <summary>
 /// Draw rectangle to image
 /// </summary>
 /// <param name="o">Rectangle</param>
 /// <param name="img">Image</param>
 public void DrawIndicator(object o, Emgu.CV.Image<Emgu.CV.Structure.Bgr, byte> img) {
   img.Draw((Rectangle)o, new Emgu.CV.Structure.Bgr(Color.Green), 1);
 }
    /// <summary>
    /// Draw pattern to image.
    /// </summary>
    /// <param name="img">Colored image to draw to.</param>
    /// <param name="image_points">Image center points.</param>
    /// <param name="pattern_found">If true green indicators are drawn, red ones otherwise.</param>
    public virtual void DrawPattern(Emgu.CV.Image<Bgr, Byte> img, PointF[] image_points, bool pattern_found) {
      if (image_points == null)
        return;

      System.Drawing.Color color = pattern_found ? System.Drawing.Color.Green : System.Drawing.Color.Red;
      Bgr bgr = new Bgr(color);
      MCvFont f = new MCvFont(Emgu.CV.CvEnum.FONT.CV_FONT_HERSHEY_PLAIN, 0.8, 0.8);
      int count = 1;
      foreach (PointF point in image_points) {
        img.Draw(new CircleF(point, 4), bgr, 2);
        Point p = point.ToNearestPoint();
        img.Draw(count.ToString(), ref f, new System.Drawing.Point(p.X + 5, p.Y - 5), bgr);
        count++;
      }
    }
Beispiel #10
0
        /// <summary>
        /// Function takes in a grayscale image and a colour image. Rectangles are located
        /// in the grayscale image (this is usually a thresholded image - so shapes are 
        /// strongly visible) and borders around them are drawn on the same coordinates in
        /// the colour image.
        /// </summary>
        /// <param name="img">Grayscale image where rectangles would be located</param>
        /// <param name="original">Original (colour) image where the rectangle borders will be drawn</param>
        private void drawBoxes(Emgu.CV.Image<Gray, Byte> img, Emgu.CV.Image<Bgr, Byte> original)
        {
            Gray cannyThreshold = new Gray(180);
            Gray cannyThresholdLinking = new Gray(120);
            Gray circleAccumulatorThreshold = new Gray(120);

            Image<Gray, Byte> cannyEdges = img.Canny(cannyThreshold, cannyThresholdLinking);
            LineSegment2D[] lines = cannyEdges.HoughLinesBinary(
                1, //Distance resolution in pixel-related units
                Math.PI / 45.0, //Angle resolution measured in radians.
                20, //threshold
                30, //min Line width
                10 //gap between lines
                )[0]; //Get the lines from the first channel

            #region Find rectangles
            List<MCvBox2D> boxList = new List<MCvBox2D>();

            using (MemStorage storage = new MemStorage()) //allocate storage for contour approximation
                for (Contour<Point> contours = cannyEdges.FindContours(); contours != null; contours = contours.HNext)
                {
                    Contour<Point> currentContour = contours.ApproxPoly(contours.Perimeter * 0.05, storage);

                    if (contours.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 the range of [80, 100] degree
                            bool isRectangle = true;
                            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) boxList.Add(currentContour.GetMinAreaRect());
                        }
                    }
                }
            #endregion

            #region draw rectangles
            Image<Bgr, Byte> rectangleImage = new Image<Bgr, byte>(img.Width, img.Height);
            foreach (MCvBox2D box in boxList)
            {
                rectangleImage.Draw(box, new Bgr(Color.DarkOrange), 2);
                original.Draw(box, new Bgr(Color.DarkOrange), 2);
            }

            capturedImageBox.Image = rectangleImage;
            #endregion
        }