Minimum Area Rectangle in CSharp: Difference between revisions

From EMGU
Jump to navigation Jump to search
Updated sample code to Emgu CV v4.4
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
== System Requirement ==
== System Requirement ==
{| style="text-align:center" border="1px" cellpadding="10" cellspacing="0"
{| style="text-align:center" border="1px" cellpadding="10" cellspacing="0"
!Component || Requirement || Detail
!Component || Requirement
|-
|-
|Emgu CV || Version 2.0.1.0 || Available only from SVN
|Emgu CV || Version 4.4.0  
|-
|-
|Operation System || Cross Platform ||
|Operation System || Cross Platform  
|}
|}


Line 18: Line 18:


Stopwatch watch = Stopwatch.StartNew();
Stopwatch watch = Stopwatch.StartNew();
MCvBox2D box = PointCollection.MinAreaRect(pts);
RotatedRect box = CvInvoke.MinAreaRect(pts);
watch.Stop();
watch.Stop();


#region draw the points and the box
#region draw the points and the box
Image<Bgr, byte> img = new Image<Bgr, byte>(400, 400, new Bgr(Color.White));
Mat img = new Mat(400, 400, DepthType.Cv8U, 3);
img.Draw(box, new Bgr(Color.Red), 1);
img.SetTo(new MCvScalar(255, 255, 255));
 
Point[] vertices = Array.ConvertAll(box.GetVertices(), Point.Round);
 
CvInvoke.Polylines(img, vertices, true, new MCvScalar(0, 0, 255), 1);
foreach (PointF p in pts)
foreach (PointF p in pts)
  img.Draw(new CircleF(p, 2), new Bgr(Color.Green), 1);
    CvInvoke.Circle(img, Point.Round(p), 2, new MCvScalar(0, 255, 0), 1);
#endregion
#endregion


ImageViewer.Show(img, String.Format("Time used: {0} milliseconds", watch.ElapsedMilliseconds));</source>
Emgu.CV.UI.ImageViewer.Show(img, String.Format("Time used: {0} milliseconds", watch.ElapsedMilliseconds));
</source>


== Result ==
== Result ==
[[image:MinimumAreaRectangleExample.png]]
[[image:MinimumAreaRectangleExample.png]]

Latest revision as of 13:38, 22 October 2020

System Requirement

Component Requirement
Emgu CV Version 4.4.0
Operation System Cross Platform

Source Code

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

Stopwatch watch = Stopwatch.StartNew();
RotatedRect box = CvInvoke.MinAreaRect(pts);
watch.Stop();

#region draw the points and the box
Mat img = new Mat(400, 400, DepthType.Cv8U, 3);
img.SetTo(new MCvScalar(255, 255, 255));

Point[] vertices = Array.ConvertAll(box.GetVertices(), Point.Round);

CvInvoke.Polylines(img, vertices, true, new MCvScalar(0, 0, 255), 1);
foreach (PointF p in pts)
    CvInvoke.Circle(img, Point.Round(p), 2, new MCvScalar(0, 255, 0), 1);
#endregion

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

Result