问题
I have a project which i should implement a video stabilizer using fft and filters (lpf or hpf)
Here is some part of the code that i want to modify it:
import cv2
import numpy as np
# Create a VideoCapture object and read from input file
cap = cv2.VideoCapture('Vibrated2.avi')
# load data
data = np.loadtxt('Vibrated2.txt', delimiter=',')
# Define the codec and create VideoWriter object
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
out = cv2.VideoWriter('Stabilized.avi',cv2.VideoWriter_fourcc('M','J','P','G'), 25, (frame_width,frame_height))
# Check if camera opened successfully
if (cap.isOpened()== False):
print("Error opening video stream or file")
def transform(frame, param):
ti = cv2.getRotationMatrix2D((0,0), 0, 1)
ti[0,2] += param[0]
ti[1,2] += param[1]
frame = cv2.warpAffine(frame, ti, frame.shape[1:-4:-1])
return frame
num = 0
# Read until video is completed
while(cap.isOpened()):
# Capture frame-by-frame
ret, frame = cap.read()
if ret == True:
# apply transformation
frame = transform(frame, data[num])
print (frame, "dn")
num+=1
# Display the resulting frame
cv2.imshow('Frame',frame)
# Write the frame into the file 'output.avi'
out.write(frame)
# Press Q on keyboard to exit
if cv2.waitKey(25) & 0xFF == ord('q'):
break
# Break the loop
else:
break
# When everything done, release the video capture object
cap.release()
# Closes all the frames
cv2.destroyAllWindows()
There is a video named Vibrated1.avi And a text file which includes frames difference named Virated1.txt and it is looked like this:
0.341486, -0.258215
0.121945, 1.27605
-0.0811261, 0.78985
,...
I don't know how and where should i add some filters to this code to remove video vibration
Could anyone help me ?
回答1:
I can write short pseudocode with C++ parts:
cv::Mat homoFiltered = cv::Mat::eye(3, 3, CV_32F);
const double alpha = 0.9;
cv::Mat a1 = cv::Mat::eye(3, 3, CV_32F) * alpha;
cv::Mat a2 = cv::Mat::eye(3, 3, CV_32F) * (1. - alpha);
while cap >> frame:
cv::Mat homo = CalcHomography(frame)
homoFiltered = a1 * (homoFiltered * homo) + a2 * homo;
cv::warpPerspective(..., homoFiltered, ...)
alpha - low pass smoothing coefficient in [0; 1]
a1 and a2 - matrices for applying alpha and (1 - alpha) to the transformation matrix
homoFiltered - filtered transformation matrix
homo - current matrix between Frame(t-1) and Frame(t)
来源:https://stackoverflow.com/questions/54368246/python-video-stabilizer-using-low-pass-filter