仅在一个多维阵列的一个轴上卷积(视频的帧上运行平均值)
如果我有一堆栈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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您仍然可以为3D数组使用
convolve1d
,只需相应地设置参数axis
即可。旁注:当您到达输入数组的边界时,您的3D数组的方法无法处理情况。因此您的输出形状将为(4970、250、250、3)。
You can still use
convolve1d
for your 3D array and just set the parameteraxis
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).