I have a homework assignment to stitch images together. It's quite easy to stitch 2 or 3 small images together, but the requirement of the assignment is to stitch 18 images. The problem arises when I try to stitch 18 images together using the stitching library of OpenCV. The resulting large image is missing some parts, and I found that some images cannot be stitched together using this method (even though they are all the same size). Please help me overcome this issue, or if you have any better image stitching techniques in OpenCV for stitching multiple images, please introduce them to me. Thank you!
import cv2
import os
folder_path = "C:/Users/HP ZBOOK 15 G3/Desktop/OPENCV/lab5/Z2"
size = 18
images = []
stitched_images = []
stitcher = cv2.Stitcher_create()
for filename in os.listdir(folder_path):
if filename.endswith(".jpg"):
image_path = os.path.join(folder_path, filename)
image = cv2.imread(image_path)
if image is not None:
images.append(image)
if len(images) >= size:
status, stitched_image = stitcher.stitch(images)
if status == cv2.Stitcher_OK:
cv2.imshow("Anh ghep", stitched_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
stitched_images.append(stitched_image)
images.clear()
if len(images) > 0:
status, stitched_image = stitcher.stitch(images)
if status == cv2.Stitcher_OK:
cv2.imshow("Anh ghep", stitched_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
stitched_images.append(stitched_image)
if len(stitched_images) > 0:
status, final_stitched_image = stitcher.stitch(stitched_images)
if status == cv2.Stitcher_OK:
cv2.imshow('img', final_stitched_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
else:
print("Error stitching")
else:
print("No images to stitch.")
I found that some images cannot be stitched together using this method (even though they are all the same size).