<gh_stars>0
import cv2 
import numpy as np
from skimage.exposure import rescale_intensity
import argparse


def convolve(image, kernel):
    """
    Args:
        image: cv2.imread image
        kernel: n x n kernel
    """
    iH, iW = image.shape[:2]
    kH, kW = kernel.shape[:2]
    # using padding to keep the shape same
    pad = (kW - 1) // 2
    image = cv2.copyMakeBorder(image, pad, pad, pad, pad, 
        cv2.BORDER_REPLICATE)
    # image = np.pad(image, 1, mode='edge') # same as the line above
    output = np.zeros((iH, iW), dtype='float')
    
    for y in np.arange(pad, iH + pad):
        for x in np.arange(pad, iW + pad):
            roi = image[y-pad : y+pad+1, x-pad : x+pad+1]
            k = (roi * kernel).sum()
            output[y-pad, x-pad] = k

    output = rescale_intensity(output, in_range=(0, 255))
    output = (output * 255).astype('uint8')

    return output

smallBlur = np.ones((7, 7), dtype='float') * (1.0 / (7 * 7))
largeBlur = np.ones((21, 21), dtype='float') * (1.9 / (21 * 21))
sharpen = np.array((
    [0, -1, 0],
    [-1, 5, -1],
    [0, -1, 0],
), dtype='int')

laplacian = np.array((
    [0, 1, 0],
    [1, -4, 1],
    [0, 1, 0],
), dtype='int')

sobelX = np.array([
    [-1, 0, 1],
    [-2, 0, 2],
    [-1, 0, 1],
], dtype='int')

sobelY = np.array([
    [-1, -2, -1],
    [0, 0, 0],
    [1, 2, 1],
], dtype='int')

emboss = np.array([
    [-2, -1, 0],
    [-1, 1, 1],
    [0, 1, 2],
], dtype='int')


kernelBank = (
    ('small_blur', smallBlur),
    ('large_blur', largeBlur),
    ('sharpen', sharpen),
    ('laplacian', laplacian),
    ('sobel_X', sobelX),
    ('sobel_Y', sobelY),
    ('emboss', emboss),
)


if __name__ == '__main__':
    ap = argparse.ArgumentParser()
    ap.add_argument('-i', '--image', required=True,
        help='path to the input image')
    args = vars(ap.parse_args())

    image = cv2.imread(args['image'])
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    for (kernelName, K) in kernelBank:
        print('[INFO] apply {} kernel'.format(kernelName))
        convolveOutput = convolve(gray, K)
        opencvOutput = cv2.filter2D(gray, -1, K)

        cv2.imshow('Original', gray)
        cv2.imshow('{} - convole'.format(kernelName), convolveOutput)
        cv2.imshow('{} = opencv'.format(kernelName), opencvOutput)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
import cv2 
import numpy as np
from skimage.exposure import rescale_intensity
import argparse


def convolve(image, kernel):
    """
    Args:
        image: cv2.imread image
        kernel: n x n kernel
    """
    iH, iW = image.shape[:2]
    kH, kW = kernel.shape[:2]
    # using padding to keep the shape same
    pad = (kW - 1) // 2
    image = cv2.copyMakeBorder(image, pad, pad, pad, pad, 
        cv2.BORDER_REPLICATE)
    # image = np.pad(image, 1, mode='edge') # same as the line above
    output = np.zeros((iH, iW), dtype='float')
    
    for y in np.arange(pad, iH + pad):
        for x in np.arange(pad, iW + pad):
            roi = image[y-pad : y+pad+1, x-pad : x+pad+1]
            k = (roi * kernel).sum()
            output[y-pad, x-pad] = k

    output = rescale_intensity(output, in_range=(0, 255))
    output = (output * 255).astype('uint8')

    return output

smallBlur = np.ones((7, 7), dtype='float') * (1.0 / (7 * 7))
largeBlur = np.ones((21, 21), dtype='float') * (1.9 / (21 * 21))
sharpen = np.array((
    [0, -1, 0],
    [-1, 5, -1],
    [0, -1, 0],
), dtype='int')

laplacian = np.array((
    [0, 1, 0],
    [1, -4, 1],
    [0, 1, 0],
), dtype='int')

sobelX = np.array([
    [-1, 0, 1],
    [-2, 0, 2],
    [-1, 0, 1],
], dtype='int')

sobelY = np.array([
    [-1, -2, -1],
    [0, 0, 0],
    [1, 2, 1],
], dtype='int')

emboss = np.array([
    [-2, -1, 0],
    [-1, 1, 1],
    [0, 1, 2],
], dtype='int')


kernelBank = (
    ('small_blur', smallBlur),
    ('large_blur', largeBlur),
    ('sharpen', sharpen),
    ('laplacian', laplacian),
    ('sobel_X', sobelX),
    ('sobel_Y', sobelY),
    ('emboss', emboss),
)


if __name__ == '__main__':
    ap = argparse.ArgumentParser()
    ap.add_argument('-i', '--image', required=True,
        help='path to the input image')
    args = vars(ap.parse_args())

    image = cv2.imread(args['image'])
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    for (kernelName, K) in kernelBank:
        print('[INFO] apply {} kernel'.format(kernelName))
        convolveOutput = convolve(gray, K)
        opencvOutput = cv2.filter2D(gray, -1, K)

        cv2.imshow('Original', gray)
        cv2.imshow('{} - convole'.format(kernelName), convolveOutput)
        cv2.imshow('{} = opencv'.format(kernelName), opencvOutput)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
Args:         image: cv2.imread image         kernel: n x n kernel # using padding to keep the shape same # image = np.pad(image, 1, mode='edge') # same as the line above