-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.py
More file actions
56 lines (41 loc) · 1.57 KB
/
2.py
File metadata and controls
56 lines (41 loc) · 1.57 KB
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
import cv2 as cv
import numpy as np
cap = cv.VideoCapture('image/shaketest2.mp4')
orb = cv.ORB_create()
fps = cap.get(cv.CAP_PROP_FPS)
ret, old_frame = cap.read()
if not ret:
print("Cannot read video file or stream.")
exit()
old_gray = cv.cvtColor(old_frame, cv.COLOR_BGR2GRAY)
key1 = orb.detect(old_gray)
key1, des1 = orb.compute(old_gray, key1)
p0 = np.float32([keypoint.pt for keypoint in key1]).reshape(-1, 1, 2)
lk_params = dict(winSize=(15, 15), maxLevel=2, criteria=(cv.TERM_CRITERIA_EPS | cv.TERM_CRITERIA_COUNT, 10, 0.03))
cv.namedWindow('Comparison Frame', cv.WINDOW_NORMAL)
cv.resizeWindow('Comparison Frame', 3840, 1000)
while True:
ret, frame = cap.read()
if not ret:
break
frame_gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
p1, st, err = cv.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, None, **lk_params)
good_new = p1[st == 1]
good_old = p0[st == 1]
M, inliers = cv.estimateAffinePartial2D(good_old, good_new)
if M is not None:
translation = M[:, 2]
translation_gaussian = cv.GaussianBlur(translation.reshape(-1, 1), (5, 5), 0)
translation_gaussian = translation_gaussian.squeeze()
M[:, 2] = translation_gaussian
warped_img = cv.warpAffine(frame, M, (frame.shape[1], frame.shape[0]))
else:
warped_img = frame
comparison_img = np.hstack((frame, warped_img))
cv.imshow('Comparison Frame', comparison_img)
if cv.waitKey(int(1000 / fps)) == ord('q'):
break
old_gray = frame_gray.copy()
p0 = good_new.reshape(-1, 1, 2)
cap.release()
cv.destroyAllWindows()