def reading_order(lines,highlight=None,debug=0): """Given the list of lines (a list of 2D slices), computes the partial reading order. The output is a binary 2D array such that order[i,j] is true if line i comes before line j in reading order.""" order = zeros((len(lines),len(lines)),'B') def x_overlaps(u,v): return u[1].start<v[1].stop and u[1].stop>v[1].start def above(u,v): return u[0].start<v[0].start def left_of(u,v): return u[1].stop<v[1].start def separates(w,u,v): if w[0].stop<min(u[0].start,v[0].start): return 0 if w[0].start>max(u[0].stop,v[0].stop): return 0 if w[1].start<u[1].stop and w[1].stop>v[1].start: return 1 if highlight is not None: clf(); title("highlight"); imshow(binary); ginput(1,debug) for i,u in enumerate(lines): for j,v in enumerate(lines): if x_overlaps(u,v): if above(u,v): order[i,j] = 1 else: if [w for w in lines if separates(w,u,v)]==[]: if left_of(u,v): order[i,j] = 1 if j==highlight and order[i,j]: print (i,j), y0,x0 = sl.center(lines[i]) y1,x1 = sl.center(lines[j]) plot([x0,x1+200],[y0,y1]) if highlight is not None: print ginput(1,debug) return order
def reading_order(lines,highlight=None,debug=0): """Given the list of lines (a list of 2D slices), computes the partial reading order. The output is a binary 2D array such that order[i,j] is true if line i comes before line j in reading order.""" order = zeros((len(lines),len(lines)),'B') def x_overlaps(u,v): return u[1].start<v[1].stop and u[1].stop>v[1].start def above(u,v): return u[0].start<v[0].start def left_of(u,v): return u[1].stop<v[1].start def separates(w,u,v): if w[0].stop<min(u[0].start,v[0].start): return 0 if w[0].start>max(u[0].stop,v[0].stop): return 0 if w[1].start<u[1].stop and w[1].stop>v[1].start: return 1 if highlight is not None: clf(); title("highlight"); imshow(binary); ginput(1,debug) for i,u in enumerate(lines): for j,v in enumerate(lines): if x_overlaps(u,v): if above(u,v): order[i,j] = 1 else: if [w for w in lines if separates(w,u,v)]==[]: if left_of(u,v): order[i,j] = 1 if j==highlight and order[i,j]: print((i, j), end=' ') y0,x0 = sl.center(lines[i]) y1,x1 = sl.center(lines[j]) plot([x0,x1+200],[y0,y1]) if highlight is not None: print() ginput(1,debug) return order
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)