MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / _get_patch

Method _get_patch

monai/data/wsi_reader.py:1502–1540  ·  view source on GitHub ↗

Extracts and returns a patch image form the whole slide image. Args: wsi: a whole slide image object loaded from a file or a list of such objects location: (top, left) tuple giving the top left pixel in the level 0 reference frame. Defaults to (0, 0).

(
        self, wsi, location: tuple[int, int], size: tuple[int, int], level: int, dtype: DtypeLike, mode: str
    )

Source from the content-addressed store, hash-verified

1500 return wsi_list if len(filenames) > 1 else wsi_list[0]
1501
1502 def _get_patch(
1503 self, wsi, location: tuple[int, int], size: tuple[int, int], level: int, dtype: DtypeLike, mode: str
1504 ) -> np.ndarray:
1505 """
1506 Extracts and returns a patch image form the whole slide image.
1507
1508 Args:
1509 wsi: a whole slide image object loaded from a file or a list of such objects
1510 location: (top, left) tuple giving the top left pixel in the level 0 reference frame. Defaults to (0, 0).
1511 size: (height, width) tuple giving the patch size at the given level (`level`).
1512 If None, it is set to the full image size at the given level.
1513 level: the level number.
1514 dtype: the data type of output image.
1515 mode: the output image mode, 'RGB' or 'RGBA'.
1516
1517 """
1518 # Load the entire image
1519 wsi_image: np.ndarray = wsi.asarray(level=level).astype(dtype)
1520 if len(wsi_image.shape) < 3:
1521 wsi_image = wsi_image[..., None]
1522
1523 # Extract patch
1524 downsampling_ratio = self.get_downsample_ratio(wsi=wsi, level=level)
1525 location_ = [round(location[i] / downsampling_ratio) for i in range(len(location))]
1526 patch = wsi_image[location_[0] : location_[0] + size[0], location_[1] : location_[1] + size[1], :]
1527
1528 # Make the channel to desired dimensions
1529 patch = np.moveaxis(patch, -1, self.channel_dim)
1530
1531 # Check if the color channel is 3 (RGB) or 4 (RGBA)
1532 if mode in "RGB":
1533 if patch.shape[self.channel_dim] not in [3, 4]:
1534 raise ValueError(
1535 f"The image is expected to have three or four color channels in '{mode}' mode but has "
1536 f"{patch.shape[self.channel_dim]}. "
1537 )
1538 patch = np.take(patch, [0, 1, 2], self.channel_dim)
1539
1540 return patch
1541
1542 def _resize_to_mpp_res(self, wsi, closest_lvl, mpp_list, user_mpp: tuple):
1543 """

Callers

nothing calls this directly

Calls 2

get_downsample_ratioMethod · 0.95
astypeMethod · 0.80

Tested by

no test coverage detected