python - 使用 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 以尽可能加快整个过程。
解决方案
推荐阅读
- python - 尝试验证从 @uniswap/v3-periphery 导入的合约时出现编译器错误
- reactjs - 如何使用自定义 JSX 与 React 共存?
- c - scanf() 覆盖下一个变量
- express - 关于 Express 特别是中间件的问题
- pokeapi - 如何一次获取所有口袋妖怪?
- python - 如何遍历日期时间索引并传递给函数?
- mariadb - MariaDB:来自网络的 root 访问也意味着来自 SSH?
- vmware - 一台 VMWARE ESXI 机器不工作
- rxjs - 按可观察属性的值过滤可观察数组?
- c# - 为什么 Read 和 ReadAync 会产生完全不同的结果