Given a list of edges, return the points at which they intersect within `tolerance` pixels.
(edges, x_tolerance=1, y_tolerance=1)
| 1320 | |
| 1321 | |
| 1322 | def edges_to_intersections(edges, x_tolerance=1, y_tolerance=1) -> dict: |
| 1323 | """ |
| 1324 | Given a list of edges, return the points at which they intersect |
| 1325 | within `tolerance` pixels. |
| 1326 | """ |
| 1327 | intersections = {} |
| 1328 | v_edges, h_edges = [ |
| 1329 | list(filter(lambda x: x["orientation"] == o, edges)) for o in ("v", "h") |
| 1330 | ] |
| 1331 | for v in sorted(v_edges, key=itemgetter("x0", "top")): |
| 1332 | for h in sorted(h_edges, key=itemgetter("top", "x0")): |
| 1333 | if ( |
| 1334 | (v["top"] <= (h["top"] + y_tolerance)) |
| 1335 | and (v["bottom"] >= (h["top"] - y_tolerance)) |
| 1336 | and (v["x0"] >= (h["x0"] - x_tolerance)) |
| 1337 | and (v["x0"] <= (h["x1"] + x_tolerance)) |
| 1338 | ): |
| 1339 | vertex = (v["x0"], h["top"]) |
| 1340 | if vertex not in intersections: |
| 1341 | intersections[vertex] = {"v": [], "h": []} |
| 1342 | intersections[vertex]["v"].append(v) |
| 1343 | intersections[vertex]["h"].append(h) |
| 1344 | return intersections |
| 1345 | |
| 1346 | |
| 1347 | def obj_to_bbox(obj): |