Exemplo n.º 1
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
            });
        }
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());
        }
 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);
         }
     }
 }
        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));
        }
        public Bitmap DoSegmentation(double[,,] arrayImage, double sigma, double k, int minSize, IColorSheme colorSheme)
        {
            m_height = arrayImage.GetLength(1);
            m_width  = arrayImage.GetLength(2);

            //debug
            System.Diagnostics.Debug.WriteLine("Reading done: " + DateTime.Now);

            m_colorSheme = colorSheme;

            //препроцессинг иображения
            arrayImage = colorSheme.Convert(arrayImage);

            //сохранение для проведения оценки качества сегментации
            m_arrayImageCopy = (double[, , ])arrayImage.Clone();

            //debug
            System.Diagnostics.Debug.WriteLine("color sheme changed: " + DateTime.Now);
            //DebugImageInfo(arrayImage);

            //smoothing
            GaussianBlur gaussianBlur = new GaussianBlur();

            double[][] filter = gaussianBlur.getKernel(sigma);
            double[,,] blurredImage = DoubleArrayImageOperations.ConvolutionFilter(arrayImage, filter);

            //debug
            System.Diagnostics.Debug.WriteLine("Smooting done: " + DateTime.Now);
            //тест размещения преобразования цвета
            //arrayImage = colorSheme.Convert(arrayImage);

            //построение графа
            Edge[] edges = buildGraphByImage(blurredImage)
                           .OrderBy(el => el.w)
                           .ToArray();

            //debug
            System.Diagnostics.Debug.WriteLine("graph builded: " + DateTime.Now);

            //debugging

            System.Diagnostics.Debug.WriteLine("edges total: " + edges.Length);

            //double minWeight = edges.Min(el => el.w);
            //double maxWeight = edges.Max(el => el.w);
            //Edge[] EdgesMoreThanMin = edges.Where(el => el.w > minWeight + 0.1).ToArray();
            //Edge[] EdgesZeroWidth = edges.Where(el => el.w < 0.01).ToArray();
            //
            //Edge[] edgesHor = edges.Where(el => el.neightbourType == NeightbourType.Horizontal).ToArray();
            //Edge[] edgesVer = edges.Where(el => el.neightbourType == NeightbourType.Vertical).ToArray();
            //Edge[] edgesTopDiag = edges.Where(el => el.neightbourType == NeightbourType.TopDiagonal).ToArray();
            //Edge[] edgesBottom = edges.Where(el => el.neightbourType == NeightbourType.BottomDiagonal).ToArray();

            //сегментированный лес непересекающихся деревьев
            DisjointSet segmentedSet = SegmentOnDisjointSet(k, m_height * m_width, edges);  //картинка тут только для передачи размера потому осталась arrayImage

            //запоминание в поле для проведения оценки
            m_segmentedSet = segmentedSet;

            //debug
            System.Diagnostics.Debug.WriteLine("Segmented: " + DateTime.Now);

            //присоеденить те, что меньше min_size к соседу по ребру
            PostProcessSmallComponents(edges, segmentedSet, minSize);

            //debug
            System.Diagnostics.Debug.WriteLine("Small Component Merged: " + DateTime.Now);

            return(SegmentedSetConverter.ConvertToBitmap(segmentedSet, m_height, m_width, out m_componentLength));
            //var a = SegmentedSetConverter.ConvertToRealCoordsSegments(segmentedSet, height, width);
            //return SegmentedSetConverter.RealCoordsSegmentResultToBitmap(a);
        }