Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

accept a ROI as a geojson #108

Closed
kaczmarj opened this issue Mar 24, 2023 · 2 comments · Fixed by #114
Closed

accept a ROI as a geojson #108

kaczmarj opened this issue Mar 24, 2023 · 2 comments · Fixed by #114

Comments

@kaczmarj
Copy link
Member

use case here is in qupath. one would be in qupath and would annotate a region of interest. this roi would be run by wsinfer.

the roi would be encoded as geojson. we can check if the patches overlap with the ROI and apply the model to those patches.

@kaczmarj
Copy link
Member Author

kaczmarj commented Apr 7, 2023

the ROI would be in geojson format. it could include multiple regions potentially. here's a tentative plan:

  1. encode the ROI as a shapely polygon.
  2. encode all of the patches as shapely polygons (boxes).
  3. use shapely STRTree to find patches that intersect with the ROI.
  4. keep the patches that intersect and throw away patches that do not.
  5. save those patches and carry on!
def get_patches_in_rois(geojson_path: str, coords: np.ndarray, patch_size: int):
    with open(geojson_path) as f:
        geo = geojson.load(f)
    num_rois = len(geo["features"])
    print(f"Processing {num_rois} rois")
    for roi in geo["features"]:
        assert roi.is_valid, "an ROI geometry is not valid"
    # Each geom is a shapely Polygon type.
    geoms_rois = [shape(roi["geometry"]) for roi in geo["features"]]
    boxes = [box(x, y, x+patch_size, y+patch_size) for x, y in coords]
    tree = shapely.STRtree(boxes)
    _, intersecting_ids = tree.query([geom, geom], predicate="intersects")
    intersecting_ids = np.sort(np.unique(intersecting_ids))
    return coords[intersecting_ids]

@kaczmarj
Copy link
Member Author

kaczmarj commented Apr 7, 2023

it works!

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant