繁体   English   中英

使用 scipy.signal.lfilter 时,实现巴特沃斯带通滤波器遇到:“ValueError: object of too small depth for desired array”错误

[英]Implementing Butterworth bandpass filter running into: "ValueError: object of too small depth for desired array" error when using scipy.signal.lfilter

我正在尝试按照本文实现欧拉视频放大,但是在使用巴特沃斯带通滤波器时,它一直遇到“ValueError:object 对于所需阵列的深度太小”

这是我的巴特沃斯带通滤波器代码:

def butter_bandpass_filter(data, lowcut, highcut, fs, order=5):
    b, a = butter_bandpass(lowcut, highcut, fs, order=order)
    y = scipy.signal.lfilter([b], [a], data, axis=0)     #The line that errors
    return y

def butter_bandpass(lowcut, highcut, fs, order=5):
    nyq = 0.5 * fs
    low = lowcut / nyq
    high = highcut / nyq
    b, a = scipy.signal.butter(order, [low, high], btype='band')
    return b, a

我在以下代码行中使用了 butter_bandpass_filter:

magnify_motion(tired_me, 0.4, 3)
def magnify_motion(video, low, high, n=4, sigma=3, amp=20):
    lap_video_lst = video.get_laplacian_lst(n=n, sigma=sigma)

    print("lap_video_lst shapes:")
    for i in range(n):
        print("{}:".format(i), get_list_shape(lap_video_lst[i]))

    ret_lst = []
    for layer in range(n):
        filtered_layer = butter_bandpass_filter(lap_video_lst[layer], low, high, video.fps)     #This line
        filtered_layer *= amp
        ret_lst.append(filtered_layer)

    return ret_lst

其中每个 lap_video_lst[layer] 的格式为 numpy 数组,其中包含具有形状(frame_count、height、width、colour_channels)的视频的所有帧,打印时如下:

0: (330, 360, 640, 3)
1: (330, 180, 320, 3)
2: (330, 90, 160, 3)
3: (330, 45, 80, 3)

请注意,每个“层”具有不同维度的原因是它们是原始视频的拉普拉斯金字塔。

如果有用,I 这是 b 和 np 数组的形状,以及它们各自的值。

b: (1, 11)
[[ 0.00069339  0.         -0.00346694  0.          0.00693387  0.
  -0.00693387  0.          0.00346694  0.         -0.00069339]]
a: (1, 11)
[[  1.          -8.02213491  29.18702261 -63.4764537   91.44299881
  -91.21397148  63.81766134 -30.92689236   9.93534351  -1.91057439
  0.16700076]]

这是完整的错误跟踪,以防我忽略了一些细节:

Traceback (most recent call last):
  File "d:\Desktop\Stuff\Uni notes B\2021 Fall\Cs194\Projects\Project Final 1\tester.py", line 84, in <module>
    main()
  File "d:\Desktop\Stuff\Uni notes B\2021 Fall\Cs194\Projects\Project Final 1\tester.py", line 71, in main
    magnify_motion(tired_me, 0.4, 3)
  File "d:\Desktop\Stuff\Uni notes B\2021 Fall\Cs194\Projects\Project Final 1\tester.py", line 32, in magnify_motion
    filtered_layer = butter_bandpass_filter(lap_video_lst[layer], low, high, video.fps)
  File "d:\Desktop\Stuff\Uni notes B\2021 Fall\Cs194\Projects\Project Final 1\tester.py", line 17, in butter_bandpass_filter
    y = scipy.signal.lfilter([b], [a], data, axis=0)
  File "C:\Users\nick-\AppData\Roaming\Python\Python38\site-packages\scipy\signal\signaltools.py", line 1972, in lfilter
    raise ValueError('object of too small depth for desired array')
ValueError: object of too small depth for desired array

任何提示都会有所帮助:谢谢:D

我不看报纸。

您看到的问题已通过应用解决

y = scipy.signal.lfilter(b, a, data, axis=0)

这意味着过滤器会将每个数组data[:,x,y,c]视为一个信号,过滤每个像素的值,可能使视频具有一些运动模糊,不同于用于制作边缘的空间过滤更锐利或更平滑。 对于空间过滤,您将使用axis=1axis=2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM