def sobel_filter(img, K_size=3):
    if (len(img.shape) == 3):
        (H, W, C) = img.shape
    else:
        (H, W) = img.shape
    pad = (K_size // 2)
    out = np.zeros(((H + (pad * 2)), (W + (pad * 2))), dtype=np.float)
    out[pad:(pad + H), pad:(pad + W)] = img.copy().astype(np.float)
    tmp = out.copy()
    out_v = out.copy()
    out_h = out.copy()
    Kv = [[1.0, 2.0, 1.0], [0.0, 0.0, 0.0], [(- 1.0), (- 2.0), (- 1.0)]]
    Kh = [[1.0, 0.0, (- 1.0)], [2.0, 0.0, (- 2.0)], [1.0, 0.0, (- 1.0)]]
    for y in range(H):
        for x in range(W):
            out_v[((pad + y), (pad + x))] = np.sum((Kv * tmp[y:(y + K_size), x:(x + K_size)]))
            out_h[((pad + y), (pad + x))] = np.sum((Kh * tmp[y:(y + K_size), x:(x + K_size)]))
    out_v = np.clip(out_v, 0, 255)
    out_h = np.clip(out_h, 0, 255)
    out_v = out_v[pad:(pad + H), pad:(pad + W)]
    out_v = out_v.astype(np.uint8)
    out_h = out_h[pad:(pad + H), pad:(pad + W)]
    out_h = out_h.astype(np.uint8)
    return (out_v, out_h)
def sobel_filter(img, K_size=3):
    if (len(img.shape) == 3):
        (H, W, C) = img.shape
    else:
        (H, W) = img.shape
    pad = (K_size // 2)
    out = np.zeros(((H + (pad * 2)), (W + (pad * 2))), dtype=np.float)
    out[pad:(pad + H), pad:(pad + W)] = img.copy().astype(np.float)
    tmp = out.copy()
    out_v = out.copy()
    out_h = out.copy()
    Kv = [[1.0, 2.0, 1.0], [0.0, 0.0, 0.0], [(- 1.0), (- 2.0), (- 1.0)]]
    Kh = [[1.0, 0.0, (- 1.0)], [2.0, 0.0, (- 2.0)], [1.0, 0.0, (- 1.0)]]
    for y in range(H):
        for x in range(W):
            out_v[((pad + y), (pad + x))] = np.sum((Kv * tmp[y:(y + K_size), x:(x + K_size)]))
            out_h[((pad + y), (pad + x))] = np.sum((Kh * tmp[y:(y + K_size), x:(x + K_size)]))
    out_v = np.clip(out_v, 0, 255)
    out_h = np.clip(out_h, 0, 255)
    out_v = out_v[pad:(pad + H), pad:(pad + W)]
    out_v = out_v.astype(np.uint8)
    out_h = out_h[pad:(pad + H), pad:(pad + W)]
    out_h = out_h.astype(np.uint8)
    return (out_v, out_h)