Skip to content

Commit

Permalink
Try to fix broken tissue mask polygon topologies when encountering them.
Browse files Browse the repository at this point in the history
  • Loading branch information
jjhbarkeywolf authored and jjhbw committed Apr 7, 2021
1 parent e019105 commit b701725
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions infer_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import numpy as np
import cv2
import openslide
from shapely.geometry import shape, MultiPolygon, Polygon, box
from shapely.geometry import MultiPolygon, Polygon, box
from shapely.affinity import scale
from shapely.ops import unary_union
from PIL import ImageDraw
import h5py

Expand Down Expand Up @@ -66,10 +67,17 @@ def construct_tissue_polygon(foreground_contours, hole_contours, min_area):
if poly.area < min_area:
continue

if not poly.is_valid:
# This is likely becausee the polygon is self-touching or self-crossing.
# Try and 'correct' the polygon using the zero-length buffer() trick.
# See https://shapely.readthedocs.io/en/stable/manual.html#object.buffer
poly = poly.buffer(0)

# Punch the holes in the polygon
for hole_contour in holes:
if len(hole_contour) < 3:
continue

hole = Polygon(np.squeeze(hole_contour))

if not hole.is_valid:
Expand All @@ -84,7 +92,7 @@ def construct_tissue_polygon(foreground_contours, hole_contours, min_area):
polys.append(poly)

# Combine all polygons into a MultiPolygon
return MultiPolygon(polys)
return MultiPolygon(unary_union(polys))


def make_tile_QC_fig(tile_sets, slide, level, line_width_pix):
Expand Down

0 comments on commit b701725

Please sign in to comment.