$npx -y skills add K-Dense-AI/scientific-agent-skills --skill histolabLightweight WSI tile extraction and preprocessing. Use for basic slide processing, tissue detection, tile extraction, and stain normalization for H&E images. Best for simple pipelines, dataset preparation, and quick tile-based analysis. For advanced spatial proteomics, multiplexe
| 1 | # Histolab |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | Histolab is a Python library for processing whole slide images (WSI) in digital pathology. It automates tissue detection, extracts informative tiles from gigapixel images, and prepares datasets for deep learning pipelines. The library handles multiple WSI formats, implements sophisticated tissue segmentation, and provides flexible tile extraction strategies. |
| 6 | |
| 7 | ## Installation |
| 8 | |
| 9 | Install OpenSlide system libraries first ([OpenSlide download](https://openslide.org/download/)), then install histolab: |
| 10 | |
| 11 | ```bash |
| 12 | uv pip install histolab |
| 13 | ``` |
| 14 | |
| 15 | For built-in TCGA sample slides via `histolab.data`, also install pooch: |
| 16 | |
| 17 | ```bash |
| 18 | uv pip install pooch |
| 19 | ``` |
| 20 | |
| 21 | Histolab 0.7.0 (latest stable) supports Python 3.8–3.11 on Linux and macOS. Windows is not supported as of 0.7.0. |
| 22 | |
| 23 | ## Quick Start |
| 24 | |
| 25 | Basic workflow for extracting tiles from a whole slide image: |
| 26 | |
| 27 | ```python |
| 28 | from histolab.slide import Slide |
| 29 | from histolab.tiler import RandomTiler |
| 30 | |
| 31 | # Load slide |
| 32 | slide = Slide("slide.svs", processed_path="output/") |
| 33 | |
| 34 | # Configure tiler |
| 35 | tiler = RandomTiler( |
| 36 | tile_size=(512, 512), |
| 37 | n_tiles=100, |
| 38 | level=0, |
| 39 | seed=42 |
| 40 | ) |
| 41 | |
| 42 | # Preview tile locations |
| 43 | tiler.locate_tiles(slide, n_tiles=20) |
| 44 | |
| 45 | # Extract tiles |
| 46 | tiler.extract(slide) |
| 47 | ``` |
| 48 | |
| 49 | ## Core Capabilities |
| 50 | |
| 51 | ### 1. Slide Management |
| 52 | |
| 53 | Load, inspect, and work with whole slide images in various formats. |
| 54 | |
| 55 | **Common operations:** |
| 56 | - Loading WSI files (SVS, TIFF, NDPI, etc.) |
| 57 | - Accessing slide metadata (dimensions, magnification, properties) |
| 58 | - Generating thumbnails for visualization |
| 59 | - Working with pyramidal image structures |
| 60 | - Extracting regions at specific coordinates |
| 61 | |
| 62 | **Key classes:** `Slide` |
| 63 | |
| 64 | **Reference:** `references/slide_management.md` contains comprehensive documentation on: |
| 65 | - Slide initialization and configuration |
| 66 | - Built-in sample datasets (prostate, ovarian, breast, heart, kidney tissues) |
| 67 | - Accessing slide properties and metadata |
| 68 | - Thumbnail generation and visualization |
| 69 | - Working with pyramid levels |
| 70 | - Multi-slide processing workflows |
| 71 | |
| 72 | **Example workflow:** |
| 73 | ```python |
| 74 | from histolab.slide import Slide |
| 75 | from histolab.data import prostate_tissue |
| 76 | |
| 77 | # Load sample data |
| 78 | prostate_svs, prostate_path = prostate_tissue() |
| 79 | |
| 80 | # Initialize slide |
| 81 | slide = Slide(prostate_path, processed_path="output/") |
| 82 | |
| 83 | # Inspect properties |
| 84 | print(f"Dimensions: {slide.dimensions}") |
| 85 | print(f"Levels: {slide.levels}") |
| 86 | print(f"Magnification: {slide.properties.get('openslide.objective-power')}") |
| 87 | |
| 88 | # Save thumbnail to processed_path |
| 89 | from pathlib import Path |
| 90 | Path(slide.processed_path).mkdir(parents=True, exist_ok=True) |
| 91 | slide.thumbnail.save(Path(slide.processed_path) / f"{slide.name}_thumbnail.png") |
| 92 | ``` |
| 93 | |
| 94 | ### 2. Tissue Detection and Masks |
| 95 | |
| 96 | Automatically identify tissue regions and filter background/artifacts. |
| 97 | |
| 98 | **Common operations:** |
| 99 | - Creating binary tissue masks |
| 100 | - Detecting largest tissue region |
| 101 | - Excluding background and artifacts |
| 102 | - Custom tissue segmentation |
| 103 | - Removing pen annotations |
| 104 | |
| 105 | **Key classes:** `TissueMask`, `BiggestTissueBoxMask`, `BinaryMask` |
| 106 | |
| 107 | **Reference:** `references/tissue_masks.md` contains comprehensive documentation on: |
| 108 | - TissueMask: Segments all tissue regions using automated filters |
| 109 | - BiggestTissueBoxMask: Returns bounding box of largest tissue region (default) |
| 110 | - BinaryMask: Base class for custom mask implementations |
| 111 | - Visualizing masks with `locate_mask()` |
| 112 | - Creating custom rectangular and annotation-exclusion masks |
| 113 | - Mask integration with tile extraction |
| 114 | - Best practices and troubleshooting |
| 115 | |
| 116 | **Example workflow:** |
| 117 | ```python |
| 118 | from histolab.masks import TissueMask, BiggestTissueBoxMask |
| 119 | |
| 120 | # Create tissue mask for all tissue regions |
| 121 | tissue_mask = TissueMask() |
| 122 | |
| 123 | # Visualize mask on slide |
| 124 | slide.locate_mask(tissue_mask) |
| 125 | |
| 126 | # Get mask array |
| 127 | mask_array = tissue_mask(slide) |
| 128 | |
| 129 | # Use largest tissue region (default for most extractors) |
| 130 | biggest_mask = BiggestTissueBoxMask() |
| 131 | ``` |
| 132 | |
| 133 | **When to use each mask:** |
| 134 | - `TissueMask`: Multiple tissue sections, comprehensive analysis |
| 135 | - `BiggestTissueBoxMask`: Single main tissue section, exclude artifacts (default) |
| 136 | - Custom `BinaryMask`: Specific ROI, exclude annotations, custom segmentation |
| 137 | |
| 138 | ### 3. Tile Extraction |
| 139 | |
| 140 | Extract smaller regions from large WSI using different strategies. |
| 141 | |
| 142 | **Three extraction strategies:** |
| 143 | |
| 144 | **RandomTiler:** Extract fixed number of randomly positioned tiles |
| 145 | - Best for: Sampling diverse regions, exploratory analysis, training data |
| 146 | - Key parameters: `n_tiles`, `seed` for reprod |