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++;
      }
    }
Example #2
0
        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++;
            }
        }
Example #3
0
    /// <summary>
    /// Find ellipses in image
    /// </summary>
    /// <param name="img">Image to search pattern for</param>
    /// <param name="image_points">Detected centers</param>
    /// <returns>True if pattern was found, false otherwise</returns>
    public override bool FindPattern(Emgu.CV.Image<Gray, byte> img, out System.Drawing.PointF[] image_points) {
      Emgu.CV.Image<Gray, byte> gray = img.Convert<Gray, byte>();
      gray._ThresholdBinary(new Gray(_binary_threshold), new Gray(255.0));
      gray._Not(); // Circles are black, black is considered backgroud, therefore flip.

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

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

      // Filter out all ellipses below rating threshold
      List<Parsley.Core.DetectedEllipse> finals =
        new List<Parsley.Core.DetectedEllipse>(
          ellipses.Where(e => { return e.Rating < this.MeanDistanceThreshold; })
        );

      // At least the number of required ellipses need to be found
      if (finals.Count < _number_circle_centers.Width * _number_circle_centers.Height) {
        image_points = new System.Drawing.PointF[0];
        return false;
      }

      int[] marker_ids;
      if (!FindMarkerEllipses(gray, finals, out marker_ids)) {
        image_points = new System.Drawing.PointF[0];
        return false;
      }

      // Check that all markers are found
      if (marker_ids.Length != 4) {
        image_points = new System.Drawing.PointF[0];
        return false;
      }

      // Find intrinsic/extrinsic calibration matrices based on known marker correspondences
      Emgu.CV.IntrinsicCameraParameters icp;
      Emgu.CV.ExtrinsicCameraParameters ecp;
      ApproximatePlane(finals, marker_ids, out icp, out ecp, img.Size);

      // Project all object points to image points
      MCvPoint3D32f[] converted_object_points = Array.ConvertAll(
        this.ObjectPoints.ToArray(),
        value => { return value.ToEmguF(); });

      System.Drawing.PointF[] expected_image_points = 
        Emgu.CV.CameraCalibration.ProjectPoints(converted_object_points, ecp, icp);

      image_points =
        expected_image_points.Select(
          e => { return NearestEllipseCenter(finals, e); }
        ).Where(
          ne => { return Math.Sqrt(ne.dist2) < _ellipse_distance; }
        ).Select(
          ne => { return ne.center; }
        ).ToArray();

      // currently we need to detect all requested ellipses.
      return image_points.Length == _number_circle_centers.Width * _number_circle_centers.Height;

    }
Example #4
0
        /// <summary>
        /// Find ellipses in image
        /// </summary>
        /// <param name="img">Image to search pattern for</param>
        /// <param name="image_points">Detected centers</param>
        /// <returns>True if pattern was found, false otherwise</returns>
        public override bool FindPattern(Emgu.CV.Image <Gray, byte> img, out System.Drawing.PointF[] image_points)
        {
            Emgu.CV.Image <Gray, byte> gray = img.Convert <Gray, byte>();
            gray._ThresholdBinary(new Gray(_binary_threshold), new Gray(255.0));
            gray._Not(); // Circles are black, black is considered backgroud, therefore flip.

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

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

            // Filter out all ellipses below rating threshold
            List <Parsley.Core.DetectedEllipse> finals =
                new List <Parsley.Core.DetectedEllipse>(
                    ellipses.Where(e => { return(e.Rating < this.MeanDistanceThreshold); })
                    );

            // At least the number of required ellipses need to be found
            if (finals.Count < _number_circle_centers.Width * _number_circle_centers.Height)
            {
                image_points = new System.Drawing.PointF[0];
                return(false);
            }

            int[] marker_ids;
            if (!FindMarkerEllipses(gray, finals, out marker_ids))
            {
                image_points = new System.Drawing.PointF[0];
                return(false);
            }

            // Check that all markers are found
            if (marker_ids.Length != 4)
            {
                image_points = new System.Drawing.PointF[0];
                return(false);
            }

            // Find intrinsic/extrinsic calibration matrices based on known marker correspondences
            Emgu.CV.IntrinsicCameraParameters icp;
            Emgu.CV.ExtrinsicCameraParameters ecp;
            ApproximatePlane(finals, marker_ids, out icp, out ecp, img.Size);

            // Project all object points to image points
            MCvPoint3D32f[] converted_object_points = Array.ConvertAll(
                this.ObjectPoints.ToArray(),
                value => { return(value.ToEmguF()); });

            System.Drawing.PointF[] expected_image_points =
                Emgu.CV.CameraCalibration.ProjectPoints(converted_object_points, ecp, icp);

            image_points =
                expected_image_points.Select(
                    e => { return(NearestEllipseCenter(finals, e)); }
                    ).Where(
                    ne => { return(Math.Sqrt(ne.dist2) < _ellipse_distance); }
                    ).Select(
                    ne => { return(ne.center); }
                    ).ToArray();

            // currently we need to detect all requested ellipses.
            return(image_points.Length == _number_circle_centers.Width * _number_circle_centers.Height);
        }