[Python] Stitching computer images by feature without warping (no camera images)

Discussão em 'Python' iniciado por Stack, Abril 21, 2025.

  1. Stack

    Stack Membro Participativo

    I've did quite a search about image stitching on python and most are for panoramic images, warping and rotating the images to combine them into one.

    What I'm trying to do is using computer images, so they are digital and can be template matched without a problem, it will always be 2D without need of warping.

    Basically here I have pieces of a map that is zoomed in and I want to make a massive image of this small pictures, here we have all the images used: https://i.sstatic.net/Z20OL.jpg

    import os
    import numpy as np
    import cv2

    def stitchImagesWithoutWarp(img1, img2):
    orb = cv2.ORB_create()
    kp1, des1 = orb.detectAndCompute(img1,None)
    kp2, des2 = orb.detectAndCompute(img2,None)
    bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
    matches = bf.match(des1,des2)
    matches = sorted(matches, key = lambda x:x.distance)
    good_matches = matches[:10]
    src_pts = np.float32([kp1[m.queryIdx].pt for m in good_matches]).reshape(-1,1,2)
    dst_pts = np.float32([kp2[m.trainIdx].pt for m in good_matches]).reshape(-1,1,2)

    start = (abs(int(dst_pts[0][0][0]-src_pts[0][0][0])), abs(int(dst_pts[0][0][1]-src_pts[0][0][1])))
    h1, w1 = img1.shape[:2]
    h2, w2 = img2.shape[:2]
    vis = np.zeros((start[1]+h1,start[0]+w1,3), np.uint8)
    vis[start[1]:start[1]+h1, start[0]:start[0]+w1, :3] = img1
    vis[:h2, :w2, :3] = img2
    return vis

    imgList = []
    for it in os.scandir("images"):
    imgList.append(cv2.imread(it.path))

    vis = stitchImagesWithoutWarp(imgList[0],imgList[1])
    for index in range(2,len(imgList)):
    cv2.imshow("result", vis)
    cv2.waitKey()
    vis = stitchImagesWithoutWarp(vis,imgList[index])


    By running this code I can successfully stitch the first four images together, such as this: [​IMG]

    But once I stitch the fifth image it seems to have wrong match and incorrectly, but I always get the best match by distance on NORM_HAMMING, this is the result: [​IMG]

    The thing is, this is the first image, in this order, that the best match point (var start) is negative in the x axis, here is the matching points in the imgur order:

    1. (7, 422)
    2. (786, 54)
    3. (394, 462)
    4. (-350, 383)

    I attempted switching the top image, doing specific code for negative match but I've believe I was deviating the performance.

    Also noting from the docs the first image should be the query and the second supposed to be the target, but I couldn't get it to work by inverting the vis variable in function param.

    Continue reading...

Compartilhe esta Página