Python OpenCV Create Map with Video Frames

Viewed 476

I would like to ask if someone can help me with my question. I am currently working on making a map in real time with the frames captured from a camera. I succeed finding the keypoints and the homography by using ORB,Brute Force Matcher and findHomography() function. Now i want to stitch the frames by using the Homography Matrix to create a map. I am working on Windows environment with Python and OpenCV.

Any help is appreciated.

def homography(current_frame_gray, previous_frame_gray):
    orb = cv2.ORB_create()

    kpts1, descs1 = orb.detectAndCompute(previous_frame_gray, None)
    kpts2, descs2 = orb.detectAndCompute(current_frame_gray, None)

    bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)

    matches = bf.match(descs1, descs2)
    dmatches = sorted(matches, key=lambda x: x.distance)

    src_pts = np.float32([kpts1[m.queryIdx].pt for m in dmatches]).reshape(-1, 1, 2)
    dst_pts = np.float32([kpts2[m.trainIdx].pt for m in dmatches]).reshape(-1, 1, 2)

    if dst_pts is None:
        return False

    M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
    print(M)
    print("M exact")
    print(M[0,2])
    h, w = previous_frame.shape[:2]
    pts = np.float32([[0, 0], [0, h - 1], [w - 1, h - 1], [w - 1, 0]]).reshape(-1, 1, 2)


    if current_frame is not None:
        print("Not None")
        print(type(current_frame))
    else:
        print("NONE!!!")

    if previous_frame is not None:
        print("Not None")
    else:
        print("NONE!!!")

    res = cv2.drawMatches(previous_frame, kpts1, current_frame, kpts2, dmatches[:20], None, flags=2)


    cv2.imshow("orb_match", res)

    return res
0 Answers
Related