加快素材的实时打印(cv2) [英] Speed up live plotting of a footage (cv2)

查看:107
本文介绍了加快素材的实时打印(cv2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用cv2模块为视频的每一帧创建实时散点图.下面的代码正是这样做的.

I am attempting to use the cv2 module to create a live scatter plot for each frame of a video. The code below does exactly that.

但是,如果要处理超过8分钟的镜头(包含超过60000帧),则代码效率不高,并且比获得所需输出所需的时间要长得多.

However with more than 8 minute of footage containing more than 60000 frames to process, the code is not efficient and does take much longer than necessary to get the desired output.

vidcap = cv2.VideoCapture(filepath)
fig, ax = plt.subplots(1)
plt.ion()

x=df["time"][7:100]
y=df["force"][7:100]

for i in range(len(x)):
   vidcap.set(1,590)
   ret, image = vidcap.read()
   frameId = vidcap.get(1) 
   plt.imshow(image,extent=[0,200,0,100], aspect='auto')
   plt.subplot(221)
   plt.plot(x[0+i:1+i],y[0+i:1+i],'or', lw=2)
   plt.subplot(222)
   fig.set_size_inches(20, 10)
   plt.pause(.000001)
   plt.draw()

我已经考虑过使用pyqtgraph来提高处理速度.有没有更好的方法来处理和绘制视频帧?

I have considered using pyqtgraph to increase the speed of the process. Is there a better way to process and plot on a frame of a video?

推荐答案

这个问题的答案显示了两种在matplotlib中获取视频的方法.

This question's answer shows two ways to obtain a video in matplotlib.

主要要点是不要在每次迭代时都重新创建完整的图.如果使用该答案中的第二种方法,则使用blit=True可能会进一步提高速度.如下面的代码所示.

The main point is not to recreate the complete plot on every iteration. If using the second approach from that answer, the use of blit=True may increase speed even more. This is shown in the below code.

import cv2
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

df = pd.DataFrame({"time": np.linspace(0,20, num=100), 
                   "force" : np.cumsum(np.random.randn(100))})

def grab_frame(cap):
    ret,frame = cap.read()
    return frame # or cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)

#Initiate 
vidcap = cv2.VideoCapture(0)
# vidcap.set(1,590)

fig, (ax,ax2) = plt.subplots(ncols=2,figsize=(20, 10))

x=df["time"][7:100]
y=df["force"][7:100]

#create two image plots
im1 = ax.imshow(grab_frame(vidcap),extent=[0,200,0,100], aspect='auto')
line, = ax2.plot(x[0:1],y[0:1],'or')
ax2.set_xlim(x.min(), x.max())
ax2.set_ylim(y.min(), y.max())

def update(i):
    im1.set_data(grab_frame(vidcap))
    line.set_data(x[0+i:1+i],y[0+i:1+i])
    return im1, line


ani = FuncAnimation(fig, update, frames=len(x), interval=1, blit=True)
plt.show()

这篇关于加快素材的实时打印(cv2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆