Tissue content tile selection

From wenmin-wu/ds-skills · View interactive version

Tissue content tile selection is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Selects the top-N most informative tiles from a gigapixel whole slide image by ranking on pixel intensity sum, keeping tiles with the most tissue content.

How to use this skill

Skill file

Tissue-Content Tile Selection

Overview

Whole slide images (WSIs) in histopathology are gigapixel-scale — too large for direct model input. Most of the image is white background. This technique splits the image into a grid of fixed-size tiles using numpy reshape+transpose (no Python loops), ranks tiles by pixel sum (darkest = most tissue on white-background H&E slides), and selects the top-N tiles. This is the standard first step in WSI classification pipelines, reducing a 50,000x50,000 image to 16-36 informative patches.

Quick Start

import numpy as np
import skimage.io

def extract_top_tiles(img, tile_size=256, n_tiles=36):
    """Extract top-N tiles by tissue content from WSI."""
    h, w, c = img.shape
    pad_h = (tile_size - h % tile_size) % tile_size
    pad_w = (tile_size - w % tile_size) % tile_size
    img = np.pad(img, [[pad_h//2, pad_h-pad_h//2],
                        [pad_w//2, pad_w-pad_w//2], [0,0]],
                 constant_values=255)
    # Reshape into grid of tiles (no loops)
    tiles = img.reshape(img.shape[0]//tile_size, tile_size,
                        img.shape[1]//tile_size, tile_size, 3)
    tiles = tiles.transpose(0, 2, 1, 3, 4).reshape(-1, tile_size, tile_size, 3)
    # Rank by pixel sum (lowest = darkest = most tissue)
    sums = tiles.reshape(tiles.shape[0], -1).sum(axis=1)
    idxs = np.argsort(sums)[:n_tiles]
    return tiles[idxs]

wsi = skimage.io.MultiImage('slide.tiff')[1]  # median resolution
tiles = extract_top_tiles(wsi, tile_size=256, n_tiles=36)

Workflow

  1. Load WSI at appropriate pyramid level (level 1 or 2)
  2. Pad image so dimensions are divisible by tile size
  3. Reshape into grid using numpy reshape+transpose
  4. Sort tiles by pixel sum ascending (darkest first)
  5. Select top-N tiles as model input

Key Decisions

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.