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

标签 python numpy opencv scipy lowpass-filter

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

相关文章:

c++ - 使用 `libopencv_ffmpeg.so` 在 Linux 上构建 OpenCV 2.4.11

python - 如何在 if 语句中收集元组值?

python - 用于原始 blender 导出器的 blender python 脚本

python - 在 Python 中跟踪文件加载进度

python - (Python) If Else 问题和列表到字符串转换问题

python - numpy.tile 非整数次数

python - 基于另一个数组的 ndarray 的子集

python - 向量和 pandas 列(线性向量)之间的余弦相似度

python - Predix:没有名为 _tkinter 的模块

opencv - 如何在 Deepnote 中导入 cv2?