def matrix_cluster(boxes): sizes = np.array([[b,b.x_size()] for b in boxes]) normal = reject_outliers(sizes)[:,0] x_clusters = cluster(list(normal)) y_clusters = cluster(list(normal),x=False) numbers = [] for c in x_clusters: new_clust = [] for y_c in y_clusters: new_clust.append(Cluster(list(set(tuple(c.boxes)) & set(tuple(y_c.boxes))))) numbers.append(new_clust) root = tk.Tk() vis = Visualizer(root,1000,600) #return the clusters of boxes for i in numbers: for c in i: for b in c.boxes: box = Box2D((b.x1,b.y1),size=b.size_t()) vis.add_drawable(box) box = Box2D((c.bounding_box().x1,c.bounding_box().y1), size=c.bounding_box().size_t()) box.fill = None box.outline = "green" vis.add_drawable(box) #vis.run() #root.mainloop() return numbers
def display_boxes(boxes): root = tk.Tk() vis = Visualizer(root, 1000, 600) for b in boxes: box = Box2D((b.x1, b.y1), size=b.size_t()) vis.add_drawable(box) vis.run() root.mainloop()
def cluster(boxes,x=True): clusters = [] while boxes: cluster = Cluster() b = boxes.pop() cluster.boxes.append(b) changed = True while changed: changed = False for other in list(boxes): if x: if cluster.bounding_box().overlaps_x(other): changed = True cluster.boxes.append(other) boxes.remove(other) else: if cluster.bounding_box().overlaps_y(other): changed = True cluster.boxes.append(other) boxes.remove(other) #boxes = tmp #if len(cluster.boxes)<5 and cluster.bounding_box().size()>10: clusters.append(cluster) root = tk.Tk() vis = Visualizer(root,1000,600) if x: clusters = sorted(clusters,key = lambda c:c.mid_x()) else: clusters = sorted(clusters,key = lambda c:c.mid_y()) #return the clusters of boxes for c in clusters: for b in c.boxes: box = Box2D((b.x1,b.y1),size=b.size_t()) vis.add_drawable(box) box = Box2D((c.bounding_box().x1,c.bounding_box().y1), size=c.bounding_box().size_t()) box.fill = None box.outline = "green" vis.add_drawable(box) #vis.run() #root.mainloop() return clusters
] colors = ["red", "blue", "green", "yellow"] sizes = np.array([[b, b.y_size()] for b in boxes]) abnormal = accept_outliers(sizes)[:, 0] normal = reject_outliers(sizes)[:, 0] first = -1 num = 0 for i in range(100): num = i + 1 clusters, error = cluster_boxes(normal, num_clusters=i + 1) if first != -1 and error / (first - error) < 0.15: break first = error colors *= len(clusters) / len(colors) root = tk.Tk() vis = Visualizer(root, 1000, 600) for cluster, color in zip(clusters, colors): for b in cluster.boxes: box = Box2D((b.x1, b.y1), size=b.size_t()) box.fill = color vis.add_drawable(box) box = Box2D((cluster.bounding_box().x1, cluster.bounding_box().y1), size=cluster.bounding_box().size_t()) box.fill = None vis.add_drawable(box) vis.run() root.mainloop()