public List <Polygon> RecognizeImagePolygon(Image pImage, out List <Line> lines, bool deepSearch = true) { var f = new LineFinder(); lines = f.ScanBitmap(pImage as Bitmap); var s = new PolygonFinder(); List <Polygon> maxPolygon = new List <Polygon>(); int maxPoints = 0; double maxPolygonLength = 0; for (int n = 0; n < lines.Count * 2; n++) { int pointsCovered = 0; double polygonLength = 0; var polygon = s.FindContour(lines, out pointsCovered, out polygonLength, n); if (pointsCovered > maxPoints || (pointsCovered == maxPoints && polygonLength < maxPolygonLength)) { // si cubre más puntos, o cubre los mismos con menos tramos maxPolygon = polygon; maxPolygonLength = polygonLength; maxPoints = pointsCovered; } if (n == 10 && deepSearch == false) { break; } } // si tiene pendientes, retoma la búsqueda return(maxPolygon); }
private bool mergeOverlap() { for (int n = 0; n < Lines.Count; n++) { Line l1 = Lines[n]; for (int i = 0; i < Lines.Count; i++) { if (i != n) { Line l2 = Lines[i]; if (PolygonFinder.doLinesIntersect(l1, l2)) { Lines.RemoveAt(Math.Max(i, n)); Lines.RemoveAt(Math.Min(i, n)); if (l1.P1.X < l2.P1.X) { Lines.Add(new Line(l1.P1, l2.P2)); } else { Lines.Add(new Line(l2.P1, (l1.P2.X < l2.P2.X ? l2.P2 : l1.P2))); } return(true); } } } } return(false); }
private static List <Coordinate> CreatePointsSet(RadioInfo radio, bool project, ref double xMin, ref double xMax, ref double yMin, ref double yMax, Polygon polygon) { if (polygon[0] != polygon[polygon.Count - 1]) { if (polygon.Count == 2) { // le hace triángulo polygon.Add(new System.Drawing.Point(polygon[0].X, polygon[1].Y)); } polygon.Add(polygon[0]); } ////////////////////// Coordinate from = radio.GetFrom(); Coordinate to = radio.GetTo(); double xScale = (to.X - from.X) / radio.ImageExtents.Width; double yScale = (to.Y - from.Y) / radio.ImageExtents.Height; List <Coordinate> puntos = new List <Coordinate>(); IPoint lastP = null; foreach (System.Drawing.Point p in polygon) { var newPoint = new NetTopologySuite.Geometries.Point(xScale * p.X + from.X, yScale * (radio.ImageExtents.Height - p.Y) + from.Y); IPoint point; if (project) { point = Projections.UnprojectPoint(newPoint, Context.Projection); } else { point = newPoint; } xMin = Math.Min(point.X, xMin); xMax = Math.Max(point.X, xMax); yMin = Math.Min(point.Y, yMin); yMax = Math.Max(point.Y, yMax); puntos.Add(new Coordinate(point.X, point.Y)); if (lastP != null) { radio.TotalPerimeter += PolygonFinder.EuclideanDist(point, lastP); } lastP = point; } return(puntos); }