/// <summary> /// Count the number of black/white transitions for a single ellipse /// </summary> /// <param name="e">Ellipse</param> /// <param name="matrix">Affine ellipse frame that transforms the ellipse to a circle located at origin</param> /// <param name="gray">Binary image</param> /// <returns>The number of black/white transitions found</returns> private int CountBinaryTransitions(DetectedEllipse e, Matrix matrix, Emgu.CV.Image <Gray, byte> gray) { // Generate points on circle double r = e.Ellipse.MCvBox2D.size.Height * 0.5; double t_step = (2 * Math.PI) / _number_circle_points; System.Drawing.Rectangle rect = new System.Drawing.Rectangle(System.Drawing.Point.Empty, gray.Size); int count_transitions = 0; double last_intensity = 0; for (double t = 0; t <= 2 * Math.PI; t += t_step) { Vector v = new Vector(new double[] { r *Math.Cos(t), r * Math.Sin(t), 1.0 }); Vector x = matrix.Multiply(v.ToColumnMatrix()).GetColumnVector(0); System.Drawing.Point p = new System.Drawing.Point((int)Math.Round(x[0]), (int)Math.Round(x[1])); if (rect.Contains(p)) { if (t == 0) { last_intensity = gray[p].Intensity; } else { double i = gray[p].Intensity; if (i != last_intensity) { count_transitions += 1; last_intensity = i; } } } } return(count_transitions); }
/// <summary> /// Count the number of black/white transitions for a single ellipse /// </summary> /// <param name="e">Ellipse</param> /// <param name="matrix">Affine ellipse frame that transforms the ellipse to a circle located at origin</param> /// <param name="gray">Binary image</param> /// <returns>The number of black/white transitions found</returns> private int CountBinaryTransitions(DetectedEllipse e, Matrix matrix, Emgu.CV.Image<Gray, byte> gray) { // Generate points on circle double r = e.Ellipse.MCvBox2D.size.Height * 0.5; double t_step = (2 * Math.PI) / _number_circle_points; System.Drawing.Rectangle rect = new System.Drawing.Rectangle(System.Drawing.Point.Empty, gray.Size); int count_transitions = 0; double last_intensity = 0; for (double t = 0; t <= 2 * Math.PI; t += t_step) { Vector v = new Vector(new double[] { r * Math.Cos(t), r * Math.Sin(t), 1.0 }); Vector x = matrix.Multiply(v.ToColumnMatrix()).GetColumnVector(0); System.Drawing.Point p = new System.Drawing.Point((int)Math.Round(x[0]), (int)Math.Round(x[1])); if (rect.Contains(p)) { if (t == 0) { last_intensity = gray[p].Intensity; } else { double i = gray[p].Intensity; if (i != last_intensity) { count_transitions += 1; last_intensity = i; } } } } return count_transitions; }