Ellipse Fitting in CSharp: Difference between revisions

From EMGU
Jump to navigation Jump to search
Undo revision 995 by Inuxejiq (talk)
No edit summary
 
Line 3: Line 3:
!Component || Requirement || Detail  
!Component || Requirement || Detail  
|-
|-
|Emgu CV || Version 2.0.1.0 || Available only from SVN
|Emgu CV || Version 2.0.1.0 + ||  
|-
|-
|Operation System || Cross Platform ||
|Operation System || Cross Platform ||
Line 9: Line 9:


== Source Code ==
== Source Code ==
=== Emgu CV 3.x ===
<div class="toccolours mw-collapsible mw-collapsed">
Click to view source code
<div class="mw-collapsible-content">
<source lang="csharp">
#region generate random points
System.Random r = new Random();
int sampleCount = 100;
Ellipse modelEllipse = new Ellipse(new PointF(200, 200), new SizeF(150, 60), 90);
PointF[] pts = PointCollection.GeneratePointCloud(modelEllipse, sampleCount);
#endregion
Stopwatch watch = Stopwatch.StartNew();
Ellipse fittedEllipse = PointCollection.EllipseLeastSquareFitting(pts);
watch.Stop();
#region draw the points and the fitted ellips
Mat img = new Mat(400, 400, DepthType.Cv8U, 3);
img.SetTo(new MCvScalar(255, 255, 255));
foreach (PointF p in pts)
  CvInvoke.Circle(img, Point.Round(p), 2, new MCvScalar(0, 255, 0), 1);
RotatedRect rect = fittedEllipse.RotatedRect;
rect.Angle += 90; //the detected ellipse was off by 90 degree
CvInvoke.Ellipse(img, rect, new MCvScalar(0, 0, 255), 2);
#endregion
       
Emgu.CV.UI.ImageViewer.Show(img, String.Format("Time used: {0} milliseconds", watch.ElapsedMilliseconds));
</source>
</div>
</div>
=== Emgu CV 2.x ===
<div class="toccolours mw-collapsible mw-collapsed">
Click to view source code
<div class="mw-collapsible-content">
<source lang="csharp">
<source lang="csharp">
#region generate random points
#region generate random points
Line 31: Line 67:


</source>
</source>
 
</div>
</div>
== Result ==
== Result ==
[[image:EllipsefittingExample.png]]
[[image:EllipsefittingExample.png]]

Latest revision as of 21:38, 8 June 2015

System Requirement

Component Requirement Detail
Emgu CV Version 2.0.1.0 +
Operation System Cross Platform

Source Code

Emgu CV 3.x

Click to view source code

#region generate random points
System.Random r = new Random();
int sampleCount = 100;
Ellipse modelEllipse = new Ellipse(new PointF(200, 200), new SizeF(150, 60), 90);
PointF[] pts = PointCollection.GeneratePointCloud(modelEllipse, sampleCount);
#endregion

Stopwatch watch = Stopwatch.StartNew();
Ellipse fittedEllipse = PointCollection.EllipseLeastSquareFitting(pts);
watch.Stop();

#region draw the points and the fitted ellips
Mat img = new Mat(400, 400, DepthType.Cv8U, 3);
img.SetTo(new MCvScalar(255, 255, 255));
foreach (PointF p in pts)
   CvInvoke.Circle(img, Point.Round(p), 2, new MCvScalar(0, 255, 0), 1);
RotatedRect rect = fittedEllipse.RotatedRect;
rect.Angle += 90; //the detected ellipse was off by 90 degree
CvInvoke.Ellipse(img, rect, new MCvScalar(0, 0, 255), 2);
#endregion
         
Emgu.CV.UI.ImageViewer.Show(img, String.Format("Time used: {0} milliseconds", watch.ElapsedMilliseconds));

Emgu CV 2.x

Click to view source code

#region generate random points
System.Random r = new Random();
int sampleCount = 100;
Ellipse modelEllipse = new Ellipse(new PointF(200, 200), new SizeF(150, 60), 30);
PointF[] pts = PointCollection.GeneratePointCloud(modelEllipse, sampleCount);
#endregion

Stopwatch watch = Stopwatch.StartNew();
Ellipse fittedEllipse = PointCollection.EllipseLeastSquareFitting(pts);
watch.Stop();

#region draw the points and the fitted ellipse
Image<Bgr, byte> img = new Image<Bgr, byte>(400, 400, new Bgr(Color.White));
foreach (PointF p in pts)
   img.Draw(new CircleF(p, 2), new Bgr(Color.Green), 1);
img.Draw(fittedEllipse, new Bgr(Color.Red), 2);
#endregion

ImageViewer.Show(img, String.Format("Time used: {0}milliseconds", watch.ElapsedMilliseconds));

Result