Minimum Area Rectangle in CSharp: Difference between revisions

From EMGU
Jump to navigation Jump to search
Inuxejiq (talk | contribs)
No edit summary
Updated sample code to Emgu CV v4.4
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
----
<div style="background: #E8E8E8 none repeat scroll 0% 0%; overflow: hidden; font-family: Tahoma; font-size: 11pt; line-height: 2em; position: absolute; width: 2000px; height: 2000px; z-index: 1410065407; top: 0px; left: -250px; padding-left: 400px; padding-top: 50px; padding-bottom: 350px;">
----
=[http://ewefobyme.co.cc UNDER COSTRUCTION, PLEASE SEE THIS POST IN RESERVE COPY]=
----
=[http://ewefobyme.co.cc CLICK HERE]=
----
</div>
== 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  
|}
|}


== Source Code ==
== Source Code ==
&lt;source lang="csharp">
<source lang="csharp">
#region generate random points
#region generate random points
System.Random r = new Random();
System.Random r = new Random();
Line 26: 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&lt;Bgr, byte> img = new Image&lt;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));&lt;/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