Speed up live plotting of a footage (cv2)

2019-02-15 22:27发布

问题:

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.

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

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?

回答1:

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

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