简体   繁体   中英

Convolution 3D image with 2D filter

I have a single image of shape 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)

ValueError: operands could not be broadcast together with shapes (5,5,3) (3,5,5)

np.multiply is doing an elementwise multiplication. 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.

ValueError: operands could not be broadcast together with shapes (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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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