public static void make_line_segmentation_black(Intarray a) { check_line_segmentation(a); ImgMisc.replace_values(a, 0xFFFFFF, 0); for (int i = 0; i < a.Length1d(); i++) { a.Put1d(i, a.At1d(i) & 0xFFF); } }
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); }
protected void rescale(Floatarray v, Floatarray input) { if (input.Rank() != 2) { throw new Exception("CHECK_ARG: sub.Rank()==2"); } Floatarray sub = new Floatarray(); // find the largest connected component // and crop to its bounding box // (use a binary version of the character // to compute the bounding box) Intarray components = new Intarray(); float threshold = PGetf("threshold") * NarrayUtil.Max(input); Global.Debugf("biggestcc", "threshold {0}", threshold); components.MakeLike(input); components.Fill(0); for (int i = 0; i < components.Length(); i++) { components[i] = (input[i] > threshold ? 1 : 0); } int n = ImgLabels.label_components(ref components); Intarray totals = new Intarray(n + 1); totals.Fill(0); for (int i = 0; i < components.Length(); i++) { totals[components[i]]++; } totals[0] = 0; Narray <Rect> boxes = new Narray <Rect>(); ImgLabels.bounding_boxes(ref boxes, components); int biggest = NarrayUtil.ArgMax(totals); Rect r = boxes[biggest]; int pad = (int)(PGetf("pad") + 0.5f); r.PadBy(pad, pad); Global.Debugf("biggestcc", "({0}) {1}[{2}] :: {3} {4} {5} {6}", n, biggest, totals[biggest], r.x0, r.y0, r.x1, r.y1); // now perform normal feature extraction // (use the original grayscale input) sub = input; ImgMisc.Crop(sub, r); int csize = PGeti("csize"); float s = Math.Max(sub.Dim(0), sub.Dim(1)) / (float)csize; if (PGetf("noupscale") > 0 && s < 1.0f) { s = 1.0f; } float sig = s * PGetf("aa"); float dx = (csize * s - sub.Dim(0)) / 2f; float dy = (csize * s - sub.Dim(1)) / 2f; if (sig > 1e-3f) { Gauss.Gauss2d(sub, sig, sig); } v.Resize(csize, csize); v.Fill(0f); for (int i = 0; i < csize; i++) { for (int j = 0; j < csize; j++) { float x = i * s - dx; float y = j * s - dy; if (x < 0 || x >= sub.Dim(0)) { continue; } if (y < 0 || y >= sub.Dim(1)) { continue; } float value = ImgOps.bilin(sub, x, y); v[i, j] = value; } } /*Global.Debugf("biggestcc", "{0} {1} ({2}) -> {3} {4} ({5})", * sub.Dim(0), sub.Dim(1), NarrayUtil.Max(sub), * v.Dim(0), v.Dim(1), NarrayUtil.Max(v));*/ }
public static void make_page_segmentation_white(Intarray a) { ImgMisc.replace_values(a, 0, 0xFFFFFF); check_page_segmentation(a); }
public static void make_page_segmentation_black(Intarray a) { check_page_segmentation(a); ImgMisc.replace_values(a, 0xFFFFFF, 0); }