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")
image2 = imread("../../data/blobs_stitching2_overlap40.tif")
fig, ax = plt.subplots(1, 2)
stackview.imshow(image1, plot=ax[0])
stackview.imshow(image2, plot=ax[1])
../_images/591fe8a49e1f533b2270ee9cd6922b3051078502a7619f6d635dba6799504234.png

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)
dtypefloat32
size293.7 kB
min8.0
max248.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)
dtypefloat32
size254.0 kB
min8.0
max248.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)
dtypefloat32
size78.1 kB
min1.0
max2.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)
dtypefloat32
size66.4 kB
min1.0
max2.0

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