python-opencv【图像处理-暴力匹配】

暴力匹配

import cv2
import sys
import numpy as np
import matplotlib.pyplot as plot




#特征匹配
def bf(filepath1,filepath2):
    img1=cv2.imread(filepath1,0)
    img2=cv2.imread(filepath2,0)
    def rd(name,img):
        img=cv2.resize(img,(0,0),fx=0.5,fy=0.5)
        cv2.imshow(name,img)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
    rd('1',img1)
    rd('2',img2)
    sift = cv2.xfeatures2d.SIFT_create()
    kp1, dst1 = sift.compute(img1, None)
    kp2, dst2 = sift.compute(img2, None)
    df=cv2.BFMatcher()
    matches=df.match(dst1,dst2)
    matches=sorted(matches,key=lambda  x:x.distance)
    img3=cv2.drawMarker(img1, kp1, img2, kp2,matches[:10],None,flags=2)
    cv2.imshow('img3',img3)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

bf(r'F:\z\2\u-net-vgg-bmp\0501.bmp',r'F:\z\2\u-net-vgg-bmp\0551.bmp')

你可能感兴趣的:(opencv,python,图像处理)