Leichti
Leichti

Reputation: 93

Image stitching with opencv-python

So I've been trying to stitch some images together to create one big panorama but the Stitcher from OpenCV doesn't want to do so. There's enough overlap on the images so the Stitcher should be able to stitch them together but it can't. I also tried to downscale the images to help the program to identify matches, because on the original photos, the edges were a little bit blurred and that surely didn't make it easier to stitch them together.

I also tried adjusting the code a couple of times but that also didn't help. Only a couple of images can be stitched together but the majority of them doesn't work. How can I make it work?

this the link for two of the many images i've been trying to stitch together

https://imgur.com/a/fxoQOTR

__

This is the code:

import cv2

img1 = cv2.imread('eleImages/Electron Image 60.jpeg')
img2 = cv2.imread('eleImages/Electron Image 61.jpeg')

gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)

orb = cv2.ORB_create()
kp1, des1 = orb.detectAndCompute(gray1, None)
kp2, des2 = orb.detectAndCompute(gray2, None)

if des1 is not None and des2 is not None and len(des1) > 0 and len(des2) > 0:
    bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
    matches = bf.match(des1, des2)
    matches = sorted(matches, key=lambda x: x.distance)
    matches_Image = cv2.drawMatches(img1, kp1, img2, kp2, matches, None, flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)
    cv2.imwrite('PanoramasAndMatches/Matches.jpeg', matches_Image)
    imgs = [img1, img2]

    stitcher = cv2.Stitcher_create()

    status, stitched_img = stitcher.stitch(imgs)

    if status == cv2.Stitcher_OK:
        cv2.imwrite('PanoramasAndMatches/Stitched_Image.jpeg', stitched_img)
    else:
        print("Stitching failed.")
else:
    print("No keypoints found in one or both images.")
    ```

Upvotes: 1

Views: 212

Answers (0)

Related Questions