python-每个通道分别具有2d内核的3d数组的卷积

Pri*_*moz 2 python numpy convolution scipy

我有一个矩阵,[c, n, m]其中c有多个渠道。nm是宽度和高度。在特定的示例中,我有一个具有1000个通道的矩阵。我想分别与a x a每个通道的大小的内核进行卷积。在我的示例中,内核大小为3 x 3。是否有任何功能,scipy或者numpy没有通过循环遍历通道的那种功能?

我找到了scipy.ndimage.convolve函数,但是我认为如果不使用循环就无法在该问题上应用该函数。

And*_*idy 6

将矩阵视为图像并使用 opencv。将数组的形状更改为 [height, width, num_channels]。然后在opencv中运行filter2D(图像的卷积函数)。

image = cv2.imread("some_image.jpg")
image.shape # (height, width, 3) # 3 is 3 channels for Red, Green, Blue
kernel = np.ones((3,3)) / 9.
image_blurred = cv2.filter2D(image, cv2.CV_64F, kernel) # will apply the kernel for each channel. You can have more than 3 channels.
Run Code Online (Sandbox Code Playgroud)


far*_*rth 5

我认为您只需要使内核成为三维即可。这样的事情应该起作用:

kernel = kernel[:, :, None]
Run Code Online (Sandbox Code Playgroud)

如果scipy.ndimage.convolve不适用于3D阵列,则可以尝试scipy.signal.convolve

  • 如果你想远离 opencv 的好建议 (3认同)