public override void Clear() { data.Clear(); classes.Clear(); nc = 0; nf = -1; }
public override void Extract(Narray <Floatarray> outarrays, Floatarray inarray) { outarrays.Clear(); Floatarray image = outarrays.Push(new Floatarray()); image.Copy(inarray); }
public static void segmentation_correspondences(Narray<Intarray> outsegments, Intarray seg, Intarray cseg) { if (NarrayUtil.Max(seg) >= 10000) throw new Exception("CHECK_ARG: (max(seg)<10000)"); if (NarrayUtil.Max(cseg) >= 10000) throw new Exception("CHECK_ARG: (max(cseg)<10000)"); int nseg = NarrayUtil.Max(seg) + 1; int ncseg = NarrayUtil.Max(cseg) + 1; Intarray overlaps = new Intarray(nseg, ncseg); overlaps.Fill(0); if (seg.Length() != cseg.Length()) throw new Exception("CHECK_ARG: (seg.Length()==cseg.Length())"); for (int i = 0; i < seg.Length(); i++) overlaps[seg.At1d(i), cseg.At1d(i)]++; outsegments.Clear(); outsegments.Resize(ncseg); for (int i = 0; i < nseg; i++) { int j = NarrayRowUtil.RowArgMax(overlaps, i); if (!(j >= 0 && j < ncseg)) throw new Exception("ASSERT: (j>=0 && j<ncseg)"); if (outsegments[j] == null) outsegments[j] = new Intarray(); outsegments[j].Push(i); } }
public override void Extract(Narray<Floatarray> outarrays, Floatarray inarray) { outarrays.Clear(); Floatarray image = outarrays.Push(); rescale(image, inarray); //image /= Math.Max(1.0f, NarrayUtil.Max(image)); }
public override void Extract(Narray <Floatarray> outarrays, Floatarray inarray) { outarrays.Clear(); Floatarray image = outarrays.Push(); rescale(image, inarray); //image /= Math.Max(1.0f, NarrayUtil.Max(image)); }
public void Init(params string[] books) { bool retrain = PGetb("retrain"); bool randomize = PGetb("randomize"); string cbookstore = PGet("cbookstore"); bookstores.Clear(); all_lines.Clear(); cseg_variant = "cseg.gt"; text_variant = "gt"; if (retrain) { cseg_variant = "cseg"; text_variant = ""; } int nbooks = books.Length; bookstores.Resize(nbooks); int totalNumberOfPages = 0; for (int i = 0; i < nbooks; i++) { bookstores[i] = ComponentCreator.MakeComponent <IBookStore>(cbookstore); bookstores[i].SetPrefix(books[i].Trim()); Global.Debugf("info", "{0}: {1} pages", books[i], bookstores[i].NumberOfPages()); totalNumberOfPages += bookstores[i].NumberOfPages(); } //CHECK_ARG(totalNumberOfPages > 0, "totalNumberOfPages > 0"); // compute a list of all lines Intarray triple = new Intarray(3); for (int i = 0; i < nbooks; i++) { for (int j = 0; j < bookstores[i].NumberOfPages(); j++) { for (int k = 0; k < bookstores[i].LinesOnPage(j); k++) { triple[0] = i; triple[1] = j; triple[2] = k; NarrayRowUtil.RowPush(all_lines, triple); } } } Global.Debugf("info", "got {0} lines", all_lines.Dim(0)); // randomly permute it so that we train in random order if (randomize) { Shuffle(); } index = 0; }
/// <summary> ///A test for Clear ///</summary> public void ClearTestHelper <T>() { const int sizeD0 = 10; var target = new Narray <T>(sizeD0); Assert.AreEqual(sizeD0, target.Length()); Assert.AreEqual(sizeD0, target.Dim(0)); target.Clear(); Assert.AreEqual(0, target.Length()); Assert.AreEqual(0, target.Dim(0)); }
/// <summary> /// Compute the bounding boxes for the pixels in the image. /// </summary> public static void bounding_boxes(ref Narray<Rect> result, Intarray image) { result.Clear(); int n = NarrayUtil.Max(image); if (n < 1) return; result.Resize(n + 1); result.Fill(Rect.CreateEmpty()); for (int i = 0; i < image.Dim(0); i++) for (int j = 0; j < image.Dim(1); j++) { int value = image[i, j]; Rect r = result[value]; r.Include(i, j); result[value] = r; //original: result(value).include(i, j); } }
public static void segmentation_correspondences(Narray <Intarray> outsegments, Intarray seg, Intarray cseg) { if (NarrayUtil.Max(seg) >= 10000) { throw new Exception("CHECK_ARG: (max(seg)<10000)"); } if (NarrayUtil.Max(cseg) >= 10000) { throw new Exception("CHECK_ARG: (max(cseg)<10000)"); } int nseg = NarrayUtil.Max(seg) + 1; int ncseg = NarrayUtil.Max(cseg) + 1; Intarray overlaps = new Intarray(nseg, ncseg); overlaps.Fill(0); if (seg.Length() != cseg.Length()) { throw new Exception("CHECK_ARG: (seg.Length()==cseg.Length())"); } for (int i = 0; i < seg.Length(); i++) { overlaps[seg.At1d(i), cseg.At1d(i)]++; } outsegments.Clear(); outsegments.Resize(ncseg); for (int i = 0; i < nseg; i++) { int j = NarrayRowUtil.RowArgMax(overlaps, i); if (!(j >= 0 && j < ncseg)) { throw new Exception("ASSERT: (j>=0 && j<ncseg)"); } if (outsegments[j] == null) { outsegments[j] = new Intarray(); } outsegments[j].Push(i); } }
/// <summary> /// Compute the bounding boxes for the pixels in the image. /// </summary> public static void bounding_boxes(ref Narray <Rect> result, Intarray image) { result.Clear(); int n = NarrayUtil.Max(image); if (n < 1) { return; } result.Resize(n + 1); result.Fill(Rect.CreateEmpty()); for (int i = 0; i < image.Dim(0); i++) { for (int j = 0; j < image.Dim(1); j++) { int value = image[i, j]; Rect r = result[value]; r.Include(i, j); result[value] = r; //original: result(value).include(i, j); } } }
/// <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); } } }
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")); }
public override void Clear() { data.Clear(); }
public override void Extract(Narray<Floatarray> outarrays, Floatarray inarray) { outarrays.Clear(); Floatarray image = outarrays.Push(new Floatarray()); image.Copy(inarray); }
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")); }