使用Numba加速帧过滤

时间:2020-08-25 19:43:26

标签: python numpy numba

我有以下代码,其中我将视频中的多个帧中的每个像素都采集并通过低通滤波器(基本上是每个像素值的时间滤波)。然后,我将获取这些经过过滤的像素,并在buf2数组中创建新帧。

import cv2, numpy as np
from scipy.signal import butter, lfilter, freqz
from numba import jit

# Filter requirements.
order = 1
fs = 30.0       # sample rate, Hz
cutoff = 0.3  # desired cutoff frequency of the filter, Hz

buf2 = np.empty((frameCount, frameHeight, frameWidth, 3), np.dtype('uint8'))

for j in range(rows_in_frame):
    for k in range(columns_in_frame):
        l = array_containing_all_frames[:, j, k, 1] #Only looking at green channel
        y = butter_lowpass_filter(l, cutoff, fs, order)
        buf2[:, j, k, 1] = y

这需要很长时间才能运行,具体取决于帧的大小和帧数。我想尽可能地加快速度,因此在以下尝试将Numba应用于此问题:

@jit(nopython=True)
def butter_lowpass_filter(data, cutoff, fs, order=5):
    b, a = butter_lowpass(cutoff, fs, order=order)
    y = lfilter(b, a, data)
    return y

但是,它只是返回一个错误,提示TypingError: Failed in nopython mode pipeline (step: nopython frontend) Untyped global name 'lfilter': cannot determine Numba type of <class 'function'>

我想知道如何根据自己的情况正确使用Numba,以尽可能加快整个过程。

0 个答案:

没有答案