在视频上叠加图表

时间:2015-08-20 07:09:46

标签: python matplotlib ffmpeg

是否有解决方案可以打开mp4并叠加图形(Matplotlib)或类似的东西?

this

这样的东西

1 个答案:

答案 0 :(得分:1)

您可以使用openCV从视频中获取帧,如此example,并使用matplotlib在顶部绘制图形,例如

import cv2
import matplotlib.pyplot as plt
import numpy as np

filename = './SampleVideo.mp4'
cap = cv2.VideoCapture(filename)

frames = int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT))
width  = int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT))

fig, ax = plt.subplots(1,1)
plt.ion()
plt.show()

#Setup a dummy path
x = np.linspace(0,width,frames)
y = x/2. + 100*np.sin(2.*np.pi*x/1200)

for i in range(frames):
    fig.clf()
    flag, frame = cap.read()

    plt.imshow(frame)
    plt.plot(x,y,'k-', lw=2)
    plt.plot(x[i],y[i],'or')

    if cv2.waitKey(1) == 27:
        break

我从here获得了视频。