Difference between revisions of "Convex Hull in CSharp"

From Emgu CV: OpenCV in .NET (C#, VB, C++ and more)
Jump to: navigation, search
(Emgu CV 3.x)
Line 15: Line 15:
 
<div class="mw-collapsible-content">
 
<div class="mw-collapsible-content">
 
<source lang="csharp">
 
<source lang="csharp">
        #region Create some random points
+
#region Create some random points
        Random r = new Random();
+
Random r = new Random();
        PointF[] pts = new PointF[200];
+
PointF[] pts = new PointF[200];
        for (int i = 0; i < pts.Length; i++)
+
for (int i = 0; i < pts.Length; i++)
        {
+
{
            pts[i] = new PointF((float)(100 + r.NextDouble() * 400), (float)(100 + r.NextDouble() * 400));
+
  pts[i] = new PointF((float)(100 + r.NextDouble() * 400), (float)(100 + r.NextDouble() * 400));
        }
+
}
        #endregion
+
#endregion
  
        Mat img = new Mat(600, 600, DepthType.Cv8U, 3);
+
Mat img = new Mat(600, 600, DepthType.Cv8U, 3);
        img.SetTo(new MCvScalar(255.0, 255.0, 255.0));
+
img.SetTo(new MCvScalar(255.0, 255.0, 255.0));
        //Draw the points  
+
//Draw the points  
        foreach (PointF p in pts)
+
foreach (PointF p in pts)
            CvInvoke.Circle(img, Point.Round(p), 3, new MCvScalar(0.0, 0.0, 0.0));
+
  CvInvoke.Circle(img, Point.Round(p), 3, new MCvScalar(0.0, 0.0, 0.0));
  
        //Find and draw the convex hull
+
//Find and draw the convex hull
  
        Stopwatch watch = Stopwatch.StartNew();
+
Stopwatch watch = Stopwatch.StartNew();
        PointF[] hull = CvInvoke.ConvexHull(pts, true);
+
PointF[] hull = CvInvoke.ConvexHull(pts, true);
        watch.Stop();
+
watch.Stop();
        CvInvoke.Polylines(
+
CvInvoke.Polylines(
            img,
+
  img,
 
#if NETFX_CORE
 
#if NETFX_CORE
            Extensions.ConvertAll<PointF, Point>(hull, Point.Round),
+
  Extensions.ConvertAll<PointF, Point>(hull, Point.Round),
 
#else
 
#else
            Array.ConvertAll<PointF, Point>(hull, Point.Round),
+
  Array.ConvertAll<PointF, Point>(hull, Point.Round),
 
#endif
 
#endif
            true, new MCvScalar(255.0, 0.0, 0.0));
+
  true, new MCvScalar(255.0, 0.0, 0.0));
 
 
        Emgu.CV.UI.ImageViewer.Show(img, String.Format("Convex Hull Computed in {0} milliseconds", watch.ElapsedMilliseconds));
 
  
 +
Emgu.CV.UI.ImageViewer.Show(img, String.Format("Convex Hull Computed in {0} milliseconds", watch.ElapsedMilliseconds));
 
</source>
 
</source>
 
</div>
 
</div>

Revision as of 21:25, 8 June 2015

System Requirement

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

Source Code

Emgu CV 3.x

Click to view source code

#region Create some random points
Random r = new Random();
PointF[] pts = new PointF[200];
for (int i = 0; i < pts.Length; i++)
{
   pts[i] = new PointF((float)(100 + r.NextDouble() * 400), (float)(100 + r.NextDouble() * 400));
}
#endregion

Mat img = new Mat(600, 600, DepthType.Cv8U, 3);
img.SetTo(new MCvScalar(255.0, 255.0, 255.0));
//Draw the points 
foreach (PointF p in pts)
   CvInvoke.Circle(img, Point.Round(p), 3, new MCvScalar(0.0, 0.0, 0.0));

//Find and draw the convex hull

Stopwatch watch = Stopwatch.StartNew();
PointF[] hull = CvInvoke.ConvexHull(pts, true);
watch.Stop();
CvInvoke.Polylines(
   img,
#if NETFX_CORE
   Extensions.ConvertAll<PointF, Point>(hull, Point.Round),
#else
   Array.ConvertAll<PointF, Point>(hull, Point.Round),
#endif
   true, new MCvScalar(255.0, 0.0, 0.0));

Emgu.CV.UI.ImageViewer.Show(img, String.Format("Convex Hull Computed in {0} milliseconds", watch.ElapsedMilliseconds));

Emgu CV 1.x

Click to view source code

#region Create some random points
Random r = new Random();
PointF[] pts = new PointF[200];
for (int i = 0; i < pts.Length; i++)
{
   pts[i] = new PointF((float)(100 + r.NextDouble() * 400), (float)(100 + r.NextDouble() * 400));
}
#endregion

Image<Bgr, Byte> img = new Image<Bgr, byte>(600, 600, new Bgr(255.0, 255.0, 255.0));
//Draw the points 
foreach (PointF p in pts)
   img.Draw(new CircleF(p, 3), new Bgr(0.0, 0.0, 0.0), 1);

//Find and draw the convex hull
using (MemStorage storage = new MemStorage())
{
   Stopwatch watch = Stopwatch.StartNew();
   PointF[] hull = PointCollection.ConvexHull(pts, storage, Emgu.CV.CvEnum.ORIENTATION.CV_CLOCKWISE).ToArray();
   watch.Stop();
   img.DrawPolyline(
       Array.ConvertAll<PointF, Point>(hull, Point.Round),
       true, new Bgr(255.0, 0.0, 0.0), 1);

   ImageViewer.Show(img, String.Format("Convex Hull Computed in {0} milliseconds", watch.ElapsedMilliseconds));
}

Result

ConvexHullExample.png