full code :
import cv2, numpy as np
img1 = cv2.imread('img1.jpg')
img2 = cv2.imread('img2.jpg')
gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
# ORB, BF-Hamming 로 knnMatch ---①
detector = cv2.ORB_create()
kp1, desc1 = detector.detectAndCompute(gray1, None)
kp2, desc2 = detector.detectAndCompute(gray2, None)
matcher = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = matcher.match(desc1, desc2)
# 매칭 결과를 거리기준 오름차순으로 정렬 ---③
matches = sorted(matches, key=lambda x:x.distance)
# 모든 매칭점 그리기 ---④
res1 = cv2.drawMatches(img1, kp1, img2, kp2, matches, None, \
flags=cv2.DRAW_MATCHES_FLAGS_NOT_DRAW_SINGLE_POINTS)
# 매칭점으로 원근 변환 및 영역 표시 ---⑤
src_pts = np.float32([ kp1[m.queryIdx].pt for m in matches ])
dst_pts = np.float32([ kp2[m.trainIdx].pt for m in matches ])
# RANSAC으로 변환 행렬 근사 계산 ---⑥
mtrx, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
h,w = img1.shape[:2]
pts = np.float32([ [[0,0]],[[0,h-1]],[[w-1,h-1]],[[w-1,0]] ])
dst = cv2.perspectiveTransform(pts,mtrx)
#img2 = cv2.polylines(img2,[np.int32(dst)],True,255,3, cv2.LINE_AA)
# 정상치 매칭만 그리기 ---⑦
matchesMask = mask.ravel().tolist()
res2 = cv2.drawMatches(img1, kp1, img2, kp2, matches, None, \
matchesMask = matchesMask,
flags=cv2.DRAW_MATCHES_FLAGS_NOT_DRAW_SINGLE_POINTS)
# 모든 매칭점과 정상치 비율 ---⑧
accuracy=float(mask.sum()) / mask.size
print("accuracy: %d/%d(%.2f%%)"% (mask.sum(), mask.size, accuracy))
# 결과 출력
from google.colab.patches import cv2_imshow
cv2_imshow(res2)
cv2.waitKey()
cv2.destroyAllWindows()
#summary
how to get indexs of these points
I wanna find index of feature matching points on two images.
I can check total matching points amout by this line
accuracy=float(mask.sum()) / mask.size
print("accuracy: %d/%d(%.2f%%)"% (mask.sum(), mask.size, accuracy))
and I tried to find index of matching points
print(matchesMask)
kp2_test = []
i = 0
while i < len(matchesMask):
if matchesMask[i] == 1:
kp2_test.append(kp2[i])
i = i + 1
kp2_test2 = []
kp2_test2.append(kp2_test)
print(len(kp2_test))
print(kp2_test)
test2 = cv2.drawKeypoints(img2, kp2_test, img1, (0,255,0),flags =0)
cv2_imshow(test2)
cv2.waitKey()
cv2.destroyAllWindows()
but the result is quite different compared to origin_image


