def setSegmentation(self, segmentation, cseg=0): """Set the line segmentation.""" # reorder the labels by the x center of bounding box segmentation = common.renumber_labels_by_boxes( segmentation, key=lambda x: mean((x[1].start, x[1].stop))) # compute the bounding boxes in order boxes = [None] + measurements.find_objects(segmentation) n = len(boxes) # now consider groups of boxes groups = [] for i in range(1, n): for r in range(1, self.maxrange + 1): box = None gap = 0 labels = [] for j in range(i, min(n, i + r)): if box is not None: gap = max(gap, boxes[j][1].start - box[1].stop) box = sl.union(box, boxes[j]) labels.append(j) # skip if two constituent boxes have too large a gap between them if gap > self.maxdist: continue a = sl.aspect(box) # skip if the aspect ratio is wrong if 1.0 / a > self.maxaspect: continue groups.append((box, labels)) # compute some statistics mw = median([sl.dim0(g[0]) for g in groups]) # now select based on statistics groups = [g for g in groups if sl.dim1(g[0]) < self.maxwidth * mw] # now we have a list of candidate groups self.segmentation = segmentation self.groups = groups self.clearLattice() return len(self.groups)
def compute_lines(segmentation, scale, min_width): """Given a line segmentation map, computes a list of tuples consisting of 2D slices and masked images.""" lobjects = morph.find_objects(segmentation) lines = [] for i, o in enumerate(lobjects): if o is None: continue if sl.dim1(o) < min_width * scale or sl.dim0(o) < 0.5 * scale: print(sl.dim1(o), sl.dim0(o)) continue mask = (segmentation[o] == i + 1) if amax(mask) == 0: continue result = record() result.label = i + 1 result.bounds = o result.mask = mask lines.append(result) return lines
def compute_lines(segmentation,scale): """Given a line segmentation map, computes a list of tuples consisting of 2D slices and masked images.""" lobjects = morph.find_objects(segmentation) lines = [] for i,o in enumerate(lobjects): if o is None: continue if sl.dim1(o)<2*scale or sl.dim0(o)<scale: continue mask = (segmentation[o]==i+1) if amax(mask)==0: continue result = record() result.label = i+1 result.bounds = o result.mask = mask lines.append(result) return lines
def show_lines(image, lines, lsort): """Overlays the computed lines on top of the image, for debugging purposes.""" ys, xs = [], [] clf() cla() imshow(image) for i in range(len(lines)): l = lines[lsort[i]] y, x = sl.center(l.bounds) xs.append(x) ys.append(y) o = l.bounds r = matplotlib.patches.Rectangle((o[1].start, o[0].start), edgecolor='r', fill=0, width=sl.dim1(o), height=sl.dim0(o)) gca().add_patch(r) h, w = image.shape ylim(h, 0) xlim(0, w) plot(xs, ys)
def show_lines(image,lines,lsort): """Overlays the computed lines on top of the image, for debugging purposes.""" ys,xs = [],[] clf(); cla() imshow(image) for i in range(len(lines)): l = lines[lsort[i]] y,x = sl.center(l.bounds) xs.append(x) ys.append(y) o = l.bounds r = matplotlib.patches.Rectangle((o[1].start,o[0].start),edgecolor='r',fill=0,width=sl.dim1(o),height=sl.dim0(o)) gca().add_patch(r) h,w = image.shape ylim(h,0); xlim(0,w) plot(xs,ys)