Image Warping from Scratch for Panoroma

33 Views Asked by At

I am doing finding homology than warping the image and finally stitching them. But something wrong in my code? I could not find what is wrong.The warped images are too bad. Could you guys please help me?

Warped image

def warp_image(src_img, homography_matrix):
src_height, src_width, _ = src_img.shape
#initialize the empty destination image for transformation.
dst_img = np.zeros((src_height, src_width,3), dtype=np.uint8)
for y in range(src_height):
    for x in range(src_width):  
        # Applying the inverse homography matrix to transform coordinates from the destination image to the source image. 
        w_inv = homography_matrix[2, 0] * x + homography_matrix[2, 1] * y + homography_matrix[2, 2]
        src_x = (homography_matrix[0, 0] * x + homography_matrix[0, 1] * y + homography_matrix[0, 2]) / w_inv
        src_y = (homography_matrix[1, 0] * x + homography_matrix[1, 1] * y + homography_matrix[1, 2]) / w_inv    
        
        # Check if the transformed coordinates are within the bounds of the source image
        if 0 <= src_x < src_width - 1 and 0 <= src_y < src_height - 1:
            # Perform bilinear interpolation to get the color value at non-integer coordinates
            x_floor, y_floor = math.floor(src_x), math.floor(src_y)
            x_frac, y_frac = src_x - x_floor, src_y - y_floor

            color_tl = src_img[y_floor, x_floor]
            color_tr = src_img[y_floor, x_floor + 1]
            color_bl = src_img[y_floor + 1, x_floor]
            color_br = src_img[y_floor + 1, x_floor + 1]

            color = (1 - x_frac) * (1 - y_frac) * color_tl + x_frac * (1 - y_frac) * color_tr + \
                           (1 - x_frac) * y_frac * color_bl + x_frac * y_frac * color_br
            dst_img[y][x] = color
return dst_img


def stitch_images(src_img, dst_img):
result = np.concatenate((src_img,dst_img), axis = 1)
return result

def merge_into_panoroma(images, homographies):

panoroma = images[0].copy()
 
for i in range(1,len(images)):
    sub_img = images[i]
    homography = homographies[i]
    
    warped_img = warp_image(sub_img, homography)

    
    panoroma = stitch_images(panoroma, warped_img)
 
    plt.imshow(panoroma)
    plt.show()
return panoroma
0

There are 0 best solutions below