Ejemplo n.º 1
0
        public static void Thin(ref Bytearray uci)
        {
            int w = uci.Dim(0) - 1;
            int h = uci.Dim(1) - 1;

            for (int i = 0, n = uci.Length1d(); i < n; i++)
            {
                if (uci.At1d(i) > 0)
                    uci.Put1d(i, ON);
                else
                    uci.Put1d(i, OFF);
            }

            bool flag;
            do
            {
                flag = false;
                for (int j = 0; j < 8; j += 2)
                {
                    for (int x = 1; x < w; x++)
                        for (int y = 1; y < h; y++)
                        {
                            if (uci[x, y] != ON)
                                continue;
                            if (uci[x + nx[j], y + ny[j]] != OFF)
                                continue;
                            int b = 0;
                            for (int i = 7; i >= 0; i--)
                            {
                                b <<= 1;
                                b |= (uci[x + nx[i], y + ny[i]] != OFF ? 1 : 0);
                            }
                            if (ttable[b] > 0)
                                uci[x, y] = SKEL;
                            else
                            {
                                uci[x, y] = DEL;
                                flag = true;
                            }
                        }
                    if (!flag)
                        continue;
                    for (int x = 1; x < w; x++)
                        for (int y = 1; y < h; y++)
                            if (uci[x, y] == DEL)
                                uci[x, y] = OFF;
                }
            } while (flag);

            for (int i = 0, n = uci.Length1d(); i < n; i++)
            {
                if (uci.At1d(i) == SKEL)
                    uci.Put1d(i, 255);
                else
                    uci.Put1d(i, 0);
            }
        }
Ejemplo n.º 2
0
 public static void binarize_by_range(Bytearray outa, Floatarray ina, float fraction)
 {
     float imin = NarrayUtil.Min(ina);
     float imax = NarrayUtil.Max(ina);
     float thresh = (int)(imin + (imax - imin) * fraction);
     outa.MakeLike(ina);
     for (int i = 0; i < ina.Length1d(); i++)
     {
         if (ina.At1d(i) > thresh) outa.Put1d(i, 255);
         else outa.Put1d(i, 0);
     }
 }
Ejemplo n.º 3
0
 public static void make_binary(Bytearray image)
 {
     for (int i = 0; i < image.Length1d(); i++)
     {
         image.Put1d(i, (byte)(image.At1d(i) > 0 ? 255 : 0));
     }
 }
Ejemplo n.º 4
0
 public static int binarize_simple(Bytearray result, Bytearray image)
 {
     int threshold = (NarrayUtil.Max(image)/* + NarrayUtil.Min(image)*/) / 2;
     result.MakeLike(image);
     for (int i = 0; i < image.Length1d(); i++)
         result.Put1d(i, image.At1d(i) < threshold ? (byte)0 : (byte)255);
     return threshold;
 }
Ejemplo n.º 5
0
 public static void binary_invert(Bytearray image)
 {
     check_binary(image);
     for (int i = 0; i < image.Length1d(); i++)
     {
         image.Put1d(i, (byte)(255 - image.At1d(i)));
     }
 }
Ejemplo n.º 6
0
 public static void binarize_with_threshold(Bytearray result, Floatarray image, float threshold)
 {
     result.MakeLike(image);
     for (int i = 0; i < image.Length1d(); i++)
     {
         result.Put1d(i, image.At1d(i) < threshold ? (byte)0 : (byte)255);
     }
 }
Ejemplo n.º 7
0
        public static void Invert(Bytearray a)
        {
            int n = a.Length1d();

            for (int i = 0; i < n; i++)
            {
                a.Put1d(i, (byte)(255 - a.At1d(i)));
            }
        }
Ejemplo n.º 8
0
        public void SetImage(Bytearray image_)
        {
            Bytearray image = new Bytearray();

            //image = image_;
            image.Copy(image_);
            dimage.Copy(image);
            if (PGeti("fill_holes") > 0)
            {
                Bytearray holes = new Bytearray();
                SegmRoutine.extract_holes(ref holes, image);
                for (int i = 0; i < image.Length(); i++)
                {
                    if (holes.At1d(i) > 0)
                    {
                        image.Put1d(i, 255);
                    }
                }
            }
            int w = image.Dim(0), h = image.Dim(1);

            wimage.Resize(w, h);
            wimage.Fill(0);
            float s1 = 0.0f, sy = 0.0f;

            for (int i = 1; i < w; i++)
            {
                for (int j = 0; j < h; j++)
                {
                    if (image[i, j] > 0)
                    {
                        s1++; sy += j;
                    }
                    if (image[i, j] > 0)
                    {
                        wimage[i, j] = inside_weight;
                    }
                    else
                    {
                        wimage[i, j] = outside_weight;
                    }
                }
            }
            if (s1 == 0)
            {
                where = image.Dim(1) / 2;
            }
            else
            {
                where = (int)(sy / s1);
            }
            for (int i = 0; i < dimage.Dim(0); i++)
            {
                dimage[i, where] = 0x008000;
            }
        }
Ejemplo n.º 9
0
 public static Bitmap read_image_binary(Bytearray image, string path)
 {
     Bitmap bitmap = LoadBitmapFromFile(path);
     image.Resize(bitmap.Width, bitmap.Height);
     ImgRoutine.NarrayFromBitmap(image, bitmap);
     double threshold = (NarrayUtil.Min(image) + NarrayUtil.Max(image)) / 2.0;
     for (int i = 0; i < image.Length1d(); i++)
         image.Put1d(i, (byte)((image.At1d(i) < threshold) ? 0 : 255));
     return bitmap;
 }
Ejemplo n.º 10
0
        public static void binarize_by_range(Bytearray outa, Floatarray ina, float fraction)
        {
            float imin   = NarrayUtil.Min(ina);
            float imax   = NarrayUtil.Max(ina);
            float thresh = (int)(imin + (imax - imin) * fraction);

            outa.MakeLike(ina);
            for (int i = 0; i < ina.Length1d(); i++)
            {
                if (ina.At1d(i) > thresh)
                {
                    outa.Put1d(i, 255);
                }
                else
                {
                    outa.Put1d(i, 0);
                }
            }
        }
Ejemplo n.º 11
0
        public static int binarize_simple(Bytearray result, Bytearray image)
        {
            int threshold = (NarrayUtil.Max(image) /* + NarrayUtil.Min(image)*/) / 2;

            result.MakeLike(image);
            for (int i = 0; i < image.Length1d(); i++)
            {
                result.Put1d(i, image.At1d(i) < threshold ? (byte)0 : (byte)255);
            }
            return(threshold);
        }
Ejemplo n.º 12
0
 public static void segmentation_as_bitmap(Bytearray image, Intarray cseg)
 {
     image.MakeLike(cseg);
     for (int i = 0; i < image.Length1d(); i++)
     {
         int value = cseg.At1d(i);
         if (value == 0 || value == 0xffffff)
         {
             image.Put1d(i, 255);
         }
         //if (value == 0xffffff) image.Put1d(i, 255);
     }
 }
Ejemplo n.º 13
0
        public static Bitmap read_image_binary(Bytearray image, string path)
        {
            Bitmap bitmap = LoadBitmapFromFile(path);

            image.Resize(bitmap.Width, bitmap.Height);
            ImgRoutine.NarrayFromBitmap(image, bitmap);
            double threshold = (NarrayUtil.Min(image) + NarrayUtil.Max(image)) / 2.0;

            for (int i = 0; i < image.Length1d(); i++)
            {
                image.Put1d(i, (byte)((image.At1d(i) < threshold) ? 0 : 255));
            }
            return(bitmap);
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Remove segments from start to end.
        /// </summary>
        /// <param name="cseg">Output</param>
        /// <param name="rseg">Input</param>
        /// <param name="start">start remove position</param>
        /// <param name="end">end remove position</param>
        public static void rseg_to_cseg_remove(Intarray cseg, Intarray rseg,
                                               Bytearray outimg, Bytearray img, int start, int end)
        {
            int maxSegNum = NarrayUtil.Max(rseg);

            if (start > end)
            {
                throw new Exception("segmentation encoded in IDs looks seriously broken!");
            }
            if (start > maxSegNum || end > maxSegNum)
            {
                throw new Exception("segmentation encoded in IDs doesn't fit!");
            }
            if (rseg.Length1d() != img.Length1d())
            {
                throw new Exception("rseg and img must have same a dimension!");
            }
            Intarray map = new Intarray(maxSegNum + 1);

            map.Fill(0);

            int color = 1;

            for (int i = 1; i <= maxSegNum; i++)
            {
                map[i] = color;
                if (i < start || i > end)
                {
                    color++;
                }
                else
                {
                    map[i] = 0;
                }
            }
            cseg.MakeLike(rseg);
            outimg.Copy(img);
            for (int i = 0; i < cseg.Length1d(); i++)
            {
                int val = rseg.At1d(i);
                cseg.Put1d(i, map[val]);
                if (val > 0 && map[val] == 0)
                {
                    outimg.Put1d(i, 255);
                }
            }
        }
Ejemplo n.º 15
0
 public void SetImage(Bytearray image_)
 {
     Bytearray image = new Bytearray();
     //image = image_;
     image.Copy(image_);
     dimage.Copy(image);
     if (PGeti("fill_holes") > 0)
     {
         Bytearray holes = new Bytearray();
         SegmRoutine.extract_holes(ref holes, image);
         for (int i = 0; i < image.Length(); i++)
             if (holes.At1d(i) > 0) image.Put1d(i, 255);
     }
     int w = image.Dim(0), h = image.Dim(1);
     wimage.Resize(w, h);
     wimage.Fill(0);
     float s1 = 0.0f, sy = 0.0f;
     for (int i = 1; i < w; i++)
         for (int j = 0; j < h; j++)
         {
             if (image[i, j] > 0) { s1++; sy += j; }
             if (image[i, j] > 0) wimage[i, j] = inside_weight;
             else wimage[i, j] = outside_weight;
         }
     if(s1==0) where = image.Dim(1)/2;
     else where = (int)(sy / s1);
     for (int i = 0; i < dimage.Dim(0); i++) dimage[i, where] = 0x008000;
 }
Ejemplo n.º 16
0
 public static void binarize_with_threshold(Bytearray result, Floatarray image, float threshold)
 {
     result.MakeLike(image);
     for (int i = 0; i < image.Length1d(); i++)
         result.Put1d(i, image.At1d(i) < threshold ? (byte)0 : (byte)255);
 }
Ejemplo n.º 17
0
 public static void make_binary(Bytearray image)
 {
     for (int i = 0; i < image.Length1d(); i++)
         image.Put1d(i, (byte)(image.At1d(i) > 0 ? 255 : 0));
 }
Ejemplo n.º 18
0
 public static void binary_invert(Bytearray image)
 {
     check_binary(image);
     for (int i = 0; i < image.Length1d(); i++)
         image.Put1d(i, (byte)(255 - image.At1d(i)));
 }
Ejemplo n.º 19
0
        public static void Thin(ref Bytearray uci)
        {
            int w = uci.Dim(0) - 1;
            int h = uci.Dim(1) - 1;

            for (int i = 0, n = uci.Length1d(); i < n; i++)
            {
                if (uci.At1d(i) > 0)
                {
                    uci.Put1d(i, ON);
                }
                else
                {
                    uci.Put1d(i, OFF);
                }
            }

            bool flag;

            do
            {
                flag = false;
                for (int j = 0; j < 8; j += 2)
                {
                    for (int x = 1; x < w; x++)
                    {
                        for (int y = 1; y < h; y++)
                        {
                            if (uci[x, y] != ON)
                            {
                                continue;
                            }
                            if (uci[x + nx[j], y + ny[j]] != OFF)
                            {
                                continue;
                            }
                            int b = 0;
                            for (int i = 7; i >= 0; i--)
                            {
                                b <<= 1;
                                b  |= (uci[x + nx[i], y + ny[i]] != OFF ? 1 : 0);
                            }
                            if (ttable[b] > 0)
                            {
                                uci[x, y] = SKEL;
                            }
                            else
                            {
                                uci[x, y] = DEL;
                                flag      = true;
                            }
                        }
                    }
                    if (!flag)
                    {
                        continue;
                    }
                    for (int x = 1; x < w; x++)
                    {
                        for (int y = 1; y < h; y++)
                        {
                            if (uci[x, y] == DEL)
                            {
                                uci[x, y] = OFF;
                            }
                        }
                    }
                }
            } while (flag);

            for (int i = 0, n = uci.Length1d(); i < n; i++)
            {
                if (uci.At1d(i) == SKEL)
                {
                    uci.Put1d(i, 255);
                }
                else
                {
                    uci.Put1d(i, 0);
                }
            }
        }
Ejemplo n.º 20
0
 public static void Invert(Bytearray a)
 {
     int n = a.Length1d();
     for (int i = 0; i < n; i++)
     {
         a.Put1d(i, (byte)(255 - a.At1d(i)));
     }
 }