Skip to main content

Stitch microscopy images together into large scans.

Project description

A program for automatically tiling microscope images

OpenFlexure Stitching is a Python package for tiling and stitching 2D microscope scans into panoramic images. This program has relatively few dependencies and runs quickly with low memory requirements.

The primary target for OpenFlexure Stitching is images collected on the OpenFlexure Microscope. It automatically reads OpenFlexure metadata from the image's exif data. If this exif data is not available it will try to infer the stage positions from the file name.

An example dataset can be downloaded from:
https://doi.org/10.5281/zenodo.13768403 [[TOC]]

Installation

You can install OpenFlexure Stitching with

pip install openflexure-stitching[libvips]

This package requires Python 3.10 or later. Other dependencies should be declared in pyproject.toml (and automatically installed by pip).

Note: If you get an error that pyvips-binary could not be installed this means it is not packaged for your system. Instead you can run pip install openflexure-stitching to install OpenFlexure Stitching. But you will then need to manually install libvips onto your system and add it to the PATH.

Running the program

This program is designed to work without user input. If you have a collection of images from an OpenFlexure Microscope scan, then from the terminal, running

openflexure-stitch <path to folder of images>

will tile the images together.

Consulting openflexure-stitch --help will list available settings and options.

If the images are poorly stitched the first setting to try adjusting is --high_pass_sigma. If that doesn't help you can try setting thresholding to manual.

Running on an OpenFlexure Microscope

OpenFlexure Stitching has been written to allow live stitching of microscope images in the upcoming v3 release of the OpenFlexure Microscope Server. For the current v2 OpenFlexure Microscope Server, the operating system is too old to support the correct version of Python. Until v3 is stable and released (expected in late 2025), you will need to run OpenFlexure Stitching on a different computer.

Use as a library

To use openflexure-stitching as a python-library rather than a command line program see our API documentation.

How it works

Given a folder path, OpenFlexure Stitching will use all images in the folder, ignoring any phrases contained in stitching outputs:

['stitched', 'comparison', 'Fused', 'stage', 'stitching', 'preview'] are all ignored.

After this the the program performs:

  • Offset estimation
  • Thresholding and placement optimisation
  • Stitching images

as explained below.

Offset estimation

For each image the stage position will be loaded and converted to image coordinates. Overlapping images are identified as images where the overlap is above the --minimum_overlap parameter. Overlapping images are then compared by cross-correlation to calculate an improved estimate of displacement.

{width=50%}
A correlation map with a single, narrow peak. Useful for tiling.

{width=50%}
A correlation map with no clear peak or preference for offset. This cross-correlation should removed by thresholding (see below).

Cross-correlations are calculated from Fourier Transforms, this technically returns a circular cross-correlation.The circular cross-correlation introduces an ambiguity, this can be fixed by padding the images (default behaviour). Use --no-pad to stop the padding, this will reduce the memory requirements of this stage by a factor of 4, but introduces ambiguity. OpenFlexure Stitching will attempt to break this ambiguity, but this isn't 100% reliable.

One of the key advantages of OpenFlexure Stitching is that the correlation values are cached to disk after each run. If the settings are unchanged, the time consuming cross-correlations do not need to be repeated. As new images are added to the folder, only new offsets are calculated. This means live-updating scans can be tiled in near real-time.

Thresholding and placement optimisation

From the image offsets calculated from cross-correlation, a least squares optimisation of absolute positions is performed. The correlation peaks between some pairs of images may be incorrect, and need to be ignored. For this reason, the fit can be performed with thresholds that ignore offsets which differ too much from the stage-estimated position or where the peak quality in the cross-correlation is too low.

As standard thresholding is automatic, it is adjusted to optimise the image positions. The RMS error of the least squares optimisation is used as a quality metric for thresholding. Multiple thresholds for both position discrepancy and peak quality are tested to create the best estimate of image position.

If manual thresholding is used the program will pause once all offsets have been estimated. Then, a graph will be saved to the file stitching_correlations.png showing the discrepancy between the stage-estimated offset and the correlation-estimated offset plotted against the quality of the correlation peak. By reading this graph the user can manually set thresholds.

{width=75%}
A scatter plot of the quality of each cross-correlation between two images. In manual mode the discrepancy and peak quality can be set thresholds to exclude poorly cross-correlated image pairs from the final optimisation calculation. Colours are for clarity and have no data significance.

Stitching images

Finally the image is stitched together. By default a jpeg image is produced. For large scans the resulting images can be multiple giga-pixel, and very hard to open. Options also exist to create dzi (deep zoom images) and Pyramidal TIFFs. These both reduce the memory needed to open the image, but they require special software for viewing. When opened in microscopy programs such as Qupath, important metadata such as the physical pixel size will be used for labelling the image.

OpenFlexure Stitching also produces a OFMTileConfig.txt file, which can be read by Fiji Stitching, taking advantage of their range of blending and averaging. To avoid producing artefacts in scans, the stitching in OpenFlexure Stitching does no averaging between images.

Development

Clone this repository by navigating in your terminal to your required containing folder. Run

git clone https://gitlab.com/openflexure/openflexure-stitching

to download the files locally.

Create and activate a virtual environment:

cd openflexure-stitching
python -m venv .venv
.\.venv\Scripts\activate

Upgrade pip if needed, so you can use the pyproject.toml based setup for an editable install

python -m pip install --upgrade pip

Install the necessary libraries using

pip install -e .[libvips]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

openflexure_stitching-0.2.3.tar.gz (59.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

openflexure_stitching-0.2.3-py3-none-any.whl (59.2 kB view details)

Uploaded Python 3

File details

Details for the file openflexure_stitching-0.2.3.tar.gz.

File metadata

  • Download URL: openflexure_stitching-0.2.3.tar.gz
  • Upload date:
  • Size: 59.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for openflexure_stitching-0.2.3.tar.gz
Algorithm Hash digest
SHA256 b1b2fb8b8c9f0dc29dca5d52d15aacad452221857974ec64be2d4f48fa799b52
MD5 9aabc64973d90a622e777088f1bb67e4
BLAKE2b-256 3f7d1deb7377751e58e7a77b406b98b423718d7fd765356d1cfe6c861f89e32c

See more details on using hashes here.

File details

Details for the file openflexure_stitching-0.2.3-py3-none-any.whl.

File metadata

File hashes

Hashes for openflexure_stitching-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 1b8a9d257a3e80932a5d0fbf63588a2130f1908b7a854e4413e129bf7c4c978d
MD5 633fa9406922e0ea505e4ab8baf4a315
BLAKE2b-256 b8cd630c645a37c9870160606f7f26221c213cd902fa13721f4bcff851e7956d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page