python opencv-3.0 SIFT/SURF 特徵提取與匹配__python

來源:互聯網
上載者:User
一、環境準備

目前 Opencv 2.x 3.x 版本,兩個版本之間的差異主要是一些功能函數被放置到了不同的功能模組,因此大多數情況兩個版本的代碼並不能通用。建議安裝 Anaconda,自行下載相應版本。直接命令安裝Opencv3, lake :

conda install -c menpo opencv3pip install lake

二、SIFT/SURF 特徵提取與匹配

# coding: utf-8from matplotlib import pyplot as pltfrom lake.decorator import time_costimport cv2print 'cv version: ', cv2.__version__def bgr_rgb(img):    (r, g, b) = cv2.split(img)    return cv2.merge([b, g, r])def orb_detect(image_a, image_b):    # feature match    orb = cv2.ORB_create()    kp1, des1 = orb.detectAndCompute(image_a, None)    kp2, des2 = orb.detectAndCompute(image_b, None)    # create BFMatcher object    bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)    # Match descriptors.    matches = bf.match(des1, des2)    # Sort them in the order of their distance.    matches = sorted(matches, key=lambda x: x.distance)    # Draw first 10 matches.    img3 = cv2.drawMatches(image_a, kp1, image_b, kp2, matches[:100], None, flags=2)    return bgr_rgb(img3)@time_costdef sift_detect(img1, img2, detector='surf'):    if detector.startswith('si'):        print "sift detector......"        sift = cv2.xfeatures2d.SURF_create()    else:        print "surf detector......"        sift = cv2.xfeatures2d.SURF_create()    # find the keypoints and descriptors with SIFT    kp1, des1 = sift.detectAndCompute(img1, None)    kp2, des2 = sift.detectAndCompute(img2, None)    # BFMatcher with default params    bf = cv2.BFMatcher()    matches = bf.knnMatch(des1, des2, k=2)    # Apply ratio test    good = [[m] for m, n in matches if m.distance < 0.5 * n.distance]    # cv2.drawMatchesKnn expects list of lists as matches.    img3 = cv2.drawMatchesKnn(img1, kp1, img2, kp2, good, None, flags=2)    return bgr_rgb(img3)if __name__ == "__main__":    # load image    image_a = cv2.imread('./img1.jpg')    image_b = cv2.imread('./img2.png')    # ORB    # img = orb_detect(image_a, image_b)    # SIFT or SURF    img = sift_detect(image_a, image_b)    plt.imshow(img)    plt.show()
三、輸出展示
cv version:  3.1.0surf detector......==> time-cost: 0.187422     sift_detect

Output:

img1

img2

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.