【问题标题】:Python Video Stabilizer Using Low Pass Filter使用低通滤波器的 Python 视频稳定器
【发布时间】:2019-01-25 15:26:09
【问题描述】:

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

,...

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

谁能帮帮我?

【问题讨论】:

    标签: python numpy opencv scipy lowpass-filter


    【解决方案1】:

    我可以用 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) 之间的当前矩阵

    【讨论】:

    • 谢谢。我应该在您的 cpp 代码中包含任何其他库吗? (我的意思是除了opencv库)
    • 这不是完整的 C++ 代码,只是用变换矩阵来说明数学。
    猜你喜欢
    • 2019-04-29
    • 2014-03-14
    • 2013-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-29
    • 2013-08-22
    相关资源
    最近更新 更多