仅在一个多维阵列的一个轴上卷积(视频的帧上运行平均值)

发布于 2025-01-24 21:31:33 字数 594 浏览 3 评论 0原文

如果我有一堆栈1D数组,那么很容易在第一个轴上获得均值的均值:

import numpy as np
from scipy.ndimage import convolve1d
arr = np.random.random(size=(5000,10)) # a stack of 5000 1D arrays, each of length 10
running_mean = convolve1d(arr,np.ones(30)/30,axis=0) # replace each array by an average over 30 of them

明显的解决方案变得非常慢。

import numpy as np
arr = np.random.random(size=(5000,250,250,3)) # an rgb video with 5000 images, resolution 250x250
running_mean = np.array([arr[i:i+30].mean(0) for i in range(len(arr)-30)])

但是,如果我有一个3D数组的堆栈,那么 要在只有一个轴上与一堆阵列卷积的内核?

If I have a stack of 1D arrays, it is easy to get a running mean over the first axis:

import numpy as np
from scipy.ndimage import convolve1d
arr = np.random.random(size=(5000,10)) # a stack of 5000 1D arrays, each of length 10
running_mean = convolve1d(arr,np.ones(30)/30,axis=0) # replace each array by an average over 30 of them

However if I have a stack of 3D arrays, the obvious solutions become extremely slow

import numpy as np
arr = np.random.random(size=(5000,250,250,3)) # an rgb video with 5000 images, resolution 250x250
running_mean = np.array([arr[i:i+30].mean(0) for i in range(len(arr)-30)])

Is there a vectorized method in scipy, numpy, or opencv to convolve a kernel with a stack of arrays over only one axis?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌辣 2025-01-31 21:31:33

您仍然可以为3D数组使用convolve1d,只需相应地设置参数axis即可。

旁注:当您到达输入数组的边界时,您的3D数组的方法无法处理情况。因此您的输出形状将为(4970、250、250、3)。

You can still use convolve1d for your 3D array and just set the parameter axis accordingly.

Side note: Your method for the 3D array does not handle the case, when you reach the boundaries of the input array. So your output shape will be (4970, 250, 250, 3).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文