Untitled

 avatar
unknown
plain_text
a year ago
1.8 kB
12
Indexable
import cv2
import numpy as np

img2 = cv2.imread('dog1_resize.jpg', cv.IMREAD_GRAYSCALE)
cap = cv2.VideoCapture('example.mp4', cv.IMREAD_GRAYSCALE)
sift = cv.SIFT_create()
sift.setContrastThreshold(0.03)
sift.setEdgeThreshold(5)
print(img2.shape)
width  = cap.get(3)   # float `width`
height = cap.get(4)  # float `height
size = (width, height)
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
result = cv2.VideoWriter('output.mp4', fourcc, 24, (944, 1280), 0)
while(True): 
    # Capture frames in the video 
    ret, frame = cap.read()    
    
    keypoints_1, descriptors_1 = sift.detectAndCompute(frame, None)
    keypoints_2, descriptors_2 = sift.detectAndCompute(img2, None)

    # feature matching
    bf = cv.BFMatcher(cv.NORM_L1, crossCheck=False)
    matches = bf.match(descriptors_1, descriptors_2)
    matches = sorted(matches, key = lambda x:x.distance)
    img3 = cv.drawMatches(frame, keypoints_1, img2, keypoints_2, matches[:50], img2, flags=2)
    matches = bf.knnMatch(descriptors_1, descriptors_2, k = 2)

    # apply ratio test
    good = []
    for m,n in matches:
        if m.distance < 0.8 * n.distance:
            good.append([m])
    img4 = cv.drawMatchesKnn(frame, keypoints_1, img2, keypoints_2, good, None, 
                             matchColor=(0, 255, 0), matchesMask = None,
                             singlePointColor=(255, 0, 0), flags=0)
    
    result.write(cv2.cvtColor(img4, cv2.COLOR_BGR2GRAY))
    # Display the resulting frame 
    cv2.imshow('Video', img4)
    # creating 'q' as the quit  
    # button for the video
    if cv2.waitKey(1) & 0xFF == ord('q'): 
        break
## release the cap object 
cap.release()
result.release()
## close all windows 
cv2.destroyAllWindows()
Editor is loading...
Leave a Comment