我有一个项目,应该使用fft和过滤器(lpf或hpf)实现视频稳定器
这是我要修改的部分代码:
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()
有一个名为Vibrated1.avi的视频 还有一个包含框架差异的文本文件,名为Virated1.txt,看起来像这样:
0.341486,-0.258215
0.121945,1.27605
-0.0811261,0.78985
,...
我不知道如何以及在此代码中添加一些过滤器以消除视频振动
有人可以帮助我吗?
答案 0 :(得分:0)
我可以用C ++部分编写简短的伪代码:
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 -在[0; 1]
a1 和 a2 -用于将alpha和(1-alpha)应用于变换矩阵的矩阵
homoFiltered -过滤后的转换矩阵
homo -帧(t-1)和帧(t)之间的当前矩阵