使用低通滤波器的Python视频

使用低通滤波器的Python视频

本文介绍了使用低通滤波器的Python视频稳定器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,应该使用fft和过滤器(lpf或hpf)实现视频稳定器

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()

有一个名为Vibrated1.avi的视频还有一个包含框架差异的文本文件,名为Virated1.txt,看起来像这样:

There is a video named Vibrated1.aviAnd a text file which includes frames difference named Virated1.txt and it is looked like this:

0.341486,-0.258215

0.341486, -0.258215

0.121945,1.27605

0.121945, 1.27605

-0.0811261,0.78985

-0.0811261, 0.78985

,...

我不知道如何在此代码中以及在何处添加一些过滤器以消除视频振动

I don't know how and where should i add some filters to this code to remove video vibration

有人可以帮助我吗?

推荐答案

我可以用C ++部分编写简短的伪代码:

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 -在[0; 1]

a1 a2 -用于将alpha和(1-alpha)应用于转换矩阵的矩阵

a1 and a2 - matrices for applying alpha and (1 - alpha) to the transformation matrix

homoFiltered -过滤后的转换矩阵

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

homo - current matrix between Frame(t-1) and Frame(t)

这篇关于使用低通滤波器的Python视频稳定器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 00:19