Skip to content
shimat edited this page Jan 19, 2019 · 1 revision

This sample needs System.Linq.Enumerable extension methods.

Initialization

const int Size = 600;

// Creates random point list
var rand = new Random();
var points = Enumerable.Range(0, 100).Select(_ =>
    new Point2f(rand.Next(0, Size), rand.Next(0, Size))).ToArray();

// Initializes Subdiv2D
var subdiv = new Subdiv2D();
subdiv.InitDelaunay(new Rect(0, 0, Size, Size));
subdiv.Insert(points);

Voronoi Diagram Extraction

// Extracts voronoi diagram
Point2f[][] facetList;
Point2f[] facetCenters;
subdiv.GetVoronoiFacetList(null, out facetList, out facetCenters);

// Draws voronoi diagram
Mat voronoi = Mat.Zeros(Size, Size, MatType.CV_8UC3);
foreach (var list in facetList)
{
    var before = list.Last(); 
    foreach (var p in list)
    {
        voronoi.Line(before, p, new Scalar(128, 255, 64), 1);
        before = p;
    }
}

Delaunay Diagram Extraction

// Extracts delaunay diagram
Vec4f[] edgeList = subdiv.GetEdgeList();

// Draws delaunay diagram
Mat delaunay = Mat.Zeros(Size, Size, MatType.CV_8UC3);
foreach (var edge in edgeList)
{
    var p1 = new Point(edge.Item0, edge.Item1);
    var p2 = new Point(edge.Item2, edge.Item3);
    delaunay.Line(p1, p2, new Scalar(128, 255, 64), 1);
}
Clone this wiki locally