OpenCV Convert Image into Sketch

import cv2
import numpy as np
import scipy.ndimage

image = cv2.imread('bull.png')

def grayscale(rgb):
    # 2 dimensional array to convert image to sketch
    return np.dot(
        rgb[..., :3], [0.2989, 0.5870, 0.1140]
    )
 
def dodge(front, back):
    # if image is greater than 255 (which is not possible) it will convert it to 255
    result = front * 255 / (255 - back)
    result[result > 255] = 255
    result[back == 255] = 255
    # uint8 is for 8-bit signed integer
    return result.astype('uint8')
 
gray = grayscale(image)
i = 255 - gray
 
blur = scipy.ndimage.gaussian_filter(i, sigma=13)
sketch = dodge(blur, gray)

cv2.imwrite('sketch.png', sketch)

Input Image:

Output Image:

Post your comment

Search
Popular Posts
Subscribe
{{post}}