Sobel filter

として畳み込み

def conv_filter(img, stride, padding, kernel):
    h, w = img.shape
    f, f = kernel.shape
    n_h = int(int(h + 2 * padding - f) / stride + 1)
    n_w = int(int(w + 2 * padding - f) / stride + 1)
    z = np.zeros([n_h, n_w])
 
    img_pad = zero_pad(img, padding)
 
    for h in range(n_h):
        vertical_start = stride * h
        verttical_end = vertical_start + f
        for w in range(n_w):
            horizontal_start = stride * w
            horizontal_end = horizontal_start + f
            target = img_pad[vertical_start:verttical_end, horizontal_start:horizontal_end]
            conv = np.multiply(target, kernel)
            conv_sum = np.sum(conv)
            z[h, w] = conv_sum
 
    return z

参考文献