Skip to content

Commit

Permalink
fix getting mask of tile entirely within annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
pjl54 committed May 16, 2021
1 parent dfe4c30 commit 5a46c36
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
22 changes: 13 additions & 9 deletions WSI_handling/wsi.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,23 @@ def mask_out_tile(self,desired_mpp,coords,wh,colors_to_use=None,annotation_idx=N
elif annotation_idx is not None:
points = [points[annotation_idx]]

outerbounds = [coords[0]+base_wh[0],coords[1]+base_wh[1]]
points = [pointSet for pointSet in points if any([((p[0] > coords[0]) and (p[0] < outerbounds[0])) and ((p[1] > coords[1]) and (p[1] < outerbounds[1])) for p in pointSet ])]
tile_poly = Polygon([(coords[0],coords[1]),(coords[0],coords[1]+base_wh[1]),(coords[0]+base_wh[0],coords[1]+base_wh[1]),(coords[0]+base_wh[0],coords[1])])

# this rounding may de-align the mask and RGB image
points = self.resize_points(points,resize_factor)

coords = tuple([int(c * resize_factor) for c in coords])
mask = np.zeros((wh[1],wh[0]),dtype=np.uint8)

points_maps = [point_map for point_map in zip(points,map_idx) if tile_poly.intersects(Polygon(point_map[0]))]
if points_maps:
points,map_idx = zip(*points_maps)

# this rounding may de-align the mask and RGB image
points = self.resize_points(points,resize_factor)

coords = tuple([int(c * resize_factor) for c in coords])

points = [[(int(p[0] - coords[0]), int(p[1] - coords[1])) for p in pointSet] for pointSet in points]
points = [[(int(p[0] - coords[0]), int(p[1] - coords[1])) for p in pointSet] for pointSet in points]

for annCount, pointSet in enumerate(points):
cv2.fillPoly(mask,[np.asarray(pointSet).reshape((-1,1,2))],map_idx[annCount])
for annCount, pointSet in enumerate(points):
cv2.fillPoly(mask,[np.asarray(pointSet).reshape((-1,1,2))],map_idx[annCount])

return mask

Expand Down
5 changes: 2 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@
setup(
name = 'WSI_handling',
packages = ['WSI_handling'],
version = '0.16',
version = '0.16.1',
license='MIT',
description = 'Convienent handling of annotated whole slide images',
long_description=long_description,
long_description_content_type='text/markdown',
author = 'Patrick Leo',
author_email = '[email protected]',
url = 'https://github.com/pjl54/WSI_handling',
download_url = 'https://github.com/pjl54/WSI_handling/archive/v0.16.tar.gz',
url = 'https://github.com/pjl54/WSI_handling',
keywords = ['whole slide image', 'digital pathology', 'annotations'],
install_requires=[
'numpy',
Expand Down

0 comments on commit 5a46c36

Please sign in to comment.