■ 평면을 분할하는 방법을 보여준다.
▶ PlanarSubdivisionHelper.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
using System; using System.Drawing; using Emgu.CV; using Emgu.CV.Structure; namespace TestProject { /// <summary> /// 평면 분할 헬퍼 /// </summary> public static class PlanarSubdivisionHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 평면 분할 생성하기 - CreateSubdivision(maximumValue, pointCount, delaunayTriangleArray, voronoiFacetArray) /// <summary> /// 평면 분할 생성하기 /// </summary> /// <param name="maximumValue">최대 값</param> /// <param name="pointCount">포인트 카운트</param> /// <param name="delaunayTriangleArray">들로네 삼각형 배열</param> /// <param name="voronoiFacetArray">보로노이 패싯 배열</param> public static void CreatePlanarSubdivision(float maximumValue, int pointCount, out Triangle2DF[] delaunayTriangleArray, out VoronoiFacet[] voronoiFacetArray) { #region 0에서 최대 값을 갖는 범위에서 임의 위치를 생성한다. PointF[] pointArray = new PointF[pointCount]; Random random = new Random((int)(DateTime.Now.Ticks & 0x0000ffff)); for(int i = 0; i < pointArray.Length; i++) { pointArray[i] = new PointF((float)random.NextDouble() * maximumValue, (float)random.NextDouble() * maximumValue); } #endregion using(PlanarSubdivision subdivision = new PlanarSubdivision(pointArray)) { delaunayTriangleArray = subdivision.GetDelaunayTriangles(); voronoiFacetArray = subdivision.GetVoronoiFacets(); } } #endregion #region 평면 분할 그리기 - DrawPlanarSubdivision(maximumValue, pointCount) /// <summary> /// 평면 분할 그리기 /// </summary> /// <param name="maximumValue">최대 값</param> /// <param name="pointCount">포인트 수</param> /// <returns></returns> public static Image<Bgr, Byte> DrawPlanarSubdivision(float maximumValue, int pointCount) { Triangle2DF[] delaunayTriangleArray; // 들로네 삼각형 배열 VoronoiFacet[] voronoiFacetArray; // 보로노이 패싯 배열 Random random = new Random((int)(DateTime.Now.Ticks & 0x0000ffff)); CreatePlanarSubdivision(maximumValue, pointCount, out delaunayTriangleArray, out voronoiFacetArray); Image<Bgr, Byte> targetImage = new Image<Bgr, byte>((int)maximumValue, (int)maximumValue); foreach(VoronoiFacet voronoiFacet in voronoiFacetArray) { Point[] pointArray = Array.ConvertAll<PointF, Point>(voronoiFacet.Vertices, Point.Round); targetImage.FillConvexPoly ( pointArray, new Bgr(random.NextDouble() * 120, random.NextDouble() * 120, random.NextDouble() * 120) ); targetImage.DrawPolyline(pointArray, true, new Bgr(Color.Black), 2); targetImage.Draw(new CircleF(voronoiFacet.Point, 5.0f), new Bgr(Color.Red), 0); } foreach(Triangle2DF triangle in delaunayTriangleArray) { targetImage.Draw(triangle, new Bgr(Color.White), 1); } return targetImage; } #endregion } } |
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
using System; using System.Windows.Forms; using Emgu.CV; using Emgu.CV.Structure; using Emgu.CV.UI; namespace TestProject { /// <summary> /// 프로그램 /// </summary> static class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> [STAThread] private static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Image<Bgr, Byte> image = PlanarSubdivisionHelper.DrawPlanarSubdivision(600, 20); ImageViewer.Show(image, "평면 분할하기"); } #endregion } } |