예제 #1
0
        //private static void SegmentVert(int[] Disset, edge[] edges, int EdgeCount, int VertCount, double c, int[] Size, ref int NumC)
        private static void SegmentVert(int[] Disset, ImprovedEdge[] edges, int EdgeCount, int VertCount, double c, int[] Size, ref int NumC, int w)
        {
            NumC = VertCount;
            QSort(edges, 0, EdgeCount - 1);
            //Array.Sort(edges, new Cmysort());

            int[] UnionSize = new int[VertCount];
            Djs.init(Disset, VertCount);
            double[] CritFunc = new double[VertCount];
            for (int i = 0; i < VertCount; i++)
            {
                CritFunc[i] = c;
                Size[i]     = 1;
            }
            for (int i = 0; i < EdgeCount; i++)
            {
                int start = (edges[i].a) >> 1;
                int fin   = 0;
                if ((edges[i].a) % 2 == 0)
                {
                    fin = start + w;
                }
                else
                {
                    fin = start + 1;
                }
                int SetA = Djs.findset(start, Disset);
                int SetB = Djs.findset(fin, Disset);
                if (SetA != SetB)
                {
                    if (edges[i].w < CritFunc[SetA] && edges[i].w < CritFunc[SetB])
                    {
                        NumC--;
                        Djs.union1(SetA, SetB, Disset, Size);
                        SetA           = Djs.findset(SetA, Disset);
                        CritFunc[SetA] = edges[i].w + c / Size[SetA];
                    }
                }
            }
        }
예제 #2
0
        public static void SegmentImageRGB(MyFloatColor[,] src, double c, int minSeg, int w, int h, ProgressBar p1)
        {
            edges = new ImprovedEdge[2 * w * h + 6];
            int          ct = 0;
            MyFloatColor c1, c2;

            MainForm.UpdateLabel("Felzen.. Segmentation");
            //building graph
            p1.Value = 0;
            int pvalue = 0;

            p1.Maximum = 2 * h / 10;

            /*
             *
             * right-1
             * down-0
             *
             */
            for (int y = 0; y < h; y++)
            {
                for (int x = 0; x < w; x++)
                {
                    c1 = src[x, y];
                    if (x + 1 < w)
                    {
                        c2 = src[x + 1, y];

                        //edges[ct].a = y * w + x;
                        edges[ct].a = ((y * w + x) << 1) + 1;
                        //edges[ct].b = y * w + x + 1;//right

                        edges[ct].w = diff(c1, c2);
                        ct++;
                    }
                    if (y + 1 < h)
                    {
                        c2 = src[x, y + 1];

                        edges[ct].a = (y * w + x) << 1;
                        //edges[ct].b = (y + 1) * w + x;//down
                        edges[ct].w = diff(c1, c2);
                        ct++;
                    }
                }
                pvalue++;
                if (pvalue == 10)
                {
                    p1.Value++;
                    pvalue = 0;
                    Application.DoEvents();
                }
            }
            int[] Disset = new int[w * h];
            int[] Size   = new int[w * h];
            int   NumC   = 0;

            SegmentVert(Disset, edges, ct, w * h, c, Size, ref NumC, w);
            GC.Collect();
            for (int i = 0; i < ct; i++)
            {
                int start = (edges[i].a) >> 1;
                int fin   = 0;
                if ((edges[i].a) % 2 == 0)
                {
                    fin = start + w;
                }
                else
                {
                    fin = start + 1;
                }

                int a = Djs.findset(start, Disset);
                int b = Djs.findset(fin, Disset);
                if ((a != b) && ((Size[a] < minSeg) || (Size[b] < minSeg)))
                {
                    Djs.union1(a, b, Disset, Size);
                    NumC--;
                }
            }

            byte[]  R = new byte[w * h];
            byte[]  G = new byte[w * h];
            byte [] B = new byte[w * h];
            Random  r = new Random();

            r.NextBytes(R);
            r.NextBytes(G);
            r.NextBytes(B);
            //DrawPictureBox
            edges = null;

            int normIndex;

            for (int y = 0; y < h; y++)
            {
                for (int x = 0; x < w; x++)
                {
                    normIndex = y * w + x;
                    int set = Djs.findset(normIndex, Disset);
                    src[x, y].G = ((float)1.0 * G[set]) / 255;;
                    src[x, y].R = ((float)(1.0 * R[set])) / 255;;
                    src[x, y].B = ((float)(1.0 * B[set])) / 255;;
                }
                pvalue++;
                if (pvalue == 10)
                {
                    pvalue = 0;
                    p1.Value++;
                    Application.DoEvents();
                }
            }
        }
예제 #3
0
        public static void SegmentImageRGBgs(float[,] src, double c, int minSeg, int w, int h, ProgressBar p1)
        {
            edges = new ImprovedEdge[2 * w * h + 6];
            int   ct = 0;
            float c1, c2;

            MainForm.UpdateLabel("Felzen.. Segmentation");
            //building graph
            p1.Value = 0;
            int pvalue = 0;

            p1.Maximum = 2 * h / 10;

            for (int y = 0; y < h; y++)
            {
                for (int x = 0; x < w; x++)
                {
                    c1 = src[x, y];
                    if (x + 1 < w)
                    {
                        c2 = src[x + 1, y];

                        //edges[ct].a = y * w + x;
                        //  edges[ct].b = y * w + x + 1;
                        edges[ct].a = ((y * w + x) << 1) + 1;
                        edges[ct].w = diff(c1, c2);
                        ct++;
                    }
                    if (y + 1 < h)
                    {
                        c2 = src[x, y + 1];

                        // edges[ct].a = y * w + x;
                        //edges[ct].b = (y + 1) * w + x;
                        edges[ct].a = ((y * w + x) << 1) + 1;
                        edges[ct].w = diff(c1, c2);
                        ct++;
                    }
                }
                pvalue++;
                if (pvalue == 10)
                {
                    p1.Value++;
                    pvalue = 0;
                    Application.DoEvents();
                }
            }
            int[] Disset = new int[w * h];
            int[] Size   = new int[w * h];
            int   NumC   = 0;

            SegmentVert(Disset, edges, ct, w * h, c, Size, ref NumC, w);
            GC.Collect();
            for (int i = 0; i < ct; i++)
            {
                int start = (edges[i].a) >> 1;
                int fin   = 0;
                if ((edges[i].a) % 2 == 0)
                {
                    fin = start + w;
                }
                else
                {
                    fin = start + 1;
                }
                int a = Djs.findset(start, Disset);
                int b = Djs.findset(fin, Disset);
                if ((a != b) && ((Size[a] < minSeg) || (Size[b] < minSeg)))
                {
                    Djs.union1(a, b, Disset, Size);
                    NumC--;
                }
            }
            Color[] Colors = new Color[w * h];
            for (int i = 0; i < w * h; i++)
            {
                Colors[i] = GetRandomColor();
            }
            //DrawPictureBox
            edges = null;

            int normIndex;

            for (int y = 0; y < h; y++)
            {
                for (int x = 0; x < w; x++)
                {
                    normIndex = y * w + x;
                    src[x, y] = (float)1.0 * (Colors[Djs.findset(normIndex, Disset)].G) / 255;;
                    src[x, y] = (float)1.0 * (Colors[Djs.findset(normIndex, Disset)].R) / 255;;
                    src[x, y] = (float)1.0 * (Colors[Djs.findset(normIndex, Disset)].B) / 255;;
                }
                pvalue++;
                if (pvalue == 10)
                {
                    pvalue = 0;
                    p1.Value++;
                    Application.DoEvents();
                }
            }
        }