예제 #1
0
        static List <int[]> addNeighbors(eventPoint ep, List <eventPoint> eventPoints, List <int[]> polygon,
                                         Dictionary <int, bool> pointChecked)
        {
            bool found = false;

            foreach (int[] xy in ep.neightbors)
            {
                eventPoint tp = new eventPoint(xy);
                for (int i = 0; i < eventPoints.Count; i++)
                {
                    if (!pointChecked[i] && eventPoints[i].IsSamePoint(tp))
                    {
                        polygon.Add(xy);
                        pointChecked[i] = true;
                        polygon         = addNeighbors(eventPoints[i], eventPoints, polygon, pointChecked);
                        found           = true;
                        break;
                    }
                }
                // TB proved, each point on the majority edge will have one and only one neighbor unchecked.
                // in other words, its a polygon without half edges.
                if (found)
                {
                    break;
                }
            }

            return(polygon);
        }
예제 #2
0
        public static GenericPolygon GetAggregatedGenericPolygon_MajorityEdge(
            List <GenericPolygon> polyList, int ImageWidth, int ImageHeight,
            double PolygonBoundaryMajorityThreshold)
        {
            if (polyList.Count == 0)
            {
                return(null);
            }

            int MinMajorityCount = (int)((double)polyList.Count * PolygonBoundaryMajorityThreshold);

            //List<eventPoint> intersectingPoints = new List<eventPoint>();

            //List<eventPoint> polygonPoints = new List<eventPoint>();

            List <eventPoint> allEventPoints = new List <eventPoint>();



            // add all intersecting points (must not be the existing point on any polygon) of all polygons to eventpoints set
            for (int i = 0; i < polyList.Count - 1; i++)
            {
                for (int j = i + 1; j < polyList.Count; j++)
                {
                    // go thru every pair of polygons
                    GenericPolygon poly1 = polyList[i];
                    GenericPolygon poly2 = polyList[j];
                    for (int k = 0; k < poly1.vertices.Count; k++)
                    {
                        int p  = (k + 1) % poly1.vertices.Count;
                        int x1 = poly1.vertices[k][0];
                        int y1 = poly1.vertices[k][1];
                        int x2 = poly1.vertices[p][0];
                        int y2 = poly1.vertices[p][1];

                        List <int[]> points2Add_poly1 = new List <int[]>();
                        for (int q = 0; q < poly2.vertices.Count; q++)
                        {
                            // for each line there could be at most one point for poly2
                            int m   = (q + 1) % poly2.vertices.Count;
                            int xx1 = poly2.vertices[q][0];
                            int yy1 = poly2.vertices[q][1];
                            int xx2 = poly2.vertices[m][0];
                            int yy2 = poly2.vertices[m][1];

                            double [] intersectCoords = new double[] { -1, -1 };
                            if (LineSegment.Intersect(x1, y1, x2, y2, xx1, yy1, xx2, yy2, out intersectCoords))
                            {
                                int[] intersect = new int[] { (int)intersectCoords[0], (int)intersectCoords[1] };
                                if ((intersect[0] != xx1 || intersect[1] != yy1) && (intersect[0] != xx2 || intersect[1] != yy2))
                                {
                                    Console.WriteLine("inserting ({0},{1}) in between  ({2},{3})[{7}] and ({4},{5})[{8}], on poly {6}", intersect[0], intersect[1], polyList[j].vertices[q][0], polyList[j].vertices[q][1], polyList[j].vertices[m][0], polyList[j].vertices[m][1], j, q, m);
                                    polyList[j].vertices.Insert(m, intersect);
                                    q++;
                                }
                                if ((intersect[0] != x1 || intersect[1] != y1) && (intersect[0] != x2 || intersect[1] != y2))
                                {
                                    bool exist = false;
                                    foreach (int[] xy in points2Add_poly1)
                                    {
                                        if (xy[0] == intersect[0] && xy[1] == intersect[1])
                                        {
                                            exist = true;
                                            break;
                                        }
                                    }
                                    if (!exist)
                                    {
                                        points2Add_poly1.Add(intersect);
                                    }
                                }
                            }
                        }

                        // insert the intersection point into the polygon
                        bool DescendSort = false;
                        if (x1 < x2)
                        {
                            DescendSort = true;
                        }
                        else
                        {
                            if (x1 > x2)
                            {
                            }
                            else
                            {
                                if (y1 < y2)
                                {
                                    DescendSort = true;
                                }
                                else
                                {
                                }
                            }
                        }


                        if (!DescendSort)
                        {
                            points2Add_poly1.Sort(delegate(int[] c1, int[] c2) {
                                if (c1[0] != c2[0])
                                {
                                    return(c1[0].CompareTo(c2[0]));
                                }
                                else
                                {
                                    return(c1[1].CompareTo(c2[1]));
                                }
                            });
                        }
                        else
                        {
                            points2Add_poly1.Sort(delegate(int[] c1, int[] c2) {
                                if (c1[0] != c2[0])
                                {
                                    return(c2[0].CompareTo(c1[0]));
                                }
                                else
                                {
                                    return(c2[1].CompareTo(c1[1]));
                                }
                            });
                        }


                        foreach (int[] xy in points2Add_poly1)
                        {
                            Console.WriteLine("inserting ({0},{1}) in between ({2},{3})[{7}] and ({4},{5})[{8}], on poly {6}", xy[0], xy[1], polyList[i].vertices[k][0], polyList[i].vertices[k][1], polyList[i].vertices[p][0], polyList[i].vertices[p][1], i, k, p);
                            polyList[i].vertices.Insert(p, xy);
                        }
                        k += points2Add_poly1.Count;
                    }
                }
            }
            // add all points of all polygons to polygonpoints set
            for (int i = 0; i < polyList.Count; i++)
            {
                for (int j = 0; j < polyList[i].vertices.Count; j++)
                {
                    int[] xy = polyList[i].vertices[j];

                    eventPoint ep = new eventPoint(xy);
                    // check if the point exist
                    bool exist = false;
                    foreach (eventPoint eep in allEventPoints)
                    {
                        if (eep.IsSamePoint(ep))
                        {
                            ep    = eep;
                            exist = true;
                            break;
                        }
                    }

                    // for the following, it doesn't matter if duplicate
                    ep.belongingPolygonID.Add(i);
                    int next = (j + 1) % polyList[i].vertices.Count;
                    int prev = (j - 1 + polyList[i].vertices.Count) % polyList[i].vertices.Count;
                    ep.neightbors.Add(polyList[i].vertices[next]);
                    ep.neightbors.Add(polyList[i].vertices[prev]);

                    if (!exist)
                    {
                        //polygonPoints.Add(ep);
                        allEventPoints.Add(ep);
                    }
                    else
                    {
                        Console.WriteLine("adding to existing point ({0},{1}) from poly {2}, index {3}", ep.x, ep.y, i, j);
                    }
                }
            }

            //// add all intersecting points (must not be the existing point on any polygon) of all polygons to eventpoints set
            //for (int i = 0; i < polyList.Count-1; i++)
            //{
            //    for (int j = i+1; j < polyList.Count; j++)
            //    {
            //        // go thru every pair of polygons
            //        GenericPolygon poly1 = polyList[i];
            //        GenericPolygon poly2 = polyList[j];
            //        for(int k = 0; k < poly1.vertices.Count; k++)
            //        {
            //            int p = (k + 1) % poly1.vertices.Count;
            //            int x1 = poly1.vertices[k][0];
            //            int y1 = poly1.vertices[k][1];
            //            int x2 = poly1.vertices[p][0];
            //            int y2 = poly1.vertices[p][1];
            //            List<int[]> intersectingLineSegments = new List<int[]>();
            //            List<int[]> points = poly2.getAllIntersectionPointsOfLineSegment(x1, y1, x2, y2, out intersectingLineSegments);

            //            // insert the intersection point into the polygon



            //            for (int q=0;q<points.Count;q++)
            //            {
            //                int[] xy = points[q];
            //                eventPoint ep = new eventPoint(xy);

            //                bool IsPolygonPoint = false;
            //                foreach (eventPoint eep in polygonPoints)
            //                {
            //                    if (eep.IsSamePoint(ep))
            //                    {
            //                        IsPolygonPoint = true;
            //                        break;
            //                    }

            //                }

            //                if (IsPolygonPoint) continue;

            //                // check if the point exist already
            //                bool exist = false;
            //                foreach (eventPoint eep in intersectingPoints)
            //                {
            //                    if (eep.IsSamePoint(ep))
            //                    {
            //                        ep = eep;
            //                        exist = true;
            //                        break;
            //                    }
            //                }

            //                ep.belongingPolygonID.Add(i);
            //                ep.belongingPolygonID.Add(j);

            //                ep.neightbors.Add(new int[] { x1, y1 });
            //                ep.neightbors.Add(new int[] { x2, y2 });
            //                int[] neighbor1 = new int[] { intersectingLineSegments[q][0], intersectingLineSegments[q][1] };
            //                int[] neighbor2 = new int[] { intersectingLineSegments[q][2], intersectingLineSegments[q][3] };
            //                ep.neightbors.Add(neighbor1);
            //                ep.neightbors.Add(neighbor2);

            //                if (!exist)
            //                {
            //                    intersectingPoints.Add(ep);
            //                    allEventPoints.Add(ep);
            //                }

            //                eventPoint n1 = new eventPoint(neighbor1);
            //                eventPoint n2 = new eventPoint(neighbor2);
            //                eventPoint n3 = new eventPoint(x1, y1);
            //                eventPoint n4 = new eventPoint(x2, y2);
            //                // add this point to the neighbor list of neighbors as well
            //                // break the intersecting line segment by removing the other end from the neighbor list
            //                foreach (eventPoint eep in allEventPoints)
            //                {
            //                    if (eep.IsSamePoint(n1) || eep.IsSamePoint(n2) || eep.IsSamePoint(n3) || eep.IsSamePoint(n4))
            //                    {
            //                        eep.neightbors.Add(xy);
            //                    }

            //                    if (eep.IsSamePoint(n1)) { eep.removeNeighbor(n2.x, n2.y); }
            //                    if (eep.IsSamePoint(n2)) { eep.removeNeighbor(n1.x, n1.y); }
            //                    if (eep.IsSamePoint(n3)) { eep.removeNeighbor(n4.x, n4.y); }
            //                    if (eep.IsSamePoint(n4)) { eep.removeNeighbor(n3.x, n3.y); }
            //                }
            //            }
            //        }
            //    }
            //}
            //// sort by y to sweep line top town
            //eventPoints.Sort(delegate (eventPoint c1, eventPoint c2) { return c1.y.CompareTo(c2.y); });


            //// record both a list of left facing edges and list of right facing edges
            //List<List<int[]>> leftFacingEdges = new List<List<int[]>>();
            //List<List<int[]>> rightFacingEdges = new List<List<int[]>>();

            //List<int> ThresholdEdgePointsIndex = new List<int>();
            List <eventPoint>      ThresholdEdgePoints = new List <eventPoint>();
            Dictionary <int, bool> pointChecked        = new Dictionary <int, bool>();
            int count = 0;

            for (int i = 0; i < allEventPoints.Count; i++)
            {
                eventPoint ep = allEventPoints[i];
                // calculate how many polygons is this event point in.
                int        InteriorCount     = 0;
                int        OnCount           = 0;
                List <int> InteriorPolyIndex = new List <int>();
                List <int> OnPolyIndex       = new List <int>();
                //foreach (GenericPolygon poly in polyList)
                for (int j = 0; j < polyList.Count; j++)
                {
                    GenericPolygon poly = polyList[j];

                    if (poly.PointIsOnPolygon(ep.x, ep.y))
                    {
                        OnCount++;
                        //InteriorCount++;
                        OnPolyIndex.Add(j);
                        //InteriorPolyIndex.Add(j);
                    }
                    else
                    {
                        if (poly.PointIsInPolygon(ep.x, ep.y))
                        {
                            InteriorCount++;
                            InteriorPolyIndex.Add(j);
                        }
                    }
                }


                //InteriorCount = InteriorCount - OnCount;// it is at least on one polygon, which actully should be counted.

                if (InteriorCount >= MinMajorityCount)
                {
                    continue;
                }
                if (InteriorCount + OnCount < MinMajorityCount)
                {
                    continue;
                }



                // an event point that is right on the boarder of majority threshold
                ThresholdEdgePoints.Add(ep);
                pointChecked.Add(count, false);
                count++;
            }

            // delete all neighbors thats not in majority edge
            foreach (eventPoint ep in ThresholdEdgePoints)
            {
                List <int[]> ValidNeighbor = new List <int[]>();
                for (int i = 0; i < ep.neightbors.Count; i++)
                {
                    eventPoint tp = new eventPoint(ep.neightbors[i]);
                    foreach (eventPoint eep in ThresholdEdgePoints)
                    {
                        if (eep.IsSamePoint(tp))
                        {
                            ValidNeighbor.Add(ep.neightbors[i]);
                        }
                    }
                }
                ep.neightbors.Clear();
                ep.neightbors = ValidNeighbor;
            }

            string debugstr     = JSonUtils.ConvertObjectToJSon(ThresholdEdgePoints);
            string polylistStr  = JSonUtils.ConvertObjectToJSon(polyList);
            string allpointsstr = JSonUtils.ConvertObjectToJSon(allEventPoints);

            List <List <int[]> > outputPolygon = new List <List <int[]> >();

            for (int p = 0; p < ThresholdEdgePoints.Count; p++)
            {
                if (pointChecked[p])
                {
                    continue;
                }

                List <int[]> polygon1 = new List <int[]>();
                List <int[]> polygon2 = new List <int[]>();
                polygon1.Add(new int[] { ThresholdEdgePoints[p].x, ThresholdEdgePoints[p].y });
                polygon1 = addNeighbors(ThresholdEdgePoints[p], ThresholdEdgePoints, polygon1, pointChecked);
                if (!pointChecked[p])
                {
                    polygon2.Add(new int[] { ThresholdEdgePoints[p].x, ThresholdEdgePoints[p].y });
                    polygon2 = addNeighbors(ThresholdEdgePoints[p], ThresholdEdgePoints, polygon2, pointChecked);
                    for (int i = polygon2.Count - 1; i >= 0; i--)
                    {
                        polygon1.Add(polygon2[i]);
                    }
                }
                //loop closed
                outputPolygon.Add(polygon1);


                //debug
                string poly = "";
                foreach (int [] xy in polygon1)
                {
                    poly += "[" + xy[0].ToString() + "," + xy[1].ToString() + "] ";
                }
                Console.WriteLine(poly);
            }


            //// debug
            List <LineSegment> lines = new List <LineSegment>();

            for (int k = 0; k < ThresholdEdgePoints.Count; k++)
            {
                int x1 = ThresholdEdgePoints[k].x;
                int y1 = ThresholdEdgePoints[k].y;

                foreach (int[] xy in ThresholdEdgePoints[k].neightbors)
                {
                    int         x2   = xy[0];
                    int         y2   = xy[1];
                    LineSegment line = new LineSegment(x1, y1, x2, y2);
                    lines.Add(line);
                }
            }
            Color c = ColorSet.getColorByObjectType("cat");

            //Image originalImage = ImageUtilities.getImageFromURI("https://satyamresearchjobstorage.blob.core.windows.net/kittisegmentation/test/4-cats-on-tree-fb-cover.jpg");
            Image originalImage = ImageUtilities.getImageFromURI("https://satyamresearchjobstorage.blob.core.windows.net/kittisegmentation/testpascal/2007_000121.jpg");


            Image im = DrawingBoxesAndLinesOnImages.addLinesToImage(originalImage, lines, c);

            //string filename = count.ToString();
            string filename = "majorityEdge";

            ImageUtilities.saveImage(im, DirectoryConstants.defaultTempDirectory, filename);


            // TB changed to Segments
            return(new GenericPolygon(outputPolygon[0]));
        }
예제 #3
0
 public bool IsSamePoint(eventPoint ep)
 {
     return(x == ep.x && y == ep.y);
 }