OpenCV 基于SIFT的特征匹配

import cv2


'''
src_img: source pic
dst_img: target pic
dst_img_set: target pic set
src_coor: (src_x, src_y, src_w, src_h)
dst_coor: (dst_x, dst_y, dst_w, dst_h)
dst_img_set: [(dst_x1, dst_y1, dst_w1, dst_h1), (dst_x2, dst_y2, dst_w2, dst_h2), ...]
'''
def get_feature_matched(src_coor, src_img, dst_coor, dst_img_set):
	img_frame1 = cv2.imread(src_img,cv2.IMREAD_GRAYSCALE)
    img_frame1 = img_frame1[src_coor[1]: src_coor[1] + src_coor[3], src_coor[0]: src_coor[0] + src_coor[2]]
    
	sift = cv2.xfeatures2d.SIFT_create()
    kp1, des1 = sift.detectAndCompute(img_frame1, None)
    bf = cv2.BFMatcher()
    All_set = {}

    for dst_img in dst_img_set:
	    img_frame2 = cv2.imread(img2,cv2.IMREAD_GRAYSCALE)
	    tmp_img_frame2 = img_frame2.copy()
	    tmp_img_frame2 = tmp_img_frame2[detection_y: detection_y + detection_h, detection_x: detection_x + detection_w]
	    kp2, des2 = sift.detectAndCompute(tmp_img_frame2, None)
	    matches = bf.knnMatch(des1, des2, k=2)
	    good = []
	    for m, n in matches:
	        if m.distance < 0.75 * n.distance:
	            good.append([m])
	    All_set[len(good)] = dst_coor
	tmp = []
	for x in All_set.keys()
		tmp.append(x)
	best = max(tmp)
	best_coor = All_set.get(best)
	return best_coor


if __name__ == '__main__':
	src_img = ''
	src_coor = (x0, y0, w0, h0)
	dst_img = ''
	dst_img_set = [(x1, y1, w1, h1), (x2, y2, w2, h2)]
	target_coor = get_feature_matched(src_img, src_coor, dst_img, dst_img_set)



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值