Пример #1
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();
                }
            }
        }
Пример #2
0
        public static unsafe void ChangeContrast(Bitmap b, ProgressBar p1, double value)
        {
            MainForm.UpdateLabel("Updating Contrast");
            p1.Maximum = (2 * b.Width) / 10;
            p1.Value   = 0;
            int Pvalue = 0;
            //Calculate Average;
            double     avR = 0, avG = 0, avB = 0;
            Color      c     = new Color();
            BitmapData bData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, b.PixelFormat);

            byte bitsPerPixel = (byte)Image.GetPixelFormatSize(b.PixelFormat);


            byte *scan0 = (byte *)bData.Scan0.ToPointer();

            for (int i = 0; i < b.Width; i++)
            {
                for (int j = 0; j < b.Height; j++)
                {
                    byte *data = scan0 + j * bData.Stride + i * bitsPerPixel / 8;
                    //b.UnlockBits(bData);
                    //c = b.GetPixel(i, j);
                    avR += data[2];
                    avG += data[1];
                    avB += data[0];
                }
                Pvalue++;
                if (Pvalue == 10)
                {
                    Pvalue = 0;
                    p1.Value++;
                }
            }
            ;

            avB /= (b.Width * b.Height);
            avG /= (b.Width * b.Height);
            avR /= (b.Width * b.Height);
            int R, G, B;

            for (int i = 0; i < b.Width; i++)
            {
                for (int j = 0; j < b.Height; j++)
                {
                    byte *data = scan0 + j * bData.Stride + i * bitsPerPixel / 8;


                    R = Convert.ToInt32((data[2] - avR) * value + avR);
                    G = Convert.ToInt32((data[1] - avG) * value + avG);
                    B = Convert.ToInt32((data[0] - avB) * value + avB);
                    NormalizeRGB(ref B);
                    NormalizeRGB(ref R);
                    NormalizeRGB(ref G);

                    data[2] = (byte)R;
                    data[1] = (byte)G;
                    data[0] = (byte)B;
                }
                Pvalue++;
                if (Pvalue == 10)
                {
                    Pvalue = 0;
                    p1.Value++;
                }
            }
            b.UnlockBits(bData);

            return;
        }
Пример #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();
                }
            }
        }
Пример #4
0
        public static double getCurrentImageDisper(byte[,] src, int w, int h, ProgressBar progressBar1)
        {
            MainForm.UpdateLabel("getting Image Static Data");

            double answ = 0;
            int    ct   = 0;

            progressBar1.Minimum = 0;
            progressBar1.Maximum = h;
            byte c1, c2;

            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];

                        answ += diff(c1, c2);
                        ct++;
                    }
                    if (y + 1 < h)
                    {
                        c2    = src[x, y + 1];
                        answ += diff(c1, c2);
                        ct++;
                    }
                }
                progressBar1.Value = y;
            }
            return((int)(answ * 10 / ct));

            /*
             * double mid = answ / ct;
             * answ = 0;
             * ct = 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];
             *
             *          answ += (mid - diff(c1, c2)) * (mid - diff(c1, c2));
             *          ct++;
             *      }
             *      if (y + 1 < h)
             *      {
             *          c2 = src[x, y + 1];
             *          answ += (mid - diff(c1, c2)) * (mid - diff(c1, c2));
             *          ct++;
             *      }
             *  }
             *  progressBar1.Value = y + h;
             * }
             * return Math.Sqrt(answ / ct);
             */
        }