티스토리 뷰

robot

[map merging] 맵 합치기

별의 별 2021. 7. 21. 17:29

[map merging] 맵 합치기

feature 검출의 다양한 방법 사용해 ransac까지


특징점(feature) 검출하는 detector는 다양하다.

SIFT,SURF,KAZE,ORB등등이 있다.

 

그 중에 KAZE와 ORB로 맵의 특징점을 뽑고 매칭 해보겠다.

 

1. KAZE

detector = cv2.KAZE_create()
import cv2
import numpy as np


img1 = cv2.imread('lab.pgm')
img2 = cv2.imread('lab2.pgm')

cv2.imshow('lab',img1)
cv2.imshow("lab2",img2)

detector = cv2.KAZE_create()

kp1, des1 = detector.detectAndCompute(img1,None)
kp2, des2 = detector.detectAndCompute(img2,None)

bf = cv2.BFMatcher()
matches= bf.knnMatch(des1,des2,k=2)

good = []
for m,n in matches:
    if m.distance < 0.6*n.distance:
        good.append([m])

print('good',len(good))
img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,good,None,flags=2)

cv2.imshow("matching",img3)
cv2.imwrite('matching.jpg',img3)
cv2.waitKey(0)

지금은 RANSAC을 진행하지 않고 굿 매칭만 한 것이라 오매칭이 더러 있다.

kaze matching

good match - 76

 

2. ORB뿐

detector = cv2.ORB_create()

위 kaze코드에서 detector만 바꾸면 된다.

ORB matching

good match가 7개 뿐


결론

kaze가 좀 더 성능이 좋은 것으로 보임

ransac까지 진행해보자.


RANSAC

ORB

정확도

accuracy: 86/143(0.60%)

ORB에서 ransac

 

 

KAZE ransac이 안되서

AKAZE로 시도하니 성공 ..? 왜일까

일단 오류는 올려둔다.

오류

match하는 부분에서 계속 오류가 나옴

 

AKAZE로 RANSAC

정확도

accuracy: 85/124(0.69%)

kaze ransac

둘 중 kaze가 피처가 적지만 알맞은 매칭이 더 많이 발생

 

AKAZE ransac코드

import cv2
import numpy as np


img1 = cv2.imread('lab2.pgm')
img2 = cv2.imread('lab.pgm')

cv2.imshow('lab',img1)
cv2.imshow("lab2",img2)

detector = cv2.AKAZE_create()

kp1, des1 = detector.detectAndCompute(img1,None)
kp2, des2 = detector.detectAndCompute(img2,None)

matcher = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = matcher.match(des1, des2)

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 ])

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))

cv2.imshow('Matching-All', res1)
cv2.imwrite('matching-all_kaze.jpg',res1)
cv2.imshow('Matching-Inlier', res2)
cv2.imwrite('matching-inlier_kaze.jpg',res2)
cv2.waitKey()
cv2.destroyAllWindows()
댓글