import numpy as np import os import torch import tqdm import cv2 import matplotlib.pyplot as plt import numpy as np
from modules.xfeat import XFeat
def warp_corners_and_draw_matches(ref_points, dst_points, img1, img2): # Calculate the Homography matrix H, mask = cv2.findHomography(ref_points, dst_points, cv2.USAC_MAGSAC, 3.5, maxIters=1_000, confidence=0.999) mask = mask.flatten()
# Get corners of the first image (image1) h, w = img1.shape[:2] corners_img1 = np.array([[0, 0], [w-1, 0], [w-1, h-1], [0, h-1]], dtype=np.float32).reshape(-1, 1, 2)
# Warp corners to the second image (image2) space warped_corners = cv2.perspectiveTransform(corners_img1, H)
# # Draw the warped corners in image2 img2_with_corners = img2.copy() for i in range(len(warped_corners)): start_point = tuple(warped_corners[i-1][0].astype(int)) end_point = tuple(warped_corners[i][0].astype(int)) cv2.line(img2_with_corners, start_point, end_point, (0, 255, 0), 4) # Using solid green for corners
# Prepare keypoints and matches for drawMatches function keypoints1 = [cv2.KeyPoint(p[0], p[1], 5) for p in ref_points] keypoints2 = [cv2.KeyPoint(p[0], p[1], 5) for p in dst_points] matches = [cv2.DMatch(i,i,0) for i in range(len(mask)) if mask[i]]
# Draw inlier matches img_matches = cv2.drawMatches(img1, keypoints1, img2_with_corners, keypoints2, matches, None, matchColor=(0, 255, 0), flags=2)
return img_matches
xfeat = XFeat()
im1 = cv2.imread('wally.png') # reference 찾으려는 물체 im2 = cv2.imread('where.png') # target 찾으려는 물체가 포함된 장면
mkpts_0, mkpts_1 = xfeat.match_xfeat(im1, im2, top_k = 2000) canvas = warp_corners_and_draw_matches(mkpts_0, mkpts_1, im1, im2)
cv2.imshow('result', canvas) cv2.waitKey(0)
|
Member discussion