opencvimage-processingtransformhomographyimage-stitching

How to get image coordinates after successful match and stitch using OpenCV


Morning!

I am trying to get the coordinates (x,y of corners) of two images that have successfully been matched using ORB and stitched together using cv2.warpPerspective.

I have tried this by calculating the Homography (cv2.findHomography) from the two ORB matched pairs.

I am however struggling to get the coordinates for the corners of each image after these have been stitched.

Any help would be great.

R

original images

Find coordinates of stitched image

This is the code that I am running at the moment, but I am at a loss. Not even sure this is the right approach

matchess = np.asarray(good)
if len(good)>500: # the number here is the number of matches deemed good
    src_pts = np.float32([kp1[m.queryIdx].pt for m in matchess[:,0] ]).reshape(-1,1,2)
    dst_pts = np.float32([kp2[m.trainIdx].pt for m in matchess[:,0] ]).reshape(-1,1,2)
    H, masked = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)

    pts = np.float32([[0,0],[0,-1],[-1,-1],[-1,0]]).reshape(-1,1,2)

    dst = cv2.perspectiveTransform(pts,H)
        

    dst = cv2.warpPerspective(img1,H,(img2.shape[1] + img1.shape[1], img2.shape[0])) #wraped image

    print("dst 1:", dst)


    dst[0:img2.shape[0], 0:img2.shape[1]] = img2 #stitched image


    print("dst 2:",dst)

Solution

  • perspectiveTransform() is the function to use.

    Give it points and either H or inverted H. It will apply the homography to those points.

    The corner pixels of an image are (0,0) and (width-1, height-1), and the other two corners of course.