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(); }
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); }
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); }
public static void combine_segmentations(ref Intarray dst, Intarray src) { dst.SameDims(src); int n = NarrayUtil.Max(dst) + 1; for (int i = 0; i < dst.Length1d(); i++) { dst.Put1d(i, (dst.At1d(i) + src.At1d(i) * n)); } ImgLabels.renumber_labels(dst, 1); }
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); }