使用 Numba 加速过滤帧

lio*_*art 5 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
Run Code Online (Sandbox Code Playgroud)

这需要很长时间才能运行,具体取决于帧的大小和帧数。我想尽可能地加快速度,所以我一直在尝试将 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
Run Code Online (Sandbox Code Playgroud)

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

我想知道我应该如何在我的情况下正确使用 Numba 以尽可能加快整个过程。