-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathstabilization.py
executable file
·75 lines (52 loc) · 2.17 KB
/
stabilization.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Copyright (c) 2021 Project Bee4Exp.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
""" Stablilizes a video using ORB detector.
CL Args:
-i Path to input video file.
-o Path to output video file.
"""
import cv2
import numpy as np
import skvideo.io
from util import get_parser
from skimage.util import img_as_ubyte
from skimage.transform import warp, AffineTransform
from skimage.measure import ransac
args = get_parser().parse_args()
INFILE = args.input
OUTFILE = args.output
cap = cv2.VideoCapture(INFILE)
n_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
print(n_frames)
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS))
metadata = skvideo.io.ffprobe(INFILE)
rate = metadata['video']['@r_frame_rate']
writer = skvideo.io.FFmpegWriter(OUTFILE, inputdict={'-r': rate, '-pix_fmt': 'bgr24'},
outputdict={'-r': rate, '-pix_fmt': 'yuv420p'})
_, ref_frame = cap.read()
writer.writeFrame(ref_frame)
ref_frame = cv2.cvtColor(ref_frame, cv2.COLOR_BGR2GRAY)
orb = cv2.ORB_create(2000)
matcher = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
frameA, descA = orb.detectAndCompute(ref_frame[300:h-300, 300:w-300], None)
for k in range(1, n_frames):
print(k)
_, frame = cap.read()
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Skip 300px from each border to prevent problems with white tapes on
# minefield edges.
frameB, descB = orb.detectAndCompute(frame_gray[300:h-300, 300:w-300], None)
matches = matcher.match(descA, descB)
pointsA = np.float32([frameA[m.queryIdx].pt for m in matches]).reshape(-1, 2)
pointsB = np.float32([frameB[m.trainIdx].pt for m in matches]).reshape(-1, 2)
tform, inliers = ransac((pointsA, pointsB), AffineTransform,
min_samples=4, residual_threshold=1,
max_trials=100)
warped_frame = warp(frame, tform, output_shape=frame.shape)
writer.writeFrame(img_as_ubyte(warped_frame))
writer.close()
cap.release()