Ejemplo n.º 1
0
        private void ProcessSegmentationMethod(object sender, RoutedEventArgs e)
        {
            string       strSermenterName = (sender as Control).Tag.ToString();
            ISegmentLine segmenter        = ComponentCreator.MakeComponent <ISegmentLine>(strSermenterName);

            if (segmenter == null || currBookLine == null)
            {
                return;
            }

            // приведем к чернобелому
            Bytearray image = currBookLine.ImageBytearray;

            //IBinarize binarizer = new BinarizeByOtsu();
            //binarizer.Binarize(image, image);
            OcrRoutine.binarize_simple(image, image);
            // сегментация
            Intarray charseg = new Intarray();

            segmenter.Charseg(ref charseg, image);
            // фон равен 0
            SegmRoutine.make_line_segmentation_black(charseg);
            // удалим маленькие сегменты
            SegmRoutine.remove_small_components(charseg, 3, 3);
            ImgLabels.renumber_labels(charseg, 1);

            currBookLine.CharsegIntarray = charseg;
            CurrSegmentsCount            = NarrayUtil.Max(charseg);
            // Show segmented image
            ShowCharsegImage(charseg,
                             (currBookLine.HaveTranscript && CurrSegmentsCount == currBookLine.Transcript.Length) ?
                             currBookLine.Transcript : "");
            // to enable save button
            EnableCharsegCmdButtons();
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
        public void SetLine(Bytearray image)
        {
            CHECK_ARG(image.Dim(1) < PGeti("maxheight"), "image.Dim(1) < PGeti(\"maxheight\")");

            // run the segmenter

            /*Narray<Rect> bboxes = new Narray<Rect>();
             * Intarray iar = new Intarray();
             * iar.Copy(image);
             * ImgLabels.bounding_boxes(ref bboxes, iar);*/
            //Console.WriteLine("IMG SETLINE: imin:{0} imax:{1}", NarrayUtil.ArgMin(iar), NarrayUtil.ArgMax(iar));
            //Console.WriteLine("INDEX_BLACK:{0} {1} {2} {3}", bboxes[0].x0, bboxes[0].y0, bboxes[0].x1, bboxes[0].y1);
            //ImgIo.write_image_gray("image.png", image);
            OcrRoutine.binarize_simple(binarized, image);
            segmenter.Object.Charseg(ref segmentation, binarized);

            /*Intarray segm = new Intarray();
             * segm.Copy(segmentation);
             * ImgLabels.simple_recolor(segm);
             * ImgIo.write_image_packed("segm_image.png", segm);*/

            //NarrayUtil.Sub(255, binarized);

            SegmRoutine.make_line_segmentation_black(segmentation);
            SegmRoutine.remove_small_components(segmentation, 3, 3);       // i add this line
            ImgLabels.renumber_labels(segmentation, 1);

            // set up the grouper
            grouper.Object.SetSegmentation(segmentation);
        }
Ejemplo n.º 4
0
        public override void Charseg(ref Intarray outimage, Bytearray inimage)
        {
            int       swidth  = PGeti("swidth");
            int       sheight = PGeti("sheight");
            Bytearray image   = new Bytearray();

            image.Copy(inimage);
            OcrRoutine.binarize_simple(image);
            OcrRoutine.Invert(image);
            outimage.Copy(image);
            if (swidth > 0 || sheight > 0)
            {
                Morph.binary_close_rect(image, swidth, sheight);
            }
            Intarray labels = new Intarray();

            labels.Copy(image);
            ImgLabels.label_components(ref labels);
            for (int i = 0; i < outimage.Length1d(); i++)
            {
                if (outimage.At1d(i) > 0)
                {
                    outimage.Put1d(i, SegmRoutine.cseg_pixel(labels.At1d(i)));
                }
            }
            SegmRoutine.make_line_segmentation_white(outimage);
            SegmRoutine.check_line_segmentation(outimage);
        }
Ejemplo n.º 5
0
        public override void FindBestCuts()
        {
            unchecked
            {
                for (int i = 0; i < cutcosts.Length(); i++)
                {
                    NarrayUtil.ExtPut(dimage, i, (int)(cutcosts[i] + 10), 0xff0000);
                }
                for (int i = 0; i < cutcosts.Length(); i++)
                {
                    NarrayUtil.ExtPut(dimage, i, (int)(min_thresh + 10), 0x800000);
                }
            }
            Floatarray temp = new Floatarray();

            Gauss.Gauss1d(temp, cutcosts, 3.0f);
            cutcosts.Move(temp);
            SegmRoutine.local_minima(ref bestcuts, cutcosts, min_range, min_thresh);
            for (int i = 0; i < bestcuts.Length(); i++)
            {
                Narray <Point> cut = cuts[bestcuts[i]];
                for (int j = 0; j < cut.Length(); j++)
                {
                    Point p = cut[j];
                    NarrayUtil.ExtPut(dimage, p.X, p.Y, 0x00ff00);
                }
            }
            ///-if(debug.Length > 0) write_image_packed(debug, dimage);
            // dshow1d(cutcosts,"Y");
            //dshow(dimage,"Y");
        }
Ejemplo n.º 6
0
        public void TestSimple()
        {
            Global.SetEnv("debug", Global.GetEnv("debug") + "");
            // image file name to recognize
            string imgFileName           = "line.png";
            string imgCsegFileName       = "line.cseg.png";
            string imgTranscriptFileName = "line.txt";

            // line recognizer
            Linerec lrec = (Linerec)Linerec.LoadLinerec("default.model");

            //Linerec lrec = (Linerec)Linerec.LoadLinerec("2m2-reject.cmodel");
            //Linerec lrec = (Linerec)Linerec.LoadLinerec("multi3.cmodel");
            //Linerec lrec = (Linerec)Linerec.LoadLinerec("latin-ascii.model");
            lrec.Info();

            // language model
            OcroFST default_lmodel = OcroFST.MakeOcroFst();

            default_lmodel.Load("default.fst");
            OcroFST lmodel = default_lmodel;

            // read image
            Bytearray image = new Bytearray(1, 1);

            ImgIo.read_image_gray(image, imgFileName);

            // recognize!
            OcroFST  fst  = OcroFST.MakeOcroFst();
            Intarray rseg = new Intarray();

            lrec.RecognizeLine(rseg, fst, image);

            // show result 1
            string resStr;

            fst.BestPath(out resStr);
            Console.WriteLine("Fst BestPath:   {0}", resStr);

            // find result 2
            Intarray   v1   = new Intarray();
            Intarray   v2   = new Intarray();
            Intarray   inp  = new Intarray();
            Intarray   outp = new Intarray();
            Floatarray c    = new Floatarray();

            BeamSearch.beam_search(v1, v2, inp, outp, c, fst, lmodel, 100);

            FstUtil.remove_epsilons(out resStr, outp);
            Console.WriteLine("Fst BeamSearch: {0}", resStr);

            Intarray cseg = new Intarray();

            SegmRoutine.rseg_to_cseg(cseg, rseg, inp);
            SegmRoutine.make_line_segmentation_white(cseg);
            ImgLabels.simple_recolor(cseg); // for human readable
            ImgIo.write_image_packed(imgCsegFileName, cseg);
            File.WriteAllText(imgTranscriptFileName, resStr.Replace(" ", ""));
        }
Ejemplo n.º 7
0
        public override void Charseg(ref Intarray outimage, Bytearray inarray)
        {
            Bytearray image = new Bytearray();

            image.Copy(inarray);
            OcrRoutine.binarize_simple(image);
            OcrRoutine.Invert(image);
            outimage.Copy(image);
            Intarray labels = new Intarray();

            labels.Copy(image);
            ImgLabels.label_components(ref labels);
            Narray <Rect> boxes = new Narray <Rect>();

            ImgLabels.bounding_boxes(ref boxes, labels);
            Intarray equiv = new Intarray(boxes.Length());

            for (int i = 0; i < boxes.Length(); i++)
            {
                equiv[i] = i;
            }
            for (int i = 1; i < boxes.Length(); i++)
            {
                Rect p = boxes[i];
                for (int j = 1; j < boxes.Length(); j++)
                {
                    if (i == j)
                    {
                        continue;
                    }
                    Rect q  = boxes[j];
                    int  x0 = Math.Max(p.x0, q.x0);
                    int  x1 = Math.Min(p.x1, q.x1);
                    int  iw = x1 - x0;
                    if (iw <= 0)
                    {
                        continue;         // no overlap
                    }
                    int   ow   = Math.Min(p.Width(), q.Width());
                    float frac = iw / (float)(ow);
                    if (frac < 0.5f)
                    {
                        continue;             // insufficient overlap
                    }
                    // printf("%d %d : %d %d : %g\n",i,j,iw,ow,frac);
                    equiv.Put1d(Math.Max(i, j), Math.Min(i, j));
                }
            }
            for (int i = 0; i < labels.Length(); i++)
            {
                labels.Put1d(i, equiv.At1d(labels.At1d(i)));
            }
            ImgLabels.renumber_labels(labels, 1);
            outimage.Move(labels);
            SegmRoutine.make_line_segmentation_white(outimage);
            SegmRoutine.check_line_segmentation(outimage);
        }
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 void GetCharSegmentation(Intarray image, int page, int line, string variant = null)
        {
            string v = "cseg";

            if (!String.IsNullOrEmpty(variant))
            {
                v += "."; v += variant;
            }
            GetLine(image, page, line, v);
            SegmRoutine.make_line_segmentation_black(image);
        }
Ejemplo n.º 10
0
        public void GetPageSegmentation(Intarray image, int page, string variant = null)
        {
            string v = "pseg";

            if (!String.IsNullOrEmpty(variant))
            {
                v += "."; v += variant;
            }
            GetPage(image, page, v);
            //SegmRoutine.check_page_segmentation(image);   // dublicate!
            SegmRoutine.make_page_segmentation_black(image);
        }
Ejemplo n.º 11
0
        public void PutCharSegmentation(Intarray image, int page, int line, string variant = null)
        {
            string v = "cseg";

            if (!String.IsNullOrEmpty(variant))
            {
                v += "."; v += variant;
            }
            Intarray simage = new Intarray();

            simage.Copy(image);
            SegmRoutine.make_line_segmentation_white(simage);
            PutLine(simage, page, line, v);
        }
Ejemplo n.º 12
0
 /// <summary>
 /// Set a character segmentation.
 /// </summary>
 public override void SetCSegmentation(Intarray segmentation)
 {
     maxrange = 1;
     maxdist  = 2;
     labels.Copy(segmentation);
     SegmRoutine.make_line_segmentation_black(labels);
     GrouperRoutine.check_approximately_sorted(labels);
     boxes.Dealloc();
     segments.Dealloc();
     class_outputs.Dealloc();
     class_costs.Dealloc();
     spaces.Dealloc();
     computeGroups();
 }
Ejemplo n.º 13
0
        public override void Charseg(ref Intarray segmentation, Bytearray inraw)
        {
            Logger.Default.Image("segmenting", inraw);

            OcrRoutine.optional_check_background_is_lighter(inraw);
            Bytearray image = new Bytearray();

            image.Copy(inraw);
            OcrRoutine.binarize_simple(image);
            OcrRoutine.Invert(image);

            segmenter.SetImage(image);
            segmenter.FindAllCuts();
            segmenter.FindBestCuts();

            Intarray seg = new Intarray();

            seg.Copy(image);

            for (int r = 0; r < segmenter.bestcuts.Length(); r++)
            {
                int            w   = seg.Dim(0);
                int            c   = segmenter.bestcuts[r];
                Narray <Point> cut = segmenter.cuts[c];
                for (int y = 0; y < image.Dim(1); y++)
                {
                    for (int i = -1; i <= 1; i++)
                    {
                        int x = cut[y].X;
                        if (x < 1 || x >= w - 1)
                        {
                            continue;
                        }
                        seg[x + i, y] = 0;
                    }
                }
            }
            ImgLabels.label_components(ref seg);
            // dshowr(seg,"YY"); dwait();
            segmentation.Copy(image);
            ImgLabels.propagate_labels_to(ref segmentation, seg);

            SegmRoutine.line_segmentation_merge_small_components(ref segmentation, small_merge_threshold);
            SegmRoutine.line_segmentation_sort_x(segmentation);

            SegmRoutine.make_line_segmentation_white(segmentation);
            // set_line_number(segmentation, 1);
            Logger.Default.Image("resulting segmentation", segmentation);
        }
Ejemplo n.º 14
0
        public void PutPageSegmentation(Intarray image, int page, string variant = null)
        {
            string v = "pseg";

            if (!String.IsNullOrEmpty(variant))
            {
                v += "."; v += variant;
            }
            Intarray simage = new Intarray();

            simage.Copy(image);
            SegmRoutine.check_page_segmentation(simage);
            SegmRoutine.make_page_segmentation_white(simage);
            PutPage(simage, page, v);
        }
Ejemplo n.º 15
0
        private void ProcessMergeSegments(object sender, RoutedEventArgs e)
        {
            int start = numUpDnStart.Value ?? 1;
            int end   = numUpDnEnd.Value ?? 1;

            start = Math.Min(start, CurrSegmentsCount);
            end   = Math.Min(end, CurrSegmentsCount);

            SegmRoutine.rseg_to_cseg(currBookLine.CharsegIntarray, currBookLine.CharsegIntarray, start, end);

            // пересчитаем число сегментов
            CurrSegmentsCount = NarrayUtil.Max(currBookLine.CharsegIntarray);
            // Show segmented image
            ShowCharsegImage(currBookLine.CharsegIntarray,
                             (currBookLine.HaveTranscript && CurrSegmentsCount == currBookLine.Transcript.Length) ?
                             currBookLine.Transcript : "");
        }
Ejemplo n.º 16
0
        public override void Charseg(ref Intarray result_segmentation, Bytearray orig_image)
        {
            Bytearray image = new Bytearray();

            image.Copy(orig_image);
            OcrRoutine.optional_check_background_is_lighter(image);
            OcrRoutine.binarize_simple(image);
            OcrRoutine.Invert(image);

            Intarray ccseg = new Intarray();

            ccseg.Copy(image);
            ImgLabels.label_components(ref ccseg);

            base.Charseg(ref result_segmentation, orig_image);
            SegmRoutine.combine_segmentations(ref result_segmentation, ccseg);
        }
Ejemplo n.º 17
0
        public static Bitmap ConvertCharsegToBitmapRecolor(Intarray charseg, string trans = "")
        {
            Intarray cseg = new Intarray();

            cseg.Copy(charseg);
            Narray <Rect> bboxes = new Narray <Rect>();

            //SegmRoutine.make_line_segmentation_black(cseg);
            ImgLabels.bounding_boxes(ref bboxes, cseg);
            SegmRoutine.make_line_segmentation_white(cseg);
            ImgLabels.simple_recolor(cseg);
            return(DrawSegmentTranscript(
                       DrawSegmentNumbers(
                           ImgRoutine.NarrayToRgbBitmap(cseg),
                           bboxes),
                       bboxes,
                       trans));
        }
Ejemplo n.º 18
0
        public override void Charseg(ref Intarray segmentation, Bytearray image)
        {
            Bytearray timage = new Bytearray();

            timage.Copy(image);
            //for (int i = 0; i < image.Length(); i++) image[i] = (byte)(image[i] > 0 ? 0 : 1);
            OcrRoutine.binarize_simple(timage);
            OcrRoutine.Invert(image);
            Skeleton.Thin(ref timage);
            //ImgIo.write_image_gray("_thinned.png", timage);
            ImgMisc.remove_singular_points(ref timage, 2);
            //ImgIo.write_image_gray("_segmented.png", timage);
            Intarray tsegmentation = new Intarray();

            tsegmentation.Copy(timage);
            ImgLabels.label_components(ref tsegmentation);
            SegmRoutine.remove_small_components(tsegmentation, 4, 4);
            //ImgIo.write_image_packed("_labeled.png", tsegmentation);
            segmentation.Copy(image);
            ImgLabels.propagate_labels_to(ref segmentation, tsegmentation);
            //ImgIo.write_image_packed("_propagated.png", segmentation);
        }
Ejemplo n.º 19
0
        private void ProcessDeleteSegment(object sender, RoutedEventArgs e)
        {
            int start = numUpDnStart.Value ?? 1;

            start = Math.Min(start, CurrSegmentsCount);

            Bytearray outgray = new Bytearray();

            SegmRoutine.rseg_to_cseg_remove(
                currBookLine.CharsegIntarray, currBookLine.CharsegIntarray,
                outgray, currBookLine.ImageBytearray, start, start);

            // пересчитаем число сегментов
            CurrSegmentsCount = NarrayUtil.Max(currBookLine.CharsegIntarray);
            // Show segmented image
            ShowCharsegImage(currBookLine.CharsegIntarray,
                             (currBookLine.HaveTranscript && CurrSegmentsCount == currBookLine.Transcript.Length) ?
                             currBookLine.Transcript : "");

            currBookLine.ImageBytearray = outgray;
            currBookLine.Image          = ImgRoutine.NarrayToRgbBitmap(outgray);
            isCurrDeletedSegment        = true;
        }
Ejemplo n.º 20
0
        public void FindBestCuts()
        {
            /*Intarray segm = new Intarray();
             * segm.Copy(dimage);
             * ImgLabels.simple_recolor(segm);
             * ImgIo.write_image_packed("debug1.png", segm);*/
            unchecked
            {
                for (int i = 0; i < cutcosts.Length(); i++)
                {
                    NarrayUtil.ExtPut(dimage, i, (int)(cutcosts[i] + 10), 0xff0000);
                }
                for (int i = 0; i < cutcosts.Length(); i++)
                {
                    NarrayUtil.ExtPut(dimage, i, (int)(min_thresh + 10), 0x800000);
                }
            }
            Floatarray temp = new Floatarray();

            Gauss.Gauss1d(temp, cutcosts, cost_smooth);
            cutcosts.Move(temp);
            SegmRoutine.local_minima(ref bestcuts, cutcosts, min_range, min_thresh);
            for (int i = 0; i < bestcuts.Length(); i++)
            {
                Narray <Point> cut = cuts[bestcuts[i]];
                for (int j = 0; j < cut.Length(); j++)
                {
                    Point p = cut[j];
                    NarrayUtil.ExtPut(dimage, p.X, p.Y, 0x00ff00);
                }
            }

            /*segm.Copy(dimage);
             * ImgLabels.simple_recolor(segm);
             * ImgIo.write_image_packed("debug2.png", segm);*/
        }
Ejemplo n.º 21
0
        public override void Extract(Narray <Floatarray> outarrays, Floatarray inarray)
        {
            outarrays.Clear();
            Floatarray input = new Floatarray();

            input.Copy(inarray);
            int        w = input.Dim(0), h = input.Dim(1);
            Floatarray a     = new Floatarray();        // working array
            int        csize = PGeti("csize");

            // get rid of small components
            SegmRoutine.erase_small_components(input, PGetf("minsize"), PGetf("threshold"));

            // compute a thresholded version for morphological operations
            Bytearray thresholded = new Bytearray();

            OcrRoutine.threshold_frac(thresholded, input, PGetf("threshold"));

            // compute a smoothed version of the input for gradient computations
            float      sigma    = PGetf("gradsigma");
            Floatarray smoothed = new Floatarray();

            smoothed.Copy(input);
            Gauss.Gauss2d(smoothed, sigma, sigma);

            // x gradient
            a.Resize(w, h);
            for (int j = 0; j < h; j++)
            {
                for (int i = 0; i < w; i++)
                {
                    float delta;
                    if (i == 0)
                    {
                        delta = 0f;
                    }
                    else
                    {
                        delta = smoothed[i, j] - smoothed[i - 1, j];
                    }
                    a[i, j] = delta;
                }
            }
            Floatarray xgrad = outarrays.Push(new Floatarray());

            OcrRoutine.scale_to(xgrad, a, csize, PGetf("noupscale"), PGetf("aa"));
            for (int j = 0; j < csize; j++)
            {
                for (int i = 0; i < csize; i++)
                {
                    if (j % 2 == 0)
                    {
                        xgrad[i, j] = Math.Max(xgrad[i, j], 0f);
                    }
                    else
                    {
                        xgrad[i, j] = Math.Min(xgrad[i, j], 0f);
                    }
                }
            }

            // y gradient
            a.Resize(w, h);
            for (int i = 0; i < w; i++)
            {
                for (int j = 0; j < h; j++)
                {
                    float delta;
                    if (j == 0)
                    {
                        delta = 0f;
                    }
                    else
                    {
                        delta = smoothed[i, j] - smoothed[i, j - 1];
                    }
                    a[i, j] = delta;
                }
            }
            Floatarray ygrad = outarrays.Push(new Floatarray());

            OcrRoutine.scale_to(ygrad, a, csize, PGetf("noupscale"), PGetf("aa"));
            for (int i = 0; i < csize; i++)
            {
                for (int j = 0; j < csize; j++)
                {
                    if (i % 2 == 0)
                    {
                        ygrad[i, j] = Math.Max(ygrad[i, j], 0f);
                    }
                    else
                    {
                        ygrad[i, j] = Math.Min(ygrad[i, j], 0f);
                    }
                }
            }

            // junctions, endpoints, and holes
            Floatarray junctions  = new Floatarray();
            Floatarray endpoints  = new Floatarray();
            Floatarray holes      = new Floatarray();
            Bytearray  junctions1 = new Bytearray();
            Bytearray  endpoints1 = new Bytearray();
            Bytearray  holes1     = new Bytearray();
            Bytearray  dilated    = new Bytearray();
            Bytearray  binary     = new Bytearray();

            junctions.MakeLike(input, 0f);
            endpoints.MakeLike(input, 0f);
            holes.MakeLike(input, 0f);
            int   n    = PGeti("n");
            float step = PGetf("step");
            int   bs   = PGeti("binsmooth");

            for (int i = 0; i < n; i++)
            {
                sigma = step * i;
                if (bs > 0)
                {
                    OcrRoutine.binsmooth(binary, input, sigma);
                }
                else
                {
                    binary.Copy(thresholded);
                    Morph.binary_dilate_circle(binary, (int)(sigma));
                }
                OcrRoutine.skeletal_features(endpoints1, junctions1, binary, 0.0f, 0.0f);
                NarrayUtil.Greater(junctions1, (byte)0, (byte)0, (byte)1);
                junctions.Copy(junctions1);
                NarrayUtil.Greater(endpoints1, (byte)0, (byte)0, (byte)1);
                endpoints.Copy(endpoints1);
                SegmRoutine.extract_holes(ref holes1, binary);
                NarrayUtil.Greater(holes1, (byte)0, (byte)0, (byte)1);
                holes.Copy(holes1);
            }
            junctions *= 1.0f / (float)n;
            endpoints *= 1.0f / (float)n;
            holes     *= 1.0f / (float)n;

            OcrRoutine.scale_to(outarrays.Push(new Floatarray()), junctions, csize, PGetf("noupscale"), PGetf("aa"));
            OcrRoutine.scale_to(outarrays.Push(new Floatarray()), endpoints, csize, PGetf("noupscale"), PGetf("aa"));
            OcrRoutine.scale_to(outarrays.Push(new Floatarray()), holes, csize, PGetf("noupscale"), PGetf("aa"));
        }
Ejemplo n.º 22
0
        public override void Charseg(ref Intarray segmentation, Bytearray inraw)
        {
            setParams();
            //Logger.Default.Image("segmenting", inraw);

            int PADDING = 3;

            OcrRoutine.optional_check_background_is_lighter(inraw);
            Bytearray image = new Bytearray();

            image.Copy(inraw);
            OcrRoutine.binarize_simple(image);
            OcrRoutine.Invert(image);

            SetImage(image);
            FindAllCuts();
            FindBestCuts();

            Intarray seg = new Intarray();

            seg.MakeLike(image);
            seg.Fill(255);

            for (int r = 0; r < bestcuts.Length(); r++)
            {
                int            w   = seg.Dim(0);
                int            c   = bestcuts[r];
                Narray <Point> cut = cuts[c];
                for (int y = 0; y < image.Dim(1); y++)
                {
                    for (int i = -1; i <= 1; i++)
                    {
                        int x = cut[y].X;
                        if (x < 1 || x >= w - 1)
                        {
                            continue;
                        }
                        seg[x + i, y] = 0;
                    }
                }
            }
            ImgLabels.label_components(ref seg);
            // dshowr(seg,"YY"); dwait();
            segmentation.Copy(image);

            for (int i = 0; i < seg.Length1d(); i++)
            {
                if (segmentation.At1d(i) == 0)
                {
                    seg.Put1d(i, 0);
                }
            }

            ImgLabels.propagate_labels_to(ref segmentation, seg);

            if (PGeti("component_segmentation") > 0)
            {
                Intarray ccseg = new Intarray();
                ccseg.Copy(image);
                ImgLabels.label_components(ref ccseg);
                SegmRoutine.combine_segmentations(ref segmentation, ccseg);
                if (PGeti("fix_diacritics") > 0)
                {
                    SegmRoutine.fix_diacritics(segmentation);
                }
            }
#if false
            SegmRoutine.line_segmentation_merge_small_components(ref segmentation, small_merge_threshold);
            SegmRoutine.line_segmentation_sort_x(segmentation);
#endif

            SegmRoutine.make_line_segmentation_white(segmentation);
            // set_line_number(segmentation, 1);
            //Logger.Default.Image("resulting segmentation", segmentation);
        }
Ejemplo n.º 23
0
        public void TestTrainLenetCseg()
        {
            string bookPath    = "data\\0000\\";
            string netFileName = "latin-lenet.model";

            Linerec.GDef("linerec", "use_reject", 1);
            Linerec.GDef("lenet", "junk", 1);
            Linerec.GDef("lenet", "epochs", 4);

            // create Linerec
            Linerec linerec;

            if (File.Exists(netFileName))
            {
                linerec = Linerec.LoadLinerec(netFileName);
            }
            else
            {
                linerec = new Linerec("lenet");
                LenetClassifier classifier = linerec.GetClassifier() as LenetClassifier;
                if (classifier != null)
                {
                    classifier.InitNumSymbLatinAlphabet();
                }
            }

            // temporary disable junk
            //linerec.DisableJunk = true;

            linerec.StartTraining();
            int        nepochs = 10;
            LineSource lines   = new LineSource();

            lines.Init(new string[] { "data2" });

            //linerec.GetClassifier().Set("epochs", 1);

            for (int epoch = 1; epoch <= nepochs; epoch++)
            {
                linerec.Epoch(epoch);

                // load cseg samples
                while (!lines.Done())
                {
                    lines.MoveNext();
                    Intarray cseg = new Intarray();
                    //Bytearray image = new Bytearray();
                    string transcript = lines.GetTranscript();
                    //lines.GetImage(image);
                    if (!lines.GetCharSegmentation(cseg) && cseg.Length() == 0)
                    {
                        Global.Debugf("warn", "skipping book {0} page {1} line {2} (no or bad cseg)",
                                      lines.CurrentBook, lines.CurrentPage, lines.Current);
                        continue;
                    }
                    SegmRoutine.make_line_segmentation_black(cseg);
                    linerec.AddTrainingLine(cseg, transcript);
                }

                lines.Reset();
                lines.Shuffle();
                // do Train and clear Dataset
                linerec.FinishTraining();
                // do save
                if (epoch % 1 == 0)
                {
                    linerec.Save(netFileName);
                }

                // recognize test line
                bool bakDisJunk = linerec.DisableJunk;
                linerec.DisableJunk = false;
                DoTestLinerecRecognize(linerec, "data2\\", "test1.png");
                linerec.DisableJunk = bakDisJunk;
            }

            // finnaly save
            linerec.Save(netFileName);
        }
Ejemplo n.º 24
0
        /// <summary>
        /// Create char segmentation (cseg) files if missing
        /// </summary>
        /// <param name="bookPath">path to bookstore</param>
        /// <param name="modelFilename">Linerec model file</param>
        /// <param name="langModel">language model file</param>
        /// <param name="suffix">e.g., 'gt'</param>
        public void ComputeMissingCsegForBookStore(string bookPath, string model = "default.model",
                                                   string suffix = "", bool saveRseg = false, string langModel = "default.fst")
        {
            // create line recognizer
            Linerec linerec = Linerec.LoadLinerec(model);

            // create IBookStore
            IBookStore bookstore = new SmartBookStore();

            bookstore.SetPrefix(bookPath);
            bookstore.Info();

            // language model
            OcroFST lmodel = OcroFST.MakeOcroFst();

            lmodel.Load(langModel);

            // iterate lines of pages
            for (int page = 0; page < bookstore.NumberOfPages(); page++)
            {
                int nlines = bookstore.LinesOnPage(page);
                Console.WriteLine("Page {0} has {1} lines", page, nlines);
                for (int j = 0; j < nlines; j++)
                {
                    int line = bookstore.GetLineId(page, j);

                    Bytearray image = new Bytearray();
                    bookstore.GetLine(image, page, line);
                    Intarray cseg = new Intarray();
                    bookstore.GetCharSegmentation(cseg, page, line, suffix);
                    // check missing cseg file
                    if (cseg.Length() <= 0 && image.Length() > 0)
                    {
                        // recognize line
                        OcroFST  fst  = OcroFST.MakeOcroFst();
                        Intarray rseg = new Intarray();
                        linerec.RecognizeLine(rseg, fst, image);
                        // find best results
                        string     resText;
                        Intarray   inp       = new Intarray();
                        Floatarray costs     = new Floatarray();
                        double     totalCost = BeamSearch.beam_search(out resText, inp, costs, fst, lmodel, 100);
                        Console.WriteLine(bookstore.PathFile(page, line, suffix));
                        Console.Write("  beam_search score: {0}", totalCost);

                        /*string resText2;
                         * fst.BestPath(out resText2);*/

                        // write cseg to bookstore
                        string trans;
                        bookstore.GetLine(out trans, page, line, suffix);
                        resText = resText.Replace(" ", "");
                        if (String.IsNullOrEmpty(trans))
                        {
                            bookstore.PutLine(resText, page, line, suffix);
                            Console.Write("; transcript saved");
                        }
                        else if (trans == resText)
                        {
                            // convert inputs and rseg to cseg
                            SegmRoutine.rseg_to_cseg(cseg, rseg, inp);
                            bookstore.PutCharSegmentation(cseg, page, line, suffix);
                            Console.Write("; cseg saved");
                        }
                        else if (saveRseg)
                        {
                            // convert inputs and rseg to cseg
                            SegmRoutine.rseg_to_cseg(cseg, rseg, inp);
                            //SegmRoutine.remove_small_components(cseg, 4);

                            /*bookstore.PutCharSegmentation(cseg, page, line, suffix);
                             * Console.Write("; cseg saved");*/
                            SegmRoutine.make_line_segmentation_white(cseg);
                            ImgLabels.simple_recolor(cseg);
                            string v = "rseg";
                            if (!String.IsNullOrEmpty(suffix))
                            {
                                v += "."; v += suffix;
                            }
                            string rsegpath = bookstore.PathFile(page, line, v, "png");
                            ImgIo.write_image_packed(rsegpath, cseg);
                            Console.Write("; rseg saved");
                        }
                        Console.WriteLine();
                    }
                }
            }
        }