首页 > 解决方案 > 使用 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 以尽可能加快整个过程。

标签: pythonnumpynumba

解决方案


推荐阅读