Esempio n. 1
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));
     }
 }
Esempio n. 2
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;
 }
Esempio n. 3
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)));
     }
 }
Esempio n. 4
0
 public static void binarize_with_threshold(Bytearray result, Bytearray image, int threshold)
 {
     result.MakeLike(image);
     for (int i = 0; i < image.Length1d(); i++)
     {
         result.Put1d(i, image.At1d(i) < threshold ? (byte)0 : (byte)255);
     }
 }
Esempio n. 5
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);
            }
        }
Esempio n. 6
0
 public static void binary_autoinvert(Bytearray image)
 {
     check_binary(image);
     int count = 0;
     for (int i = 0; i < image.Length1d(); i++)
         if (image.At1d(i) > 0) count++;
     if (count > image.Length1d() / 2)
         binary_invert(image);
 }
Esempio 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)));
            }
        }
Esempio 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;
            }
        }
Esempio 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;
 }
Esempio n. 10
0
 public static void check_binary(Bytearray image)
 {
     for (int i = 0; i < image.Length1d(); i++)
     {
         int value = image.At1d(i);
         if (!(value == 0 || value == 255))
         {
             throw new Exception("check_binary: value must be 0 or 255");
         }
     }
 }
Esempio 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);
        }
Esempio n. 12
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);
        }
Esempio n. 13
0
        public static void binary_autoinvert(Bytearray image)
        {
            check_binary(image);
            int count = 0;

            for (int i = 0; i < image.Length1d(); i++)
            {
                if (image.At1d(i) > 0)
                {
                    count++;
                }
            }
            if (count > image.Length1d() / 2)
            {
                binary_invert(image);
            }
        }
Esempio n. 14
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;
 }
Esempio n. 15
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));
 }
Esempio n. 16
0
 public static void check_binary(Bytearray image)
 {
     for (int i = 0; i < image.Length1d(); i++)
     {
         int value = image.At1d(i);
         if (!(value == 0 || value == 255))
             throw new Exception("check_binary: value must be 0 or 255");
     }
 }
Esempio n. 17
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)));
 }
Esempio n. 18
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);
                }
            }
        }
Esempio n. 19
0
 public static void binarize_with_threshold(Bytearray result, Bytearray image, int threshold)
 {
     result.MakeLike(image);
     for (int i = 0; i < image.Length1d(); i++)
         result.Put1d(i, image.At1d(i) < threshold ? (byte)0 : (byte)255);
 }
Esempio 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)));
     }
 }