tissuemask.py
Methods of masking tissue and background. Adapted from tiatoolbox.tools.tissuemask, replaced cv2 with skimage
MorphologicalMasker
Bases: OtsuTissueMasker
Tissue masker which uses a threshold and simple morphological operations.
This method applies Otsu's threshold before a simple small region removal, followed by a morphological dilation. The kernel for the dilation is an ellipse of radius 64/mpp unless a value is given for kernel_size. MPP is estimated from objective power via func:tiatoolbox.utils.misc.objective_power2mpp if a power argument is given instead of mpp to the initialiser.
For small region removal, the minimum area size defaults to the area of the kernel. If no mpp, objective power, or kernel_size arguments are given then the kernel defaults to a size of 1x1.
The scale of the morphological operations can also be manually specified with the kernel_size argument, for example if the automatic scale from mpp or objective power is too large or small.
Examples: >>> from tiatoolbox.tools.tissuemask import MorphologicalMasker >>> from tiatoolbox.wsicore.wsireader import WSIReader >>> wsi = WSIReader.open("slide.svs") >>> thumbnail = wsi.slide_thumbnail(32, "mpp") >>> masker = MorphologicalMasker(mpp=32) >>> masks = masker.fit_transform([thumbnail])
An example reading a thumbnail from a file where the objective power is known:
>>> from tiatoolbox.tools.tissuemask import MorphologicalMasker
>>> from tiatoolbox.utils import imread
>>> thumbnail = imread("thumbnail.png")
>>> masker = MorphologicalMasker(power=1.25)
>>> masks = masker.fit_transform([thumbnail])
Source code in lavlab/tissuemask.py
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | |
__init__(*, mpp=None, power=None, kernel_size=None, min_region_size=None)
Initialise a morphological masker.
Args: mpp (float or tuple(float)): The microns per-pixel of the image to be masked. Used to calculate kernel_size as 64/mpp, optional. power (float or tuple(float)): The objective power of the image to be masked. Used to calculate kernel_size as 64/objective_power2mpp(power), optional. kernel_size (int or tuple(int)): Size of elliptical kernel in x and y, optional. min_region_size (int): Minimum region size in pixels to consider as foreground. Defaults to area of the kernel.
Source code in lavlab/tissuemask.py
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | |
OtsuTissueMasker
Bases: TissueMasker
Tissue masker which uses Otsu's method to determine background.
Otsu's method.
Examples: >>> from tiatoolbox.tools.tissuemask import OtsuTissueMasker >>> masker = OtsuTissueMasker() >>> masker.fit(thumbnail) >>> masks = masker.transform([thumbnail])
>>> from tiatoolbox.tools.tissuemask import OtsuTissueMasker
>>> masker = OtsuTissueMasker()
>>> masks = masker.fit_transform([thumbnail])
Source code in lavlab/tissuemask.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | |
__init__()
Initialize :class:OtsuTissueMasker.
Source code in lavlab/tissuemask.py
127 128 129 130 131 132 133 | |
fit(images, masks=None)
Find a binary threshold using Otsu's method.
Args: images (:class:numpy.ndarray): List of images with a length 4 shape (N, height, width, channels). masks (:class:numpy.ndarray): Unused here, for API consistency.
Source code in lavlab/tissuemask.py
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | |
transform(images)
Create masks using the threshold found during :func:fit.
Args: images (:class:numpy.ndarray): List of images with a length 4 shape (N, height, width, channels).
Returns: :class:numpy.ndarray: List of images with a length 4 shape (N, height, width, channels).
Source code in lavlab/tissuemask.py
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | |
TissueMasker
Bases: ABC
Base class for tissue maskers.
Takes an image as in put and outputs a mask.
Source code in lavlab/tissuemask.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | |
fit(images, masks=None) abstractmethod
Fit the masker to the images and parameters.
Args: images (:class:numpy.ndarray): List of images, usually WSI thumbnails. Expected shape is NHWC (number images, height, width, channels). masks (:class:numpy.ndarray): Target/ground-truth masks. Expected shape is NHW (n images, height, width).
Source code in lavlab/tissuemask.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | |
fit_transform(images, **kwargs)
Perform :func:fit then :func:transform.
Sometimes it can be more optimal to perform both at the same time for a single sample. In this case the base implementation of :func:fit followed by :func:transform can be overridden.
Args: images (:class:numpy.ndarray): Image to create mask from. **kwargs (dict): Other key word arguments passed to fit.
Source code in lavlab/tissuemask.py
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | |
transform(images) abstractmethod
Create and return a tissue mask.
Args: images (:class:numpy.ndarray): RGB image, usually a WSI thumbnail.
Returns: :class:numpy.ndarray: Map of semantic classes spatially over the WSI e.g. regions of tissue vs background.
Source code in lavlab/tissuemask.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 | |
objective_power2mpp(objective_power)
Approximate mpp from objective power.
The formula used for estimation is :math:power = \frac{10}{mpp}. This is a self-inverse function and therefore :func:mpp2objective_power is simply an alias to this function.
Note that this function is wrapped in :class:numpy.vectorize.
Args: objective_power (float or tuple(float, ...)): Objective power.
Returns: float or tuple(float | np.ndarray): Microns per-pixel (MPP) approximations.
Examples: >>> objective_power2mpp(40) array(0.25)
>>> objective_power2mpp([40, 20, 10])
array([0.25, 0.5, 1.])
Source code in lavlab/tissuemask.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | |
transform(self, images)
Create masks using the found threshold followed by morphological operations.
Args: images (:class:numpy.ndarray): List of images with a length 4 shape (N, height, width, channels).
Returns: :class:numpy.ndarray: List of images with a length 4 shape (N, height, width, channels).
Source code in lavlab/tissuemask.py
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | |