简体   繁体   English

使用 2D 过滤器卷积 3D 图像

[英]Convolution 3D image with 2D filter

I have a single image of shape img.shape = (500, 439, 3)我有一张形状为img.shape = (500, 439, 3)

The convolution function is卷积函数是

def convolution(image, kernel, stride=1, pad=0):

    n_h, n_w, _ = image.shape

    f = kernel.shape[0]
    kernel = np.repeat(kernel[None,:], 3, axis=0)

    n_H = int(((n_h + (2*pad) - f) / stride) + 1)
    n_W = int(((n_w + (2*pad) - f) / stride) + 1)
    n_C = 1

    out = np.zeros((n_H, n_W, n_C))

    for h in range(n_H):
        vert_start = h*stride
        vert_end = h*stride + f

        for w in range(n_W):
            horiz_start = w*stride
            horiz_end = w*stride + f

            for c in range(n_C):
                a_slice_prev = image[vert_start:vert_end,
                                     horiz_start:horiz_end, :]

                s = np.multiply(a_slice_prev, kernel)
                out[h, w, c] = np.sum(s, dtype=float)

    return out

I want to see the image after any kernel/filter applied to the image, so I got the following我想在对图像应用任何内核/过滤器之后查看图像,所以我得到了以下内容

img = plt.imread('cat.png')
kernel = np.arange(25).reshape((5, 5))
out2 = convolution(img, kernel)
plt.imshow(out2)
plt.show()

I get我得到

s = np.multiply(a_slice_prev, kernel) s = np.multiply(a_slice_prev, 内核)

ValueError: operands could not be broadcast together with shapes (5,5,3) (3,5,5) ValueError:操作数无法与形状一起广播 (5,5,3) (3,5,5)

np.multiply is doing an elementwise multiplication. np.multiply 正在进行元素乘法。 However, your arguments do not have matching dimensions.但是,您的参数没有匹配的维度。 You could transpose your kernel or image with this to ensure it can work:您可以使用此转置您的内核或图像以确保它可以工作:

kernel = kernel.transpose()

You could do this prior to your np.multiply call.您可以在np.multiply调用之前执行此np.multiply

ValueError: operands could not be broadcast together with shapes (5,5,3) (3,5,5) ValueError:操作数无法与形状一起广播 (5,5,3) (3,5,5)

Since, Convolution is element-wise multiplication, your shapes should be (5,5,3) for the image region and (5,5,3) for the kernel, thus repeat your kernel like this:因为,卷积是逐元素相乘,你的形状应该是(5,5,3)的图像区域(5,5,3)的内核,从而重复你的内核是这样的:

kernel = np.arange(25).reshape((5, 5, 1))
kernel = np.repeat(kernel, 3, axis=2)

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

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