CellCounter — Visium Cell Counting for Spatial scDRS
Tags: Computer Vision · Spatial Transcriptomics · Python · StarDist · OpenCV · AnnData · BayesSpace · scDRS
CellCounter is a Python package that estimates the number of cells per spot in 10x Visium spatial transcriptomics data using the paired histology image. It runs a nuclei instance-segmentation pipeline built on StarDist and OpenCV, with additional preprocessing and analysis utilities.
Background
10x Visium is a spatial transcriptomics technique that measures gene expression across a tissue section while preserving spatial location. Expression is measured on a grid of spots overlaid on the tissue, allowing analysis of what genes are expressed where.
Each Visium spot typically captures RNA from multiple cells, so the data is observed at the spot level rather than at true single-cell resolution.
scDRS
scDRS (single-cell Disease Relevance Score) is a method developed for single-cell RNA-seq data. Given a trait- or disease-associated gene set (often derived from GWAS), scDRS assigns a score to each cell indicating how strongly that cell’s expression is associated with the trait.
This makes it possible to identify which cells or cell states are most associated with a given disease or phenotype.
Extending scDRS to spatial transcriptomics
Extending scDRS to Visium data allows trait association to be studied in spatial context. Instead of only identifying which cells are associated with a disease, spatial data makes it possible to see where those cells are located within the tissue.
This enables analyses such as identifying spatial neighborhoods enriched for disease-associated cells, detecting spatial gradients, and understanding how trait-associated cells are organized relative to tissue structure.
Approach
- Use histology images to estimate the number of cells per Visium spot (this package).
- Use BayesSpace to subdivide each spot into that many subspots and infer subspot-level expression. BayesSpace is a Bayesian method commonly used in Visium analysis for spatial clustering and resolution enhancement.
- Treat each subspot as an approximate single-cell unit and apply scDRS-style scoring to associate traits or diseases with spatially localized cells.
What CellCounter does
- Loads Visium histology images from AnnData, image paths, or large TIFF files into numpy arrays.
- Crops regions of interest and extracts patches to support scalable processing of large tissue slides.
- Applies preprocessing operations including CLAHE, unsharp masking, Laplacian filtering, histogram equalization, and normalization.
- Runs StarDist-based nuclei segmentation and extracts per-nucleus features such as centroid coordinates, area, intensity statistics, and bounding boxes.
- Aggregates nuclei detections into predicted cells-per-spot using known Visium spot coordinates.
- Compares predictions against manual annotations using spatial overlap and Intersection-over-Union (IoU)–based matching.
Applications
- Mapping disease- or trait-associated cells across tissue sections.
- Identifying spatial neighborhoods enriched for trait relevance.
- Detecting spatial gradients and boundaries in disease association.
- Enabling scDRS-style analyses on spatial transcriptomics data.
Repository Structure
utils.py— histology image I/O and AnnData integrationextraction.py— cropping and patch extraction utilitiespreprocessing.py— image preprocessing transformationsstardist_model.py— nuclei segmentation and feature extractionanalysis.py— spot-level aggregation and annotation comparison