Accurate integration of gene expression data and staining microscopy images via spatial alignment is important for analyzing spatial transcriptomics data. We introduce STReg, an image registration method and software to align high-resolution sequencing-based spatial transcriptomics data. This method enables both intensity-based and trackline-based registration, is computationally efficient, and easy to use.

Ensure that you have Conda installed on your system.
# 1. Clone the repository
git clone https://github.com/BMCV/STReg
# 2. Create a conda environment
conda create --name STReg python=3.14.6
# 3. Activate the newly created conda environment
conda activate STReg
# 4. Move into the downloaded project directory
cd STReg
# 5. Install dependencies
pip install -r requirements.txtSTReg uses a gene expression matrix (.csv) and the corresponding nuclei staining image (.tif) as input. The staining image should be a single-channel grayscale image of size (H, W), and the gene expression data should have the following format:
| Gene | x | y | UMI-Count |
|---|---|---|---|
| mt-Nd6 | 3417 | 6648 | 1 |
| Actb | 24391 | 12859 | 1 |
| Ank | 7379 | 6343 | 2 |
| ... | ... | ... | ... |
To verify your installation, a demo dataset is provided in test_data/. You can run STReg using the command line or via the Graphical User Interface (GUI).
You can run STReg from the terminal using:
python run.py \
--gene_image test_data/gene_matrix_test.csv.gz \
--stain_image test_data/staining_image_test.tif \
--out_dir test_data/results/ \
--max_size 512 \
--loss MSE \
--lr 0.01 \
--niter 1000 \
--optimizer Adam \
--flip_h 0 \
--flip_v 0 \
--rot90 0To launch the GUI, run the following command:
python run_GUI.pyThe following window will appear, where you can select the paths to the files for the gene expression matrix and the corresponding staining image. Pressing Load example image will automatically insert the directories for the test images.
Once the files are selected, press Open to proceed to the main interface shown below.
Notes:
- The brightness and contrast adjustments on the right-hand-side affect only the display and do not change the intensity values of the gene image and the staining image.
- If something does not work, press
Reset allto restart the registration process.
| Parameter/Action | GUI Control | CLI Flag & Value | Description |
|---|---|---|---|
| Image Size | Resize images |
--max_size 512 |
Size at which the images are displayed. Accepts integers |
| Translation | Click & Drag Mouse |
- | Shifts the moving image. |
| Scaling | Mouse Wheel |
- | Scales the moving image. |
| Flip Horizontal | Flip Horizontal |
--flip_h {0|1} |
Flips the moving image left-to-right. |
| Flip Vertical | Flip Vertical |
--flip_v {0|1} |
Flips the moving image top-to-bottom. |
| 90° Rotation | Rotate 90 |
--rot90 {0|1|2|3} |
Rotates the moving image by 90° clockwise. |
| Center-of-mass alignment | Center-of-mass alignment |
- | If no translation or scaling was performed, the center-of-masses of image intensities will be aligned prior to intensity-based registration. |
| Optimization Algorithm | Optimizer |
--optimizer {Adam|SGD} |
Algorithm used for gradient-based optimization. Options: Adam, SGD. Default: Adam. |
| Loss Function | Loss |
--loss {MSE|NCC|NMI} |
Intensity-based similarity metric to be optimized. Options: MSE, NCC, NMI. Default: MSE. |
| Learning Rate | Learning Rate |
--lr 0.01 |
Controls the step size to find the optimal transformation parameters. Accepts float 0.01. |
| Iterations | Iterations |
--niter 1000 |
Number of steps to find the optimal transformation parameters. Accepts integers 1000. |
Tips:
- For best results, ensure both images have the same orientation by using horizontal and vertical flipping.
- If the intensity-based registration method does not work well, we recommend decreasing the learning rate (e.g., 0.001).
STReg generates the following files into the specified output directory:
gene_image.tif- The gene image which was created from the gene expression matrixstaining_image_registered.tif- The registered nuclei staining imageparameters.npy- The transformation parameters
If you would like to apply the estimated transformation to other images (e.g., different channel), use python streg/apply_transformation. For example:
python streg/apply_transformation.py \
--image test_data/staining_image_test.tif \
--parameters test_data/results/parameters.npy \
--out test_data/results/staining_image_ch2.tif
The scripts/ directory contains bash scripts to reproduce the results in the paper. Download the required datasets from the links below, place them in the specified folders, and run the script.
- Download Link: https://db.cngb.org/stomics/mosta/download/
- Files to download:
Bin1 matrix/E16.5_E2S4_GEM_bin1.tsv.gzImage of nuclei acid stainin/E16.5_E2S6.tif
- Save to:
datasets/mouse_embryo/ - Run:
bash scripts/mouse_embryo.sh
- Download Link: https://db.cngb.org/stomics/artista/download/
- Files to download:
Bin1 file/10DPI_2.gem.gzand rename the file to10DPI_2.tsv.gzImage of nuclei acid staining/10DPI_2.tif
- Save to:
datasets/axolotl_brain/ - Run:
bash scripts/axolotl_brain.sh
- Download Link: https://github.com/STOmics/STCellbin/
- Files to download:
C01344C4.gem.gzand rename the file toC01344C4.tsv.gzC01344C4_staining_image.tarwhich contains the staining imageC01344C4/C01344C4.tif
- Save to:
datasets/mouse_liver/ - Run:
bash scripts/mouse_liver.sh
This project is licensed under the MIT License - see the LICENSE file for details.
Chen, A. et al.: Spatiotemporal transcriptomic atlas of mouse organogenesis using DNA nanoball-patterned arrays. Cell 185.10 (2022)
Wei, X., et al.: Single-cell Stereo-seq reveals induced progenitor cells involved in axolotl brain regeneration. Science 377, 9444 (2022)
Zhang, B., et al.: Generating single-cell gene expression profiles for high-resolution spatial transcriptomics based on cell boundary images. GigaByte 2024 (2024)

