Skip to content

Instantly share code, notes, and snippets.

@99991
Created July 12, 2023 07:39
Show Gist options
  • Save 99991/23f6b994382950665f161f8af13b0ab4 to your computer and use it in GitHub Desktop.
Save 99991/23f6b994382950665f161f8af13b0ab4 to your computer and use it in GitHub Desktop.
Example on how to use OpenCV's image alignment/image registration module https://docs.opencv.org/4.8.0/db/d61/group__reg.html
import cv2
import numpy as np
import urllib.request
import os
# Download Tsukuba stereo test images
urls = [
"https://vision.middlebury.edu/stereo/data/scenes2001/data/tsukuba/scene1.row3.col1.ppm",
"https://vision.middlebury.edu/stereo/data/scenes2001/data/tsukuba/scene1.row3.col2.ppm",
]
for url in urls:
filename = url.split("/")[-1]
if not os.path.isfile(filename):
print("Download", url, "to", filename)
urllib.request.urlretrieve(url, filename)
img1 = cv2.imread("scene1.row3.col1.ppm")
img2 = cv2.imread("scene1.row3.col2.ppm")
# Bug: images must be float for correct registration
# https://github.com/opencv/opencv/issues/12210
img1 = img1.astype(np.float32) / 255.0
img2 = img2.astype(np.float32) / 255.0
# Several possible transformation methods
# Translation only
mapper = cv2.reg.MapperGradShift()
# Translation and rotation
mapper = cv2.reg.MapperGradEuclid()
# Translation, rotation and scaling
mapper = cv2.reg.MapperGradSimilar()
# Affine transform (Translation, rotation, scaling, shearing)
mapper = cv2.reg.MapperGradAffine()
# Projective transform
mapper = cv2.reg.MapperGradProj()
# Mapper optimization does not work well by default.
# Need to put mapper into pyramid mapper for hierarchical optimization.
# Bug: mapper must be stored as variable to avoid segfault
# https://github.com/opencv/opencv_contrib/issues/1935
mapper_pyramid = cv2.reg.MapperPyramid(mapper)
aligned = mapper_pyramid.calculate(img1, img2).inverseWarp(img2)
# Look at aligned images
while True:
cv2.imshow("image", img1)
key = cv2.waitKey(100)
if key == ord('q'): break
cv2.imshow("image", aligned)
key = cv2.waitKey(100)
if key == ord('q'): break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment