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

Class OpenSlideWSIReader

monai/data/wsi_reader.py:1024–1287  ·  view source on GitHub ↗

Read whole slide images and extract patches using OpenSlide library. Args: level: the whole slide image level at which the patches are extracted. mpp: the resolution in micron per pixel at which the patches are extracted. mpp_rtol: the acceptable relative tolerance

Source from the content-addressed store, hash-verified

1022
1023@require_pkg(pkg_name="openslide")
1024class OpenSlideWSIReader(BaseWSIReader):
1025 """
1026 Read whole slide images and extract patches using OpenSlide library.
1027
1028 Args:
1029 level: the whole slide image level at which the patches are extracted.
1030 mpp: the resolution in micron per pixel at which the patches are extracted.
1031 mpp_rtol: the acceptable relative tolerance for resolution in micro per pixel.
1032 mpp_atol: the acceptable absolute tolerance for resolution in micro per pixel.
1033 power: the objective power at which the patches are extracted.
1034 power_rtol: the acceptable relative tolerance for objective power.
1035 power_atol: the acceptable absolute tolerance for objective power.
1036 channel_dim: the desired dimension for color channel. Default to 0 (channel first).
1037 dtype: the data type of output image. Defaults to `np.uint8`.
1038 device: target device to put the extracted patch. Note that if device is "cuda"",
1039 the output will be converted to torch tenor and sent to the gpu even if the dtype is numpy.
1040 mode: the output image color mode, "RGB" or "RGBA". Defaults to "RGB".
1041 kwargs: additional args for `openslide.OpenSlide` module.
1042
1043 Notes:
1044 Only one of resolution parameters, `level`, `mpp`, or `power`, should be provided.
1045 If such parameters are provided in `get_data` method, those will override the values provided here.
1046 If none of them are provided here or in `get_data`, `level=0` will be used.
1047
1048 """
1049
1050 supported_suffixes = ["tif", "tiff", "svs"]
1051 backend = "openslide"
1052
1053 def __init__(self, **kwargs):
1054 super().__init__(**kwargs)
1055
1056 @staticmethod
1057 def get_level_count(wsi) -> int:
1058 """
1059 Returns the number of levels in the whole slide image.
1060
1061 Args:
1062 wsi: a whole slide image object loaded from a file.
1063
1064 """
1065 return wsi.level_count # type: ignore
1066
1067 def get_size(self, wsi, level: int) -> tuple[int, int]:
1068 """
1069 Returns the size (height, width) of the whole slide image at a given level.
1070
1071 Args:
1072 wsi: a whole slide image object loaded from a file.
1073 level: the level number where the size is calculated.
1074
1075 """
1076 return (wsi.level_dimensions[level][1], wsi.level_dimensions[level][0])
1077
1078 def get_downsample_ratio(self, wsi, level: int) -> float:
1079 """
1080 Returns the down-sampling ratio of the whole slide image at a given level.
1081

Callers 2

__init__Method · 0.85

Calls

no outgoing calls

Tested by 1

Used in the wild real call sites across dependent graphs

searching dependent graphs…