Example #1
0
        /// <summary>
        ///A test for At1d
        ///</summary>
        public void At1dTestHelper <T>()
        {
            var target   = new Narray <T>(120);
            int i        = 0;
            T   expected = default(T);

            Assert.AreEqual(expected, target.At1d(i));
            Assert.AreEqual(expected, target.At1d(10));
            Assert.AreEqual(expected, target.At1d(100));
        }
Example #2
0
        /// <summary>
        ///A test for Put1d
        ///</summary>
        public void Put1dTestHelper <T>()
        {
            Narray <T> target = new Narray <T>(15, 10);
            int        i0     = 50;
            T          value  = (T)Convert.ChangeType(99, typeof(T));

            Assert.AreEqual(default(T), target.At1d(i0));
            target.Put1d(i0, value);
            Assert.AreEqual(value, target.At1d(i0));
        }
Example #3
0
 public static int Max(Narray<sbyte> a)
 {
     sbyte value = a.At1d(0);
     for (int i = 1; i < a.Length1d(); i++)
     {
         sbyte nvalue = a.At1d(i);
         if (nvalue <= value) continue;
         value = nvalue;
     }
     return value;
 }
Example #4
0
        /// <summary>
        ///A test for Fill
        ///</summary>
        public void FillTestHelper <T1, T2>()
        {
            var target = new Narray <T1>(5, 6);
            T1  valueT = (T1)Convert.ChangeType(99, typeof(T1));
            T2  valueS = (T2)Convert.ChangeType(99, typeof(T2));

            target.Fill(valueT);
            Assert.AreEqual(valueT, target.At1d(10));
            Assert.AreEqual(valueT, target.At1d(11));
            target.Fill(valueS);
            Assert.AreEqual(valueT, target.At1d(0));
            Assert.AreEqual(valueT, target.At1d(1));
        }
Example #5
0
        /// <summary>
        ///A test for Renew
        ///</summary>
        public void RenewTestHelper <T>()
        {
            Narray <T> target = new Narray <T>(2, 5, 3);
            int        i0     = 10;
            T          value  = (T)Convert.ChangeType(99, typeof(T));

            target.Put1d(i0, value);
            Assert.AreEqual(3, target.Rank());
            Assert.AreEqual(value, target.At1d(i0));
            target.Renew(2, 7);
            Assert.AreEqual(2, target.Rank());
            Assert.AreNotEqual(value, target.At1d(i0));
            Assert.AreEqual(default(T), target.At1d(i0));
        }
Example #6
0
        public static int Max(Narray <sbyte> a)
        {
            sbyte value = a.At1d(0);

            for (int i = 1; i < a.Length1d(); i++)
            {
                sbyte nvalue = a.At1d(i);
                if (nvalue <= value)
                {
                    continue;
                }
                value = nvalue;
            }
            return(value);
        }
Example #7
0
        /// <summary>
        ///A test for Push
        ///</summary>
        public void PushTestHelper <T>()
        {
            int        size   = 12;
            Narray <T> target = new Narray <T>(size);
            T          value  = (T)Convert.ChangeType(99, typeof(T));

            target.Push(value);
            Assert.AreEqual(size + 1, target.Length1d());
            Assert.AreEqual(value, target.At1d(target.Length1d() - 1));
        }
Example #8
0
 public static void replace_values <T>(Narray <T> a, T from, T to)
 {
     for (int i = 0; i < a.Length1d(); i++)
     {
         if (a.At1d(i).Equals(from))
         {
             a.Put1d(i, to);
         }
     }
 }
Example #9
0
        /// <summary>
        /// Return the segmentation-derived mask for the character.
        /// This may optionally be grown by some pixels.
        /// </summary>
        public override void GetMask(out Rect r, ref Bytearray outmask, int index, int grow)
        {
            r = boxes.At1d(index).Grow(grow);
            r.Intersect(new Rect(0, 0, labels.Dim(0), labels.Dim(1)));
            if (fullheight)
            {
                r.y0 = 0;
                r.y1 = labels.Dim(1);
            }
            int      x = r.x0, y = r.y0, w = r.Width(), h = r.Height();
            Intarray segs = segments.At1d(index);

            outmask.Resize(w, h);
            outmask.Fill(0);
            for (int i = 0; i < w; i++)
            {
                for (int j = 0; j < h; j++)
                {
                    int label = labels[x + i, y + j];
                    if (NarrayUtil.first_index_of(segs, label) >= 0)
                    {
                        outmask[i, j] = (byte)255;
                    }
                }
            }
            if (grow > 0)
            {
                Morph.binary_dilate_circle(outmask, grow);
            }
        }
Example #10
0
        /// <summary>
        ///A test for Resize
        ///</summary>
        public void ResizeTestHelper <T>()
        {
            int        len    = 2000;
            Narray <T> target = new Narray <T>(len);
            int        i0     = 10;
            T          value  = (T)Convert.ChangeType(99, typeof(T));

            target.Put1d(i0, value);
            Assert.AreEqual(len, target.Length());
            target.Resize(101, 20);
            Assert.AreNotEqual(len, target.Length());
            // check exist value
            Assert.AreNotEqual(value, target.At1d(i0));
        }
Example #11
0
        /// <summary>
        /// Propagate labels across the entire image from a set of non-zero seeds.
        /// </summary>
        public static void propagate_labels(ref Intarray image)
        {
            Floatarray     dist   = new Floatarray();
            Narray <Point> source = new Narray <Point>();

            dist.Copy(image);
            BrushFire.brushfire_2(ref dist, ref source, 1000000);
            for (int i = 0; i < dist.Length1d(); i++)
            {
                Point p = source.At1d(i);
                if (image.At1d(i) == 0)
                {
                    image.Put1d(i, image[p.X, p.Y]);
                }
            }
        }
Example #12
0
        public static void propagate_labels_to(ref Intarray target, Intarray seed)
        {
            Floatarray     dist   = new Floatarray();
            Narray <Point> source = new Narray <Point>();

            dist.Copy(seed);
            BrushFire.brushfire_2(ref dist, ref source, 1000000);
            for (int i = 0; i < dist.Length1d(); i++)
            {
                Point p = source.At1d(i);
                if (target.At1d(i) > 0)
                {
                    target.Put1d(i, seed[p.X, p.Y]);
                }
            }
        }
Example #13
0
        public static void remove_dontcares(ref Intarray image)
        {
            Floatarray     dist   = new Floatarray();
            Narray <Point> source = new Narray <Point>();

            dist.Resize(image.Dim(0), image.Dim(1));
            for (int i = 0; i < dist.Length1d(); i++)
            {
                if (!dontcare(image.At1d(i)))
                {
                    dist.Put1d(i, (image.At1d(i) > 0 ? 1 : 0));
                }
            }
            BrushFire.brushfire_2(ref dist, ref source, 1000000);
            for (int i = 0; i < dist.Length1d(); i++)
            {
                Point p = source.At1d(i);
                if (dontcare(image.At1d(i)))
                {
                    image.Put1d(i, image[p.X, p.Y]);
                }
            }
        }
Example #14
0
        /// <summary>
        /// Compute the groups for a segmentation (internal method).
        /// </summary>
        private void computeGroups()
        {
            rboxes.Clear();
            ImgLabels.bounding_boxes(ref rboxes, labels);
            int n = rboxes.Length();

            // NB: we start with i=1 because i=0 is the background
            for (int i = 1; i < n; i++)
            {
                for (int range = 1; range <= maxrange; range++)
                {
                    if (i + range > n)
                    {
                        continue;
                    }
                    Rect     box = rboxes.At1d(i);
                    Intarray seg = new Intarray();
                    bool     bad = false;
                    for (int j = i; j < i + range; j++)
                    {
                        if (j > i && rboxes.At1d(j).x0 - rboxes.At1d(j - 1).x1 > maxdist)
                        {
                            bad = true;
                            break;
                        }
                        box.Include(rboxes.At1d(j));
                        seg.Push(j);
                    }
                    if (bad)
                    {
                        continue;
                    }
                    boxes.Push(box);
                    segments.Push(seg);
                }
            }
        }
Example #15
0
        public static void fix_diacritics(Intarray segmentation)
        {
            Narray <Rect> bboxes = new Narray <Rect>();

            ImgLabels.bounding_boxes(ref bboxes, segmentation);
            if (bboxes.Length() < 1)
            {
                return;
            }
            Intarray assignments = new Intarray(bboxes.Length());

            for (int i = 0; i < assignments.Length(); i++)
            {
                assignments[i] = i;
            }
            for (int j = 0; j < bboxes.Length(); j++)
            {
                float dist    = 1e38f;
                int   closest = -1;
                for (int i = 0; i < bboxes.Length(); i++)
                {
                    // j should overlap i in the x direction
                    if (bboxes.At1d(j).x1 < bboxes.At1d(i).x0)
                    {
                        continue;
                    }
                    if (bboxes.At1d(j).x0 > bboxes.At1d(i).x1)
                    {
                        continue;
                    }
                    // j should be above i
                    if (!(bboxes.At1d(j).y0 >= bboxes.At1d(i).y1))
                    {
                        continue;
                    }
#if false
                    // j should be smaller than i
                    if (!(bboxes.At1d(j).area() < bboxes.At1d(i).area()))
                    {
                        continue;
                    }
#endif
                    float d = Math.Abs((bboxes[j].x0 + bboxes[j].x1) / 2 - (bboxes[i].x0 + bboxes[i].x1) / 2);
                    if (d >= dist)
                    {
                        continue;
                    }
                    dist    = d;
                    closest = i;
                }
                if (closest < 0)
                {
                    continue;
                }
                assignments[j] = closest;
            }
            for (int i = 0; i < segmentation.Length(); i++)
            {
                segmentation.Put1d(i, assignments[segmentation.At1d(i)]);
            }
            ImgLabels.renumber_labels(segmentation, 1);
        }
Example #16
0
 /// <summary>
 /// Return the bounding box for a character.
 /// </summary>
 public override Rect BoundingBox(int index)
 {
     return(boxes.At1d(index));
 }
Example #17
0
 public static void remove_dontcares(ref Intarray image)
 {
     Floatarray dist = new Floatarray();
     Narray<Point> source = new Narray<Point>();
     dist.Resize(image.Dim(0), image.Dim(1));
     for (int i = 0; i < dist.Length1d(); i++)
         if (!dontcare(image.At1d(i))) dist.Put1d(i, (image.At1d(i) > 0 ? 1 : 0));
     BrushFire.brushfire_2(ref dist, ref source, 1000000);
     for (int i = 0; i < dist.Length1d(); i++)
     {
         Point p = source.At1d(i);
         if (dontcare(image.At1d(i))) image.Put1d(i, image[p.X, p.Y]);
     }
 }
Example #18
0
 public static void propagate_labels_to(ref Intarray target, Intarray seed)
 {
     Floatarray dist = new Floatarray();
     Narray<Point> source = new Narray<Point>();
     dist.Copy(seed);
     BrushFire.brushfire_2(ref dist, ref source, 1000000);
     for (int i = 0; i < dist.Length1d(); i++)
     {
         Point p = source.At1d(i);
         if (target.At1d(i) > 0) target.Put1d(i, seed[p.X, p.Y]);
     }
 }
Example #19
0
 /// <summary>
 /// Propagate labels across the entire image from a set of non-zero seeds.
 /// </summary>
 public static void propagate_labels(ref Intarray image)
 {
     Floatarray dist = new Floatarray();
     Narray<Point> source = new Narray<Point>();
     dist.Copy(image);
     BrushFire.brushfire_2(ref dist, ref source, 1000000);
     for (int i = 0; i < dist.Length1d(); i++)
     {
         Point p = source.At1d(i);
         if (image.At1d(i) == 0) image.Put1d(i, image[p.X, p.Y]);
     }
 }