Currently i am developing a software for my microscopes. Therefore i was on the search for already existing focus stacking and image stitching API's (application programming interface). Luckily the python opencv library has some image stitching functions.
Today i found a nice python script that lets one do image stitching of microscope silde images.
Code: Select all
# import the necessary packages
from imutils import paths
import numpy as np
import argparse
import imutils
import cv2
import time
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--images", type=str, required=True,
help="path to input directory of images to stitch")
ap.add_argument("-o", "--output", type=str, required=True,
help="path to the output image")
ap.add_argument("-c", "--crop", type=int, default=0,
help="whether to crop out largest rectangular region")
args = vars(ap.parse_args())
# grab the paths to the input images and initialize our images list
print("[INFO] loading images...")
imagePaths = sorted(list(paths.list_images(args["images"])))
images = []
# loop over the image paths, load each one, and add them to our
# images to stich list
for imagePath in imagePaths:
image = cv2.imread(imagePath)
images.append(image)
# initialize OpenCV's image sticher object and then perform the image
# stitching
print("[INFO] stitching images...")
stitcher = cv2.createStitcher(
cv2.Stitcher_SCANS
) if imutils.is_cv3() else cv2.Stitcher_create(
cv2.Stitcher_SCANS
)
# Reduce cropping by setting the compositing resolution to 'full resolution'
stitcher.setCompositingResol(-1) # This sets stitching at full available resolution
(status, stitched) = stitcher.stitch(images)
# write the output stitched image to disk
cv2.imwrite(str(time.time())+args["output"], stitched)
# display the output stitched image to our screen
cv2.imshow("Stitched", stitched)
cv2.waitKey(0)
# usage:
# python3 image_stitch.py -i public/media/tmp_image_stitching_images/ -o python_image_stitch.jpg
Code: Select all
enblend: warning: some images are redundant and will not be blended
enblend: note: usually this means that at least one of the images
enblend: note: does not belong to the set
enblend: info: loading next image: public/media/tmp_image_stitching_images/2024-05-13_00:08:33.png 1/1
enblend: warning: some images are redundant and will not be blended
enblend: note: usually this means that at least one of the images
enblend: note: does not belong to the set
enblend: info: loading next image: public/media/tmp_image_stitching_images/2024-05-13_00:08:34.png 1/1
enblend: warning: some images are redundant and will not be blended
enblend: note: usually this means that at least one of the images
enblend: note: does not belong to the set
enblend: info: writing final output
If anyone knows other API's i am glad if you are willing to share information about them, so maybe i can include it in my software
Anyways, here is the result: