深度学习深度学习-推荐系统-CV-NLP影子的密码

计算机视觉随笔(全景图实战)

2019-10-08  本文已影响0人  zidea

全景图

1对1特征匹配

1对1的特征匹配

k对最佳匹配

k对最佳匹配

随机抽样一致算法 RANSAC

最小2乘 VS RANSAC
n = 2 我们就随机选取 2 个点,因为两个点可以确定一条直线,inline outline 点

import cv2
import numpy as np
class Stitcher:
    # 
    def stitch(self,images,ratio=0.75,reprojThresh=4.0,showMatches=False):
        (imageA, imageB) = images
        print imageA

        (kpsA,featuresA) = self.detectAndDescribe(imageA)
        (kpsB,featuresB) = self.detectAndDescribe(imageB)

        M = self.matchKeypoints(kpsA,kpsB,featuresA,featuresB,ratio,reprojThresh)

        if M is None:
            return None

        (matches, H, status) = M
        result = cv2.warpPerspective(imageA,H,(imageA.shape[1] + imageB.shape[1],imageA.shape[0]))
        cv2.imshow("res",result)
        cv2.waitKey(1)
        cv2.destroyAllWindows()
        result[0:imageB.shape[0],0:imageB.shape[1]] = imageB

        if showMatches:
            pass
            # vis = self.drawMatches(imageA,imageB,kpsA,kpsB,matches,status)

        return result

    def detectAndDescribe(self,image):
        gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

        sift = cv2.xfeatures2d.SIFT_create()

        (kps,features) = sift.detectAndCompute(gray,None)

        kps = np.float32([kp.pt for kp in kps])

        return (kps,features)

    
    def matchKeypoints(self,kpsA,kpsB,featuresA,featuresB,ratio,reprojThresh):
        matcher = cv2.BFMatcher()
        rawMatches = matcher.knnMatch(featuresA,featuresB,2)

        matches = []
        for m in rawMatches:
            if len(m) == 2 and m[0].distance < m[1].distance * ratio:
                matches.append((m[0].trainIdx,m[0].queryIdx))
        
        if len(matches) > 4:
            ptsA = np.float32([kpsA[i] for (_,i) in matches])
            ptsB = np.float32([kpsB[i] for (i,_) in matches])

            (H,status) = cv2.findHomography(ptsA,ptsB,cv2.RANSAC,reprojThresh)

            return (matches, H, status)
上一篇下一篇

猜你喜欢

热点阅读