使用扭曲的透视图像

时间:2018-02-15 15:02:31

标签: python python-2.7 opencv computer-vision homography

简介:根据我的理解,给定移动物体的同一场景的2张图像,我可以在每个像素上制作一个均值以“移除”移动的物体(它会有重影效果,但如果我将重复几个图像,它会很好。)

然而,如果我有不同的场景,这根本不起作用,所以我想将我的所有图像移动到同一场景(扭曲透视图),然后执行上面的建议。

我想从我拥有的电影帧中删除移动物体。为此,我找到了两个图像之间的匹配关键点,使用RANSAC去除异常值,并将图像A的透视图扭曲到图像B的透视图以获得扭曲的图像。

现在我希望扭曲的图像与src,dst图像的大小相同,但这不是我得到的:enter image description here

最终我希望从几个帧中获得一个完整的扭曲图像,并使用它来移除从一帧到另一帧移动的对象。

  1. 如何以完整尺寸创建扭曲图像?
  2. 如何找到我想表示值​​的相应像素,以便删除移动物体?
  3. # find key points + features of a given image
    def get_keyPoints_and_features(img):
        descriptor = cv2.xfeatures2d.SIFT_create()
        kps, features = descriptor.detectAndCompute(img, None)
        kps = np.float32([kp.pt for kp in kps])
        return kps, features
    
    # match key points of 2 images 
    def match_key_points(a, b, ratio = 0.75):
        # unpack 
        kpsA, featuresA = a
        kpsB, featuresB = b
    
        matcher = cv2.DescriptorMatcher_create("BruteForce")
        rawMatches = matcher.knnMatch(featuresA, featuresB, 2)
        matches = []
    
        # remove outliers using RANSAC
        for m in rawMatches:
            if len(m) == 2 and m[0].distance < m[1].distance * ratio:
                matches.append((m[0].trainIdx, m[0].queryIdx))
    
        # must have more than 4 matches
        # the more matches we have, the more noise robust it will be
        assert(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, method = cv2.RANSAC, 
                                            ransacReprojThreshold = 0.4)
        return matches, H
    
    # warp src image to have perspective like dst image using the 
    # homogrpahy between both images
    def warp_perspective(src, dst):
        # read images
        a,b = cv2.imread(src), cv2.imread(dst)
    
        # generate key points and features
        kps_and_features_a = get_keyPoints_and_features(a)
        kps_and_features_b = get_keyPoints_and_features(b)
    
        # get homography
        _, H = match_key_points(kps_and_features_a, kps_and_features_b)
    
        warped = cv2.warpPerspective(a, H, (a.shape[1], a.shape[0]))
        inv_warped = cv2.warpPerspective(warped, inv(H), (a.shape[1], a.shape[0]))
    
        cv2.imshow("a", a)
        cv2.imshow("b", b)
        cv2.imshow("warped", warped)
        cv2.imshow("inv_warped", inv_warped)
        cv2.moveWindow("a",0,0)
        cv2.moveWindow("b",0,370)
        cv2.moveWindow("warped",600,0)
        cv2.moveWindow("inv_warped",600,370)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
    
    def main():
        # get images
        path = r'...'
        images = [os.path.join(path, file) for file in os.listdir(path)]
        warp_perspective(images[0], images[1])
    
    main()
    

0 个答案:

没有答案
相关问题