Skip to main content

Python implementation of ImageJ template matching and stack alignment

Project description

TemplateMatchingPy

Python License OpenCV Coverage

TemplateMatchingPy is a Python implementation of the popular ImageJ/Fiji template matching and stack alignment plugins originally developed by Qingzong Tseng, providing a programmatic, GUI-free interface for template matching and image stack alignment with sub-pixel precision designed specifically for microscopy workflows. Key features include multiple OpenCV correlation methods (TM_SQDIFF, TM_CCORR, TM_CCOEFF variants), sub-pixel precision through Gaussian peak fitting for enhanced alignment accuracy, and flexible configuration options including customizable search areas, interpolation methods, and precision settings.

This registration package is limited to Translation operations (Movements in the X-Y axis), which makes it suitable for registering time-lapses where the main image is drifting. It helps stabilising the image across time-frames. Below you can see a demostration of the capabilities of this package:

Template Matching Alignment Demonstration

*Before and after alignment comparison showing drift correction in a microscopy time-lapse sequence. The left panel shows the original drifting images, while the right panel demonstrates the stabilized result after template matching alignment.*

Installation

pip install git+https://github.com/yourusername/TemplateMatchingPy.git

Or you can also build form source:

git clone https://github.com/yourusername/TemplateMatchingPy.git
cd TemplateMatchingPy
pip install -e .

Dependencies

  • Python ≥ 3.7
  • NumPy ≥ 1.19.0
  • OpenCV ≥ 4.5.0

Basic Usage

import numpy as np
from templatematchingpy import (
    register_stack,
    AlignmentConfig,
    create_test_image_stack,
    calculate_alignment_quality,
)

# Create test image stack (or load your own)
image_stack, true_displacements = create_test_image_stack(
    n_slices=8, height=256, width=256, translation_range=5.0, noise_level=0.1
)

# Define template region (x, y, width, height)
bbox = (100, 100, 64, 64)

# Configure alignment
config = AlignmentConfig(method=5, subpixel=True)

# Perform alignment
aligned_stack, displacements = register_stack(
    image_stack=image_stack,
    bbox=bbox,
    reference_slice=0,
    config=config
)

print(f"Aligned {len(displacements)} slices")
print(f"Mean displacement: {np.mean([np.sqrt(dx**2 + dy**2) for dx, dy in displacements]):.2f} pixels")

Working with Files

import cv2
import numpy as np
from templatematchingpy import register_stack, AlignmentConfig

# Load multi-page TIFF stack
ret, images = cv2.imreadmulti("./examples/data/example_image_stack.tiff", flags=cv2.IMREAD_GRAYSCALE)

if not ret:
    raise ValueError("Could not load TIFF stack")

# Convert list of images to 3D numpy array [frames, height, width]
image_stack = np.array(images, dtype=np.float32)

# Normalize to [0, 1] range if needed
if image_stack.max() > 1.0:
    image_stack = image_stack / 255.0

print(f"Loaded stack with shape: {image_stack.shape}")

# Get image dimensions and calculate centered bbox
height, width = image_stack.shape[1], image_stack.shape[2]
box_width = 1200
box_height = 1200  
x = (width - box_width) // 2
y = (height - box_height) // 2

# Define template region (x, y, width, height)
bbox = (x, y, box_width, box_height)

# Configure and perform alignment
config = AlignmentConfig(method=5, subpixel=True)
aligned_stack, displacements = register_stack(
    image_stack, bbox=bbox, reference_slice=0, config=config
)

# Save aligned stack as float32 multi-page TIFF
# OpenCV requires list of individual frames for multi-page TIFF
aligned_frames = [frame.astype(np.float32) for frame in aligned_stack]
cv2.imwritemulti("aligned_stack.tiff", aligned_frames)

print(f"Alignment completed with {len(displacements)} slices")
print(f"Displacements: {displacements}")

Configuration Options

AlignmentConfig Parameters

from templatematchingpy import AlignmentConfig
import cv2

config = AlignmentConfig(
    method=5,                    # Template matching method (0-5)
    search_area=0,               # Search area in pixels (0 = full image)  
    subpixel=True,               # Enable sub-pixel precision
    interpolation=cv2.INTER_LINEAR  # Interpolation method
)

Template Matching Methods

Method OpenCV Constant Description Best For
0 TM_SQDIFF Squared Difference High contrast templates
1 TM_SQDIFF_NORMED Normalized Squared Difference Robust matching
2 TM_CCORR Cross Correlation Bright templates
3 TM_CCORR_NORMED Normalized Cross Correlation Illumination invariant
4 TM_CCOEFF Correlation Coefficient General purpose
5 TM_CCOEFF_NORMED Normalized Correlation Coefficient Recommended

License

This project is licensed under the European Union Public Licence v. 1.2 (EUPL-1.2) - see the LICENSE file for details.

Acknowledgments

  • Qingzong Tseng: Original ImageJ Template Matching plugin author
  • Laurent Thomas & Jochen Gehrig: Multi-Template Matching ImageJ plugin
  • ImageJ/Fiji Community: Foundational image analysis tools
  • OpenCV Contributors: Computer vision library

Citation

If you use TemplateMatchingPy in your research, please cite:

@software{templatematchingpy,
  title={TemplateMatchingPy: Python implementation of ImageJ template matching and stack alignment},
  author={TemplateMatchingPy Santiago Cano Muniz},
  year={2024},
  url={https://github.com/phisanti/TemplateMatchingPy}
}

This implementation is based on the template matching methods described in the original research:

  1. Thomas, L. & Gehrig, J. Multi-template matching: a versatile tool for object-localization in microscopy images. BMC Bioinformatics 21, 44 (2020). https://doi.org/10.1186/s12859-020-3363-7
  2. Tseng, Q. et al. A new micropatterning method of soft substrates reveals that different tumorigenic signals can promote or reduce cell contraction levels. Lab on a Chip 11, 2231 (2011).
  3. Tseng, Q. et al. Spatial Organization of the Extracellular Matrix Regulates Cell–cell Junction Positioning. PNAS (2012). doi:10.1073/pnas.1106377109
  4. Tseng, Qingzong. 2011. "Study of multicellular architecture with controlled microenvironment". Ph.D. dissertation, Université de Grenoble. http://tel.archives-ouvertes.fr/tel-00622264

Related Projects


TemplateMatchingPy - Bringing ImageJ template matching to Python workflows 🐍🔬

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

templatematchingpy-1.0.2.tar.gz (27.9 MB view details)

Uploaded Source

Built Distribution

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

templatematchingpy-1.0.2-py3-none-any.whl (19.8 kB view details)

Uploaded Python 3

File details

Details for the file templatematchingpy-1.0.2.tar.gz.

File metadata

  • Download URL: templatematchingpy-1.0.2.tar.gz
  • Upload date:
  • Size: 27.9 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for templatematchingpy-1.0.2.tar.gz
Algorithm Hash digest
SHA256 9e3eba44a054f56a7169134f9654cd901378825f0684fd8c8e8c6af11fa49469
MD5 24ac0a559bd67ba2a5789c0f34c65577
BLAKE2b-256 c6881b75f24ddb077297c8d776cbeff7d1981a5115a8b3548d699c5a57fe65f7

See more details on using hashes here.

Provenance

The following attestation bundles were made for templatematchingpy-1.0.2.tar.gz:

Publisher: python-publish.yml on phisanti/TemplateMatchingPy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file templatematchingpy-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for templatematchingpy-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c8c6455e742d97fb1e1fdddf8bfeebeead276c0fa5ff76de14b70dd4e1bfa057
MD5 23cde78ffa071838f75cbe5f68854969
BLAKE2b-256 7b8ac2c19d4a59c6e7fe1acc825d18e5c8d620a8313d065700029a3e9c8020bf

See more details on using hashes here.

Provenance

The following attestation bundles were made for templatematchingpy-1.0.2-py3-none-any.whl:

Publisher: python-publish.yml on phisanti/TemplateMatchingPy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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