Everyone loves creating a sketch or potraits of their pictures with great details.
Here we are with another visual effect The Pencil Sketch effect. This project demonstrates how to
apply such kind of effect in an image.
This is a good approache towards making some good pencil sketches.
This projects involves various methods like image sharpening, applying grayscale, gaussian blurs and
invertion of an image.This project is a combination of our previous basic projects.
Use the package manager pip to install cv2 and numpy.
pip install cv2
pip install numpy
Use import keyword to import modules.
import cv2
import numpy as np
img = cv2.imread("cat.png")
1.Sharpen the original image.
kernel_sharpening = np.array([[-1,-1,-1],
[-1, 9,-1],
[-1,-1,-1]])
sharpened = cv2.filter2D(img,-1,kernel_sharpening)
2.Convert sharpened image into Gray scale.
gray = cv2.cvtColor(sharpened , cv2.COLOR_BGR2GRAY)
3.Generate the negative or inverted image of grayscaled & store it.
inv = 255-gray
4.Apply Gaussian blur to the inverted image.
gaussgray = cv2.GaussianBlur(inv,ksize=(15,15),sigmaX=0,sigmaY=0)
Now we have gaussian blured image of inverted image and also gray image as well.In-order to generate pencil-sketh
we need to merge both inverted image and gray image.In-order to do so we will b using improved dodging method
defined below.
def dodgeV2(image,mask):
return cv2.divide(image,255-mask,scale=256)
Now we need to apply this to our image
pencil_img = dodgeV2(gray,gaussgray)
print('Pencil Sketch effect Applied.')
cv2.imshow('ORIGINAL',img)
cv2.imshow('PENCIL-SKETCH',pencil_img)
cv2.waitKey(0)
cv2.destroyAllWindows()