2017-11-26 20 views
1

나는 유사 감지기를 선별 사용하고 여기에 예를 들어,에, 발견 된 이미지 주위에 경계 상자를 그릴 구 탐지기를 사용하고자하고있다. 내 코드는 BFMatcher를 사용합니다. 나는 사용 된 Matcher에서 아무런 선호도가 없다.이미지 호모 그래피에 Orb 검출기를 어떻게 사용합니까? <a href="https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_feature2d/py_feature_homography/py_feature_homography.html" rel="nofollow noreferrer">SIFT Refrence</a></p> <p>링크 된 예는 FlannBasedMatcher를 사용

 MIN_MATCH_COUNT = 10 

     img1 = cv2.imread('box.png',0) 
     img2 = cv2.imread('box_in_scene.png',0) 

     orb = cv2.ORB_create() 

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

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

box_in_scene 이미지를 그릴 때 어떻게이 기호를 사용하여이 코드를 계속 사용합니까?

EDIT : 다음을 시도했지만 예상대로 출력되지 않았습니다.

src_pts = np.float32([ kp1[m.queryIdx].pt for m in matches[:50] ]).reshape(-1,1,2) 
dst_pts = np.float32([ kp2[m.trainIdx].pt for m in matches[:50] ]).reshape(-1,1,2) 
M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC,5.0) 
matchesMask = mask.ravel().tolist() 
h,w = img1.shape 
pts = np.float32([ [0,0],[0,h-1],[w-1,h-1],[w-1,0] ]).reshape(-1,1,2) 
dst = cv2.perspectiveTransform(pts,M) 
+0

경우, 포인트를 변환 (0,0) (img1.cols, 0); (0, img1.rows)와 (img1.cols, img1.rows) 사이의 호모 그래피와 선을 그립니다. – Micka

+0

@Micka'matches = sorted (일치, 키 = 람다 x : x.distance) src_pts = np.float32 (일치하는 항목 : [: 50]]에 대한 kp1 [m.queryIdx] .pt) (-1,1,2) dst_pts = np.float32 (일치하는 항목 [: 50]의 [kp2 [m.trainIdx] .pt)) reshape (-1,1,2) M, mask = cv2 .findHomography (src_pts, dst_pts, cv2.RANSAC 5.0) matchesMask mask.ravel =(). tolist() H, w = img1.shape PTS = np.float32 ([0,0] (-1,1,2) dst = cv2.perspectiveTransform (pts, M)'[0, h-1], [w-1, 나는 시도했다. 그렇지만 정확히 작동하지 않는 것 같습니다. – tester

+0

내 결과입니다. https://i.stack.imgur.com/0IS3w.png'red' 박스는'flann','green' 박스는'match'입니다. 그것들은 거의 같습니다. – Silencer

답변

4

내 결과. (설명은 주석으로 썼다)

enter image description here


코드 : 일치하는 좋은

#!/usr/bin/python3 
# 2017.11.26 23:27:12 CST 

## Find object by orb features matching 

import numpy as np 
import cv2 
imgname = "box.png"   # query image (small object) 
imgname2 = "box_in_scene.png" # train image (large scene) 

MIN_MATCH_COUNT = 4 

## Create ORB object and BF object(using HAMMING) 
orb = cv2.ORB_create() 
img1 = cv2.imread(imgname) 
img2 = cv2.imread(imgname2) 

gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) 
gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) 

## Find the keypoints and descriptors with ORB 
kpts1, descs1 = orb.detectAndCompute(gray1,None) 
kpts2, descs2 = orb.detectAndCompute(gray2,None) 

## match descriptors and sort them in the order of their distance 
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) 
matches = bf.match(descs1, descs2) 
dmatches = sorted(matches, key = lambda x:x.distance) 

## extract the matched keypoints 
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) 

## find homography matrix and do perspective transform 
M, 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] ]).reshape(-1,1,2) 
dst = cv2.perspectiveTransform(pts,M) 

## draw found regions 
img2 = cv2.polylines(img2, [np.int32(dst)], True, (0,0,255), 1, cv2.LINE_AA) 
cv2.imshow("found", img2) 

## draw match lines 
res = cv2.drawMatches(img1, kpts1, img2, kpts2, dmatches[:20],None,flags=2) 

cv2.imshow("orb_match", res); 

cv2.waitKey();cv2.destroyAllWindows()