zoukankan      html  css  js  c++  java
  • SIFT图像配准 python3.6 + opencv3.3代码

    opencv3.x 中部分函数有改变:

    1. SIFT:可以采用help(cv2.xfeatures2d)查询

    2.drawKeypoints: 同样采用help()方法查询

    opencv3 版本sift,surf 及其他不稳定的算法函数都放在opencv3.x的contrib版里。该模块下载地址 https://www.lfd.uci.edu/~gohlke/pythonlibs/

    import cv2 
    import numpy as np
    
    def sift_kp(image):
        gray_image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
        sift=cv2.xfeatures2d.SIFT_create()
        kp,des = sift.detectAndCompute(image,None)
        kp_image = cv2.drawKeypoints(gray_image,kp,None)
        return kp_image,kp,des
    
    
    def get_good_match(des1,des2):
        bf = cv2.BFMatcher()
        matches = bf.knnMatch(des1, des2, k=2) #des1为模板图,des2为匹配图
        matches = sorted(matches,key=lambda x:x[0].distance/x[1].distance)
        good = []
        for m, n in matches:
            if m.distance < 0.75 * n.distance:
                good.append(m)
        return good
    
    
    def siftImageAlignment(img1,img2):
       _,kp1,des1 = sift_kp(img1)
       _,kp2,des2 = sift_kp(img2)
       goodMatch = get_good_match(des1,des2) 
       if len(goodMatch) > 4:
           ptsA= np.float32([kp1[m.queryIdx].pt for m in goodMatch]).reshape(-1, 1, 2)
           ptsB = np.float32([kp2[m.trainIdx].pt for m in goodMatch]).reshape(-1, 1, 2)
           ransacReprojThreshold = 4
           H, status =cv2.findHomography(ptsA,ptsB,cv2.RANSAC,ransacReprojThreshold);
           imgOut = cv2.warpPerspective(img2, H, (img1.shape[1],img1.shape[0]),flags=cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP)
       return imgOut,H,status
    
    img1 = cv2.imread(r'sift_img/8.png') 
    img2 = cv2.imread(r'sift_img/7.png') 
    
    _,kp1,des1 = sift_kp(img1)
    _,kp2,des2 = sift_kp(img2)
    goodMatch = get_good_match(des1,des2)
    
    img3 = cv2.drawMatches(img1, kp1, img2, kp2, goodMatch[:5], None, flags=2)
    #----or----
    #goodMatch = np.expand_dims(goodMatch,1)
    #img3 = cv2.drawMatchesKnn(img1, kp1, img2, kp2, goodMatch[:5], None, flags=2)
    
    cv2.imshow('img',img3) 
    cv2.waitKey(0) 
    cv2.destroyAllWindows()
    

    SIFT特征详解:

    http://www.cnblogs.com/wangguchangqing/p/4853263.html

    http://blog.csdn.net/abcjennifer/article/details/7639681

  • 相关阅读:
    把arguments转化成数组
    最小化重绘和重排
    选择器API
    事件委托
    WAhaha_hnu (zoj 2010 oct月赛)
    素数计数公式全面拉丁化改写小有改进Meissel公式梅塞尔Lehmer公式莱梅=勒梅尔筛法三种形式孟庆余公式(转载)
    NBUT 2013 Timed NOJ Training #005
    2013 腾讯马拉松初赛第一场
    hrboj 1683 树形DP
    哈尔滨2013校赛训练赛 4 解题思路
  • 原文地址:https://www.cnblogs.com/P3nguin/p/8135816.html
Copyright © 2011-2022 走看看