gpt4 book ai didi

使用低通滤波器的 Python 视频稳定器

转载 作者:太空宇宙 更新时间:2023-11-03 23:08:37 24 4
gpt4 key购买 nike

我有一个项目,我应该使用 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

,...

我不知道我应该如何以及在何处向这段代码添加一些过滤器以消除视频振动

谁能帮帮我?

最佳答案

我可以用 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]

a1a2 - 将 alpha 和 (1 - alpha) 应用于变换矩阵的矩阵

homoFiltered - 过滤转换矩阵

homo - Frame(t-1) 和 Frame(t) 之间的当前矩阵

关于使用低通滤波器的 Python 视频稳定器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54368246/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com