'I'm working on Image Stitching. I can't figure this error out in Homography using RANSAC

I am trying to stitch 5 images but I'm getting this error in one of the cells (click on the link for the screenshot). The code has comments, so I think it will help in getting to know what I'm trying to do. There are too many code cells which I'm working on, but this one is where I'm stuck.

Any help will be appreciated!

def HomographyRANSAC(corr, corners_src, corners_dst, max_iter):
    # corners_src: n by 2 numpy array, column 0: x, column 1: y 
    # corners_dst: n by 2 numpy array 
    # corr: m by 2 numpy array, a list of correspondences, column 0: index of pixels in corners_src, column 1: index of pixels in corner_dst
    
    # max_iter: maximum iteration
    
    # Algorithm: 
    # 1. Randomly select 4 points
    # 2. Compute Homography matrix
    # 3. Count inliers
    # -  if the current H is better than the best one so far, replace the best H and keep the # of inliers
    # 4. Repeat 1 - 3
    
    n = corr.shape[0]
    
    best_H = np.eye(3)    # best Homography matrix
    max_inlier = 0 # maximum inlier count
    
    relevant_src_pts = np.array([[corners_src[idx[0], 0], corners_src[idx[0], 1]] for idx in corr])
    relevant_dst_pts = np.array([[corners_dst[idx[1], 0], corners_dst[idx[1], 1]] for idx in corr])
    
    
    for i in range(max_iter):
        ## Randomly select 4 points: select index first
        ## idx_src = # 4 random indices 
        ## get 4 random rows of corners_src and corners_dst using 'idx_src'
        
        idx_src = list(np.random.randint(low = 0, high=len(relevant_src_pts), size=4))
        
        ## Estimate Homograph: use the function 'FindHomography(pts1, pts2)'
        ## pts1 is the selected 4 rows from 'corners_src' and consists of two columns for x and y
        ## pts2 is 4 rows of the same index from 'corners_dst'
        pts1 = relevant_src_pts[idx_src, :]
        pts2 = relevant_dst_pts[idx_src, :]
        
        H = FindHomography(pts1, pts2)
        H = np.reshape(H, (3, 3))
        ## Count inliers 
        
            ## Use 'np.matmul' to multiply H matrix with corners_src points 
            ## Note that we need transpose of 'corners_src' and add another row with all ones
            ## See the example below and the previous cell.
            ## e.g. x1, x2, x3, x4, x5, ....
            ##      y1, y2, y3, y4, y5, ....
            ##       1,  1,  1,  1,  1, ....
            ## The result of multiplication is the transpose of [u, v, w].
            ## We can get transformed x and y coordinates by dividing u and v with w. (See the previous section.)
        
            ## Once you get the transformed points, 
            ## measure the distance between the transformed point and the corresponding corner_dst
            ## If the distance (Euclidean distance) is less then a threshold (e.g. 5~8 pixels?), count it as inlier
        new_corners = np.append(relevant_src_pts, np.ones((relevant_src_pts.shape[0], 1)), axis = 1)
        translated_corners = np.matmul(H, new_corners.T)
        translated_corners = np.divide(translated_corners[:2,:], translated_corners[-1,:])
        
        x_diff = (relevant_dst_pts[:, 0]  - translated_corners.T[:, 0])**2
        y_diff = (relevant_dst_pts[:, 1]  - translated_corners.T[:, 1])**2
        dist = np.sqrt(x_diff + y_diff)
        
        current_inliers = (dist<8).sum()
        if current_inliers>max_inlier:
            max_inlier = current_inliers
            best_H = H.copy()
        ## If the current inlier-count is greater than 'max_inlier',
        ##    replace 'best_H' with the current 'H', and assign current inlier-count to 'max_inlier'
        
    return best_H

## Use the implemented HomographyRANSAC to compute H12, H23, H34, H45
max_iter = 10000

# e.g. 

H12 = HomographyRANSAC(corr1to2, corners1, corners2, max_iter)
H23 = HomographyRANSAC(corr2to3, corners2, corners3, max_iter)
H34 = HomographyRANSAC(corr3to4, corners3, corners4, max_iter)
H45 = HomographyRANSAC(corr4to5, corners4, corners5, max_iter)
# np.ones([1,100])

The error I'm getting



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source