Panoramic Stitching is not working properly

347 Views Asked by At

I am trying to stitch two images together in which both images have different angles. Shown below: enter image description here enter image description here

As you can see, there are some overlaps. However, when I stitch them, I get the following: enter image description here

I use the following algorithm:

  • SIFT
  • Lowe Ratio test
  • Homography
  • FlannBasedMatcher (RANSAC)
  • Wrap Perspective

Lower Ratio Test

        if len(item) < 2:
            continue
        (left, right) = item
        if left.distance < 0.78 * right.distance:
            good.append(left)

Homography

# img_feature is an object containing the keypoints where 1 refer to image 1.

src_pts = np.float32([img_feature1.key_points[m.queryIdx].pt for m in good]).reshape(-1, 1, 2) 
    dst_pts = np.float32([img_feature2.key_points[m.trainIdx].pt for m in good]).reshape(-1, 1, 2)

    # Defining the Homography parameters
    homog_method = cv2.RANSAC
    homog_reprojthres = 5
    homog_mask = None
    homog_max_iter = 2000
    homog_confidence = 0.995

    homog_matrix, mask = cv2.findHomography(
        dst_pts,
        src_pts,
        homog_method,
        homog_reprojthres,
        homog_mask,
        homog_max_iter,
        homog_confidence,
    )

    return homog_matrix, mask

Flann Matcher

def compute_flann_matcher_sift(des1, des2):
    """Computing the flann matchers"""
    # Defining the FlannMatcher parameters
    flann_index_kdtree = 0
    index_params = dict(algorithm=flann_index_kdtree, trees=5)
    search_params = dict(checks=50)
    # Set up the flannMatcher
    flann = cv2.FlannBasedMatcher(index_params, search_params)

    # Get the matches
    matches = flann.knnMatch(des1, des2, k=2)
    return matches

Wrap perspective

def blend_image(img1, img2, homog_matrix):
    # Warp the image 2 (right) for image 1 (left)
    dst = cv2.warpPerspective(
        img2, homog_matrix, (img1.shape[1] + img2.shape[1], img1.shape[0])
    )

    # Concatenation
    dst[0 : img1.shape[0], 0 : img1.shape[1]] = img1
    return dst
0

There are 0 best solutions below