private void PostProcessSmallComponents(Edge[] edges, DisjointSet segmentedSet, int minSize)
 {
     for (int i = 0; i < edges.Length; i++)
     {
         int a = segmentedSet.Find(edges[i].A);
         int b = segmentedSet.Find(edges[i].B);
         if ((a != b) && ((segmentedSet.Size(a) < minSize) || (segmentedSet.Size(b) < minSize)))
         {
             segmentedSet.Join(a, b);
         }
     }
 }
Exemplo n.º 2
0
        public static AssesmentsSegment[] ConvertToAssessmentSegments(DisjointSet segmentedSet, int height, int width, double[,,] arrayImage, IColorSheme colorSheme)
        {
            Dictionary <int, List <double[]> > segments = new Dictionary <int, List <double[]> >();

            for (int h = 0; h < height; h++)
            {
                for (int w = 0; w < width; w++)
                {
                    int             comp = segmentedSet.Find(h * width + w);
                    List <double[]> currentSegmentPointsList;

                    if (!segments.TryGetValue(comp, out currentSegmentPointsList))
                    {
                        currentSegmentPointsList = new List <double[]>();
                        segments.Add(comp, currentSegmentPointsList);
                    }

                    currentSegmentPointsList.Add(new double[] { arrayImage[0, h, w], arrayImage[1, h, w], arrayImage[2, h, w] });
                }
            }

            return(segments.Values.ToList()
                   .Select(segmentPoints => new AssesmentsSegment(segmentPoints))
                   .ToArray());
        }
Exemplo n.º 3
0
        public static RealCoordsSegmentResult ConvertToRealCoordsSegments(DisjointSet segmentedSet, int height, int width)
        {
            Dictionary <int, List <Point> > segmentsDictionary = new Dictionary <int, List <Point> >();

            for (int h = 0; h < height; h++)
            {
                for (int w = 0; w < width; w++)
                {
                    int          comp = segmentedSet.Find(h * width + w);
                    List <Point> currentSegmentPointsList;

                    if (!segmentsDictionary.TryGetValue(comp, out currentSegmentPointsList))
                    {
                        currentSegmentPointsList = new List <Point>();
                        segmentsDictionary.Add(comp, currentSegmentPointsList);
                    }

                    currentSegmentPointsList.Add(new Point(w, h));
                }
            }

            RealCoordsSegment[] segments = segmentsDictionary.Values.ToList()
                                           .Select(segmentPoints => new RealCoordsSegment(segmentPoints.ToArray()))
                                           .ToArray();

            return(new RealCoordsSegmentResult()
            {
                imageheight = height,
                imageWidth = width,
                realCoordsSegments = segments
            });
        }
        private DisjointSet SegmentOnDisjointSet(double k, int vertices, Edge[] edges)
        {
            DisjointSet disjointSet = new DisjointSet(vertices);

            //начальные значения устанавливаются в k, поскольку по формулам должно быть k/claster_size
            //claster size начальное равно 1
            double[] threshold = Enumerable.Range(0, vertices)
                                 .Select(el => k)
                                 .ToArray();


            // for each edge, in non-decreasing weight order...
            for (int i = 0; i < edges.Length; i++)
            {
                if (i % 100000 == 0)
                {
                    System.Diagnostics.Debug.WriteLine("itaration: " + i);
                }

                Edge edge = edges[i];

                // components conected by this edge
                int a = disjointSet.Find(edge.A);
                int b = disjointSet.Find(edge.B);
                if (a != b)
                {
                    if ((edge.w <= threshold[a]) && (edge.w <= threshold[b]))
                    {
                        disjointSet.Join(a, b);

                        a            = disjointSet.Find(a);
                        threshold[a] = edge.w + k / disjointSet.Size(a);
                    }
                }
            }

            return(disjointSet);
        }
Exemplo n.º 5
0
        public static Bitmap ConvertToBitmap(DisjointSet segmentedSet, int height, int width, out int segmentsCount)
        {
            double[,,] im = new double[3, height, width];

            Dictionary <int, ColorCustom> colors = new Dictionary <int, ColorCustom>();
            int totalSize = 0;

            for (int h = 0; h < height; h++)
            {
                for (int w = 0; w < width; w++)
                {
                    ColorCustom ccc;

                    int comp = segmentedSet.Find(h * width + w);

                    if (colors.TryGetValue(comp, out ccc) == false)
                    {
                        ccc = ColorCustom.GetRandomColor();
                        colors.Add(comp, ccc);

                        int compSize = segmentedSet.Size(comp);
                        totalSize += compSize;
                        System.Diagnostics.Debug.WriteLine("Component: " + comp + " | size: " + compSize);
                    }


                    im[0, h, w] = BitmapConverter.Limit(ccc.r);
                    im[1, h, w] = BitmapConverter.Limit(ccc.g);
                    im[2, h, w] = BitmapConverter.Limit(ccc.b);
                }
            }

            System.Diagnostics.Debug.WriteLine("Total Size: " + totalSize);
            System.Diagnostics.Debug.WriteLine("Height*Width: " + height * width);

            segmentsCount = colors.Count;
            return(BitmapConverter.DoubleRgbToBitmap(im));
        }