/// <summary> /// Return the segmentation-derived mask for the character. /// This may optionally be grown by some pixels. /// </summary> public override void GetMask(out Rect r, ref Bytearray outmask, int index, int grow) { r = boxes.At1d(index).Grow(grow); r.Intersect(new Rect(0, 0, labels.Dim(0), labels.Dim(1))); if (fullheight) { r.y0 = 0; r.y1 = labels.Dim(1); } int x = r.x0, y = r.y0, w = r.Width(), h = r.Height(); Intarray segs = segments.At1d(index); outmask.Resize(w, h); outmask.Fill(0); for (int i = 0; i < w; i++) { for (int j = 0; j < h; j++) { int label = labels[x + i, y + j]; if (NarrayUtil.first_index_of(segs, label) >= 0) { outmask[i, j] = (byte)255; } } } if (grow > 0) { Morph.binary_dilate_circle(outmask, grow); } }
/// <summary> /// Get a mask at a given location /// </summary> public override void GetMaskAt(ref Bytearray mask, int index, Rect b) { if (!(b.x0 > -1000 && b.x1 < 10000 && b.y0 > -1000 && b.y1 < 10000)) { throw new Exception("CHECK: (b.Left > -1000 && b.Right < 10000 && b.Top > -1000 && b.Bottom < 10000)"); } mask.Resize(b.Width(), b.Height()); mask.Fill(0); Intarray segs = segments.At1d(index); int w = b.Width(), h = b.Height(); for (int i = 0; i < w; i++) { int x = b.x0 + i; unchecked { if ((uint)x >= labels.Dim(0)) { continue; } } for (int j = 0; j < h; j++) { int y = b.y0 + j; unchecked { if (((uint)y) >= labels.Dim(1)) { continue; } } int label = labels[b.x0 + i, b.y0 + j]; if (NarrayUtil.first_index_of(segs, label) >= 0) { mask[i, j] = (byte)255; } } } }
public static void extract_holes(ref Bytearray holes, Bytearray binarized) { Intarray temp = new Intarray(); temp.Copy(binarized); NarrayUtil.Sub(255, temp); ImgLabels.label_components(ref temp); int background = -1; for (int i = 0; i < temp.Dim(0); i++) { if (temp[i, 0] != 0) { background = temp[i, 0]; break; } } holes.MakeLike(temp); holes.Fill((byte)0); if (background <= 0) { throw new Exception("extract_holes: background must be more 0"); } for (int i = 0; i < temp.Dim(0); i++) { for (int j = 0; j < temp.Dim(1); j++) { if (temp[i, j] > 0 && temp[i, j] != background) { holes[i, j] = 255; } } } /*fprintf(stderr, "segholes\n"); * dsection("segholes"); * dshow(holes, "y");*/ }