(foreground_contours, hole_contours, min_area)
| 251 | return foreground_contours, hole_contours |
| 252 | |
| 253 | def construct_polygon(foreground_contours, hole_contours, min_area): |
| 254 | polys = [] |
| 255 | for foreground, holes in zip(foreground_contours, hole_contours): |
| 256 | # We remove all contours that consist of fewer than 3 points, as these won't work with the Polygon constructor. |
| 257 | if len(foreground) < 3: |
| 258 | continue |
| 259 | |
| 260 | # remove redundant dimensions from the contour and convert to Shapely Polygon |
| 261 | poly = Polygon(np.squeeze(foreground)) |
| 262 | |
| 263 | # discard all polygons that are considered too small |
| 264 | if poly.area < min_area: |
| 265 | continue |
| 266 | |
| 267 | if not poly.is_valid: |
| 268 | # This is likely becausee the polygon is self-touching or self-crossing. |
| 269 | # Try and 'correct' the polygon using the zero-length buffer() trick. |
| 270 | # See https://shapely.readthedocs.io/en/stable/manual.html#object.buffer |
| 271 | poly = poly.buffer(0) |
| 272 | |
| 273 | # Punch the holes in the polygon |
| 274 | for hole_contour in holes: |
| 275 | if len(hole_contour) < 3: |
| 276 | continue |
| 277 | |
| 278 | hole = Polygon(np.squeeze(hole_contour)) |
| 279 | |
| 280 | if not hole.is_valid: |
| 281 | continue |
| 282 | |
| 283 | # ignore all very small holes |
| 284 | if hole.area < min_area: |
| 285 | continue |
| 286 | |
| 287 | poly = poly.difference(hole) |
| 288 | |
| 289 | polys.append(poly) |
| 290 | |
| 291 | if len(polys) == 0: |
| 292 | raise Exception("Raw tissue mask consists of 0 polygons") |
| 293 | |
| 294 | # If we have multiple polygons, we merge any overlap between them using unary_union(). |
| 295 | # This will result in a Polygon or MultiPolygon with most tissue masks. |
| 296 | return unary_union(polys) |
| 297 | |
| 298 | def generate_tiles(tile_width_pix, tile_height_pix, img_width, img_height, offsets=[(0, 0)]): |
| 299 | # Generate tiles covering the entire image. |
no outgoing calls
no test coverage detected