# Stitching images#

In this notebook we demonstrate how images can be stitched. Stitching is commonly necessary when images are acquired in [overlapping] tiles. Combining these tiles vertically or horizontally is called stitching.

from skimage.io import imread
import stackview
import matplotlib.pyplot as plt
import pyclesperanto_prototype as cle
import numpy as np


After acquiring the images, you typically know how much the images overlap. Here we assume that the overlap is given as a number of pixels. If you only know the percentage of overlap, you need to compute this number by multiplying the percentage with the image size (width or height).

num_overlap_pixels = 40

image1 = imread("../../data/blobs_stitching1_overlap40.tif")
fig, ax = plt.subplots(1, 2)
stackview.imshow(image1, plot=ax[0])
stackview.imshow(image2, plot=ax[1])


Note that these images contain some overlapping pixels. If we just assemble the images side-by-side, we see these pixels twice.

cle.combine_horizontally(image1, image2)

cle._ image
 shape (254, 296) dtype float32 size 293.7 kB min 8.0 max 248.0

Pyclesperanto allows stitching the images with overlap. The overlapping region will be blended linearly.

cle.stitch_horizontally_linear_blending(image1, image2, num_pixels_overlap=num_overlap_pixels)

cle._ image
 shape (254, 256) dtype float32 size 254.0 kB min 8.0 max 248.00002

To demonstrate how the linear blending works, we create two synthetic images. First we combine them as shown above.

syn_image1 = np.ones((100, 100))
syn_image2 = np.ones((100, 100)) * 2

cle.combine_horizontally(syn_image1, syn_image2)

cle._ image
 shape (100, 200) dtype float32 size 78.1 kB min 1.0 max 2.0

Then we stitch the images using linear blending.

cle.stitch_horizontally_linear_blending(syn_image1, syn_image2, num_pixels_overlap=30)

cle._ image
 shape (100, 170) dtype float32 size 66.4 kB min 1.0 max 2.0

Note: Computing the registration parameters for optimal overlapping the images is currently not supported in pyclesperanto.