예제 #1
0
 public void Init(int n = 0)
 {
     _keys.Resize(n);
     for (int i = 0; i < n; i++)
     {
         _keys.Put1d(i, i);
     }
     _values.Resize(n);
     _values.Fill <float>(0.0f);
 }
예제 #2
0
        public void BestPath(Intarray v1, Intarray v2, Intarray inputs,
                             Intarray outputs, Floatarray costs)
        {
            stree.Clear();

            beam.Resize(1);
            beamcost.Resize(1);
            beam[0]     = stree.Add(-1, fst1.GetStart(), fst2.GetStart(), 0, 0, 0);
            beamcost[0] = 0;

            best_so_far      = 0;
            best_cost_so_far = fst1.GetAcceptCost(fst1.GetStart()) +
                               fst2.GetAcceptCost(fst1.GetStart());

            while (beam.Length() > 0)
            {
                Radiate();
            }

            stree.Get(v1, v2, inputs, outputs, costs, best_so_far);
            costs.Push(fst1.GetAcceptCost(stree.v1[best_so_far]) +
                       fst2.GetAcceptCost(stree.v2[best_so_far]));

            //logger("costs", costs);
        }
예제 #3
0
        public override void Charseg(ref Intarray result_segmentation, Bytearray orig_image)
        {
            Logger.Default.Image("segmenting", orig_image);

            int PADDING = 3;

            OcrRoutine.optional_check_background_is_lighter(orig_image);
            Bytearray     image  = new Bytearray();
            Narray <byte> bimage = image;

            image.Copy(orig_image);
            OcrRoutine.binarize_simple(image);
            OcrRoutine.Invert(image);
            ImgOps.pad_by(ref bimage, PADDING, PADDING);
            // pass image to segmenter
            segmenter.SetImage(image);
            // find all cuts in the image
            segmenter.FindAllCuts();
            // choose the best of all cuts
            segmenter.FindBestCuts();

            Intarray segmentation = new Intarray();

            segmentation.Resize(image.Dim(0), image.Dim(1));
            for (int i = 0; i < image.Dim(0); i++)
            {
                for (int j = 0; j < image.Dim(1); j++)
                {
                    segmentation[i, j] = image[i, j] > 0 ? 1 : 0;
                }
            }

            for (int r = 0; r < segmenter.bestcuts.Length(); r++)
            {
                int            c   = segmenter.bestcuts[r];
                Narray <Point> cut = segmenter.cuts[c];
                for (int y = 0; y < image.Dim(1); y++)
                {
                    for (int x = cut[y].X; x < image.Dim(0); x++)
                    {
                        if (segmentation[x, y] > 0)
                        {
                            segmentation[x, y]++;
                        }
                    }
                }
            }
            ImgOps.extract_subimage(result_segmentation, segmentation, PADDING, PADDING,
                                    segmentation.Dim(0) - PADDING, segmentation.Dim(1) - PADDING);

            if (small_merge_threshold > 0)
            {
                SegmRoutine.line_segmentation_merge_small_components(ref result_segmentation, small_merge_threshold);
                SegmRoutine.line_segmentation_sort_x(result_segmentation);
            }

            SegmRoutine.make_line_segmentation_white(result_segmentation);
            // set_line_number(segmentation, 1);
            Logger.Default.Image("resulting segmentation", result_segmentation);
        }
예제 #4
0
 /// <summary>
 /// Compute a classmap that maps a set of possibly sparse classes onto a dense
 /// list of new classes and vice versa
 /// </summary>
 public static void ClassMap(Intarray out_class_to_index, Intarray out_index_to_class, Intarray classes)
 {
     int nclasses = NarrayUtil.Max(classes) + 1;
     Intarray hist = new Intarray(nclasses);
     hist.Fill(0);
     for (int i = 0; i < classes.Length(); i++)
     {
         if (classes[i] == -1) continue;
         hist[classes[i]]++;
     }
     int count = 0;
     for (int i = 0; i < hist.Length(); i++)
         if (hist[i] > 0) count++;
     out_class_to_index.Resize(nclasses);
     out_class_to_index.Fill(-1);
     out_index_to_class.Resize(count);
     out_index_to_class.Fill(-1);
     int index = 0;
     for (int i = 0; i < hist.Length(); i++)
     {
         if (hist[i] > 0)
         {
             out_class_to_index[i] = index;
             out_index_to_class[index] = i;
             index++;
         }
     }
     CHECK_ARG(out_class_to_index.Length() == nclasses, "class_to_index.Length() == nclasses");
     CHECK_ARG(out_index_to_class.Length() == NarrayUtil.Max(out_class_to_index) + 1,
         "index_to_class.Length() == Max(class_to_index)+1");
     CHECK_ARG(out_index_to_class.Length() <= out_class_to_index.Length(),
         "index_to_class.Length() <= class_to_index.Length()");
 }
예제 #5
0
파일: ImgIo.cs 프로젝트: liaoheping/OCRonet
        public static Bitmap read_image_packed(Intarray image, string path)
        {
            Bitmap bitmap = LoadBitmapFromFile(path);

            image.Resize(bitmap.Width, bitmap.Height);
            ImgRoutine.NarrayFromBitmap(image, bitmap);
            return(bitmap);
        }
예제 #6
0
 /// <summary>
 /// constructor for a NBest data structure of size n
 /// </summary>
 public PriorityQueue(int n)
 {
     this.n = n;
     ids = new Intarray();
     tags = new Intarray();
     values = new Floatarray();
     ids.Resize(n + 1);
     tags.Resize(n + 1);
     values.Resize(n + 1);
     Clear();
 }
예제 #7
0
 /// <summary>
 /// constructor for a NBest data structure of size n
 /// </summary>
 public PriorityQueue(int n)
 {
     this.n = n;
     ids    = new Intarray();
     tags   = new Intarray();
     values = new Floatarray();
     ids.Resize(n + 1);
     tags.Resize(n + 1);
     values.Resize(n + 1);
     Clear();
 }
예제 #8
0
        public override void Charseg(ref Intarray result_segmentation, Bytearray orig_image)
        {
            Logger.Default.Image("segmenting", orig_image);

            int PADDING = 3;
            OcrRoutine.optional_check_background_is_lighter(orig_image);
            Bytearray image = new Bytearray();
            Narray<byte> bimage = image;
            image.Copy(orig_image);
            OcrRoutine.binarize_simple(image);
            OcrRoutine.Invert(image);
            ImgOps.pad_by(ref bimage, PADDING, PADDING);
            // pass image to segmenter
            segmenter.SetImage(image);
            // find all cuts in the image
            segmenter.FindAllCuts();
            // choose the best of all cuts
            segmenter.FindBestCuts();

            Intarray segmentation = new Intarray();
            segmentation.Resize(image.Dim(0), image.Dim(1));
            for (int i = 0; i < image.Dim(0); i++)
                for (int j = 0; j < image.Dim(1); j++)
                    segmentation[i, j] = image[i, j] > 0 ? 1 : 0;

            for (int r = 0; r < segmenter.bestcuts.Length(); r++)
            {
                int c = segmenter.bestcuts[r];
                Narray<Point> cut = segmenter.cuts[c];
                for (int y = 0; y < image.Dim(1); y++)
                {
                    for (int x = cut[y].X; x < image.Dim(0); x++)
                    {
                        if (segmentation[x, y] > 0) segmentation[x, y]++;
                    }
                }
            }
            ImgOps.extract_subimage(result_segmentation, segmentation, PADDING, PADDING,
                             segmentation.Dim(0) - PADDING, segmentation.Dim(1) - PADDING);

            if (small_merge_threshold > 0)
            {
                SegmRoutine.line_segmentation_merge_small_components(ref result_segmentation, small_merge_threshold);
                SegmRoutine.line_segmentation_sort_x(result_segmentation);
            }

            SegmRoutine.make_line_segmentation_white(result_segmentation);
            // set_line_number(segmentation, 1);
            Logger.Default.Image("resulting segmentation", result_segmentation);
        }
예제 #9
0
 /// <summary>
 /// Translate classes using a translation map
 /// </summary>
 private void ctranslate(Intarray result, Intarray values, Intarray translation)
 {
     result.Resize(values.Length());
     for (int i = 0; i < values.Length(); i++)
     {
         int v = values[i];
         if (v < 0)
         {
             result[i] = v;
         }
         else
         {
             result[i] = translation[v];
         }
     }
 }
예제 #10
0
        public static void line_segmentation_sort_x(Intarray segmentation)
        {
            if (NarrayUtil.Max(segmentation) > 100000)
            {
                throw new Exception("line_segmentation_merge_small_components: to many segments");
            }
            Narray <Rect> bboxes = new Narray <Rect>();

            ImgLabels.bounding_boxes(ref bboxes, segmentation);
            Floatarray x0s = new Floatarray();

            unchecked
            {
                x0s.Push((float)-999999);
            }
            for (int i = 1; i < bboxes.Length(); i++)
            {
                if (bboxes[i].Empty())
                {
                    x0s.Push(999999);
                }
                else
                {
                    x0s.Push(bboxes[i].x0);
                }
            }
            // dprint(x0s,1000); printf("\n");
            Narray <int> permutation  = new Intarray();
            Narray <int> rpermutation = new Intarray();

            NarrayUtil.Quicksort(permutation, x0s);
            rpermutation.Resize(permutation.Length());
            for (int i = 0; i < permutation.Length(); i++)
            {
                rpermutation[permutation[i]] = i;
            }
            // dprint(rpermutation,1000); printf("\n");
            for (int i = 0; i < segmentation.Length1d(); i++)
            {
                if (segmentation.At1d(i) == 0)
                {
                    continue;
                }
                segmentation.Put1d(i, rpermutation[segmentation.At1d(i)]);
            }
        }
예제 #11
0
        /// <summary>
        /// Compute a classmap that maps a set of possibly sparse classes onto a dense
        /// list of new classes and vice versa
        /// </summary>
        public static void ClassMap(Intarray out_class_to_index, Intarray out_index_to_class, Intarray classes)
        {
            int      nclasses = NarrayUtil.Max(classes) + 1;
            Intarray hist     = new Intarray(nclasses);

            hist.Fill(0);
            for (int i = 0; i < classes.Length(); i++)
            {
                if (classes[i] == -1)
                {
                    continue;
                }
                hist[classes[i]]++;
            }
            int count = 0;

            for (int i = 0; i < hist.Length(); i++)
            {
                if (hist[i] > 0)
                {
                    count++;
                }
            }
            out_class_to_index.Resize(nclasses);
            out_class_to_index.Fill(-1);
            out_index_to_class.Resize(count);
            out_index_to_class.Fill(-1);
            int index = 0;

            for (int i = 0; i < hist.Length(); i++)
            {
                if (hist[i] > 0)
                {
                    out_class_to_index[i]     = index;
                    out_index_to_class[index] = i;
                    index++;
                }
            }
            CHECK_ARG(out_class_to_index.Length() == nclasses, "class_to_index.Length() == nclasses");
            CHECK_ARG(out_index_to_class.Length() == NarrayUtil.Max(out_class_to_index) + 1,
                      "index_to_class.Length() == Max(class_to_index)+1");
            CHECK_ARG(out_index_to_class.Length() <= out_class_to_index.Length(),
                      "index_to_class.Length() <= class_to_index.Length()");
        }
예제 #12
0
        public void reconstruct_edges(Intarray inputs,
                                      Intarray outputs,
                                      Floatarray costs,
                                      Intarray vertices)
        {
            int n = vertices.Length();

            inputs.Resize(n);
            outputs.Resize(n);
            costs.Resize(n);
            for (int i = 0; i < n - 1; i++)
            {
                int        source      = vertices[i];
                int        target      = vertices[i + 1];
                Intarray   out_ins     = new Intarray();
                Intarray   out_targets = new Intarray();
                Intarray   out_outs    = new Intarray();
                Floatarray out_costs   = new Floatarray();
                fst.Arcs(out_ins, out_targets, out_outs, out_costs, source);

                costs[i] = 1e38f;

                // find the best arc
                for (int j = 0; j < out_targets.Length(); j++)
                {
                    if (out_targets[j] != target)
                    {
                        continue;
                    }
                    if (out_costs[j] < costs[i])
                    {
                        inputs[i]  = out_ins[j];
                        outputs[i] = out_outs[j];
                        costs[i]   = out_costs[j];
                    }
                }
            }
            inputs[n - 1]  = 0;
            outputs[n - 1] = 0;
            costs[n - 1]   = fst.GetAcceptCost(vertices[n - 1]);
        }
예제 #13
0
        public override void SetImage(Bytearray image)
        {
            dimage.Copy(image);
            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 - 1, j] == 0 && image[i, j] > 0)
                    {
                        wimage[i, j] = boundary_weight;
                    }
                    else if (image[i, j] > 0)
                    {
                        wimage[i, j] = inside_weight;
                    }
                    else
                    {
                        wimage[i, j] = outside_weight;
                    }
                }
            }
            where = (int)(sy / s1);
            for (int i = 0; i < dimage.Dim(0); i++)
            {
                dimage[i, where] = 0x008000;
            }
        }
예제 #14
0
        public void reconstruct_edges(Intarray inputs,
                               Intarray outputs,
                               Floatarray costs,
                               Intarray vertices)
        {
            int n = vertices.Length();
            inputs.Resize(n);
            outputs.Resize(n);
            costs.Resize(n);
            for (int i = 0; i < n - 1; i++)
            {
                int source = vertices[i];
                int target = vertices[i + 1];
                Intarray out_ins = new Intarray();
                Intarray out_targets = new Intarray();
                Intarray out_outs = new Intarray();
                Floatarray out_costs = new Floatarray();
                fst.Arcs(out_ins, out_targets, out_outs, out_costs, source);

                costs[i] = 1e38f;

                // find the best arc
                for (int j = 0; j < out_targets.Length(); j++)
                {
                    if (out_targets[j] != target) continue;
                    if (out_costs[j] < costs[i])
                    {
                        inputs[i] = out_ins[j];
                        outputs[i] = out_outs[j];
                        costs[i] = out_costs[j];
                    }
                }
            }
            inputs[n - 1] = 0;
            outputs[n - 1] = 0;
            costs[n - 1] = fst.GetAcceptCost(vertices[n - 1]);
        }
예제 #15
0
        public override void FindAllCuts()
        {
            int w = wimage.Dim(0), h = wimage.Dim(1);

            // initialize dimensions of cuts, costs etc
            cuts.Resize(w);
            cutcosts.Resize(w);
            costs.Resize(w, h);
            sources.Resize(w, h);

            costs.Fill(1000000000);
            for (int i = 0; i < w; i++)
            {
                costs[i, 0] = 0;
            }
            sources.Fill(-1);
            limit     = where;
            direction = 1;
            Step(0, w, 0);

            for (int x = 0; x < w; x++)
            {
                cutcosts[x] = costs[x, where];
                cuts[x]     = new Narray <Point>();
                cuts[x].Clear();
                // bottom should probably be initialized with 2*where instead of
                // h, because where cannot be assumed to be h/2. In the most extreme
                // case, the cut could go through 2 pixels in each row
                Narray <Point> bottom = new Narray <Point>();
                int            i = x, j = where;
                while (j >= 0)
                {
                    bottom.Push(new Point(i, j));
                    i = sources[i, j];
                    j--;
                }
                //cuts(x).resize(h);
                for (i = bottom.Length() - 1; i >= 0; i--)
                {
                    cuts[x].Push(bottom[i]);
                }
            }

            costs.Fill(1000000000);
            for (int i = 0; i < w; i++)
            {
                costs[i, h - 1] = 0;
            }
            sources.Fill(-1);
            limit     = where;
            direction = -1;
            Step(0, w, h - 1);

            for (int x = 0; x < w; x++)
            {
                cutcosts[x] += costs[x, where];
                // top should probably be initialized with 2*(h-where) instead of
                // h, because where cannot be assumed to be h/2. In the most extreme
                // case, the cut could go through 2 pixels in each row
                Narray <Point> top = new Narray <Point>();
                int            i = x, j = where;
                while (j < h)
                {
                    if (j > where)
                    {
                        top.Push(new Point(i, j));
                    }
                    i = sources[i, j];
                    j++;
                }
                for (i = 0; i < top.Length(); i++)
                {
                    cuts[x].Push(top[i]);
                }
            }

            // add costs for line "where"
            for (int x = 0; x < w; x++)
            {
                cutcosts[x] += wimage[x, where];
            }
        }
예제 #16
0
파일: ImgIo.cs 프로젝트: nickun/OCRonet
 public static Bitmap read_image_packed(Intarray image, string path)
 {
     Bitmap bitmap = LoadBitmapFromFile(path);
     image.Resize(bitmap.Width, bitmap.Height);
     ImgRoutine.NarrayFromBitmap(image, bitmap);
     return bitmap;
 }
예제 #17
0
 /// <summary>
 /// Translate classes using a translation map
 /// </summary>
 private void ctranslate(Intarray result, Intarray values, Intarray translation)
 {
     result.Resize(values.Length());
     for (int i = 0; i < values.Length(); i++)
     {
         int v = values[i];
         if (v < 0) result[i] = v;
         else result[i] = translation[v];
     }
 }